KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > rmi > RmiServiceContainer


1 /*
2  * $Id: RmiServiceContainer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.rmi;
26
27 import java.rmi.Naming JavaDoc;
28 import java.rmi.RemoteException JavaDoc;
29 import java.rmi.server.RMIClientSocketFactory JavaDoc;
30 import java.rmi.server.RMIServerSocketFactory JavaDoc;
31
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
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 /**
43  * RMI Service Engine Container / Dispatcher
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 3.0
48  */

49 public class RmiServiceContainer implements Container {
50
51     public static final String JavaDoc module = RmiServiceContainer.class.getName();
52
53     protected RemoteDispatcherImpl remote = null;
54     protected String JavaDoc configFile = null;
55     protected String JavaDoc name = null;
56
57     // Container methods
58

59     /**
60      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
61      */

62     public void init(String JavaDoc[] args, String JavaDoc configFile) {
63         this.configFile = configFile;
64     }
65     
66     public boolean start() throws ContainerException {
67         // get the container config
68
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         // check the required lookup-name property
78
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         // check the required delegator-name property
85
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 JavaDoc useCtx = initialCtxProp == null || initialCtxProp.value == null ? "false" : initialCtxProp.value;
90         String JavaDoc host = lookupHostProp == null || lookupHostProp.value == null ? "localhost" : lookupHostProp.value;
91         String JavaDoc port = lookupPortProp == null || lookupPortProp.value == null ? "1099" : lookupPortProp.value;
92         boolean clientAuth = ContainerConfig.getPropertyValue(cfg, "ssl-client-auth", false);
93
94         // setup the factories
95
RMIClientSocketFactory JavaDoc csf = null;
96         RMIServerSocketFactory JavaDoc ssf = null;
97
98         // get the classloader
99
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
100
101         // load the factories
102
if (clientProp != null && clientProp.value != null && clientProp.value.length() > 0) {
103             try {
104                 Class JavaDoc c = loader.loadClass(clientProp.value);
105                 csf = (RMIClientSocketFactory JavaDoc) c.newInstance();
106             } catch (Exception JavaDoc e) {
107                 throw new ContainerException(e);
108             }
109         }
110         if (serverProp != null && serverProp.value != null && serverProp.value.length() > 0) {
111             try {
112                 Class JavaDoc c = loader.loadClass(serverProp.value);
113                 ssf = (RMIServerSocketFactory JavaDoc) c.newInstance();
114             } catch (Exception JavaDoc e) {
115                 throw new ContainerException(e);
116             }
117         }
118
119         // set the client auth flag on our custom SSL socket factory
120
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         // get the delegator for this container
125
GenericDelegator delegator = GenericDelegator.getGenericDelegator(delegatorProp.value);
126
127         // create the LocalDispatcher
128
LocalDispatcher dispatcher = new GenericDispatcher(name, delegator);
129
130         // create the RemoteDispatcher
131
try {
132             remote = new RemoteDispatcherImpl(dispatcher, csf, ssf);
133         } catch (RemoteException JavaDoc e) {
134             throw new ContainerException("Unable to start the RMI dispatcher", e);
135         }
136
137         if (!useCtx.equalsIgnoreCase("true")) {
138             // bind RMIDispatcher to RMI Naming (Must be JRMP protocol)
139
try {
140                 Naming.rebind("//" + host + ":" + port + "/" + name, remote);
141             } catch (RemoteException JavaDoc 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 JavaDoc e) {
144                 throw new ContainerException("Invalid URL for binding", e);
145             }
146         } else {
147             // bind RMIDispatcher to InitialContext (must be RMI protocol not IIOP)
148
try {
149                 InitialContext JavaDoc ic = new InitialContext JavaDoc();
150                 ic.rebind(name, remote);
151             } catch (NamingException JavaDoc e) {
152                 throw new ContainerException("Unable to bind RMIDispatcher to JNDI", e);
153             }
154
155             // check JNDI
156
try {
157                 InitialContext JavaDoc ic = new InitialContext JavaDoc();
158                 Object JavaDoc o = ic.lookup(name);
159                 if (o == null) {
160                     throw new NamingException JavaDoc("Object came back null");
161                 }
162             } catch (NamingException JavaDoc 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