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

Quick Search    Search Deep

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


1   /*
2    * FlexDbServerHost.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.StringTokenizer;
14  import java.util.Vector;
15  
16  import com.flexstor.common.settings.Settings;
17  
18  /**
19   * FlexDbServerHost returns information about the machine hosting the FlexDBServer.
20   * Among the information available is the list of virtual names given to the machine
21   * as specified in the flexstordb.properties file.
22   */
23  public class FlexDbServerHost
24  {
25     /**
26      * Unique instance of this class
27      */
28     private static FlexDbServerHost instance = null;
29  
30     /**
31      * A Vector holding a list of the multiple names that the FlexDBServer might have.
32      * At least it will have as first element the server name specified in the host
33      * property; then, if any, the list of server names specified in the otherNames
34      * property.
35      */
36     private static Vector vNames = new Vector();
37  
38     private FlexDbServerHost() {}
39  
40     /**
41      * This method is called by the FlexDBServer to hold an instance of this class
42      * during an entire session.
43      */
44     public static FlexDbServerHost getInstance()
45     {
46        if ( instance == null )
47           instance = new FlexDbServerHost();
48  
49        return instance;
50     }
51  
52     /**
53      * Initialize the class with information from the flexstordb.properties file.
54      */
55     public static void initialize()
56     {
57        // Check if the ServerList has been initialized already; if it hasn't, then do it
58        if ( !isInitialized() )
59        {
60           if ( !ServerList.hasEntries() )
61              ServerList.initialize();
62  
63           retrieveNames();
64        }
65     }
66  
67     /**
68      * Test to find out if FlexDBServer has been initialized
69      */
70     public static boolean isInitialized()
71     {
72        return vNames.size() > 0 ? true : false;
73     }
74  
75     /**
76      * This call will return true only if this JVM is running in the FlexDBServer host machine.
77      */
78     public static boolean isLocalHost()
79     {
80        return isLocalHost( InetUtil.getHostName() );
81     }
82  
83     /**
84      * Checks if the argument corresponds to the local host.
85      * Returns true if the argument is any of the names for the FlexDBServer, false otherwise.
86      */
87     public static boolean isLocalHost( String sServer )
88     {
89        return vNames.contains( ServerList.getDNSName( sServer ) );
90     }
91  
92     /**
93      * Retrieve the name of the local host. The local host will be the value of the appserver.host
94      * property in the flexstordb.properties file. If the FlexDbServerHost has not being initialized,
95      * this call will return the name of the host machine as given by the InetUtil class.
96      */
97     public static String getLocalHostName()
98     {
99        if ( vNames.size() > 0 )
100          return (String) vNames.elementAt(0);
101       else
102          return InetUtil.getLocalHostName();
103    }
104 
105    /**
106     * Retrieve the name of the server from the host property and any other names defined
107     * in the otherNames property.
108     */
109    private static void retrieveNames()
110    {
111       // Get the actual name of the FlexDBServer stored in the host property
112       vNames.addElement( Settings.getString( Settings.APP_SERVER_HOST ) );
113 
114       // Load the list of other names
115       String sOtherNames = Settings.getString( Settings.APP_SERVER_OTHERNAMES );
116       if ( sOtherNames != null )
117       {
118          // parse this comma-separated list
119          StringTokenizer st = new StringTokenizer( sOtherNames, ", " );
120          while ( st.hasMoreTokens() )
121             vNames.addElement( ServerList.getDNSName( st.nextToken() ) );
122       }
123    }
124 }