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