001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: RMIServerRPCImpl.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.rpc.rmi.server;
025:
026: import java.rmi.RemoteException;
027:
028: import javax.rmi.PortableRemoteObject;
029:
030: import org.ow2.easybeans.api.EZBContainer;
031: import org.ow2.easybeans.api.Factory;
032: import org.ow2.easybeans.rpc.api.EJBRequest;
033: import org.ow2.easybeans.rpc.api.EJBResponse;
034: import org.ow2.easybeans.server.Embedded;
035:
036: /**
037: * Server side object which handle the EJB requests.
038: * @author Florent Benoit
039: */
040: public class RMIServerRPCImpl extends PortableRemoteObject implements
041: RMIServerRPC {
042:
043: /**
044: * Server on which it depends.
045: */
046: private Embedded ejb3server;
047:
048: /**
049: * Retry time when container is not available.
050: */
051: private static final int WAIT_TIME = 1000;
052:
053: /**
054: * This invoker will discuss with the embedded server when receiving requests.
055: * @param ejb3server the server on which send requests.
056: * @throws RemoteException if RPC fails
057: */
058: public RMIServerRPCImpl(final Embedded ejb3server)
059: throws RemoteException {
060: super ();
061: this .ejb3server = ejb3server;
062: }
063:
064: /**
065: * Handle a request and send back a response.<br>
066: * It finds the right container and its factory and send the request to the factory.
067: * @param request the ejb request to handle.
068: * @return a response.
069: * @throws RemoteException if there are errors on the prococol.
070: */
071: public EJBResponse getEJBResponse(final EJBRequest request)
072: throws RemoteException {
073:
074: String id = request.getContainerId();
075: if (id == null) {
076: throw new RemoteException("No valid container id");
077: }
078: // Get the container
079: EZBContainer container = ejb3server.getContainer(id);
080: if (container == null) {
081: throw new RemoteException(
082: "Cannot find the container with id '" + id + "'.");
083: }
084:
085: // Once the container is found, get the factory
086: String factoryName = request.getFactory();
087:
088: // while container is not available, stop the current request
089: while (!container.isAvailable()) {
090: //TODO: change it to a semaphore ?
091: try {
092: Thread.sleep(WAIT_TIME);
093: } catch (InterruptedException e) {
094: e.printStackTrace();
095: }
096: }
097:
098: // Get the container
099: Factory factory = container.getFactory(factoryName);
100: if (factory == null) {
101: throw new RemoteException(
102: "Cannot find the factory with name '" + factoryName
103: + "'.");
104: }
105:
106: // Now, need to invoke the bean
107: return factory.rpcInvoke(request);
108: //throw new RemoteException("Error in invoking method on factory", e.getCause());
109: }
110:
111: }
|