View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.jclouds.compute.domain;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import org.jclouds.javax.annotation.Nullable;
24  
25  import com.google.common.annotations.Beta;
26  
27  /**
28   * Running Operating system
29   * 
30   * @author Adrian Cole
31   */
32  @Beta
33  public class OperatingSystem {
34  
35     public static Builder builder() {
36        return new Builder();
37     }
38  
39     public static class Builder {
40        protected OsFamily family;
41        protected String name;
42        protected String arch;
43        protected String version;
44        protected String description;
45        protected boolean is64Bit;
46  
47        public Builder family(@Nullable OsFamily family) {
48           this.family = family;
49           return this;
50        }
51  
52        public Builder name(@Nullable String name) {
53           this.name = name;
54           return this;
55        }
56  
57        public Builder arch(@Nullable String arch) {
58           this.arch = arch;
59           return this;
60        }
61  
62        public Builder version(@Nullable String version) {
63           this.version = version;
64           return this;
65        }
66  
67        public Builder description(String description) {
68           this.description = checkNotNull(description, "description");
69           return this;
70        }
71  
72        public Builder is64Bit(boolean is64Bit) {
73           this.is64Bit = is64Bit;
74           return this;
75        }
76  
77        public OperatingSystem build() {
78           return new OperatingSystem(family, name, version, arch, description, is64Bit);
79        }
80  
81        public Builder fromOperatingSystem(OperatingSystem in) {
82           return family(in.getFamily()).name(in.getName()).version(in.getVersion()).arch(in.getArch()).description(
83                    in.getDescription()).is64Bit(in.is64Bit());
84        }
85     }
86  
87     @Nullable
88     protected OsFamily family;
89     @Nullable
90     protected String name;
91     @Nullable
92     protected String arch;
93     @Nullable
94     protected String version;
95     protected String description;
96     protected boolean is64Bit;
97  
98     // for serialization/deserialization
99     protected OperatingSystem() {
100 
101    }
102 
103    public OperatingSystem(@Nullable OsFamily family, @Nullable String name, @Nullable String version,
104             @Nullable String arch, String description, boolean is64Bit) {
105       this.family = family;
106       this.name = name;
107       this.arch = arch;
108       this.version = version;
109       this.description = checkNotNull(description, "description");
110       this.is64Bit = is64Bit;
111    }
112 
113    /**
114     * Type of the operating system
115     * <p/>
116     * generally, this is used to compare the means by which you use an operating system. For
117     * example, to determine compatibility of a particular bootstrapping or package installation
118     * approach.
119     */
120    @Nullable
121    public OsFamily getFamily() {
122       return family;
123    }
124 
125    /**
126     * name of the operating system; ex. {@code Red Hat Enterprise Linux}
127     * 
128     * <h2>note</h2> While this looks similar to, and may in some cases be the same as the java
129     * system property {@code os.name} it isn't guaranteed to match a particular value. For example,
130     * this value could be derived from data parsed for a cloud api or the OVF CIM OSType enum value;
131     * 
132     * @return operating system name or null if it couldn't be determined.
133     */
134    @Nullable
135    public String getName() {
136       return name;
137    }
138 
139    /**
140     * architecture of the operating system; ex. {@code x86_64}
141     * <p/>
142     * generally, this is used to decide whether an operating system will run certain binaries, for
143     * example, a 64bit JDK.
144     * 
145     * <h2>note</h2>
146     * While this looks similar to, and may in some cases be the same as the java system property
147     * {@code os.arch} it isn't guaranteed to match a particular value. For example, this value could
148     * be derived from data parsed for a cloud api or the OVF CIM OSType enum value;
149     * 
150     * @return operating system architecture or null if it couldn't be determined.
151     */
152    @Nullable
153    public String getArch() {
154       return arch;
155    }
156 
157    /**
158     * version of the operating system; ex. {@code 10.0.4}
159     * <p/>
160     * generally, this is used to compare versions of the same operating system name. It should be
161     * meaningful when sorted against, although this isn't necessary.
162     * <h2>note</h2>
163     * While this looks similar to, and may in some cases be the same as the java system property
164     * {@code os.version} it isn't guaranteed to match a particular value. For example, this value
165     * could be derived from data parsed for a cloud api or the OVF CIM OSType enum value;
166     * 
167     * @return operating system version or null if it couldn't be determined.
168     */
169    @Nullable
170    public String getVersion() {
171       return version;
172    }
173 
174    /**
175     * description of the operating system; ex. {@code CentOS 32-bit},{@code Other Linux (32-bit)}
176     * <p/>
177     * This is the only required field in the operating system object. In some implementations, it is
178     * this data that is used to parse the value of the {@link #name}, {@link #version}, and
179     * {@link #arch} fields.
180     * 
181     * @return operating system description
182     */
183    public String getDescription() {
184       return description;
185    }
186 
187    /**
188     * 
189     * @return whether this operating system supports 64 bit computation.
190     */
191    public boolean is64Bit() {
192       return is64Bit;
193    }
194 
195    @Override
196    public int hashCode() {
197       final int prime = 31;
198       int result = 1;
199       result = prime * result + ((arch == null) ? 0 : arch.hashCode());
200       result = prime * result + ((description == null) ? 0 : description.hashCode());
201       result = prime * result + ((family == null) ? 0 : family.hashCode());
202       result = prime * result + (is64Bit ? 1231 : 1237);
203       result = prime * result + ((name == null) ? 0 : name.hashCode());
204       result = prime * result + ((version == null) ? 0 : version.hashCode());
205       return result;
206    }
207 
208    @Override
209    public boolean equals(Object obj) {
210       if (this == obj)
211          return true;
212       if (obj == null)
213          return false;
214       if (getClass() != obj.getClass())
215          return false;
216       OperatingSystem other = (OperatingSystem) obj;
217       if (arch == null) {
218          if (other.arch != null)
219             return false;
220       } else if (!arch.equals(other.arch))
221          return false;
222       if (description == null) {
223          if (other.description != null)
224             return false;
225       } else if (!description.equals(other.description))
226          return false;
227       if (family == null) {
228          if (other.family != null)
229             return false;
230       } else if (!family.equals(other.family))
231          return false;
232       if (is64Bit != other.is64Bit)
233          return false;
234       if (name == null) {
235          if (other.name != null)
236             return false;
237       } else if (!name.equals(other.name))
238          return false;
239       if (version == null) {
240          if (other.version != null)
241             return false;
242       } else if (!version.equals(other.version))
243          return false;
244       return true;
245    }
246 
247    public Builder toBuilder() {
248       return builder().fromOperatingSystem(this);
249    }
250 
251    @Override
252    public String toString() {
253       return "[name=" + name + ", family=" + family + ", version=" + version + ", arch=" + arch + ", is64Bit="
254                + is64Bit + ", description=" + description + "]";
255    }
256 
257 }