Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright (c) 2011, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.as.ejb3.remote;
 
 
 import java.util.List;

Author(s):
Carlo de Wolf
 
     private static final Logger log = Logger.getLogger(EJBRemoteConnectorService.class);
 
     // TODO: Should this be exposed via the management APIs?
     private static final String EJB_CHANNEL_NAME = "jboss.ejb";
 
     public static final ServiceName SERVICE_NAME = ..append("ejb3""connector");
 
     private final InjectedValue<EndpointendpointValue = new InjectedValue<Endpoint>();
     private final ServiceName remotingConnectorServiceName;
     private volatile Registration registration;
     private final byte serverProtocolVersion;
     private final String[] supportedMarshallingStrategies;
     private final OptionMap channelCreationOptions;
 
     public EJBRemoteConnectorService(final byte serverProtocolVersionfinal String[] supportedMarshallingStrategiesfinal ServiceName remotingConnectorServiceName) {
         this(serverProtocolVersionsupportedMarshallingStrategiesremotingConnectorServiceName.);
     }
 
    public EJBRemoteConnectorService(final byte serverProtocolVersionfinal String[] supportedMarshallingStrategiesfinal ServiceName remotingConnectorServiceName,
                                     final OptionMap channelCreationOptions) {
        this. = serverProtocolVersion;
        this. = supportedMarshallingStrategies;
        this. = remotingConnectorServiceName;
        this. = channelCreationOptions;
    }
    @Override
    public void start(StartContext contextthrows StartException {
        // get the remoting server (which allows remoting connector to connect to it) service
        final ServiceContainer serviceContainer = context.getController().getServiceContainer();
        final ServiceController streamServerServiceController = serviceContainer.getRequiredService(this.);
        final AbstractStreamServerService streamServerService = (AbstractStreamServerServicestreamServerServiceController.getService();
        // we can only work off a remoting connector which is backed by a socket binding
        if (streamServerService instanceof InjectedSocketBindingStreamServerService) {
            this. = (InjectedSocketBindingStreamServerServicestreamServerService;
        }
        // Register a EJB channel open listener
        final OpenListener channelOpenListener = new ChannelOpenListener(serviceContainer);
        try {
             = .getValue().registerService(channelOpenListenerthis.);
        } catch (ServiceRegistrationException e) {
            throw new StartException(e);
        }
        // setup a EJBClientTransactionContext backed the transaction manager on this server.
        // This will be used to propagate the transactions from this server to remote servers during EJB invocations
        final EJBClientTransactionContext ejbClientTransactionContext = EJBClientTransactionContext.create(this..getValue(), this..getValue());
        EJBClientTransactionContext.setSelector(new ConstantContextSelector<EJBClientTransactionContext>(ejbClientTransactionContext));
    }
    @Override
    public void stop(StopContext context) {
        this. = null;
        .close();
        // reset the EJBClientTransactionContext on this server
        EJBClientTransactionContext.setSelector(new ConstantContextSelector<EJBClientTransactionContext>(null));
    }
    @Override
        return this;
    }
        return ;
    }
        return this.;
    }
        return this.;
    }
        if (this. == null) {
            return null;
        }
        return this..getSocketBinding();
    }
    private void sendVersionMessage(final ChannelAssociation channelAssociationthrows IOException {
        final DataOutputStream outputStream;
        final MessageOutputStream messageOutputStream;
        try {
            messageOutputStream = channelAssociation.acquireChannelMessageOutputStream();
        } catch (Exception e) {
            throw ..failedToOpenMessageOutputStream(e);
        }
        outputStream = new DataOutputStream(messageOutputStream);
        try {
            // write the version
            outputStream.write(this.);
            // write the marshaller type count
            PackedInteger.writePackedInteger(outputStreamthis..length);
            // write the marshaller types
            for (int i = 0; i < this..lengthi++) {
                outputStream.writeUTF(this.[i]);
            }
        } finally {
            channelAssociation.releaseChannelMessageOutputStream(messageOutputStream);
            outputStream.close();
        }
    }
    private class ChannelOpenListener implements OpenListener {
        private final ServiceContainer serviceContainer;
        ChannelOpenListener(final ServiceContainer serviceContainer) {
            this. = serviceContainer;
        }
        @Override
        public void channelOpened(Channel channel) {
            final ChannelAssociation channelAssociation = new ChannelAssociation(channel);
            .tracef("Welcome %s to the " +  + " channel"channel);
            channel.addCloseHandler(new CloseHandler<Channel>() {
                @Override
                public void handleClose(Channel closedIOException exception) {
                    // do nothing
                    .tracef("channel %s closed"closed);
                }
            });
            // send the server version and supported marshalling types to the client
            try {
                EJBRemoteConnectorService.this.sendVersionMessage(channelAssociation);
            } catch (IOException e) {
                ..closingChannel(channele);
                IoUtils.safeClose(channel);
            }
            // receive messages from the client
            channel.receiveMessage(new ClientVersionMessageReceiver(this.channelAssociation));
        }
        @Override
        public void registrationTerminated() {
        }
    }
    private class ClientVersionMessageReceiver implements Channel.Receiver {
        private final ServiceContainer serviceContainer;
        private final ChannelAssociation channelAssociation;
        ClientVersionMessageReceiver(final ServiceContainer serviceContainerfinal ChannelAssociation channelAssociation) {
            this. = serviceContainer;
            this. = channelAssociation;
        }
        @Override
        public void handleError(Channel channelIOException error) {
            ..closingChannel(channelerror);
            try {
                channel.close();
            } catch (IOException ioe) {
                // ignore
            }
        }
        @Override
        public void handleEnd(Channel channel) {
            ..closingChannelOnChannelEnd(channel);
            try {
                channel.close();
            } catch (IOException ioe) {
                // ignore
            }
        }
        @Override
        public void handleMessage(Channel channelMessageInputStream messageInputStream) {
            final DataInputStream dataInputStream = new DataInputStream(messageInputStream);
            try {
                final byte version = dataInputStream.readByte();
                final String clientMarshallingStrategy = dataInputStream.readUTF();
                .debug("Client with protocol version " + version + " and marshalling strategy " + clientMarshallingStrategy +
                        " trying to communicate on " + channel);
                if (!EJBRemoteConnectorService.this.isSupportedMarshallingStrategy(clientMarshallingStrategy)) {
                    ..unsupportedClientMarshallingStrategy(clientMarshallingStrategychannel);
                    channel.close();
                    return;
                }
                switch (version) {
                    case 0x01:
                        final MarshallerFactory marshallerFactory = EJBRemoteConnectorService.this.getMarshallerFactory(clientMarshallingStrategy);
                        // enroll VersionOneProtocolChannelReceiver for handling subsequent messages on this channel
                        final DeploymentRepository deploymentRepository = EJBRemoteConnectorService.this..getValue();
                        final RegistryCollector<StringList<ClientMapping>> clientMappingRegistryCollector = EJBRemoteConnectorService.this..getValue();
                        final VersionOneProtocolChannelReceiver receiver = new VersionOneProtocolChannelReceiver(this.deploymentRepository,
                                EJBRemoteConnectorService.this..getValue(), clientMappingRegistryCollector,
                                marshallerFactory.getValue());
                        // trigger the receiving
                        receiver.startReceiving();
                        break;
                    default:
                        throw ..ejbRemoteServiceCannotHandleClientVersion(version);
                }
            } catch (IOException e) {
                // log it
                .errorf(e"Exception on channel %s from message %s"channelmessageInputStream);
                IoUtils.safeClose(channel);
            } finally {
                IoUtils.safeClose(messageInputStream);
            }
        }
    }
        return ;
    }
        return this.;
    }
    }
        return this.;
    }
        return this.;
    }
    private boolean isSupportedMarshallingStrategy(final String strategy) {
        return Arrays.asList(this.).contains(strategy);
    }
    private MarshallerFactory getMarshallerFactory(final String marshallerStrategy) {
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory(marshallerStrategy);
        if (marshallerFactory == null) {
            throw ..failedToFindMarshallerFactoryForStrategy(marshallerStrategy);
        }
        return marshallerFactory;
    }
New to GrepCode? Check out our FAQ X