1 87 package org.codehaus.loom.components.manager; 88 89 import java.io.File ; 90 import javax.management.Attribute ; 91 import javax.management.AttributeNotFoundException ; 92 import javax.management.InstanceNotFoundException ; 93 import javax.management.InvalidAttributeValueException ; 94 import javax.management.MBeanException ; 95 import javax.management.MBeanServer ; 96 import javax.management.MBeanServerFactory ; 97 import javax.management.ObjectName ; 98 import javax.management.ReflectionException ; 99 import mx4j.adaptor.rmi.jrmp.JRMPAdaptorMBean; 100 import mx4j.log.Log; 101 import mx4j.util.StandardMBeanProxy; 102 103 import org.codehaus.loom.interfaces.ContainerConstants; 104 import org.codehaus.spice.salt.i18n.ResourceManager; 105 import org.codehaus.spice.salt.i18n.Resources; 106 import org.codehaus.dna.Configurable; 107 import org.codehaus.dna.Configuration; 108 import org.codehaus.dna.ConfigurationException; 109 110 117 public class MX4JSystemManager 118 extends AbstractJMXManager 119 implements Configurable 120 { 121 private static final Resources REZ = 122 ResourceManager.getPackageResources( MX4JSystemManager.class ); 123 124 private static final String DEFAULT_NAMING_FACTORY = 125 "com.sun.jndi.rmi.profile.RegistryContextFactory"; 126 private static final String DEFAULT_HTTPADAPTER_HOST = "localhost"; 127 private static final int DEFAULT_HTTPADAPTER_PORT = 128 Integer.getInteger( "loom.adapter.http", 8082 ).intValue(); 129 private static final int DEFAULT_RMIREGISTRY_PORT = 130 Integer.getInteger( "loom.rmiregistry.port", 1099 ).intValue(); 131 132 private String m_host; 133 private int m_port; 134 private boolean m_rmi; 135 private int m_rmi_registry_port; 136 private String m_stylesheetDir; 137 private String m_namingFactory; 138 private String m_password; 139 private String m_username; 140 private boolean m_http; 141 142 public void configure( final Configuration configuration ) 143 throws ConfigurationException 144 { 145 m_host = configuration.getChild( "manager-adaptor-host" ). 146 getValue( DEFAULT_HTTPADAPTER_HOST ); 147 148 m_port = configuration.getChild( "manager-adaptor-port" ). 149 getValueAsInteger( DEFAULT_HTTPADAPTER_PORT ); 150 151 m_port = configuration.getChild( "port" ). 154 getValueAsInteger( m_port ); 155 156 getLogger().debug( "MX4J HTTP listener port: " + m_port ); 157 158 m_rmi = 159 configuration.getChild( "enable-rmi-adaptor" ).getValueAsBoolean( 160 false ); 161 m_rmi_registry_port = 162 configuration.getChild( "rmi-registry-port" ).getValueAsInteger( 163 DEFAULT_RMIREGISTRY_PORT ); 164 m_http = 165 configuration.getChild( "enable-http-adaptor" ).getValueAsBoolean( 166 false ); 167 168 m_namingFactory = 169 configuration.getChild( "rmi-naming-factory" ).getValue( 170 DEFAULT_NAMING_FACTORY ); 171 172 final String stylesheets = 173 configuration.getChild( "stylesheets-dir" ).getValue( null ); 174 if( null != stylesheets ) 175 { 176 m_stylesheetDir = new File ( stylesheets ).getAbsolutePath(); 177 getLogger().debug( "MX4J XSL Stylesheet Dir: " + m_stylesheetDir ); 178 } 179 180 184 final Configuration userConfig = configuration.getChild( "user" ); 185 m_username = userConfig.getChild( "name" ).getValue( null ); 186 m_password = userConfig.getChild( "password" ).getValue( null ); 187 } 188 189 public void initialize() 190 throws Exception 191 { 192 super.initialize(); 193 194 final MBeanServer mBeanServer = getMBeanServer(); 195 196 if( m_http ) 197 { 198 startHttpAdaptor( mBeanServer ); 199 } 200 201 if( m_rmi ) 202 { 203 startRMIAdaptor( mBeanServer ); 204 } 205 } 206 207 public void dispose() 208 { 209 final MBeanServer mBeanServer = getMBeanServer(); 210 211 if( m_http ) 212 { 213 stopHttpAdaptor( mBeanServer ); 214 } 215 if( m_rmi ) 216 { 217 stopRMIAdaptor( mBeanServer ); 218 } 219 220 super.dispose(); 221 } 222 223 private void startHttpAdaptor( final MBeanServer mBeanServer ) 224 throws Exception 225 { 226 final ObjectName adaptorName = new ObjectName ( "Http:name=HttpAdaptor" ); 227 mBeanServer.createMBean( "mx4j.adaptor.http.HttpAdaptor", 228 adaptorName, 229 null ); 230 mBeanServer.setAttribute( adaptorName, 231 new Attribute ( "Host", m_host ) ); 232 mBeanServer.setAttribute( adaptorName, 233 new Attribute ( "Port", 234 new Integer ( m_port ) ) ); 235 236 if( null != m_username ) 237 { 238 configureAuthentication( mBeanServer, adaptorName ); 239 } 240 241 configureProcessor( mBeanServer, adaptorName ); 242 243 mBeanServer.invoke( adaptorName, "start", null, null ); 245 } 246 247 private void configureProcessor( final MBeanServer mBeanServer, 248 final ObjectName adaptorName ) 249 throws Exception 250 { 251 final ObjectName processorName = new ObjectName ( 252 "Http:name=XSLTProcessor" ); 253 mBeanServer.createMBean( "mx4j.adaptor.http.XSLTProcessor", 254 processorName, 255 null ); 256 mBeanServer.setAttribute( adaptorName, 257 new Attribute ( "ProcessorName", 258 processorName ) ); 259 260 if( null != m_stylesheetDir ) 261 { 262 final Attribute stylesheetDir = new Attribute ( "File", 263 m_stylesheetDir ); 264 mBeanServer.setAttribute( processorName, stylesheetDir ); 265 } 266 267 final Attribute useCache = 268 new Attribute ( "UseCache", Boolean.FALSE ); 269 mBeanServer.setAttribute( processorName, useCache ); 270 } 271 272 private void configureAuthentication( final MBeanServer mBeanServer, 273 final ObjectName adaptorName ) 274 throws InstanceNotFoundException , 275 MBeanException , 276 ReflectionException , 277 AttributeNotFoundException , 278 InvalidAttributeValueException 279 { 280 mBeanServer.invoke( adaptorName, 282 "addAuthorization", 283 new Object []{m_username, m_password}, 284 new String []{"java.lang.String", 285 "java.lang.String"} ); 286 287 mBeanServer.setAttribute( adaptorName, 289 new Attribute ( "AuthenticationMethod", 290 "basic" ) ); 291 } 292 293 private void stopHttpAdaptor( final MBeanServer server ) 294 { 295 stopJMXMBean( server, "Http:name=HttpAdaptor" ); 296 } 297 298 private void startRMIAdaptor( final MBeanServer server ) 299 throws Exception 300 { 301 final ObjectName naming = new ObjectName ( "Naming:type=rmiregistry" ); 303 server.createMBean( "mx4j.tools.naming.NamingService", 304 naming, 305 null, 306 new Object []{new Integer ( m_rmi_registry_port )}, 307 new String []{"int"} 308 ); 309 server.invoke( naming, "start", null, null ); 310 311 final ObjectName adaptor = new ObjectName ( "Adaptor:protocol=JRMP" ); 313 server.createMBean( "mx4j.adaptor.rmi.jrmp.JRMPAdaptor", 314 adaptor, 315 null ); 316 final JRMPAdaptorMBean mbean = 317 (JRMPAdaptorMBean)StandardMBeanProxy.create( 318 JRMPAdaptorMBean.class, 319 server, 320 adaptor ); 321 mbean.setJNDIName( "jrmp" ); 323 324 mbean.putNamingProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY, 325 m_namingFactory ); 326 mbean.putNamingProperty( javax.naming.Context.PROVIDER_URL, 327 "rmi://localhost:" + m_rmi_registry_port ); 328 329 mbean.start(); 331 } 332 333 private void stopRMIAdaptor( final MBeanServer server ) 334 { 335 stopJMXMBean( server, "Adaptor:protocol=JRMP" ); 337 stopJMXMBean( server, "Naming:type=rmiregistry" ); 339 } 340 341 protected MBeanServer createMBeanServer() 342 throws Exception 343 { 344 MX4JLoggerAdapter.setLogger( getLogger() ); 345 Log.redirectTo( new MX4JLoggerAdapter() ); 346 return MBeanServerFactory.createMBeanServer( 347 ContainerConstants.SOFTWARE ); 348 } 349 350 private void stopJMXMBean( final MBeanServer mBeanServer, 351 final String name ) 352 { 353 try 354 { 355 final ObjectName objectName = new ObjectName ( name ); 356 mBeanServer.invoke( objectName, "stop", null, null ); 357 } 358 catch( final Exception e ) 359 { 360 final String message = 361 REZ.format( "jmxmanager.error.jmxmbean.dispose", name ); 362 getLogger().error( message, e ); 363 } 364 } 365 } 366 | Popular Tags |