View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  
25  import javax.servlet.ServletContext;
26  import javax.xml.bind.annotation.XmlAttribute;
27  import javax.xml.bind.annotation.XmlRootElement;
28  
29  import org.apache.hadoop.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
31  import org.apache.hadoop.hbase.rest.RESTServlet;
32  import org.apache.hadoop.hbase.rest.protobuf.generated.VersionMessage.Version;
33  
34  import com.sun.jersey.spi.container.servlet.ServletContainer;
35  
36  /**
37   * A representation of the collection of versions of the REST gateway software
38   * components.
39   * <ul>
40   * <li>restVersion: REST gateway revision</li>
41   * <li>jvmVersion: the JVM vendor and version information</li>
42   * <li>osVersion: the OS type, version, and hardware architecture</li>
43   * <li>serverVersion: the name and version of the servlet container</li>
44   * <li>jerseyVersion: the version of the embedded Jersey framework</li>
45   * </ul>
46   */
47  @XmlRootElement(name="Version")
48  @InterfaceAudience.Private
49  public class VersionModel implements Serializable, ProtobufMessageHandler {
50  
51  	private static final long serialVersionUID = 1L;
52  
53  	private String restVersion;
54    private String jvmVersion;
55    private String osVersion;
56    private String serverVersion;
57    private String jerseyVersion;
58  
59    /**
60     * Default constructor. Do not use.
61     */
62    public VersionModel() {}
63    
64    /**
65     * Constructor
66     * @param context the servlet context
67     */
68  	public VersionModel(ServletContext context) {
69  	  restVersion = RESTServlet.VERSION_STRING;
70  	  jvmVersion = System.getProperty("java.vm.vendor") + ' ' +
71        System.getProperty("java.version") + '-' +
72        System.getProperty("java.vm.version");
73  	  osVersion = System.getProperty("os.name") + ' ' +
74        System.getProperty("os.version") + ' ' +
75        System.getProperty("os.arch");
76  	  serverVersion = context.getServerInfo();
77  	  jerseyVersion = ServletContainer.class.getPackage()
78        .getImplementationVersion();
79  	}
80  
81  	/**
82  	 * @return the REST gateway version
83  	 */
84  	@XmlAttribute(name="REST")
85  	public String getRESTVersion() {
86      return restVersion;
87    }
88  
89  	/**
90  	 * @return the JVM vendor and version
91  	 */
92    @XmlAttribute(name="JVM")
93    public String getJVMVersion() {
94      return jvmVersion;
95    }
96  
97    /**
98     * @return the OS name, version, and hardware architecture
99     */
100   @XmlAttribute(name="OS")
101   public String getOSVersion() {
102     return osVersion;
103   }
104 
105   /**
106    * @return the servlet container version
107    */
108   @XmlAttribute(name="Server")
109   public String getServerVersion() {
110     return serverVersion;
111   }
112 
113   /**
114    * @return the version of the embedded Jersey framework
115    */
116   @XmlAttribute(name="Jersey")
117   public String getJerseyVersion() {
118     return jerseyVersion;
119   }
120 
121   /**
122    * @param version the REST gateway version string
123    */
124   public void setRESTVersion(String version) {
125     this.restVersion = version;
126   }
127 
128   /**
129    * @param version the OS version string
130    */
131   public void setOSVersion(String version) {
132     this.osVersion = version;
133   }
134 
135   /**
136    * @param version the JVM version string
137    */
138   public void setJVMVersion(String version) {
139     this.jvmVersion = version;
140   }
141 
142   /**
143    * @param version the servlet container version string
144    */
145   public void setServerVersion(String version) {
146     this.serverVersion = version;
147   }
148 
149   /**
150    * @param version the Jersey framework version string
151    */
152   public void setJerseyVersion(String version) {
153     this.jerseyVersion = version;
154   }
155 
156   /* (non-Javadoc)
157 	 * @see java.lang.Object#toString()
158 	 */
159 	@Override
160 	public String toString() {
161 	  StringBuilder sb = new StringBuilder();
162 	  sb.append("rest ");
163 	  sb.append(restVersion);
164 	  sb.append(" [JVM: ");
165 	  sb.append(jvmVersion);
166 	  sb.append("] [OS: ");
167 	  sb.append(osVersion);
168 	  sb.append("] [Server: ");
169 	  sb.append(serverVersion);
170 	  sb.append("] [Jersey: ");
171     sb.append(jerseyVersion);
172 	  sb.append("]\n");
173 	  return sb.toString();
174 	}
175 
176 	@Override
177   public byte[] createProtobufOutput() {
178 	  Version.Builder builder = Version.newBuilder();
179 	  builder.setRestVersion(restVersion);
180 	  builder.setJvmVersion(jvmVersion);
181 	  builder.setOsVersion(osVersion);
182 	  builder.setServerVersion(serverVersion);
183 	  builder.setJerseyVersion(jerseyVersion);
184 	  return builder.build().toByteArray();
185   }
186 
187   @Override
188   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
189       throws IOException {
190     Version.Builder builder = Version.newBuilder();
191     builder.mergeFrom(message);
192     if (builder.hasRestVersion()) {
193       restVersion = builder.getRestVersion();
194     }
195     if (builder.hasJvmVersion()) {
196       jvmVersion = builder.getJvmVersion();
197     }
198     if (builder.hasOsVersion()) {
199       osVersion = builder.getOsVersion();
200     }
201     if (builder.hasServerVersion()) {
202       serverVersion = builder.getServerVersion();
203     }
204     if (builder.hasJerseyVersion()) {
205       jerseyVersion = builder.getJerseyVersion();
206     }
207     return this;
208   }
209 }