View Javadoc

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