1 25 package org.ofbiz.service.rmi; 26 27 import java.rmi.Naming ; 28 import java.rmi.RemoteException ; 29 import java.rmi.server.RMIClientSocketFactory ; 30 import java.rmi.server.RMIServerSocketFactory ; 31 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 35 import org.ofbiz.base.container.Container; 36 import org.ofbiz.base.container.ContainerConfig; 37 import org.ofbiz.base.container.ContainerException; 38 import org.ofbiz.entity.GenericDelegator; 39 import org.ofbiz.service.GenericDispatcher; 40 import org.ofbiz.service.LocalDispatcher; 41 42 49 public class RmiServiceContainer implements Container { 50 51 public static final String module = RmiServiceContainer.class.getName(); 52 53 protected RemoteDispatcherImpl remote = null; 54 protected String configFile = null; 55 protected String name = null; 56 57 59 62 public void init(String [] args, String configFile) { 63 this.configFile = configFile; 64 } 65 66 public boolean start() throws ContainerException { 67 ContainerConfig.Container cfg = ContainerConfig.getContainer("rmi-dispatcher", configFile); 69 ContainerConfig.Container.Property initialCtxProp = cfg.getProperty("use-initial-context"); 70 ContainerConfig.Container.Property lookupHostProp = cfg.getProperty("bound-host"); 71 ContainerConfig.Container.Property lookupPortProp = cfg.getProperty("bound-port"); 72 ContainerConfig.Container.Property lookupNameProp = cfg.getProperty("bound-name"); 73 ContainerConfig.Container.Property delegatorProp = cfg.getProperty("delegator-name"); 74 ContainerConfig.Container.Property clientProp = cfg.getProperty("client-factory"); 75 ContainerConfig.Container.Property serverProp = cfg.getProperty("server-factory"); 76 77 if (lookupNameProp == null || lookupNameProp.value == null || lookupNameProp.value.length() == 0) { 79 throw new ContainerException("Invalid lookup-name defined in container configuration"); 80 } else { 81 this.name = lookupNameProp.value; 82 } 83 84 if (delegatorProp == null || delegatorProp.value == null || delegatorProp.value.length() == 0) { 86 throw new ContainerException("Invalid delegator-name defined in container configuration"); 87 } 88 89 String useCtx = initialCtxProp == null || initialCtxProp.value == null ? "false" : initialCtxProp.value; 90 String host = lookupHostProp == null || lookupHostProp.value == null ? "localhost" : lookupHostProp.value; 91 String port = lookupPortProp == null || lookupPortProp.value == null ? "1099" : lookupPortProp.value; 92 boolean clientAuth = ContainerConfig.getPropertyValue(cfg, "ssl-client-auth", false); 93 94 RMIClientSocketFactory csf = null; 96 RMIServerSocketFactory ssf = null; 97 98 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 100 101 if (clientProp != null && clientProp.value != null && clientProp.value.length() > 0) { 103 try { 104 Class c = loader.loadClass(clientProp.value); 105 csf = (RMIClientSocketFactory ) c.newInstance(); 106 } catch (Exception e) { 107 throw new ContainerException(e); 108 } 109 } 110 if (serverProp != null && serverProp.value != null && serverProp.value.length() > 0) { 111 try { 112 Class c = loader.loadClass(serverProp.value); 113 ssf = (RMIServerSocketFactory ) c.newInstance(); 114 } catch (Exception e) { 115 throw new ContainerException(e); 116 } 117 } 118 119 if (ssf != null && ssf instanceof org.ofbiz.service.rmi.socket.ssl.SSLServerSocketFactory) { 121 ((org.ofbiz.service.rmi.socket.ssl.SSLServerSocketFactory) ssf).setNeedClientAuth(clientAuth); 122 } 123 124 GenericDelegator delegator = GenericDelegator.getGenericDelegator(delegatorProp.value); 126 127 LocalDispatcher dispatcher = new GenericDispatcher(name, delegator); 129 130 try { 132 remote = new RemoteDispatcherImpl(dispatcher, csf, ssf); 133 } catch (RemoteException e) { 134 throw new ContainerException("Unable to start the RMI dispatcher", e); 135 } 136 137 if (!useCtx.equalsIgnoreCase("true")) { 138 try { 140 Naming.rebind("//" + host + ":" + port + "/" + name, remote); 141 } catch (RemoteException e) { 142 throw new ContainerException("Unable to bind RMIDispatcher to RMI on " + "//host[" + host + "]:port[" + port + "]/name[" + name + "] - with remote=" + remote , e); 143 } catch (java.net.MalformedURLException e) { 144 throw new ContainerException("Invalid URL for binding", e); 145 } 146 } else { 147 try { 149 InitialContext ic = new InitialContext (); 150 ic.rebind(name, remote); 151 } catch (NamingException e) { 152 throw new ContainerException("Unable to bind RMIDispatcher to JNDI", e); 153 } 154 155 try { 157 InitialContext ic = new InitialContext (); 158 Object o = ic.lookup(name); 159 if (o == null) { 160 throw new NamingException ("Object came back null"); 161 } 162 } catch (NamingException e) { 163 throw new ContainerException("Unable to lookup bound objects", e); 164 } 165 } 166 167 return true; 168 } 169 170 public void stop() throws ContainerException { 171 remote.deregister(); 172 } 173 } 174 | Popular Tags |