Source code: com/flexstor/common/data/ejb/machine/MachineData.java
1 /*
2 * MachineData.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:50 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.data.ejb.machine;
12
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15
16 import com.flexstor.common.data.ejb.Data;
17 import com.flexstor.common.keys.ejb.MachineKey;
18
19 /**
20 * This class supports state data for <code>Machine</code>. A Data class holds
21 * all of the state information for Machine and reference pointers
22 * to other classes
23 *
24 * @author Perry Hoekstra
25 * @version 1.0, 12/14/98
26 *
27 * @see com.flexstor.ejb.flexstor.machine.Machine
28 *
29 * @since FLEXSTOR.db 3.0
30 */
31
32 public class MachineData
33 extends Data
34 {
35 // MKS macro expander
36 public final static String IDENTIFIER = "$Id: MachineData.java,v 1.4 2003/08/11 02:22:50 aleric Exp $";
37 static final long serialVersionUID = -3732204267870640481L;
38
39 // name of machine that this data pertains to
40 protected String machineName = null;
41
42 // indicates that data has changed
43 private boolean isDirty = false;
44
45 // collection of settings for this machine
46 protected Hashtable machineSettings = null;
47
48 // object reference to this instance in the database
49 protected MachineKey machineKey = null;
50
51 /**
52 * Default constructor for MachineData class<p>
53 *
54 * <b>Warning</b> Any objects created with this constructor are not updatable or insertable to the database
55 */
56
57 public MachineData()
58 {
59 }
60
61 /**
62 * Constructor for MachineData class<p>
63 *
64 * <b>Warning</b> Any objects created with this constructor are not updatable (insertable, but not updatable) to the database<br>
65 *
66 * @param java.lang.String a machine name (or host name)
67 */
68
69 public MachineData(String aMachineName)
70 {
71 machineName = aMachineName;
72 machineSettings = new Hashtable(10);
73 }
74
75 /**
76 * Remove a machine setting from the collection of machine settings
77 *
78 * @param java.lang.String machine setting to be deleted from the collection
79 */
80
81 public void deleteMachineSetting(String aKey)
82 {
83 machineSettings.remove(aKey);
84 isDirty = true;
85 }
86
87 /**
88 * Determine if these two objects are equal, i.e. their attributes are equal. This method <b>will not</b> test the
89 * equality of references to other objects
90 *
91 * @param com.flexstor.common.data.ejb.machine.MachineData the data object to compare this <code>data object</code> against
92 *
93 * @return boolean - <code>true</code> if the objects are equal; <code>false</code> if they are not
94 */
95
96 public boolean equals(MachineData aDataObject) {
97 boolean objectsEqual = true;
98
99 if (assertEquals(getMachineName(), aDataObject.getMachineName()) == false) {
100 return false;
101 }
102
103 //get keys from one hashtable
104 Enumeration t_keys = machineSettings.keys();
105 String t_key = null;
106
107 Object t_object = null;
108 String t_value1 = null;
109 String t_value2 = null;
110
111 while (t_keys.hasMoreElements()) {
112 t_key = (String)t_keys.nextElement();
113
114 t_object = machineSettings.get(t_key);
115
116 if (t_object != null) {
117 t_value1 = (String)t_object;
118 }
119
120 t_object = aDataObject.getMachineSetting(t_key);
121
122 if (t_object != null) {
123 t_value2 = (String)t_object;
124 }
125
126 if (t_value1.equals(t_value2)) {
127 //continue on
128 }
129 else {
130 objectsEqual = false;
131 break;
132 }
133 }
134
135 return objectsEqual;
136 }
137 /**
138 * Return the machine's assigned name (or host name).
139 *
140 * @return java.lang.String - a machine name
141 */
142
143 public String getMachineName() {
144 if (machineName == null) {
145 machineName = new String();
146 }
147
148 return machineName;
149 }
150 /**
151 * Retrieve a value based on the given key
152 *
153 * @param java.lang.String the unique key of a given MachineSetting
154 *
155 * @return java.lang.String - the associated value
156 */
157
158 public String getMachineSetting(String aKey) {
159 return((String)machineSettings.get(aKey));
160 }
161 /**
162 * A collection of settings pertaining to this machine
163 *
164 * @return java.util.Hashtable - A collection of settings
165 *
166 * <ul>Hashtable parameters
167 * <li>key - String
168 * <li>value - String
169 * </ul>
170 */
171
172 public Hashtable getMachineSettings() {
173 return machineSettings;
174 }
175
176 /**
177 * Return this instance's database object reference
178 *
179 * @return com.flexstor.common.keys.ejb.MachineKey - an object reference to a row in the database
180 * @deprecated see getKey()
181 */
182
183 public MachineKey getReference()
184 {
185 return machineKey;
186 }
187
188 /**
189 * Return this instance's database object reference
190 *
191 * @return com.flexstor.common.keys.ejb.MachineKey - an object reference to a row in the database
192 */
193
194 public MachineKey getKey()
195 {
196 return machineKey;
197 }
198
199 /**
200 * Return this instance's database object reference
201 *
202 * @return com.flexstor.common.keys.ejb.MachineKey - an object reference to a row in the database
203 */
204
205 public void setKey( MachineKey key )
206 {
207 machineKey = key;
208 }
209
210 /**
211 * Indicates whether a change has been made to data contained within the object
212 *
213 * @return boolean
214 */
215
216 public boolean isDirty() {
217 return isDirty;
218 }
219 /**
220 * Return the contents of the instance as a String. This method is strictly for use by testing classes<br>
221 * and should not be called by business objects
222 *
223 * @return java.lang.String - String representation of MachineData contents
224 *
225 */
226 public String toString() {
227 String t_lineSeparator = System.getProperty("line.separator", "\n");
228
229 StringBuffer t_roMachineDataContents = new StringBuffer(t_lineSeparator);
230
231 t_roMachineDataContents.append("Class: MachineData");
232 t_roMachineDataContents.append(t_lineSeparator);
233 t_roMachineDataContents.append("MachineName: ");
234 t_roMachineDataContents.append(getMachineName());
235 t_roMachineDataContents.append(t_lineSeparator);
236
237 Enumeration t_roMachineDataEnum = machineSettings.keys();
238
239 String t_key = null;
240 String t_value = null;
241
242 while (t_roMachineDataEnum.hasMoreElements()) {
243 t_key = (String)t_roMachineDataEnum.nextElement();
244
245 t_roMachineDataContents.append("Key: ");
246 t_roMachineDataContents.append(t_key);
247 t_roMachineDataContents.append(t_lineSeparator);
248
249 t_value = (String)machineSettings.get(t_key);
250
251 t_roMachineDataContents.append("Value: ");
252 t_roMachineDataContents.append(t_value);
253 t_roMachineDataContents.append(t_lineSeparator);
254 }
255
256 return t_roMachineDataContents.toString();
257 }
258
259 /**
260 * Add or change a machine setting used by the machine
261 *
262 * @param java.lang.String key of machine setting to be changed in or added to the collection
263 * @param java.lang.String value of machine setting to be changed or added
264 */
265
266 public void updateMachineSetting(String aKey, String aValue)
267 {
268 machineSettings.put(aKey, aValue);
269 isDirty = true;
270 }
271
272 /**
273 * Set the Machine Name
274 *
275 */
276 public void setMachineName( String name )
277 {
278 machineName = name;
279 }
280
281 /**
282 * Set the machine Settings
283 *
284 */
285 public void setMachineSettings( Hashtable hash )
286 {
287 machineSettings = hash;
288 }
289 }