Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/ServerList.java


1   /*
2    * ServerList.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:31 $ 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.util;
12  
13  import java.util.Enumeration;
14  import java.util.Hashtable;
15  import java.util.Vector;
16  
17  import com.flexstor.common.constants.RsrcForkConstantsI;
18  import com.flexstor.common.data.ejb.server.ServerData;
19  import com.flexstor.common.exceptions.ejb.EjbException;
20  import com.flexstor.common.gateway.ServerListGateway;
21  import com.flexstor.common.gateway.exceptions.TransactionFailedException;
22  import com.flexstor.common.keys.ejb.ServerCollectionKey;
23  
24  public class ServerList
25  {
26     private static ServerList instance;
27     private static Hashtable    servers = new Hashtable();
28     private static final boolean debug = true;
29     
30     private ServerList ( )
31     {
32     }
33     
34     public static ServerList getInstance ( )
35     {
36        if ( instance == null )
37           instance = new ServerList();
38           
39        return instance;
40     }
41     
42     /**
43      * Loads a list of all available servers.
44      */
45     public static boolean initialize ( )
46     {
47        ServerListGateway gateway = null;
48  
49        try
50        {
51           gateway = new ServerListGateway();
52           gateway.connect();
53           servers = gateway.getServerList();
54           return true;
55        }
56        catch ( TransactionFailedException e )
57        {
58           return false;
59        }
60        finally
61        {
62           if ( gateway != null )
63              gateway.dispose();
64        }
65     }
66  
67     /**
68      * Loads a list of servers for the specified keys.
69      * @param vKeys a Vector of ServerCollectionKey objects.
70      */
71     public static boolean initialize ( Vector vKeys )
72     {
73        ServerListGateway gateway = null;
74        
75        //Check if the vector is null or empty..
76        
77        if (vKeys == null || vKeys.size()==0 ) return false;
78        
79        //Check if the collection keys are null or empty
80        
81        boolean bUsefulCollection = false;
82        
83        for(int i=0;i<vKeys.size();i++)
84        {
85          if( ( (ServerCollectionKey) vKeys.elementAt(i)) !=null &&
86          ( (ServerCollectionKey) vKeys.elementAt(i)).getKeys().length > 0 ) 
87              {
88                  bUsefulCollection=true;
89                  break;
90              }
91        }
92        
93        if(!bUsefulCollection) return false;
94  
95        try
96        {
97           gateway = new ServerListGateway();
98           gateway.connect();
99           servers = gateway.getServerList ( vKeys );
100          return true;
101       }
102       catch ( TransactionFailedException e )
103       {
104          return false;
105       }
106       finally
107       {
108          if ( gateway != null )
109             gateway.dispose();
110       }
111    }
112    
113    /**
114     * Return a ServerData object based on the argument.
115     * 
116     * @param sDesc server name, DNS or IP Address
117     * @return ServerData or null if argument doesn't match any server
118     */
119    private static ServerData getServerData ( String sDesc )
120    {
121       if ( servers == null || servers.isEmpty() )
122          return null;
123          
124       // First attempt to use the argument as the key in the hashtable (server name); 
125       // if this fails then loop through each item in the Hashtable and compare the argument
126       // to the DNS and IP Address
127       ServerData data = (ServerData) servers.get( sDesc );
128       if ( data != null )
129          return data;
130          
131       for ( Enumeration e = servers.keys(); e.hasMoreElements(); )
132       {
133          data = (ServerData) servers.get( (String)e.nextElement() );
134          
135          if ( data.getDNSName().equals(sDesc) || data.getIPAddress().equals(sDesc) )
136             return data;
137       }
138 
139       return null;
140    }
141 
142    /**
143     * Returns true if the server passed in the argument is in the current server list.
144     *  
145     * @param sDesc server name, DNS or IP Address
146     * @return boolean
147     */
148    public static boolean contains ( String sDesc )
149    {
150       return getServerData(sDesc) == null ? false : true;
151    }
152 
153    /**
154     * Returns the DNS name for the specified server, or the orginal argument if not found.
155     *  
156     * @param sDesc server name or IP Address
157     * @return String
158     */
159    public static String getDNSName ( String sDesc )
160    {
161       ServerData data = getServerData(sDesc);
162       if ( data == null )
163          return sDesc;
164       else
165       {
166          // Guarantee the method will always returns a value different than null or emtpy
167          String sDNS = data.getDNSName();
168          if ( sDNS == null || sDNS.equals("") )
169             return sDesc;
170          else
171             return sDNS;
172       }
173    }
174 
175    /**
176     * Returns the IP Address for the specified server, or the orginal server name if not found.
177     * 
178     * @param sDesc server name, DNS or IP Address
179     * @return String
180     */
181    public static String getIPAddress ( String sDesc )
182    {
183       ServerData data = getServerData(sDesc);
184       if ( data == null )
185          return sDesc;
186       else
187       {
188          // Guarantee the method will always returns a value different than null or emtpy
189          String sIPAddress = data.getIPAddress();
190          if ( sIPAddress == null || sIPAddress.equals("") )
191             return sDesc;
192          else
193             return sIPAddress;
194       }
195    }
196 
197    /**
198     * Returns the Port for the specified server, or 80 if not found.
199     * 
200     * @param sDesc server name, DNS or IP Address
201     * @return String
202     */
203    public static String getPort ( String sDesc )
204    {
205       final String DEFAULT_PORT = "80";
206 
207       ServerData data = getServerData(sDesc);
208       if ( data == null )
209          return DEFAULT_PORT;
210       else
211       {
212          // Guarantee the method will always returns a value different than null or emtpy
213          try
214          {
215             String sPort = data.getPort();
216             if ( sPort == null || sPort.equals("") )
217                return DEFAULT_PORT;
218             else
219                return sPort;
220          }
221          catch ( EjbException ejbe ) { return DEFAULT_PORT; }
222       }
223    }
224 
225    /**
226     * Returns a list of server names.
227     */
228    public static String[] getServerList()
229    {
230       if ( servers == null || servers.isEmpty() )
231          return null;
232          
233       int i = 0;
234       String[] serverList = new String[servers.size()];
235       for ( Enumeration e = servers.keys(); e.hasMoreElements(); )
236          serverList[i++] = (String) e.nextElement();
237 
238       return serverList;
239    }
240 
241    /**
242     * Returns the server name using the DNS name or IP Address
243     * 
244     * @param sDesc DNS or IP Address
245     * @return String
246     */
247    public static String getServerName( String sDesc )
248    {
249       ServerData data = getServerData(sDesc);
250       if ( data == null )
251          return sDesc;
252       else
253       {
254          // Guarantee the method will always returns a value different than null or emtpy
255          String sServerName = data.getName();
256          if ( sServerName == null || sServerName.equals("") )
257             return sDesc;
258          else
259             return sServerName;
260       }
261    }
262    
263    /**
264    * Returns the AppleTalkVendor for this server given the server name, dns or ip address.
265    */
266    public static int getAppleTalkVendor( String sDesc )
267    {
268       ServerData data = getServerData(sDesc);
269       return  data == null ? RsrcForkConstantsI.NONE : data.getAppleTalkVendor();
270    }
271 
272    /**
273    * Return true if there is at least one server listed
274    */
275    public static boolean hasEntries()
276    {
277       if ( servers != null && !servers.isEmpty() )
278          return true;
279       else
280          return false;
281    }
282    
283    /**
284     * Return the string array of DNS names for the server list
285     */
286     
287     public static String[] getDNSServerList()
288     {
289         if ( servers == null || servers.isEmpty() )
290          return null;
291 
292         Vector vec = new Vector();
293         Enumeration enum = servers.keys();
294         while(enum.hasMoreElements())
295         {
296            Object key = enum.nextElement();
297            Object value = servers.get(key);
298            ServerData data = (ServerData) value;
299            //We need to get the DNS name, not the plain servername to avoid problems ;-)
300            vec.addElement(data.getDNSName());
301         }
302 
303         String[] sarr = new String[vec.size()];
304         for(int i=0;i<vec.size();i++)
305         {
306             sarr[i] = vec.elementAt(i).toString();
307         }
308         
309         return sarr;
310     }
311 }