Source code: com/flexstor/common/data/ejb/server/ServerData.java
1 /*
2 * ServerData.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.server;
12
13 import java.sql.Timestamp;
14 import java.util.Enumeration;
15 import java.util.Hashtable;
16
17 import com.flexstor.common.constants.RsrcForkConstantsI;
18 import com.flexstor.common.data.ejb.Data;
19 import com.flexstor.common.exceptions.ejb.EjbException;
20 import com.flexstor.common.io.FileAccessProtocol;
21 import com.flexstor.common.keys.ejb.ServerKey;
22
23 /**
24 * This class supports state data for <code>Server</code>. A Data class holds <br>
25 * all of the state information for a corresponding class and reference <br>
26 * pointers to other classes <br>
27 *
28 * @author Perry Hoekstra
29 * @version 1.0, 1/25/99
30 *
31 * @since FLEXSTOR.db 3.0
32 */
33
34 public class ServerData
35 extends Data
36 {
37 // MKS macro expander
38 public final static String IDENTIFIER = "$Id: ServerData.java,v 1.3 2003/08/11 02:22:50 aleric Exp $";
39 static final long serialVersionUID = 7654896623621347567L;
40 protected String name = null;
41 //protected String port = null;
42 protected Hashtable ports = null;
43 protected String dnsName = null;
44 protected String ipAddress = null;
45 protected int appleTalkVendor = RsrcForkConstantsI.NONE;
46
47 // timestamp of record in database when the object was retrieved
48 protected Timestamp currentTimestamp;
49
50 // object reference to this instance in the database
51 protected ServerKey serverKey;
52
53 /**
54 * Default constructor for the ServerData class<p>
55 *
56 * <b>Warning</b> Any objects created with this constructor are not updatable or insertable to the database
57 */
58
59 public ServerData()
60 {
61 super();
62 ports = new Hashtable(15);
63 }
64
65 /**
66 * Constructor for the ServerDataBuild class<p>
67 *
68 * <b>Warning</b> Any objects created with this constructor are not updatable (insertable, but not updatable) to the database
69 *
70 * @param java.lang.String a user-defined name for this server
71 */
72
73 public ServerData(String aName)
74 {
75 name = aName;
76 ports = new Hashtable(15);
77 }
78
79 /**
80 * Determine if these two objects are equal, i.e. their attributes are equal. This method <b>will not</b> test the
81 * equality of references to other objects
82 *
83 * @param com.flexstor.common.data.ejb.user.ServerData a server data object
84 *
85 * @return boolean - <code>true</code> if the objects are equal; <code>false</code> if they are not
86 */
87
88 public boolean equals(ServerData aDataObject)
89 {
90 boolean objectsEqual = true;
91
92 if (!(isNameEqual(aDataObject.getName()))) {
93 objectsEqual = false;
94 }
95
96 if (!(arePortsEqual(aDataObject.getPorts()))) {
97 objectsEqual = false;
98 }
99
100 if (!(isDNSNameEqual(aDataObject.getDNSName()))) {
101 objectsEqual = false;
102 }
103
104 if (!(isIPAddressEqual(aDataObject.getIPAddress()))) {
105 objectsEqual = false;
106 }
107
108 return objectsEqual;
109 }
110
111 /**
112 * Return the DNS (domain name service) name for this server
113 *
114 * @return java.lang.String
115 */
116
117 public String getDNSName()
118 {
119 if (dnsName == null) {
120 dnsName = new String();
121 }
122
123 return dnsName;
124 }
125
126 /**
127 * Return the ip address for this server
128 *
129 * @return java.lang.String
130 */
131
132 public String getIPAddress()
133 {
134 if (ipAddress == null)
135 ipAddress = new String();
136
137 return ipAddress;
138 }
139
140 /**
141 * Return the user-defined name for this server
142 *
143 * @return java.lang.String
144 */
145
146 public String getName()
147 {
148 if (name == null)
149 name = new String();
150
151 return name;
152 }
153
154 /**
155 * Appletalk Vendor getter method
156 */
157 public int getAppleTalkVendor()
158 {
159 return appleTalkVendor;
160 }
161
162 /**
163 * Appletalk Vendor setting method
164 */
165 public void setAppleTalkVendor( int vendor )
166 {
167 appleTalkVendor = vendor;
168 }
169
170 /**
171 * Set the user-defined name for this server
172 *
173 * @param java.lang.String
174 */
175
176 public void setName( String sName )
177 {
178 if (sName == null)
179 name = new String();
180
181 name = sName;
182 }
183
184 /**
185 * Returns the port number for a specific port type
186 * @param Integer Port Type
187 *
188 * @return java.lang.Integer
189 */
190 public Integer getPort( Integer nPortType )
191 throws EjbException
192 {
193 Integer nPort = (Integer)ports.get( nPortType );
194 if ( nPort == null )
195 throw new EjbException( "ServerData", "getPort", "Undefined port number." );
196
197 return nPort;
198 }
199
200 /**
201 * Returns the port number as a String for a specific port type as a string
202 * @param String Port Type Name
203 *
204 * @return java.lang.String - Number of port
205 */
206 public String getPort( String sPortName )
207 throws EjbException
208 {
209 //Change the String name to an ID
210 return getPort( new Integer( FileAccessProtocol.getProtocolId( sPortName ) ) ).toString();
211 }
212
213 /**
214 * Returns the port number for the default HTTP port
215 *
216 * @return java.lang.String
217 */
218 public String getPort()
219 throws EjbException
220 {
221 return getPort( new Integer( com.flexstor.common.io.FileAccessProtocol.HTTP_ID ) ).toString();
222 }
223
224 public Hashtable getPorts()
225 {
226 return ports;
227 }
228
229 /**
230 * Return the reference to this object
231 *
232 * @return com.flexstor.common.keys.ejb.ServerKey - an object reference
233 * @deprecated see getKey()
234 */
235
236 public ServerKey getReference()
237 {
238 return serverKey;
239 }
240
241 /**
242 * Return the reference to this object
243 *
244 * @return com.flexstor.common.keys.ejb.ServerKey - an object reference
245 */
246
247 public ServerKey getKey()
248 {
249 return serverKey;
250 }
251
252 /**
253 * Return the timestamp of this object in database when the object was retrieved
254 *
255 * @return java.sql.Timestamp
256 */
257
258 public Timestamp getTimestamp()
259 {
260 return currentTimestamp;
261 }
262
263 /**
264 * Checks whether two DNS name strings have equal values.
265 *
266 * @param java.lang.String a DNS name
267 *
268 * @return boolean
269 */
270
271 private boolean isDNSNameEqual(String aDNSName)
272 {
273 boolean objectsEqual = true;
274
275 //check DNSName
276 if (getDNSName() != null) {
277 if (aDNSName != null) {
278 if (getDNSName().equals(aDNSName)) {
279 //continue on
280 }
281 else {
282 objectsEqual = false;
283 }
284 }
285 else {
286 objectsEqual = false;
287 }
288 }
289 else {
290 if (aDNSName != null) {
291 if (aDNSName.equals("null")) {
292 // continue on
293 }
294 else {
295 objectsEqual = false;
296 }
297 }
298 }
299
300 return objectsEqual;
301 }
302
303 /**
304 * Checks whether two TCP/IP address strings have equal values.
305 *
306 * @param java.lang.String a TCP/IP address
307 *
308 * @return boolean
309 */
310
311 private boolean isIPAddressEqual(String anIPAddress)
312 {
313 boolean objectsEqual = true;
314
315 //check IPAddress
316 if (getIPAddress() != null) {
317 if (anIPAddress != null) {
318 if (getIPAddress().equals(anIPAddress)) {
319 //continue on
320 }
321 else {
322 objectsEqual = false;
323 }
324 }
325 else {
326 objectsEqual = false;
327 }
328 }
329 else {
330 if (anIPAddress != null) {
331 if (anIPAddress.equals("null")) {
332 // continue on
333 }
334 else {
335 objectsEqual = false;
336 }
337 }
338 }
339
340 return objectsEqual;
341 }
342
343 /**
344 * Checks whether two name strings have equal values.
345 *
346 * @param java.lang.String a server name
347 *
348 * @return boolean
349 */
350
351 private boolean isNameEqual(String aName)
352 {
353 boolean objectsEqual = true;
354
355 //check Name
356 if (getName() != null) {
357 if (aName != null) {
358 if (getName().equals(aName)) {
359 //continue on
360 }
361 else {
362 objectsEqual = false;
363 }
364 }
365 else {
366 objectsEqual = false;
367 }
368 }
369 else {
370 if (aName != null) {
371 if (aName.equals("null")) {
372 // continue on
373 }
374 else {
375 objectsEqual = false;
376 }
377 }
378 }
379
380 return objectsEqual;
381 }
382 /**Checks if all port data is equal
383 *
384 *@param java.util.Hashtable a collection of ports
385 *
386 *@return boolean
387 *
388 */
389 private boolean arePortsEqual(Hashtable aPorts)
390 {
391 boolean objectEqual = true;
392 if (checkSize(aPorts))
393 {
394 Enumeration p_keys = aPorts.keys();
395 Integer nType = null;
396 Integer nPort = null;
397 int n = 1;
398 //compare all of the elements in the first hashtable with the current hashtable
399 while (p_keys.hasMoreElements() && (objectEqual))
400 {
401 System.out.println(n++ + " times through arePortsEqual");
402 nType = (Integer) p_keys.nextElement();
403 nPort = (Integer) aPorts.get(nType);
404 System.out.println("The type and port of sent in is: " + nType+ " " + nPort);
405 //See if this type, port is equal to the ports of the current server
406 objectEqual = isPortEqual(nType, nPort);
407 }
408 }
409 else
410 {
411 //Not the same number, can not be equal
412 objectEqual = false;
413 }
414 return objectEqual;
415 }
416
417 /**
418 *Checks to see if both port hashtables are the same size
419 *
420 * @param java.util.Hashtable - Port hashtable
421 *
422 * @return boolean
423 * */
424 private boolean checkSize(Hashtable aPorts)
425 {
426 return new Integer(ports.size()).equals(new Integer(aPorts.size()));
427 }
428
429 /**
430 * Checks whether two server port types have equal ports
431 *
432 * @param java.lang.Integer - A port type
433 * @param java.lang.Integer - A port
434 *
435 * @return boolean
436 */
437
438 private boolean isPortEqual(Integer nType, Integer nPort)
439 {
440 System.out.println("Inside isPortEqual");
441 boolean objectsEqual = true;
442 //get the current servers ports
443 Hashtable curPorts = getPorts();
444 //see if the type exists in curPorts
445 if (curPorts.containsKey(nType))
446 {
447 if (!(curPorts.get(nType).equals(nPort)))
448 {
449 System.out.println(curPorts.get(nType) + "Not Equal");
450 //If they don't equal, set to false
451 objectsEqual = false;
452 }
453
454 }
455 else
456 {
457 System.out.println("key not found");
458 objectsEqual = false;
459 }
460
461 return objectsEqual;
462 }
463
464 /**
465 * Return the user-defined name for this server
466 *
467 * @param java.lang.String user-defined name
468 */
469
470 public void setDNSName(String aDNSName)
471 {
472 dnsName = aDNSName;
473 }
474
475 /**
476 * Set the ip address for this server
477 *
478 * @param java.lang.String the ip address
479 */
480
481 public void setIPAddress(String anIPAddress)
482 {
483 ipAddress = anIPAddress;
484 }
485
486 /**
487 * Sets the hashtable for the ports for this server
488 *
489 *@param java.util.Hashtable
490 */
491
492 public void setPorts(Hashtable aPorts)
493 {
494 ports = aPorts;
495 }
496
497 /**
498 * Sets the default port information HTTP
499 *
500 *@param java.util.Integer
501 */
502
503 public void setPort(Integer aPort)
504 {
505 updatePort(new Integer(com.flexstor.common.io.FileAccessProtocol.HTTP_ID), aPort);
506 }
507
508 public void updatePort(Integer nPortType, Integer nPort)
509 {
510 ports.put(nPortType, nPort);
511 }
512 /**
513 * Assign the reference to this record
514 *
515 * @param com.flexstor.common.keys.ejb.ServerKey an object reference
516 * @deprecated see setKey()
517 */
518
519 public void setReference(ServerKey aKey)
520 {
521 serverKey = aKey;
522 }
523
524 /**
525 * Assign the reference to this record
526 *
527 * @param com.flexstor.common.keys.ejb.ServerKey an object reference
528 */
529
530 public void setKey(ServerKey aKey)
531 {
532 serverKey = aKey;
533 }
534
535 /**
536 * Assign the current timestamp of the object
537 *
538 * @param java.sql.Timestamp current timestamp of the database object
539 */
540
541 public void setTimestamp(Timestamp aTimeStamp)
542 {
543 currentTimestamp = aTimeStamp;
544 }
545
546 /**
547 * Return the contents of the instance as a String. This method is strictly for use by testing classes<br>
548 * and should not be called by business objects
549 *
550 * @return java.lang.String - String representation of ServerData contents
551 *
552 */
553 public String toString()
554 {
555 String t_lineSeparator = System.getProperty("line.separator", "\n");
556
557 StringBuffer t_serverContents = new StringBuffer(t_lineSeparator);
558
559 t_serverContents.append("Class: ServerData");
560 t_serverContents.append(t_lineSeparator);
561
562 t_serverContents.append("Name: ");
563 t_serverContents.append(getName());
564 t_serverContents.append(t_lineSeparator);
565
566 t_serverContents.append("DNSName: ");
567 t_serverContents.append(getDNSName());
568 t_serverContents.append(t_lineSeparator);
569
570 t_serverContents.append("IPAddress: ");
571 t_serverContents.append(getIPAddress());
572 t_serverContents.append(t_lineSeparator);
573
574 t_serverContents.append("Port: ");
575 t_serverContents.append(getPorts());
576 t_serverContents.append(t_lineSeparator);
577
578 return t_serverContents.toString();
579 }
580 }