01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.examples.mbeans.helloworld;
10:
11: import java.io.FileNotFoundException;
12: import java.io.IOException;
13: import java.io.InputStream;
14: import java.util.Properties;
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.1 $
25: */
26: public class HelloWorld implements HelloWorldMBean {
27: private int m_times;
28: private Properties m_configuration;
29:
30: public String getInfoFromConfiguration(String key) {
31: // Be sure to use the configuration while it is not changed.
32: synchronized (this ) {
33: return m_configuration.getProperty(key);
34: }
35: }
36:
37: public void reloadConfiguration() throws IOException {
38: // Lookup the configuration file in the classpath
39: String configuration = "jndi.properties";
40: InputStream is = getClass().getClassLoader()
41: .getResourceAsStream(configuration);
42: if (is == null) {
43: throw new FileNotFoundException("Cannot find "
44: + configuration + " file in classpath");
45: }
46:
47: // Load the new configuration from the file
48: Properties p = new Properties();
49: p.load(is);
50:
51: // Avoid that someone reads the configuration while we are changing it
52: synchronized (this ) {
53: m_configuration = p;
54: ++m_times;
55: }
56: }
57:
58: public int getHowManyTimes() {
59: return m_times;
60: }
61: }
|