001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.rest.model;
021
022import java.io.IOException;
023import java.io.Serializable;
024
025import javax.servlet.ServletContext;
026import javax.xml.bind.annotation.XmlAttribute;
027import javax.xml.bind.annotation.XmlRootElement;
028
029import org.apache.yetus.audience.InterfaceAudience;
030import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
031import org.apache.hadoop.hbase.rest.RESTServlet;
032import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.rest.protobuf.generated.VersionMessage.Version;
034import org.glassfish.jersey.servlet.ServletContainer;
035
036
037/**
038 * A representation of the collection of versions of the REST gateway software
039 * components.
040 * <ul>
041 * <li>restVersion: REST gateway revision</li>
042 * <li>jvmVersion: the JVM vendor and version information</li>
043 * <li>osVersion: the OS type, version, and hardware architecture</li>
044 * <li>serverVersion: the name and version of the servlet container</li>
045 * <li>jerseyVersion: the version of the embedded Jersey framework</li>
046 * </ul>
047 */
048@XmlRootElement(name="Version")
049@InterfaceAudience.Private
050public class VersionModel implements Serializable, ProtobufMessageHandler {
051
052  private static final long serialVersionUID = 1L;
053
054  private String restVersion;
055  private String jvmVersion;
056  private String osVersion;
057  private String serverVersion;
058  private String jerseyVersion;
059
060  /**
061   * Default constructor. Do not use.
062   */
063  public VersionModel() {}
064
065  /**
066   * Constructor
067   * @param context the servlet context
068   */
069  public VersionModel(ServletContext context) {
070    restVersion = RESTServlet.VERSION_STRING;
071    jvmVersion = System.getProperty("java.vm.vendor") + ' ' +
072      System.getProperty("java.version") + '-' +
073      System.getProperty("java.vm.version");
074    osVersion = System.getProperty("os.name") + ' ' +
075      System.getProperty("os.version") + ' ' +
076      System.getProperty("os.arch");
077    serverVersion = context.getServerInfo();
078    jerseyVersion = ServletContainer.class.getPackage().getImplementationVersion();
079    // Currently, this will always be null because the manifest doesn't have any useful information
080    if (jerseyVersion == null) jerseyVersion = "";
081  }
082
083  /**
084   * @return the REST gateway version
085   */
086  @XmlAttribute(name="REST")
087  public String getRESTVersion() {
088    return restVersion;
089  }
090
091  /**
092   * @return the JVM vendor and version
093   */
094  @XmlAttribute(name="JVM")
095  public String getJVMVersion() {
096    return jvmVersion;
097  }
098
099  /**
100   * @return the OS name, version, and hardware architecture
101   */
102  @XmlAttribute(name="OS")
103  public String getOSVersion() {
104    return osVersion;
105  }
106
107  /**
108   * @return the servlet container version
109   */
110  @XmlAttribute(name="Server")
111  public String getServerVersion() {
112    return serverVersion;
113  }
114
115  /**
116   * @return the version of the embedded Jersey framework
117   */
118  @XmlAttribute(name="Jersey")
119  public String getJerseyVersion() {
120    return jerseyVersion;
121  }
122
123  /**
124   * @param version the REST gateway version string
125   */
126  public void setRESTVersion(String version) {
127    this.restVersion = version;
128  }
129
130  /**
131   * @param version the OS version string
132   */
133  public void setOSVersion(String version) {
134    this.osVersion = version;
135  }
136
137  /**
138   * @param version the JVM version string
139   */
140  public void setJVMVersion(String version) {
141    this.jvmVersion = version;
142  }
143
144  /**
145   * @param version the servlet container version string
146   */
147  public void setServerVersion(String version) {
148    this.serverVersion = version;
149  }
150
151  /**
152   * @param version the Jersey framework version string
153   */
154  public void setJerseyVersion(String version) {
155    this.jerseyVersion = version;
156  }
157
158  /* (non-Javadoc)
159   * @see java.lang.Object#toString()
160   */
161  @Override
162  public String toString() {
163    StringBuilder sb = new StringBuilder();
164    sb.append("rest ");
165    sb.append(restVersion);
166    sb.append(" [JVM: ");
167    sb.append(jvmVersion);
168    sb.append("] [OS: ");
169    sb.append(osVersion);
170    sb.append("] [Server: ");
171    sb.append(serverVersion);
172    sb.append("] [Jersey: ");
173    sb.append(jerseyVersion);
174    sb.append("]\n");
175    return sb.toString();
176  }
177
178  @Override
179  public byte[] createProtobufOutput() {
180    Version.Builder builder = Version.newBuilder();
181    builder.setRestVersion(restVersion);
182    builder.setJvmVersion(jvmVersion);
183    builder.setOsVersion(osVersion);
184    builder.setServerVersion(serverVersion);
185    builder.setJerseyVersion(jerseyVersion);
186    return builder.build().toByteArray();
187  }
188
189  @Override
190  public ProtobufMessageHandler getObjectFromMessage(byte[] message)
191      throws IOException {
192    Version.Builder builder = Version.newBuilder();
193    ProtobufUtil.mergeFrom(builder, message);
194    if (builder.hasRestVersion()) {
195      restVersion = builder.getRestVersion();
196    }
197    if (builder.hasJvmVersion()) {
198      jvmVersion = builder.getJvmVersion();
199    }
200    if (builder.hasOsVersion()) {
201      osVersion = builder.getOsVersion();
202    }
203    if (builder.hasServerVersion()) {
204      serverVersion = builder.getServerVersion();
205    }
206    if (builder.hasJerseyVersion()) {
207      jerseyVersion = builder.getJerseyVersion();
208    }
209    return this;
210  }
211}