1 17 18 package org.apache.james.remotemanager; 19 20 import java.rmi.Naming ; 21 import java.rmi.Remote ; 22 import java.rmi.RemoteException ; 23 import java.rmi.server.UnicastRemoteObject ; 24 import java.rmi.registry.LocateRegistry ; 25 import java.rmi.registry.Registry ; 26 import org.apache.avalon.phoenix.Block; 27 import org.apache.avalon.framework.logger.AbstractLogEnabled; 28 import org.apache.avalon.framework.logger.LogEnabled; 29 import org.apache.avalon.framework.context.Contextualizable; 30 import org.apache.avalon.framework.context.Context; 31 import org.apache.avalon.framework.context.ContextException; 32 import org.apache.avalon.framework.context.DefaultContext; 33 import org.apache.avalon.framework.component.Composable; 34 import org.apache.avalon.framework.component.ComponentManager; 35 import org.apache.avalon.framework.component.ComponentException; 36 import org.apache.avalon.framework.configuration.Configurable; 37 import org.apache.avalon.framework.configuration.Configuration; 38 import org.apache.avalon.framework.configuration.ConfigurationException; 39 import org.apache.avalon.framework.activity.Initializable; 40 41 42 46 public class RMIRemoteManager extends AbstractLogEnabled 47 implements Block, Contextualizable, Composable, Configurable, Initializable { 48 49 private static final String RMIREGISTRY = "rmiregistry"; 51 private static final String RMIREGISTRY_HOST = "host"; 52 private static final String RMIREGISTRY_PORT = "port"; 53 private static final String RMIREGISTRY_NEW = "new"; 54 private static final String RMI_OBJECTS = "objects"; 55 private static final String RMI_OBJECT = "object"; 56 private static final String RMI_OBJECT_INTERFACE = "interface"; 57 private static final String RMI_OBJECT_CLASS = "class"; 58 private static final String RMI_OBJECT_BINDNAME = "bindname"; 59 private static final String RMI_OBJECT_BIND = "bind"; 60 private static final String RMI_OBJECT_CONFIGURATION = "configuration"; 61 62 private DefaultContext context; 63 private ComponentManager componentManager; 64 65 private int rmiRegistryPort = 1099; 67 private String rmiRegistryHost = "localhost"; 68 71 private boolean createNewRmiRegistry = false; 72 private Configuration rmiObjects; 73 74 75 public void contextualize(Context context) 76 throws ContextException { 77 this.context = new DefaultContext(context); 78 } 79 80 public void compose(ComponentManager componentManager) 81 throws ComponentException { 82 this.componentManager = componentManager; 83 } 84 85 public void configure(Configuration configuration) 86 throws ConfigurationException { 87 final Configuration rmiRegistryConf = configuration.getChild(this.RMIREGISTRY); 88 this.rmiRegistryHost = rmiRegistryConf.getAttribute(this.RMIREGISTRY_HOST, "localhost"); 89 this.rmiRegistryPort = rmiRegistryConf.getAttributeAsInteger(this.RMIREGISTRY_PORT, 1099); 90 this.createNewRmiRegistry = rmiRegistryConf.getAttributeAsBoolean(this.RMIREGISTRY_NEW, false); 91 92 this.rmiObjects = configuration.getChild(this.RMI_OBJECTS); 93 } 94 95 101 public void initialize() 102 throws ConfigurationException { 103 final Configuration[] objects = this.rmiObjects.getChildren(this.RMI_OBJECT); 104 if (this.createNewRmiRegistry) { 105 try { 107 LocateRegistry.createRegistry(this.rmiRegistryPort); 108 this.getLogger().info("new rmi registry at port " + this.rmiRegistryPort + " created!"); 109 } catch (RemoteException re) { 110 getLogger().error("Couldn't create a RMI Registry at port " + this.rmiRegistryPort); 111 throw new ConfigurationException("Please check your RMI Registry settings!!!"); 112 } 113 } else { 114 try { 116 Registry registry = LocateRegistry.getRegistry(this.rmiRegistryHost, this.rmiRegistryPort); 117 } catch (RemoteException re) { 118 getLogger().error("Couldn't find RMI Registry [" + this.rmiRegistryHost + ":" + 119 this.rmiRegistryPort); 120 throw new ConfigurationException("Please check your RMI Registry settings!!!"); 121 } 122 } 123 124 for (int i = 0; i < objects.length; i++) { 126 final String rmiInterface = objects[i].getAttribute(this.RMI_OBJECT_INTERFACE); 127 final String rmiClass = objects[i].getAttribute(this.RMI_OBJECT_CLASS); 128 final String rmiBindname = objects[i].getAttribute(this.RMI_OBJECT_BINDNAME); 129 final boolean rmiBind = objects[i].getAttributeAsBoolean(this.RMI_OBJECT_BIND, false); 130 final Configuration rmiObjectConf = objects[i].getChild(this.RMI_OBJECT_CONFIGURATION); 131 try { 132 Class classObject = Class.forName(rmiClass); 133 Remote remote = (Remote )classObject.newInstance(); 134 if (remote instanceof LogEnabled) { 136 ((LogEnabled)remote).enableLogging(getLogger().getChildLogger(rmiBindname)); 137 } 138 if (remote instanceof Contextualizable) { 140 ((Contextualizable)remote).contextualize(this.context); 141 } 142 if (remote instanceof Composable) { 143 ((Composable)remote).compose(this.componentManager); 144 } 145 if (remote instanceof Configurable) { 146 ((Configurable)remote).configure(rmiObjectConf); 147 } 148 if (remote instanceof Initializable) { 149 ((Initializable)remote).initialize(); 150 } 151 UnicastRemoteObject.exportObject(remote); 153 this.getLogger().info("Export RMI Object " + rmiClass); 154 this.context.put(rmiBindname, remote); 156 if (rmiBind) { 157 String bindName = "//" + this.rmiRegistryHost + 158 ":" + this.rmiRegistryPort + 159 "/" + rmiBindname; 160 Naming.rebind(bindName, remote); 161 this.getLogger().info("Rebind '" + rmiClass + "' to " + bindName); 162 } 163 } catch (ClassNotFoundException cnfe) { 164 this.getLogger().error("Could not find RMI Class object!", cnfe); 165 } catch (ContextException ce) { 166 this.getLogger().error(ce.getMessage(), ce); 167 } catch (ComponentException cpe) { 168 this.getLogger().error(cpe.getMessage(), cpe); 169 } catch (ConfigurationException cfe) { 170 this.getLogger().error(cfe.getMessage(), cfe); 171 } catch (Exception e) { 172 this.getLogger().error(e.getMessage(), e); 173 } 174 } 175 } 176 177 } 178 179 | Popular Tags |