1 package cz.cuni.amis.pogamut.base.utils;
2
3 import java.io.IOException;
4 import java.net.InetAddress;
5 import java.net.ServerSocket;
6 import java.rmi.registry.LocateRegistry;
7 import java.util.Properties;
8 import java.util.logging.Level;
9
10 import javax.management.MBeanServer;
11 import javax.management.MBeanServerConnection;
12 import javax.management.remote.JMXConnector;
13 import javax.management.remote.JMXConnectorFactory;
14 import javax.management.remote.JMXConnectorServer;
15 import javax.management.remote.JMXConnectorServerFactory;
16 import javax.management.remote.JMXServiceURL;
17
18 import cz.cuni.amis.pogamut.base.utils.jmx.PogamutMBeanServer;
19 import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
20 import cz.cuni.amis.pogamut.base.utils.logging.NetworkLogManager;
21 import cz.cuni.amis.utils.configuration.PropertiesManager;
22 import cz.cuni.amis.utils.exception.PogamutException;
23
24
25
26
27
28 public class DefaultPogamutPlatform implements PogamutPlatform {
29
30 LogCategory log = new LogCategory("DefaultPogamutPlatform");
31
32 static PogamutMBeanServer mBeanServer = null;
33 static JMXConnectorServer cs = null;
34 JMXServiceURL jmxServiceURL = null;
35 PropertiesManager propertiesManager = new PropertiesManager();
36
37 public DefaultPogamutPlatform() {
38 log.addConsoleHandler();
39 }
40
41 @Override
42 public JMXServiceURL getMBeanServerURL() throws PogamutException {
43 try {
44 String hostNameProp = getProperty(PogamutProperty.POGAMUT_JMX_SERVER_ADDRESS.getKey());
45 String hostName = hostNameProp != null ? hostNameProp : InetAddress.getLocalHost().getHostName();
46 return new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostName + ":" + getRMIPort() + "/server");
47 } catch (Exception ex) {
48 throw new PogamutException("Error creating the JMX service URL.", ex, this);
49 }
50 }
51
52 protected int getRMIPort() throws PogamutException {
53 if (assignedPort == null) {
54 try {
55
56
57
58
59 ServerSocket socket = new ServerSocket(0);
60 assignedPort = socket.getLocalPort();
61 socket.close();
62 } catch (IOException ex) {
63 throw new PogamutException("Error while getting a port for RMI.", ex);
64 }
65 }
66 return assignedPort;
67 }
68 Integer assignedPort = null;
69 private boolean registryCreated = false;
70
71
72
73
74
75
76
77
78 @Override
79 public synchronized MBeanServer getMBeanServer() throws PogamutException {
80 try {
81 if (!registryCreated) {
82 if (log.isLoggable(Level.WARNING)) log.warning("Creating registry at " + getRMIPort() + " ...");
83 LocateRegistry.createRegistry(getRMIPort());
84 registryCreated = true;
85 }
86 if (mBeanServer == null) {
87 if (log.isLoggable(Level.WARNING)) log.warning("Starting MBean server.");
88
89 mBeanServer = new PogamutMBeanServer();
90
91
92
93 cs = JMXConnectorServerFactory.newJMXConnectorServer(getMBeanServerURL(), null, mBeanServer);
94 cs.start();
95
96 }
97 return mBeanServer;
98 } catch (Exception ex) {
99 throw new PogamutException("Error during JMX initialization.", ex);
100 }
101 }
102
103 @Override
104 public synchronized void close() throws PogamutException {
105 if (log.isLoggable(Level.WARNING)) log.warning("Closing the platform.");
106 try {
107 if (cs != null) {
108 cs.stop();
109 }
110 cs = null;
111 if (mBeanServer != null) {
112 mBeanServer.unregisterAll();
113 mBeanServer.clearSaved();
114 }
115 mBeanServer = null;
116 } catch (Exception ex) {
117 throw new PogamutException("Could not shutdown the mBeanServer!", ex, log);
118 } finally {
119 try {
120 NetworkLogManager.getNetworkLogManager().kill();
121 } catch (Exception ex2) {
122 throw new PogamutException("Could not shutdown the log manager!", ex2, log);
123 }
124 }
125 }
126
127 Properties platformProperties = null;
128
129
130
131
132
133
134
135 @Override
136 public String getProperty(String key, String def) {
137 String val = getProperty(key);
138 return val != null ? val : def;
139
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 public String getProperty(String key) {
156 return propertiesManager.getProperty(key);
157 }
158
159 protected MBeanServerConnection mbsc = null;
160
161
162
163
164
165 public MBeanServerConnection getMBeanServerConnection() throws PogamutException {
166
167 try {
168 if (mbsc == null) {
169 JMXServiceURL url = getMBeanServerURL();
170 JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
171 mbsc = jmxc.getMBeanServerConnection();
172 }
173 return mbsc;
174 } catch (IOException iOException) {
175 throw new PogamutException("IO exception occured while creating remote MBeanServer connector.",
176 iOException);
177 }
178 }
179
180 @Override
181 public int getIntProperty(String key) {
182 String s = getProperty(key);
183 if(s == null) return 0;
184 return Integer.parseInt(s);
185 }
186
187 @Override
188 public boolean getBooleanProperty(String key) {
189 String value = getProperty(key);
190 if (value == null) return false;
191 return value.equalsIgnoreCase("true");
192 }
193 }