KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > mbeans > helloworld > HelloWorld


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.mbeans.helloworld;
10
11 import java.io.FileNotFoundException JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 /**
17  * Service class that reads a configuration file and returns information about the configuration. <p>
18  * Its purpose is to show the difference between management methods and service methods. <br>
19  * This class has 3 methods, but only 2 belong to the management interface, therefore only these 2
20  * are accessible from the MBeanServer, so they're the management methods. <br>
21  * The third method can be used by any other class but it is not accessible from the MBeanServer so
22  * it's a service method (since it gives a service to callers), and not a management method.
23  *
24  * @version $Revision: 1.3 $
25  */

26 public class HelloWorld implements HelloWorldMBean
27 {
28    private int m_times;
29    private Properties JavaDoc m_configuration;
30
31    public String JavaDoc getInfoFromConfiguration(String JavaDoc key)
32    {
33       // Be sure to use the configuration while it is not changed.
34
synchronized (this)
35       {
36          return m_configuration.getProperty(key);
37       }
38    }
39
40    public void reloadConfiguration() throws IOException JavaDoc
41    {
42       // Lookup the configuration file in the classpath
43
String JavaDoc configuration = "jndi.properties";
44       InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream(configuration);
45       if (is == null)
46       {
47          throw new FileNotFoundException JavaDoc("Cannot find " + configuration + " file in classpath");
48       }
49
50       // Load the new configuration from the file
51
Properties JavaDoc p = new Properties JavaDoc();
52       p.load(is);
53
54       // Avoid that someone reads the configuration while we are changing it
55
synchronized (this)
56       {
57          m_configuration = p;
58          ++m_times;
59       }
60    }
61
62    public int getHowManyTimes()
63    {
64       return m_times;
65    }
66 }
67
Popular Tags