1 22 package org.objectweb.jonas.management; 23 24 import java.util.Hashtable ; 26 import java.util.Properties ; 27 28 import javax.management.JMException ; 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerNotification ; 31 import javax.management.Notification ; 32 import javax.management.NotificationListener ; 33 import javax.management.ObjectName ; 34 35 import org.objectweb.jonas.common.JProp; 36 import org.objectweb.jonas.common.Log; 37 import org.objectweb.jonas.jmx.JonasObjectName; 38 import org.objectweb.util.monolog.api.BasicLevel; 39 import org.objectweb.util.monolog.api.Logger; 40 53 public class ReconfigManager implements ReconfigManagerMBean, NotificationListener { 54 55 58 private static Logger logger = null; 59 60 63 MBeanServer jmxServer = null; 64 65 private static final String SERVICE_TYPE = "service"; 69 70 private static final String DATASOURCE_TYPE = "datasource"; 72 73 74 79 private static final String MAIL_RESOURCE_TYPE = "JavaMailResource"; 80 83 private static final String JTA_RESOURCE_TYPE = "JTAResource"; 84 87 private static final String JDBC_RESOURCE_TYPE = "JDBCDataSource"; 88 89 92 private static final String SECURITYREALM_FACTORY = "securityfactory"; 93 94 95 98 private static final String SECURITYREALM_FILE = "jonas-realm.xml"; 99 100 101 ObjectName reconfigManagerObectName = JonasObjectName.serverConfig(); 103 104 Properties serverProperties = null; 107 String serverConfigFileName = null; 108 109 Hashtable reconfigurators = new Hashtable (); 112 113 118 public ReconfigManager(Properties serverProperties, MBeanServer jmxServer) throws ReconfigException { 119 logger = Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX); 121 122 this.serverProperties = serverProperties; 123 this.jmxServer = jmxServer; 124 try { 125 this.serverConfigFileName = JProp.getInstance().getPropFile(); 126 } catch (Exception e) { 127 throw new ReconfigException("Can't initialize ReconfigManager because of exception: " + e.toString()); 128 } 129 130 try { 134 ObjectName delegate = new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 135 jmxServer.addNotificationListener(delegate, this, null, null); 136 } catch (JMException me) { 137 throw new ReconfigException("ReconfigManager can't listen to MBeanServerNotifications because of exception: " + me.toString()); 140 } 141 if (logger.isLoggable(BasicLevel.DEBUG)) { 142 logger.log(BasicLevel.DEBUG, "ReconfigManager MBean registered itself as listner to MBeanServerNotifications"); 143 } 144 } 145 146 152 public void handleNotification(Notification notification, java.lang.Object handback) { 153 String notificationType = notification.getType(); 154 if (notification instanceof MBeanServerNotification ) { 155 if (notificationType.equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) { 157 try { 158 handleRegistrationNotification((MBeanServerNotification ) notification); 159 } catch (ReconfigException re) { 160 logger.log(BasicLevel.ERROR, "ReconfigManager error when trying to handle REGISTRATION_NOTIFICATION"); 161 } 162 } else if (notificationType.equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) { 163 if (logger.isLoggable(BasicLevel.DEBUG)) { 164 logger.log(BasicLevel.DEBUG, "Received UNREGISTRATION_NOTIFICATION for MBean " + ((MBeanServerNotification ) notification).getMBeanName().toString()); 165 } 166 } 168 } else { 169 String name = notification.getMessage(); 172 long sequence = notification.getSequenceNumber(); 174 if (notificationType.equals(ReconfigDispatcher.RECONFIG_TYPE)) { 175 Reconfigured reconfigured = (Reconfigured) notification.getUserData(); 177 if (reconfigured instanceof ReconfiguredProp) { 178 ReconfiguredProp prop = (ReconfiguredProp) reconfigured; 179 handleReconfig(name, sequence, prop); 180 } else { 181 ReconfiguredXml reconfiguredXml = (ReconfiguredXml) reconfigured; 182 handleReconfig(name, sequence, reconfiguredXml); 183 } 184 } else if (notificationType.equals(ReconfigDispatcher.SAVE_RECONFIG_TYPE)) { 185 handleSave(name, sequence); 186 } 187 } 188 } 189 190 197 private void handleRegistrationNotification(MBeanServerNotification notification) throws ReconfigException { 198 ObjectName notificationSender = notification.getMBeanName(); 199 String senderType = notificationSender.getKeyProperty("type"); 200 String senderName = notificationSender.getKeyProperty("name"); 201 String senderJ2eeType = notificationSender.getKeyProperty("j2eeType"); 202 Reconfigurator reconfig = null; 203 if (senderJ2eeType != null) { 204 senderType = senderJ2eeType; 205 } 206 207 if (senderType == null) { 208 return; 210 } 211 212 if (senderType.equals(SERVICE_TYPE)) { 213 if (!senderName.equals("jonasServer")) { 218 if (senderName.equals("log")) { 219 String configFileName = notificationSender.getKeyProperty("fname"); 221 try { 222 reconfig = new ReconfiguratorProp(senderName, JProp.getInstance(configFileName).getPropFile(), JProp.getInstance(configFileName).getConfigFileEnv()); 223 } catch (Exception e) { 224 logger.log(BasicLevel.WARN, "Cannot do persistent reconfiguration for dynamically loaded resources!"); 228 } 229 } else { 230 reconfig = new ReconfiguratorProp(senderName, serverConfigFileName, serverProperties); 232 } 233 } 234 } else { 235 String resourceName = null; 238 try { 239 if (senderType.equals(MAIL_RESOURCE_TYPE)) { 240 resourceName = (String ) jmxServer.getAttribute(notificationSender, "FactoryName"); 242 } else if (senderType.equals(JDBC_RESOURCE_TYPE)) { 243 resourceName = (String ) jmxServer.getAttribute(notificationSender, "name"); 245 } else if (senderType.equals(JTA_RESOURCE_TYPE)) { 246 reconfig = new ReconfiguratorProp(senderName, serverConfigFileName, serverProperties); 248 } 250 251 if (senderType.equals(SECURITYREALM_FACTORY)) { 253 JProp jprop = JProp.getInstance(SECURITYREALM_FILE); 254 String propsFilename = jprop.getPropFile(); 255 String txt = jprop.getConfigFileXml(); 256 reconfig = new ReconfiguratorXml(SECURITYREALM_FILE, propsFilename, txt); 258 } else if (resourceName != null) { 259 JProp jprop = JProp.getInstance(resourceName); 260 String propsFilename = jprop.getPropFile(); 261 Properties props = JProp.getInstance(resourceName).getConfigFileEnv(); 262 reconfig = new ReconfiguratorProp(resourceName, propsFilename, props); 264 } 265 } catch (JMException me) { 266 logger.log(BasicLevel.ERROR, "Catched Exception when trying to treat reconfiguration of the following resource: " + me); 267 throw new ReconfigException("Catched Exception when trying to treat reconfiguration of the following resource: " + me.toString()); 268 } catch (Exception e) { 269 logger.log(BasicLevel.ERROR, "Catched Exception when calling JProp.getInstance(" + resourceName + "): " + e); 272 throw new ReconfigException("Catched Exception when calling JProp.getInstance(" + resourceName + "): " + e.toString()); 273 } 274 } 275 if (reconfig != null) { 276 try { 277 jmxServer.addNotificationListener(notificationSender, reconfigManagerObectName, null, null); 278 } catch (JMException me) { 279 logger.log(BasicLevel.ERROR, "ReconfigManager can't listen to Notifications because of exception: " + me.toString()); 280 throw new ReconfigException("ReconfigManager can't listen to Notifications because of exception: " + me.toString()); 281 } 282 reconfigurators.put(senderName, reconfig); 283 if (logger.isLoggable(BasicLevel.DEBUG)) { 284 logger.log(BasicLevel.DEBUG, "Received Registration Notification from " + notificationSender.toString()); 285 } 286 } 287 } 288 289 295 private void handleReconfig(String name, long sequence, ReconfiguredProp prop) throws ReconfigException { 296 if (logger.isLoggable(BasicLevel.DEBUG)) { 297 logger.log(BasicLevel.DEBUG, "Received 'jonas.management.reconfiguration' notification concerning service or ressource: " + name); 298 } 299 Reconfigurator reconfigurator = (ReconfiguratorProp) reconfigurators.get(name); 300 if (reconfigurator == null) { 301 throw new ReconfigException("Can't find Reconfigurator associated to service or resource " + name); 302 } else { 303 if (prop.getPropValue() != null) { 304 if (logger.isLoggable(BasicLevel.DEBUG)) { 305 logger.log(BasicLevel.DEBUG, "Try to reconfigure property : " + prop.getPropName() + " using value value : " + prop.getPropValue()); 306 } 307 if (prop.replaceProp()) { 308 ((ReconfiguratorProp) reconfigurator).updateConfig(prop.getPropName(), prop.getPropValue(), sequence); 309 } else { 310 if (logger.isLoggable(BasicLevel.DEBUG)) { 311 if (prop.addProp()) { 312 logger.log(BasicLevel.DEBUG, "This value has to be added to the values sequence"); 313 } else { 314 logger.log(BasicLevel.DEBUG, "This value has to be removed from the values sequence"); 315 } 316 } 317 ((ReconfiguratorProp) reconfigurator).updateConfig(prop.getPropName(), prop.getPropValue(), prop.addProp(), sequence); 318 } 319 } else { 320 if (logger.isLoggable(BasicLevel.DEBUG)) { 321 logger.log(BasicLevel.DEBUG, "Reconfiguration made on a group of properties"); 322 } 323 ((ReconfiguratorProp) reconfigurator).updateConfig(prop.getProps(), sequence); 324 } 325 } 326 } 327 328 334 private void handleReconfig(String name, long sequence, ReconfiguredXml reconfiguredXml) throws ReconfigException { 335 if (logger.isLoggable(BasicLevel.DEBUG)) { 336 logger.log(BasicLevel.DEBUG, "Received 'jonas.management.reconfiguration' notification concerning service or ressource: " + name); 337 } 338 Reconfigurator reconfigurator = (ReconfiguratorXml) reconfigurators.get(name); 339 if (reconfigurator == null) { 340 throw new ReconfigException("Can't find Reconfigurator associated to service or resource " + name); 341 } else { 342 ((ReconfiguratorXml) reconfigurator).updateConfig(reconfiguredXml.getXml(), sequence); 343 } 344 } 345 346 347 352 private void handleSave(String name, long sequence) throws ReconfigException { 353 if (logger.isLoggable(BasicLevel.DEBUG)) { 354 logger.log(BasicLevel.DEBUG, "Received 'jonas.management.reconfiguration.save' notification concerning service or ressource: " + name); 355 } 356 Reconfigurator reconfigurator = (Reconfigurator) reconfigurators.get(name); 357 if (reconfigurator == null) { 358 throw new ReconfigException("Can't find Reconfigurator associated to service or resource " + name); 359 } else { 360 reconfigurator.saveConfig(sequence); 361 } 362 } 363 } 364 | Popular Tags |