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;
024import javax.servlet.ServletContext;
025import javax.xml.bind.annotation.XmlAttribute;
026import javax.xml.bind.annotation.XmlRootElement;
027import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
028import org.apache.hadoop.hbase.rest.RESTServlet;
029import org.apache.yetus.audience.InterfaceAudience;
030
031import org.apache.hbase.thirdparty.org.glassfish.jersey.servlet.ServletContainer;
032
033import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
034import org.apache.hadoop.hbase.shaded.rest.protobuf.generated.VersionMessage.Version;
035
036/**
037 * A representation of the collection of versions of the REST gateway software
038 * components.
039 * <ul>
040 * <li>restVersion: REST gateway revision</li>
041 * <li>jvmVersion: the JVM vendor and version information</li>
042 * <li>osVersion: the OS type, version, and hardware architecture</li>
043 * <li>serverVersion: the name and version of the servlet container</li>
044 * <li>jerseyVersion: the version of the embedded Jersey framework</li>
045 * </ul>
046 */
047@XmlRootElement(name="Version")
048@InterfaceAudience.Private
049public class VersionModel implements Serializable, ProtobufMessageHandler {
050
051  private static final long serialVersionUID = 1L;
052
053  private String restVersion;
054  private String jvmVersion;
055  private String osVersion;
056  private String serverVersion;
057  private String jerseyVersion;
058
059  /**
060   * Default constructor. Do not use.
061   */
062  public VersionModel() {}
063
064  /**
065   * Constructor
066   * @param context the servlet context
067   */
068  public VersionModel(ServletContext context) {
069    restVersion = RESTServlet.VERSION_STRING;
070    jvmVersion = System.getProperty("java.vm.vendor") + ' ' +
071      System.getProperty("java.version") + '-' +
072      System.getProperty("java.vm.version");
073    osVersion = System.getProperty("os.name") + ' ' +
074      System.getProperty("os.version") + ' ' +
075      System.getProperty("os.arch");
076    serverVersion = context.getServerInfo();
077    jerseyVersion = ServletContainer.class.getPackage().getImplementationVersion();
078    // Currently, this will always be null because the manifest doesn't have any useful information
079    if (jerseyVersion == null) jerseyVersion = "";
080  }
081
082  /**
083   * @return the REST gateway version
084   */
085  @XmlAttribute(name="REST")
086  public String getRESTVersion() {
087    return restVersion;
088  }
089
090  /**
091   * @return the JVM vendor and version
092   */
093  @XmlAttribute(name="JVM")
094  public String getJVMVersion() {
095    return jvmVersion;
096  }
097
098  /**
099   * @return the OS name, version, and hardware architecture
100   */
101  @XmlAttribute(name="OS")
102  public String getOSVersion() {
103    return osVersion;
104  }
105
106  /**
107   * @return the servlet container version
108   */
109  @XmlAttribute(name="Server")
110  public String getServerVersion() {
111    return serverVersion;
112  }
113
114  /**
115   * @return the version of the embedded Jersey framework
116   */
117  @XmlAttribute(name="Jersey")
118  public String getJerseyVersion() {
119    return jerseyVersion;
120  }
121
122  /**
123   * @param version the REST gateway version string
124   */
125  public void setRESTVersion(String version) {
126    this.restVersion = version;
127  }
128
129  /**
130   * @param version the OS version string
131   */
132  public void setOSVersion(String version) {
133    this.osVersion = version;
134  }
135
136  /**
137   * @param version the JVM version string
138   */
139  public void setJVMVersion(String version) {
140    this.jvmVersion = version;
141  }
142
143  /**
144   * @param version the servlet container version string
145   */
146  public void setServerVersion(String version) {
147    this.serverVersion = version;
148  }
149
150  /**
151   * @param version the Jersey framework version string
152   */
153  public void setJerseyVersion(String version) {
154    this.jerseyVersion = version;
155  }
156
157  /* (non-Javadoc)
158   * @see java.lang.Object#toString()
159   */
160  @Override
161  public String toString() {
162    StringBuilder sb = new StringBuilder();
163    sb.append("rest ");
164    sb.append(restVersion);
165    sb.append(" [JVM: ");
166    sb.append(jvmVersion);
167    sb.append("] [OS: ");
168    sb.append(osVersion);
169    sb.append("] [Server: ");
170    sb.append(serverVersion);
171    sb.append("] [Jersey: ");
172    sb.append(jerseyVersion);
173    sb.append("]\n");
174    return sb.toString();
175  }
176
177  @Override
178  public byte[] createProtobufOutput() {
179    Version.Builder builder = Version.newBuilder();
180    builder.setRestVersion(restVersion);
181    builder.setJvmVersion(jvmVersion);
182    builder.setOsVersion(osVersion);
183    builder.setServerVersion(serverVersion);
184    builder.setJerseyVersion(jerseyVersion);
185    return builder.build().toByteArray();
186  }
187
188  @Override
189  public ProtobufMessageHandler getObjectFromMessage(byte[] message)
190      throws IOException {
191    Version.Builder builder = Version.newBuilder();
192    ProtobufUtil.mergeFrom(builder, message);
193    if (builder.hasRestVersion()) {
194      restVersion = builder.getRestVersion();
195    }
196    if (builder.hasJvmVersion()) {
197      jvmVersion = builder.getJvmVersion();
198    }
199    if (builder.hasOsVersion()) {
200      osVersion = builder.getOsVersion();
201    }
202    if (builder.hasServerVersion()) {
203      serverVersion = builder.getServerVersion();
204    }
205    if (builder.hasJerseyVersion()) {
206      jerseyVersion = builder.getJerseyVersion();
207    }
208    return this;
209  }
210}