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