Example usage for java.rmi.server UnicastRemoteObject exportObject

List of usage examples for java.rmi.server UnicastRemoteObject exportObject

Introduction

In this page you can find the example usage for java.rmi.server UnicastRemoteObject exportObject.

Prototype

public static Remote exportObject(Remote obj, int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf)
        throws RemoteException 

Source Link

Document

Exports the remote object to make it available to receive incoming calls, using a transport specified by the given socket factory.

Usage

From source file:gridool.util.remoting.RemoteBase.java

protected void bind() throws RemoteException, NamingException {
    //if(System.getSecurityManager() == null) {// create and install a security manager
    //    System.setSecurityManager(new RMISecurityManager());
    //}/*from  w w w .j  av  a 2s.  c om*/
    final Remote stub;
    if (rmiProtocol.equals(RMI_PROTOCOL_JRMP_SSL)) {
        stub = UnicastRemoteObject.exportObject(this, exportPort, new SslRMIClientSocketFactory(),
                new SslRMIServerSocketFactory());
    } else {
        assert (rmiProtocol.equals(RMI_PROTOCOL_JRMP));
        stub = UnicastRemoteObject.exportObject(this, exportPort,
                TimeoutSocketProdiver.createClientSocketFactory(), null);
    }
    try {// bind the remote object's stub in the registry
        Naming.rebind(endpointUrl, stub);
    } catch (MalformedURLException e) {
        LOG.error("failed to bind: " + endpointUrl, e);
        throw new IllegalStateException("Illegal regist url: " + endpointUrl, e);
    }
    LOG.info("Remote object is bounded at " + endpointUrl + " for " + ObjectUtils.identityToString(this));
}

From source file:org.globus.workspace.remoting.RemotingServer.java

public void initialize() throws RemoteException, AlreadyBoundException {
    if (this.socketDirectory == null) {
        throw new IllegalStateException("socketDirectory must be specified");
    }//from w w  w .j  a v  a 2 s.  c om
    if (!this.socketDirectory.isDirectory()) {
        throw new IllegalStateException(
                "socketDirectory must be an existing directory: " + this.socketDirectory.getAbsolutePath());
    }

    if (this.bindings == null || this.bindings.isEmpty()) {
        throw new IllegalStateException("at least one binding must be specified");
    }

    final AFUNIXNaming naming = AFUNIXNaming.getInstance(this.socketDirectory);
    logger.debug("Socket directory: " + naming.getSocketFactory().getSocketDir());

    // this trick allows repeated initializations within the same JVM. (like during test runs)
    Registry registry;
    try {
        registry = naming.createRegistry();
    } catch (RemoteException e) {
        registry = naming.getRegistry();
    }

    final StringBuilder logMessage = new StringBuilder();
    logMessage.append("Nimbus remoting server listening on domain socket: \"")
            .append(this.socketDirectory.getAbsolutePath()).append("\". Bindings:");

    boolean first = true;
    for (final String bindingName : bindings.keySet()) {
        final Remote obj = bindings.get(bindingName);
        if (obj == null) {
            throw new IllegalStateException("Binding object " + bindingName + "' is null");
        }

        logger.debug("Binding " + obj.toString() + " to name '" + bindingName + "'");

        final Remote remote = UnicastRemoteObject.exportObject(obj, 0, naming.getSocketFactory(),
                naming.getSocketFactory());
        try {
            registry.bind(bindingName, remote);
        } catch (AlreadyBoundException e) {
            logger.warn("RMI binding '" + bindingName + "' is already bound. Proceeding.");
        }

        if (first) {
            first = false;
        } else {
            logMessage.append(",");
        }
        logMessage.append(" ").append(bindingName).append(" (").append(obj.getClass().getCanonicalName())
                .append(")");
    }

    logger.info(logMessage.toString());
}

From source file:org.jgentleframework.integration.remoting.rmi.support.RmiExecutor.java

/**
 * Run service.//from   w ww  . j a  v a  2s . c  om
 * 
 * @param rmiExporter
 *            the rmi exporter
 * @throws RemoteException
 *             the remote exception
 */
public static void runService(RmiExporter rmiExporter) throws RemoteException {

    // Chun b khi to rmi service
    prepareService(rmiExporter);
    Remote exportedObject = rmiExporter.getExportedObject();
    int servicePort = rmiExporter.getServicePort();
    String serviceName = rmiExporter.getServiceName();
    // Export RMI object bean thnh remote object.
    if (rmiExporter.getRmiClientSocketFactory() != null) {
        UnicastRemoteObject.exportObject(exportedObject, servicePort, rmiExporter.getRmiClientSocketFactory(),
                rmiExporter.getRmiServerSocketFactory());
    } else {
        UnicastRemoteObject.exportObject(exportedObject, servicePort);
    }
    // Bind RMI object bean vo registry.
    try {
        if (rmiExporter.isReplaceExistingBinding()) {
            rmiExporter.getRegistry().rebind(serviceName, exportedObject);
        } else {
            rmiExporter.getRegistry().bind(serviceName, exportedObject);
        }
    } catch (AlreadyBoundException ex) {
        unexportObjectBeanService(rmiExporter);
        throw new IllegalStateException(
                "Already an RMI object bound for name [" + serviceName + "'] :: " + ex.toString());
    } catch (RemoteException ex) {
        unexportObjectBeanService(rmiExporter);
        throw ex;
    }
    if (log.isInfoEnabled()) {
        log.info("RMI service ['" + serviceName + "'] at service_port [" + servicePort + "], registry_host ["
                + rmiExporter.getRegistryHost() + "], registry_port [" + rmiExporter.getRegistryPort()
                + "] is running ... !");
    }
}

From source file:org.springframework.remoting.rmi.RmiServiceExporter.java

/**
 * Register the service as RMI object.//from ww  w . j a va2s.  c om
 * Creates an RMI registry on the specified port if none exists.
 */
public void afterPropertiesSet() throws Exception {
    super.afterPropertiesSet();

    if (this.serviceName == null) {
        throw new IllegalArgumentException("serviceName is required");
    }
    if (this.clientSocketFactory instanceof RMIServerSocketFactory) {
        this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory;
    }
    if ((this.clientSocketFactory != null && this.serverSocketFactory == null)
            || (this.clientSocketFactory == null && this.serverSocketFactory != null)) {
        throw new IllegalArgumentException(
                "Both RMIClientSocketFactory and RMIServerSocketFactory or none required");
    }

    Registry registry = null;
    logger.info("Looking for RMI registry at port '" + this.registryPort + "'");
    try {
        // retrieve registry
        registry = LocateRegistry.getRegistry(this.registryPort);
        registry.list();
    } catch (RemoteException ex) {
        logger.debug("RMI registry access threw exception", ex);
        logger.warn("Could not detect RMI registry - creating new one");
        // assume no registry found -> create new one
        registry = LocateRegistry.createRegistry(this.registryPort);
    }

    // determine remote object
    if (getService() instanceof Remote) {
        // conventional RMI service
        this.exportedObject = (Remote) getService();
    } else {
        // RMI invoker
        logger.info("RMI object '" + this.serviceName + "' is an RMI invoker");
        this.exportedObject = new RmiInvocationWrapper(getProxyForService(), this);
    }

    // export remote object and bind it to registry
    logger.info(
            "Binding RMI service '" + this.serviceName + "' to registry at port '" + this.registryPort + "'");
    if (this.clientSocketFactory != null) {
        UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort, this.clientSocketFactory,
                this.serverSocketFactory);
    } else {
        UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort);
    }
    registry.rebind(this.serviceName, this.exportedObject);
}

From source file:xbird.engine.backend.QueryProcessor.java

protected final Serializable wrapResult(final Sequence result, final XQExpression queryExpr,
        final QueryRequest request, final DynamicContext dynEnv) {
    assert (dynEnv != null);
    final ReturnType returnType = request.getReturnType();
    final Serializable ret;
    switch (returnType) {
    case SEQUENCE:
        final List<Item> materialized = result.materialize();
        ret = new ValueSequence(materialized, dynEnv);
        break;/* www  . j a  v a  2  s.  c  o m*/
    case MARSHALLED_SEQUENCE:
        ret = new MarshalledSequence(result, dynEnv);
        break;
    case AUTO:
    case REDIRECTABLE_MARSHALLED_SEQUENCE:
    case REMOTE_PADED_SEQUENCE: {
        final RedirectableChecker check = new RedirectableChecker();
        try {
            check.visit(queryExpr, null);
        } catch (XQueryException e) {
            throw new IllegalStateException(e);
        }
        if (check.isRedirectable()) {
            MarshalledSequence seq = new MarshalledSequence(result, dynEnv);
            seq.setRedirectable(true);
            ret = seq;
        } else if (returnType == ReturnType.REMOTE_PADED_SEQUENCE) {
            MarshalledSequence seq = new MarshalledSequence(result, dynEnv);
            seq.setRemotePaging(true);
            ret = seq;
        } else if (returnType == ReturnType.AUTO) {// treat as THROTTLED_ASYNC_REMOTE_SEQUENCE
            final ThrottedRemoteSequenceProxy remote = new ThrottedRemoteSequenceProxy(_throttle, result,
                    request);
            try {
                UnicastRemoteObject.exportObject(remote, 0, TimeoutSocketProdiver.createClientSocketFactory(),
                        TimeoutSocketProdiver.createServerSocketFactory());
            } catch (RemoteException e) {
                throw new IllegalStateException("failed exporting result sequence", e);
            }
            ret = new RemoteSequence(remote, result.getType());
            _executors.execute(remote);
        } else {
            assert (returnType == ReturnType.REDIRECTABLE_MARSHALLED_SEQUENCE) : returnType;
            ret = new MarshalledSequence(result, dynEnv);
        }
        break;
    }
    case REMOTE_SEQUENCE: {
        final RemoteSequenceProxy remote = new RemoteSequenceProxy(result, request);
        try {
            UnicastRemoteObject.exportObject(remote, 0, TimeoutSocketProdiver.createClientSocketFactory(),
                    TimeoutSocketProdiver.createServerSocketFactory());
        } catch (RemoteException e) {
            throw new IllegalStateException("failed exporting result sequence", e);
        }
        ret = new RemoteSequence(remote, result.getType());
        break;
    }
    case ASYNC_REMOTE_SEQUENCE: {
        final RunnableRemoteSequenceProxy remote = new RunnableRemoteSequenceProxy(result, request);
        try {
            UnicastRemoteObject.exportObject(remote, 0, TimeoutSocketProdiver.createClientSocketFactory(),
                    TimeoutSocketProdiver.createServerSocketFactory());
        } catch (RemoteException e) {
            throw new IllegalStateException("failed exporting result sequence", e);
        }
        ret = new RemoteSequence(remote, result.getType());
        _executors.execute(remote);
        break;
    }
    case THROTTLED_ASYNC_REMOTE_SEQUENCE: {
        final ThrottedRemoteSequenceProxy remote = new ThrottedRemoteSequenceProxy(_throttle, result, request);
        try {
            UnicastRemoteObject.exportObject(remote, 0, TimeoutSocketProdiver.createClientSocketFactory(),
                    TimeoutSocketProdiver.createServerSocketFactory());
        } catch (RemoteException e) {
            throw new IllegalStateException("failed exporting result sequence", e);
        }
        ret = new RemoteSequence(remote, result.getType());
        _executors.execute(remote);
        break;
    }
    case STRING:
        ret = result.toString();
        break;
    default:
        throw new IllegalStateException("Unexpected return type: " + returnType);
    }
    return ret;
}

From source file:xbird.engine.remote.RemoteSequenceProxy.java

public RemoteFocus iterator() throws RemoteException {
    final IFocus<Item> focus = _delegate.iterator();
    final IRemoteFocusProxy proxy = new RemoteFocusProxy(focus, this);
    UnicastRemoteObject.exportObject(proxy, 0, TimeoutSocketProdiver.createClientSocketFactory(),
            TimeoutSocketProdiver.createServerSocketFactory());
    return new RemoteFocus(proxy, _fetchSize, _fetchGrow, _fetchMethod);
}

From source file:xbird.engine.XQEngineClient.java

private static void exportMe(final ResultHandler handler, final Request request) throws RemoteException {
    // Must use zero for an anonymous export port
    // http://archives.java.sun.com/cgi-bin/wa?A2=ind0501&L=rmi-users&P=556
    UnicastRemoteObject.exportObject(handler, 0, null, TimeoutSocketProdiver.createServerSocketFactory());
}

From source file:xbird.server.RemoteServerBase.java

protected void bind() throws RemoteException, NamingException {
    final Remote stub;
    if (RMI_PROTOCOL.equals(ServerConstants.RMI_PROTOCOL_JRMP_SSL)) {
        stub = (Remote) UnicastRemoteObject.exportObject(this, _exportPort, new SslRMIClientSocketFactory(),
                new SslRMIServerSocketFactory());
    } else {//w  w  w .j  a  va2s .com
        assert (RMI_PROTOCOL.equals("jrmp"));
        stub = (Remote) UnicastRemoteObject.exportObject(this, _exportPort);
    }
    // Bind the remote object's stub in the registry
    try {
        Naming.rebind(_bindUrl, stub);
    } catch (MalformedURLException e) {
        throw new IllegalStateException("Illegal regist url:" + _bindUrl, e);
    }
    LOG.info("Remote object is bounded at " + _bindUrl);
}

From source file:xbird.xquery.expr.ext.BDQExpr.java

private static void prepareVariablesToShip(final QueryRequest request, final ShippedVariable shippedVar,
        final Sequence<? extends Item> contextSeq, final DynamicContext dynEnv) throws XQueryException {
    final Sequence result = shippedVar.eval(contextSeq, dynEnv);
    final ReturnType rettype = request.getReturnType();
    if (RemoteEval.ENV_NOWRAP_VARSHIP || !rettype.isRemoteSequnece()) {
        shippedVar.setResult(new MarshalledSequence(result, dynEnv));
    } else if (result instanceof SingleItem) {
        shippedVar.setResult(result);/* w ww  .ja v  a 2  s  .c o  m*/
    } else {
        final RemoteSequenceProxy proxy = new RemoteSequenceProxy(result, request);
        try {
            UnicastRemoteObject.exportObject(proxy, 0, TimeoutSocketProdiver.createClientSocketFactory(),
                    TimeoutSocketProdiver.createServerSocketFactory());
        } catch (RemoteException e) {
            throw new XQueryException("failed exporting variable: " + shippedVar.getName(), e);
        }
        final RemoteSequence remote = new RemoteSequence(proxy, result.getType());
        shippedVar.setResult(remote);
    }
}

From source file:xbird.xquery.ext.grid.LocalQueryExecTask.java

@SuppressWarnings("unchecked")
private static Sequence wrapResult(final Sequence result, final LocalQueryExecTask task,
        final boolean doForwarding, final boolean queryShipping) {
    if (doForwarding) {
        if (queryShipping) {// TODO take CPU usage into consideration
            if (LOG.isInfoEnabled()) {
                LOG.info("Response method for a task [" + task.getTaskId() + "]: QueryShipping");
            }//from  w w w .ja v a2 s.com
            final ThrottedRemoteSequenceProxy remote = new ThrottedRemoteSequenceProxy(result);
            try {
                UnicastRemoteObject.exportObject(remote, 0, TimeoutSocketProdiver.createClientSocketFactory(),
                        TimeoutSocketProdiver.createServerSocketFactory());
            } catch (RemoteException e) {
                throw new IllegalStateException("failed exporting result sequence", e);
            }
            final RemoteSequence ret = new RemoteSequence(remote, result.getType());
            new Thread(remote, "QueryTask[" + task.getTaskId() + "]#AsyncQueryProcessor").start();
            return ret;
        } else {
            if (LOG.isInfoEnabled()) {
                LOG.info("Response method for a task [" + task.getTaskId() + "]: DataShipping");
            }
            MarshalledSequence seq = new MarshalledSequence(result, DynamicContext.DUMMY);
            seq.setRemotePaging(true);
            return seq;
        }
    } else {
        if (LOG.isInfoEnabled()) {
            LOG.info("Response method for a task [" + task.getTaskId() + "]: Raw Sequence");
        }
        return result;
    }
}