Search in sources :

Example 1 with ServerMetaData

use of org.apache.openejb.client.ServerMetaData in project tomee by apache.

the class BasicClusterableRequestHandler method updateServer.

@Override
public void updateServer(final BeanContext beanContext, final ClusterableRequest req, final ClusterableResponse res) {
    final Container container = beanContext.getContainer();
    if (container instanceof ClusteredRPCContainer) {
        final ClusteredRPCContainer clusteredContainer = (ClusteredRPCContainer) container;
        final URI[] locations = clusteredContainer.getLocations(beanContext);
        if (null != locations) {
            final ServerMetaData server = new ServerMetaData(locations);
            if (req.getServerHash() != server.buildHash()) {
                res.setServer(server);
            }
        }
    }
}
Also used : ClusteredRPCContainer(org.apache.openejb.ClusteredRPCContainer) Container(org.apache.openejb.Container) URI(java.net.URI) ServerMetaData(org.apache.openejb.client.ServerMetaData) ClusteredRPCContainer(org.apache.openejb.ClusteredRPCContainer)

Example 2 with ServerMetaData

use of org.apache.openejb.client.ServerMetaData in project tomee by apache.

the class EjbDaemon method service.

public void service(final InputStream rawIn, final OutputStream rawOut) throws IOException {
    final ProtocolMetaData clientProtocol = new ProtocolMetaData();
    ObjectInputStream ois = null;
    ObjectOutputStream oos = null;
    RequestType requestType = null;
    byte requestTypeByte = RequestType.NOP_REQUEST.getCode();
    try {
        final RequestInfos.RequestInfo info = RequestInfos.info();
        info.setInputStream(countStreams ? new CountingInputStream(rawIn) : rawIn);
        // Read client Protocol Version
        final InputStream cis = info.getInputStream();
        clientProtocol.readExternal(cis);
        ois = new EjbObjectInputStream(cis);
        // Read ServerMetaData
        final ServerMetaData serverMetaData = new ServerMetaData();
        serverMetaData.readExternal(ois);
        ClientObjectFactory.serverMetaData.set(serverMetaData);
        // Read request type
        requestTypeByte = ois.readByte();
        requestType = RequestType.valueOf(requestTypeByte);
        if (requestType == RequestType.NOP_REQUEST) {
            return;
        }
        ClusterResponse clusterResponse = null;
        if (requestType == RequestType.CLUSTER_REQUEST) {
            clusterResponse = clusterHandler.processRequest(ois, clientProtocol);
            //Check for immediate failure
            final Throwable failure = clusterResponse.getFailure();
            if (null != clusterResponse && null != failure) {
                clusterHandler.getLogger().debug("Failed to write to ClusterResponse", failure);
                try {
                    info.setOutputStream(countStreams ? new CountingOutputStream(rawOut) : rawOut);
                    oos = new ObjectOutputStream(info.getOutputStream());
                    clusterResponse.setMetaData(clientProtocol);
                    clusterResponse.writeExternal(oos);
                    oos.flush();
                } catch (IOException ie) {
                    final String m = "Failed to write to ClusterResponse: " + ie.getMessage();
                    clusterHandler.getLogger().error(m, ie);
                    throw Exceptions.newIOException(m, ie);
                }
                throw failure;
            }
        }
        requestTypeByte = ois.readByte();
        requestType = RequestType.valueOf(requestTypeByte);
        if (requestType == RequestType.NOP_REQUEST) {
            return;
        }
        // Exceptions should not be thrown from these methods
        // They should handle their own exceptions and clean
        // things up with the client accordingly.
        final Response response;
        switch(requestType) {
            case EJB_REQUEST:
                response = processEjbRequest(ois, clientProtocol);
                break;
            case JNDI_REQUEST:
                response = processJndiRequest(ois, clientProtocol);
                break;
            case AUTH_REQUEST:
                response = processAuthRequest(ois, clientProtocol);
                break;
            case LOGOUT_REQUEST:
                response = processLogoutRequest(ois, clientProtocol);
                break;
            default:
                logger.error("\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Unknown request type " + requestType);
                return;
        }
        try {
            info.setOutputStream(countStreams ? new CountingOutputStream(rawOut) : rawOut);
            final OutputStream cos = info.getOutputStream();
            //Let client know we are using the requested protocol to respond
            clientProtocol.writeExternal(cos);
            cos.flush();
            oos = new ObjectOutputStream(cos);
            clusterHandler.processResponse(clusterResponse, oos, clientProtocol);
            oos.flush();
        } finally {
            switch(requestType) {
                case EJB_REQUEST:
                    processEjbResponse(response, oos, clientProtocol);
                    break;
                case JNDI_REQUEST:
                    processJndiResponse(response, oos, clientProtocol);
                    break;
                case AUTH_REQUEST:
                    processAuthResponse(response, oos, clientProtocol);
                    break;
                case LOGOUT_REQUEST:
                    processLogoutResponse(response, oos, clientProtocol);
                    break;
                default:
                    //Should never get here...
                    logger.error("\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Unknown response type " + requestType);
            }
        }
    } catch (IllegalArgumentException iae) {
        final String msg = "\"" + clientProtocol.getSpec() + "\" FAIL \"Unknown request type " + requestTypeByte;
        if (logger.isDebugEnabled()) {
            logger.debug(msg, iae);
        } else {
            logger.warning(msg + " - Debug for StackTrace");
        }
    } catch (SecurityException e) {
        final String msg = "\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Security error - " + e.getMessage() + "\"";
        if (logger.isDebugEnabled()) {
            logger.debug(msg, e);
        } else {
            logger.warning(msg + " - Debug for StackTrace");
        }
    } catch (Throwable e) {
        final String msg = "\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Unexpected error - " + e.getMessage() + "\"";
        if (logger.isDebugEnabled()) {
            logger.debug(msg, e);
        } else {
            logger.warning(msg + " - Debug for StackTrace");
        }
    } finally {
        try {
            ClientObjectFactory.serverMetaData.remove();
        } finally {
            if (null != oos) {
                try {
                    oos.flush();
                } catch (Throwable e) {
                //Ignore
                }
                try {
                    oos.close();
                } catch (Throwable e) {
                //Ignore
                }
            }
            if (null != ois) {
                try {
                    ois.close();
                } catch (Throwable e) {
                //Ignore
                }
            }
            // enforced in case of exception
            securityService.disassociate();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) EjbObjectInputStream(org.apache.openejb.client.EjbObjectInputStream) CountingInputStream(org.apache.openejb.server.stream.CountingInputStream) InputStream(java.io.InputStream) FlushableGZIPOutputStream(org.apache.openejb.client.FlushableGZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) CountingOutputStream(org.apache.openejb.server.stream.CountingOutputStream) CountingInputStream(org.apache.openejb.server.stream.CountingInputStream) ClusterResponse(org.apache.openejb.client.ClusterResponse) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Response(org.apache.openejb.client.Response) ClusterResponse(org.apache.openejb.client.ClusterResponse) CountingOutputStream(org.apache.openejb.server.stream.CountingOutputStream) RequestInfos(org.apache.openejb.server.context.RequestInfos) EjbObjectInputStream(org.apache.openejb.client.EjbObjectInputStream) ProtocolMetaData(org.apache.openejb.client.ProtocolMetaData) ServerMetaData(org.apache.openejb.client.ServerMetaData) ObjectInputStream(java.io.ObjectInputStream) EjbObjectInputStream(org.apache.openejb.client.EjbObjectInputStream) RequestType(org.apache.openejb.client.RequestType)

Example 3 with ServerMetaData

use of org.apache.openejb.client.ServerMetaData in project tomee by apache.

the class BasicClusterableRequestHandlerTest method testUpdateServerWhenRequestHashDiffersFromServerSideHash.

public void testUpdateServerWhenRequestHashDiffersFromServerSideHash() throws Exception {
    final int port = SystemInstance.get().getOptions().get("ejbd.port", 4201);
    final URI[] locations = new URI[] { new URI("ejbd://localhost:" + port) };
    ServerMetaData server = new ServerMetaData(locations);
    beanContext.setContainer(clusteredContainer);
    request.getServerHash();
    modify().returnValue(server.buildHash() + 1);
    response.setServer(null);
    modify().args(new AbstractExpression() {

        @Override
        public void describeWith(ExpressionDescriber arg0) throws IOException {
        }

        @Override
        public boolean passes(Object arg0) {
            ServerMetaData actualServer = (ServerMetaData) arg0;
            assertSame(locations, actualServer.getLocations());
            return true;
        }
    });
    clusteredContainer.getLocations(beanContext);
    modify().returnValue(locations);
    startVerification();
    requestHandler.updateServer(beanContext, request, response);
}
Also used : ExpressionDescriber(com.agical.rmock.core.describe.ExpressionDescriber) AbstractExpression(com.agical.rmock.core.match.operator.AbstractExpression) IOException(java.io.IOException) URI(java.net.URI) ServerMetaData(org.apache.openejb.client.ServerMetaData)

Example 4 with ServerMetaData

use of org.apache.openejb.client.ServerMetaData in project tomee by apache.

the class BasicClusterableRequestHandlerTest method testServerIsNotUpdatedWhenRequestHashEqualsServerSideHash.

public void testServerIsNotUpdatedWhenRequestHashEqualsServerSideHash() throws Exception {
    final int port = SystemInstance.get().getOptions().get("ejbd.port", 4201);
    URI[] locations = new URI[] { new URI("ejbd://localhost:" + port) };
    ServerMetaData server = new ServerMetaData(locations);
    beanContext.setContainer(clusteredContainer);
    request.getServerHash();
    modify().returnValue(server.buildHash());
    clusteredContainer.getLocations(beanContext);
    modify().returnValue(locations);
    startVerification();
    requestHandler.updateServer(beanContext, request, response);
}
Also used : URI(java.net.URI) ServerMetaData(org.apache.openejb.client.ServerMetaData)

Aggregations

ServerMetaData (org.apache.openejb.client.ServerMetaData)4 URI (java.net.URI)3 IOException (java.io.IOException)2 ExpressionDescriber (com.agical.rmock.core.describe.ExpressionDescriber)1 AbstractExpression (com.agical.rmock.core.match.operator.AbstractExpression)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ClusteredRPCContainer (org.apache.openejb.ClusteredRPCContainer)1 Container (org.apache.openejb.Container)1 ClusterResponse (org.apache.openejb.client.ClusterResponse)1 EjbObjectInputStream (org.apache.openejb.client.EjbObjectInputStream)1 FlushableGZIPOutputStream (org.apache.openejb.client.FlushableGZIPOutputStream)1 ProtocolMetaData (org.apache.openejb.client.ProtocolMetaData)1 RequestType (org.apache.openejb.client.RequestType)1 Response (org.apache.openejb.client.Response)1