001: /*
002: * $Id: RmiServiceContainer.java,v 1.1 2003/12/02 06:39:32 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.rmi;
026:
027: import org.ofbiz.base.container.Container;
028: import org.ofbiz.base.container.ContainerException;
029: import org.ofbiz.base.container.ContainerConfig;
030: import org.ofbiz.service.*;
031: import org.ofbiz.entity.GenericDelegator;
032:
033: import java.rmi.RemoteException;
034: import java.rmi.Naming;
035: import java.rmi.server.RMIClientSocketFactory;
036: import java.rmi.server.RMIServerSocketFactory;
037:
038: /**
039: * RMI Service Engine Container / Dispatcher
040: *
041: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
042: * @version $Revision: 1.1 $
043: * @since 3.0
044: */
045: public class RmiServiceContainer implements Container {
046:
047: public static final String module = RmiServiceContainer.class
048: .getName();
049:
050: protected RemoteDispatcherImpl remote = null;
051: protected String name = null;
052:
053: // Container methods
054:
055: public boolean start(String configFile) throws ContainerException {
056: // get the container config
057: ContainerConfig.Container cfg = ContainerConfig.getContainer(
058: "rmi-dispatcher", configFile);
059: ContainerConfig.Container.Property lookupNameProp = cfg
060: .getProperty("lookup-name");
061: ContainerConfig.Container.Property delegatorProp = cfg
062: .getProperty("delegator-name");
063: ContainerConfig.Container.Property clientProp = cfg
064: .getProperty("client-factory");
065: ContainerConfig.Container.Property serverProp = cfg
066: .getProperty("server-factory");
067:
068: // check the required lookup-name property
069: if (lookupNameProp == null || lookupNameProp.value == null
070: || lookupNameProp.value.length() == 0) {
071: throw new ContainerException(
072: "Invalid lookup-name defined in container configuration");
073: } else {
074: this .name = lookupNameProp.value;
075: }
076:
077: // check the required delegator-name property
078: if (delegatorProp == null || delegatorProp.value == null
079: || delegatorProp.value.length() == 0) {
080: throw new ContainerException(
081: "Invalid delegator-name defined in container configuration");
082: }
083:
084: // setup the factories
085: RMIClientSocketFactory csf = null;
086: RMIServerSocketFactory ssf = null;
087:
088: // get the classloader
089: ClassLoader loader = Thread.currentThread()
090: .getContextClassLoader();
091:
092: // load the factories
093: if (clientProp != null && clientProp.value != null
094: && clientProp.value.length() > 0) {
095: try {
096: Class c = loader.loadClass(clientProp.value);
097: csf = (RMIClientSocketFactory) c.newInstance();
098: } catch (Exception e) {
099: throw new ContainerException(e);
100: }
101: }
102: if (serverProp != null && serverProp.value != null
103: && serverProp.value.length() > 0) {
104: try {
105: Class c = loader.loadClass(serverProp.value);
106: ssf = (RMIServerSocketFactory) c.newInstance();
107: } catch (Exception e) {
108: throw new ContainerException(e);
109: }
110: }
111:
112: // get the delegator for this container
113: GenericDelegator delegator = GenericDelegator
114: .getGenericDelegator(delegatorProp.value);
115:
116: // create the LocalDispatcher
117: LocalDispatcher dispatcher = new GenericDispatcher(name,
118: delegator);
119:
120: // create the RemoteDispatcher
121: try {
122: remote = new RemoteDispatcherImpl(dispatcher, csf, ssf);
123: } catch (RemoteException e) {
124: throw new ContainerException(
125: "Unable to start the RMI dispatcher", e);
126: }
127:
128: // bind this object in JNDI
129: try {
130: Naming.rebind(name, remote);
131: } catch (RemoteException e) {
132: throw new ContainerException("Unable to bind object", e);
133: } catch (java.net.MalformedURLException e) {
134: throw new ContainerException("Problem binding JNDI name", e);
135: }
136:
137: return true;
138: }
139:
140: public void stop() throws ContainerException {
141: remote.deregister();
142: }
143: }
|