1 8 package org.enhydra.listener; 9 10 import org.apache.catalina.LifecycleEvent; 11 import org.apache.catalina.LifecycleListener; 12 13 import javax.management.MBeanServer ; 14 import javax.management.ObjectName ; 15 import javax.management.Attribute ; 16 import javax.management.MBeanServerFactory ; 17 18 import java.io.IOException ; 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 23 import java.util.Properties ; 24 25 import java.util.HashMap ; 26 import mx4j.adaptor.rmi.jrmp.JRMPAdaptor; 27 import mx4j.tools.adaptor.security.interceptor.UserPasswordAdaptorInterceptor; 28 import mx4j.security.SimplePasswordStore; 29 30 33 public class AdaptorListener implements LifecycleListener { 34 35 MBeanServer mserver; 36 37 private int httpport=-1; 38 private int jrmpport=-1; 39 40 private String httphost="localhost"; 41 private String jrmphost="localhost"; 42 43 private String authmode="none"; 44 private String authuser=null; 45 private String authpassword=null; 46 47 private boolean useXSLTProcessor = true; 48 49 private String propFile = null; 50 private File propsF = null; 51 Properties props=new Properties (); 52 53 ObjectName httpServerName=null; 54 ObjectName jrmpServerName=null; 55 56 JRMPAdaptor adaptor = null; 57 58 61 public AdaptorListener() { 62 mserver = getMBeanServer(); 63 } 64 65 67 public void setHttpPort( int i ) { 68 httpport=i; 69 } 70 71 public int getHttpPort() { 72 return httpport; 73 } 74 75 public void setHttpHost(String host ) { 76 this.httphost=host; 77 } 78 79 public String getHttpHost() { 80 return httphost; 81 } 82 83 public boolean getUseXSLTProcessor() { 84 return useXSLTProcessor; 85 } 86 87 public void setUseXSLTProcessor(boolean uxsltp) { 88 useXSLTProcessor = uxsltp; 89 } 90 91 94 public void setJrmpPort( int i ) { 95 jrmpport=i; 96 } 97 98 public int getJrmpPort() { 99 return jrmpport; 100 } 101 102 public void setJrmpHost(String host ) { 103 this.jrmphost=host; 104 } 105 106 public String getJrmpHost() { 107 return jrmphost; 108 } 109 110 113 public void setAuthMode(String mode) { 114 authmode=mode; 115 } 116 117 public String getAuthMode() { 118 return authmode; 119 } 120 121 public void setAuthUser(String user) { 122 authuser=user; 123 } 124 125 public String getAuthUser() { 126 return authuser; 127 } 128 129 public void setAuthPassword(String password) { 130 authpassword=password; 131 } 132 133 public String getAuthPassword() { 134 return authpassword; 135 } 136 137 140 public void setPropertiesFile( String p ) { 141 propFile=p; 142 loadPropertiesFile(); 143 } 144 145 public String getPropertiesFile() { 146 return propFile; 147 } 148 149 private boolean checkPropertiesFile() { 150 if(propFile == null) { 151 return false; 152 } 153 propsF = new File (propFile); 154 if(!propsF.isAbsolute()) { 155 String home = System.getProperty("catalina.base"); 156 if( home == null ) { 157 return false; 158 } 159 propsF = new File (home, propFile); 160 } 161 return propsF.exists(); 162 } 163 164 private void loadPropertiesFile() { 165 if(!checkPropertiesFile()) { 166 return; 167 } 168 try { 169 props.load( new FileInputStream (propsF) ); 170 if (props.containsKey("jrmpPort")){ 171 setJrmpPort(new Integer (getProperty("jrmpPort")).intValue()); 172 } 173 if (props.containsKey("jrmpHost")){ 174 setJrmpHost(getProperty("jrmpHost")); 175 } 176 if (props.containsKey("httpPort")){ 177 setHttpPort(new Integer (getProperty("httpPort")).intValue()); 178 } 179 if (props.containsKey("httpHost")){ 180 setHttpHost(getProperty("httpHost")); 181 } 182 if (props.containsKey("authMode")){ 183 setAuthMode(getProperty("authMode")); 184 } 185 if (props.containsKey("authUser")){ 186 setAuthUser(getProperty("authUser")); 187 } 188 if (props.containsKey("authPassword")){ 189 setAuthPassword(getProperty("authPassword")); 190 } 191 } catch(IOException ex ){ 192 193 } 194 } 195 196 public void saveProperties() { 197 if(propsF == null) { 198 return; 199 } 200 File outFile= new File (propsF.getParentFile(), propsF.getName()+".save"); 201 try { 202 props.store( new FileOutputStream (outFile), "AUTOMATICALLY GENERATED" ); 203 } catch(IOException ex ){ 204 } 205 } 206 207 210 public void setProperty( String n, String v ) { 211 props.put(n, v); 212 saveProperties(); 213 } 214 215 218 public String getProperty(String name) { 219 String result = null; 220 if(name != null) { 221 result = (String )props.get(name); 222 } 223 return result; 224 } 225 226 229 public void lifecycleEvent(LifecycleEvent arg0) { 230 if ("after_start".equals(arg0.getType())){ 231 registerAdaptors(); 232 } else if ("after_stop".equals(arg0.getType())){ 233 unRegisterAdaptors(); 234 } 235 } 236 237 private void registerAdaptors(){ 238 loadHttpAdaptor(); 239 loadRmiJrmpAdaptor(); 240 } 241 242 private void unRegisterAdaptors(){ 243 try { 244 if( httpServerName!=null ) { 245 mserver.invoke(httpServerName, "stop", null, null); 246 } 247 if( jrmpServerName!=null ) { 248 mserver.invoke(jrmpServerName, "stop", null, null); 249 } 250 if (adaptor!=null) { 251 adaptor.stop(); 252 } 253 } catch( Throwable t ) { 254 255 } 256 } 257 258 private MBeanServer getMBeanServer() { 259 MBeanServer server; 260 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) { 261 server=(MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 262 } else { 263 server=MBeanServerFactory.createMBeanServer(); 264 } 265 return (server); 266 } 267 268 private void registerObject(String className, ObjectName oName) 269 throws Exception { 270 Class c = Class.forName(className); 271 Object o = c.newInstance(); 272 mserver.registerMBean(o, oName); 273 } 274 275 private void loadHttpAdaptor (){ 276 boolean httpAdapterLoaded = false; 277 if (httpport != -1) { 278 try { 279 httpServerName = new ObjectName ("Http:name=HttpAdaptor"); 280 if (!mserver.isRegistered(httpServerName)){ 281 registerObject("mx4j.adaptor.http.HttpAdaptor", 282 httpServerName); 283 } 284 285 if( httphost!=null ) { 286 mserver.setAttribute(httpServerName, new Attribute ("Host", httphost)); 287 } 288 mserver.setAttribute(httpServerName, new Attribute ("Port", new Integer (httpport))); 289 290 if( "none".equals(authmode) || "basic".equals(authmode) || "digest".equals(authmode) ) 291 mserver.setAttribute(httpServerName, new Attribute ("AuthenticationMethod", authmode)); 292 293 if( authuser!=null && authpassword!=null ){ 294 mserver.invoke(httpServerName, "addAuthorization", 295 new Object [] { 296 authuser, 297 authpassword}, 298 new String [] { "java.lang.String", "java.lang.String" }); 299 } 300 301 if(useXSLTProcessor) { 302 ObjectName processorName = new ObjectName ("Http:name=XSLTProcessor"); 303 if (!mserver.isRegistered(processorName)){ 304 registerObject("mx4j.adaptor.http.XSLTProcessor", 305 processorName); 306 } 307 mserver.setAttribute(httpServerName, new Attribute ("ProcessorName", processorName)); 308 } 309 310 mserver.invoke(httpServerName, "start", null, null); 312 313 httpAdapterLoaded = true; 314 315 } catch( Throwable t ) { 316 httpServerName=null; 317 } 318 } 319 320 } 321 322 private void loadRmiJrmpAdaptor(){ 323 boolean jrmpAdapterLoaded = false; 324 if (jrmpport != -1) { 325 try { 326 ObjectName jrmpServerName = new ObjectName ("Naming:name=rmiregistry"); 327 if (!mserver.isRegistered(jrmpServerName)){ 328 registerObject("mx4j.tools.naming.NamingService", 329 jrmpServerName); 330 } 331 332 mserver.setAttribute(jrmpServerName, new Attribute ("Port", 333 new Integer (jrmpport))); 334 mserver.invoke(jrmpServerName, "start", null, null); 335 336 adaptor = new JRMPAdaptor(); 338 adaptor.putJNDIProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); 339 adaptor.putNamingProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); 340 ObjectName adaptorName = new ObjectName ("Adaptor:protocol=JRMP"); 341 String jndiName = "jrmp"; 342 adaptor.setJNDIName(jndiName); 343 adaptor.putJNDIProperty(javax.naming.Context.PROVIDER_URL, "rmi://"+jrmphost+":"+jrmpport); 344 adaptor.putNamingProperty(javax.naming.Context.PROVIDER_URL, "rmi://"+jrmphost+":"+jrmpport); 345 346 if (!"none".equals(authmode)){ 347 String user = null; 348 char[] password = null; 349 if (authuser!=null){ 350 user = authuser; 351 } else { 352 user = ""; 353 } 354 if (authpassword != null){ 355 password = authpassword.toCharArray(); 356 } else { 357 password = "".toCharArray(); 358 } 359 360 HashMap map = new HashMap (); 361 map.put(user, password); 362 UserPasswordAdaptorInterceptor intr = new UserPasswordAdaptorInterceptor(map,new SimplePasswordStore(map)); 363 ObjectName interceptorName = new ObjectName ("AdaptorInterceptor:type=timing"); 364 intr.setObjectName(interceptorName); 365 intr.setEnabled(true); 366 adaptor.addInterceptor(intr); 367 } 368 369 adaptor.setMBeanServer(mserver); 371 adaptor.start(); 372 373 jrmpAdapterLoaded = true; 374 375 } catch( Exception ex ) { 376 jrmpServerName = null; 377 adaptor = null; 378 } 379 } 380 381 } 382 383 } 384 | Popular Tags |