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

Quick Search    Search Deep

Source code: org/jbossmx/cluster/watchdog/util/HermesMachineProperties.java


1   /**
2    * JBoss, the OpenSource EJB server
3    *
4    * Distributable under LGPL license Version 2.1, February 1999.
5    * See terms of license at gnu.org.
6    */
7   
8   package org.jbossmx.cluster.watchdog.util;
9   
10  // Hermes JMX Packages
11  import org.jbossmx.cluster.watchdog.Configuration;
12  import org.jboss.logging.Logger;
13  
14  // Standard Java Packages
15  import java.io.FileInputStream;
16  import java.io.FileOutputStream;
17  import java.io.InputStream;
18  import java.io.OutputStream;
19  
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Properties;
23  import java.util.Set;
24  
25  
26  /**
27   * Utility class for retrieving Hermes properties from a Java Properties file
28   *
29   * @author Stacy Curl
30   */
31  public class HermesMachineProperties
32  {
33  
34      /**
35       * Get the machine that odin points to
36       *
37       * @return the machine that odin points to
38       */
39  //    public static String getOdin()
40  //    {
41  //        return getMachineName(Configuration.ACTIVE_AGENT);
42  //    }
43  
44      /**
45       * Gets the machine that <code>rmiAgentBinding</code> is running on
46       *
47       * @param    rmiAgentBinding the RMI Binding of the JMX Agent to find
48       *
49       * @return the machine that <code>rmiAgentBinding</code> is running on
50       */
51      public static String getMachineName(String rmiAgentBinding)
52      {
53          return getProperty(getMachineProperty(rmiAgentBinding));
54      }
55  
56      /**
57       * Sets the machine that <code>rmiAgentBinding</code> is running on
58       *
59       * @param    rmiAgentBinding rmiAgentBinding the RMI Binding of the JMX Agent
60       * @param    machineName the machine that <code>rmiAgentBinding</code> is running on
61       *
62       * @return whether the <code>machineName</code> was successfully stored.
63       */
64      public static boolean setMachineName(String rmiAgentBinding, String machineName)
65      {
66          return setProperty(getMachineProperty(rmiAgentBinding), machineName);
67      }
68  
69      /**
70       * Gets the Watcher details for <code>rmiAgentBinding</code>
71       *
72       * @param    rmiAgentBinding the RMI Binding of the JMX Agent to query
73       *
74       * @return an array containing two properties
75       * <p>1) The RMI Binding of the JMX Agent containing the Watchdog MBean watching
76       * <code>rmiAgentBinding</code>
77       * <p>2) The ObjectName of the Watchdog MBean watching <code>rmiAgentBinding</code>
78       */
79      public static String[] getAgentWatcherDetails(String rmiAgentBinding)
80      {
81          String[] watcherDetails = new String[2];
82  
83          watcherDetails[0] = getProperty(getWatcherRmiBindingProperty(rmiAgentBinding));
84          watcherDetails[1] = getProperty(getWatcherObjectNameProperty(rmiAgentBinding));
85  
86          return watcherDetails;
87      }
88  
89      /**
90       * Sets the Watcher details for <code>rmiAgentBinding</code>
91       *
92       * @param    rmiAgentBinding the RMI Binding of the IMX Agent to update
93       * @param    watcherAgentRmiBinding the RMI Binding of the JMX Agent containing the Watchdog
94       * MBean watching <code>rmiAgentBinding</code>
95       * @param    watcherObjectName the ObjectName of the Watchdog MBean watching
96       * <code>rmiAgentBinding</code>
97       *
98       * @return whether the watcher details were updated for <code>rmiAgentBinding</code>
99       */
100     public static boolean setAgentWatcherDetails(String rmiAgentBinding,
101         String watcherAgentRmiBinding, String watcherObjectName)
102     {
103         return setProperty(getWatcherRmiBindingProperty(rmiAgentBinding), watcherAgentRmiBinding)
104                && setProperty(getWatcherObjectNameProperty(rmiAgentBinding), watcherObjectName);
105     }
106 
107     /**
108      * Gets the value of the <code>propertyName</code> property
109      *
110      * @param    propertyName the property to obtain
111      *
112      * @return the value of the <code>propertyName</code> property
113      */
114     public static String getProperty(String propertyName)
115     {
116         String property = null;
117 
118         Properties properties = loadProperties(Configuration.MACHINE_PROPERTIES);
119 
120         if(properties != null)
121         {
122             property = properties.getProperty(propertyName);
123         }
124 
125         if(property == null)
126         {
127             listFile(Configuration.MACHINE_PROPERTIES);
128         }
129 
130         return property;
131     }
132 
133     /**
134      * Sets the value of the <code>propertyName</code> property
135      *
136      * @param    property the property to update
137      * @param    value the value of the property
138      *
139      * @return whether the property was updated
140      */
141     public static boolean setProperty(String property, String value)
142     {
143         Properties properties = loadProperties(Configuration.MACHINE_PROPERTIES);
144 
145         if(properties != null)
146         {
147             properties.setProperty(property, value);
148 
149             return saveProperties(properties, Configuration.MACHINE_PROPERTIES);
150         }
151         else
152         {
153             return false;
154         }
155     }
156 
157     /**
158      * Gets the name of the property used to store the current machine running the JMX Agent bound
159      * to <code>rmiAgentBinding</code>
160      *
161      * @param    rmiAgentBinding the JMX Agent RMI Binding
162      *
163      * @return the name of the property used to store the current machine running the JMX Agent
164      * bound to <code>rmiAgentBinding</code>
165      */
166     public static String getMachineProperty(String rmiAgentBinding)
167     {
168         return rmiAgentBinding + ".Machine";
169     }
170 
171     /**
172      * Gets the name of the property used to store the RMI Binding of the Agent watching the JMX
173      * Agent bound to <code>rmiAgentBinding</code>
174      *
175      * @param    rmiAgentBinding the JMX Agent RMI Binding
176      *
177      * @return the name of the property used to store the RMI Binding of the Agent watching the JMX
178      * Agent bound to <code>rmiAgentBinding</code>
179      */
180     public static String getWatcherRmiBindingProperty(String rmiAgentBinding)
181     {
182         return rmiAgentBinding + ".Watcher.RmiBinding";
183     }
184 
185     /**
186      * Gets the name of the property used to store the ObjectName of the Watchdog MBean watching the
187      * JMX Agent bound to <code>rmiAgentBinding</code>
188      *
189      * @param    rmiAgentBinding the JMX Agent RMI Binding
190      *
191      * @return the name of the property used to store the ObjectName of the Watchdog MBean watching the
192      * JMX Agent bound to <code>rmiAgentBinding</code>
193      */
194     public static String getWatcherObjectNameProperty(String rmiAgentBinding)
195     {
196         return rmiAgentBinding + ".Watcher.ObjectName";
197     }
198 
199     /**
200      * Gets the RMI Bindings of all the JMX Agents running on <code>machineName</code>
201      *
202      * @param    machineName the name of the machine to search for JMX Agents
203      *
204      * @return the RMI Bindings of all the JMX Agents running on <code>machineName</code>
205      */
206     public static Set getAgentsRunningOnMachine(final String machineName)
207     {
208         Set agents = null;
209 
210         Properties properties = loadProperties(Configuration.MACHINE_PROPERTIES);
211 
212         if(properties != null)
213         {
214             final Set keySet = properties.keySet();
215 
216             for(Iterator i = keySet.iterator(); i.hasNext(); )
217             {
218                 final String nextKey = (String) i.next();
219 
220                 if(isMachineProperty(nextKey)
221                     && properties.getProperty(nextKey).equals(machineName))
222                 {
223                     if(agents == null)
224                     {
225                         agents = new HashSet();
226                     }
227 
228                     agents.add(getAgentName(nextKey));
229                 }
230             }
231         }
232 
233         return agents;
234     }
235 
236     /**
237      * Gets whether <code>key</code> is a valid property key for storing the machine on which a JMX
238      * Agent is running
239      *
240      * @param    key
241      *
242      * @return whether <code>key</code> is a valid property key for storing the machine on which a JMX
243      * Agent is running
244      */
245     private static boolean isMachineProperty(final String key)
246     {
247         return ((key != null) && (key.indexOf(".Machine") != -1));
248     }
249 
250     /**
251      * Gets the substring of <code>key</code> which refers to the Agents RMI Binding
252      *
253      * @param    key
254      *
255      * @return the substring of <code>key</code> which refers to the Agents RMI Binding
256      */
257     private static String getAgentName(final String key)
258     {
259         final int indexOfMachine = key.indexOf(".Machine");
260 
261         return key.substring(0, indexOfMachine);
262     }
263 
264     /**
265      * Gets the resolved RMI Binding for <code>unresolvedRmiAgentBinding</code> by looking up
266      * the machine which is running the Agent implictly refered to in
267      * <code>unresolvedRmiAgentBinding</code>
268      * <p> Example: Unresolved 'RMI Binding': {/ActiveAgent}, resolved RMI Binding:
269      * rmi://hugin/ActiveAgent
270      *
271      * @param    unresolvedRmiAgentBinding the RMI Binding to resolve
272      *
273      * @return the resolved RMI Binding
274      */
275     public static String getResolvedRmiAgentBinding(String unresolvedRmiAgentBinding)
276     {
277         String resolvedRmiAgentBinding;
278 
279         // Format //machineName/binding
280         // Format {/binding}
281         //   -> //Configuration.getMachine("/binding") + "/binding"
282 
283         if(unresolvedRmiAgentBinding.indexOf("{") == -1)
284         {
285             resolvedRmiAgentBinding = unresolvedRmiAgentBinding;
286         }
287         else
288         {
289             int leftBrace = unresolvedRmiAgentBinding.indexOf("{");
290             int rightBrace = unresolvedRmiAgentBinding.indexOf("}");
291 
292             final String rmiAgentBinding = unresolvedRmiAgentBinding.substring(leftBrace + 1,
293                                                rightBrace);
294 
295             final String machineName = getMachineName(rmiAgentBinding);
296 
297             if(machineName != null)
298             {
299                 resolvedRmiAgentBinding = "//" + machineName + rmiAgentBinding;
300             }
301             else
302             {
303                 resolvedRmiAgentBinding = null;
304             }
305         }
306 
307         LOG.debug("getResolvedRmiAgentBinding(" + unresolvedRmiAgentBinding + ") = "
308                   + resolvedRmiAgentBinding);
309 
310         return resolvedRmiAgentBinding;
311     }
312 
313     /**
314      * Removes the leftmost and rightmost braces in <code>input</code>
315      *
316      * @param    input the String to remove braces from
317      *
318      * @return the leftmost and rightmost braces in <code>input</code>
319      */
320     public static String removeBraces(String input)
321     {
322         final int leftBrace = input.indexOf("{");
323         final int rightBrace = input.indexOf("}");
324 
325         return input.substring(leftBrace + 1, rightBrace);
326     }
327 
328     /**
329      * Prints out all the properties in <code>properties</code>
330      *
331      * @param    properties the Properties to list
332      */
333     public static void enumerateProperties(Properties properties)
334     {
335         System.out.println("enumerateProperties");
336 
337         Set keySet = properties.keySet();
338 
339         for(Iterator i = keySet.iterator(); i.hasNext(); )
340         {
341             String nextKey = (String) i.next();
342 
343             System.out.println("Prop(" + nextKey + ") = " + properties.getProperty(nextKey));
344         }
345 
346         System.out.println("enumerateProperties - done");
347     }
348 
349     /**
350      * Returns a Properties class loaded from a properties file
351      *
352      * @param    fileName the properties file to load
353      *
354      * @return a Properties class loaded from a properties file
355      */
356     private static Properties loadProperties(final String fileName)
357     {
358         Properties properties = null;
359 
360         try
361         {
362             InputStream is = new FileInputStream(fileName);
363 
364             properties = new Properties();
365 
366             properties.load(is);
367 
368             is.close();
369         }
370         catch(Exception e)
371         {
372             e.printStackTrace(System.out);
373 
374             properties = null;
375         }
376 
377         return properties;
378     }
379 
380     /**
381      * Saves a Properties object to a file
382      *
383      * @param    properties the Properties object to save
384      * @param    fileName the file to save into
385      *
386      * @return whether <code>properties</code> was saved successfully
387      */
388     private static boolean saveProperties(final Properties properties, final String fileName)
389     {
390         boolean saved = false;
391 
392         try
393         {
394             OutputStream os = new FileOutputStream(fileName);
395             properties.store(os, null);
396             os.close();
397 
398             saved = true;
399         }
400         catch(Exception e)
401         {
402             e.printStackTrace(System.out);
403 
404             saved = false;
405         }
406 
407         return saved;
408     }
409 
410     /**
411      * Lists the contents of a file
412      *
413      * @param    fileName the file to list
414      */
415     private static void listFile(String fileName)
416     {
417         System.out.println("List file(" + fileName + ")");
418 
419         try
420         {
421             InputStream is = new FileInputStream(fileName);
422 
423             int read = is.read();
424 
425             while(read != -1)
426             {
427                 System.out.write(read);
428 
429                 read = is.read();
430             }
431 
432             is.close();
433         }
434         catch(Exception e)
435         {
436             e.printStackTrace();
437         }
438 
439         System.out.println("List file(" + fileName + ") - done");
440     }
441 
442     private static Logger LOG = Logger.getLogger(HermesMachineProperties.class.getName());
443 }