Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, 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.test.integration.domain.management.util;
 
 
Connection utility allowing the DomainLifecycleUtil to share a remoting connection between potentially multiple controller clients, each using it's own channel.

Author(s):
Emanuel Muckenhuber
 
 class DomainTestConnection implements Closeable {
 
     private static final String DEFAULT_CHANNEL_SERVICE_TYPE = "management";
 
     private final ProtocolChannelClient client;
     private final CallbackHandler callbackHandler;
     private final ExecutorService executorService;
     private final AtomicBoolean closed = new AtomicBoolean(false);
 
     volatile Connection connection;
 
     DomainTestConnection(final ProtocolChannelClient clientfinal CallbackHandler callbackHandlerfinal ExecutorService executorService) {
         this. = client;
         this. = callbackHandler;
         this. = executorService;
     }

    
Create a controller client instance.

Returns:
the controller client
 
     protected DomainTestClient createClient() {
         return createClient();
     }

    
Create a controller client instance.

Parameters:
executorService the executor service
Returns:
the controller client
 
     protected DomainTestClient createClient(final ExecutorService executorService) {
         final ChannelStrategy strategy = new ChannelStrategy(executorService);
         final ManagementChannelHandler handler = strategy.handler;
         final DomainTestClient client = new DomainTestClient() {
             @Override
             Connection getConnection() {
                 return ;
             }
 
             @Override
             protected ManagementChannelAssociation getChannelAssociation() throws IOException {
                 return handler;
             }
 
             @Override
             Channel getChannel() {
                 return strategy.channel;
             }
 
             @Override
            public void close() throws IOException {
                strategy.close();
            }
        };
        handler.addHandlerFactory(client);
        return client;
    }

    
Try to connect to the remote controller.

Returns:
the underlying remoting connection
Throws:
java.io.IOException
    protected Connection connect() throws IOException {
        return connect();
    }

    
Try to connect to the remote host controller.

Parameters:
callbackHandler the security callback handler
Returns:
the underlying remoting connection
Throws:
java.io.IOException
    protected Connection connect(final CallbackHandler callbackHandlerthrows IOException {
        if(.get()) {
            throw new IllegalStateException();
        }
        synchronized (this) {
            if(isConnected()) {
                return ;
            }
             = .connectSync(callbackHandler, Collections.<StringString>emptyMap(), null);
            .addCloseHandler(new CloseHandler<Connection>() {
                @Override
                public void handleClose(Connection oldIOException exception) {
                    synchronized (DomainTestConnection.this) {
                        if( == old) {
                             = null;
                        }
                        DomainTestConnection.this.notifyAll();
                    }
                }
            });
            return ;
        }
    }

    
Check if we are currently connected.

Returns:
    protected boolean isConnected() {
        return  != null;
    }

    
Disconnect from the remote controller, allowing reconnection.

    protected void disconnect() throws IOException {
        final Connection connection = this.;
        if(connection != null) {
            connection.close();
        }
    }

    
Await the connection the be closed.

Parameters:
ref the referenced connection
Throws:
java.lang.InterruptedException
    protected void awaitConnectionClosed(final Connection refthrows InterruptedException {
        synchronized (this) {
            for(;;) {
                final Connection connection = this.;
                if(connection == null) {
                    return;
                } else if (connection != ref) {
                    return;
                }
                wait();
            }
        }
    }
    @Override
    public void close() throws IOException {
        synchronized (this) {
            if(.compareAndSet(falsetrue)) {
                final Connection connection = this.;
                if(connection != null) {
                    connection.close();
                }
            }
        }
    }
        volatile Channel channel;
        private final ManagementChannelHandler handler;
        ChannelStrategy(ExecutorService executorService) {
            this. = new ManagementChannelHandler(thisexecutorService);
        }
        @Override
        public Channel getChannel() throws IOException {
            if( == null) {
                synchronized (this) {
                    if( == null) {
                        final Connection connection = connect();
                         = openChannel(connection);
                        .receiveMessage(.getReceiver());
                    }
                }
            }
            return ;
        }
        Channel openChannel(final Connection connectionthrows IOException {
            final IoFuture<Channelfuture = connection.openChannel(.);
            final Channel channel = future.get();
            channel.addCloseHandler(new CloseHandler<Channel>() {
                @Override
                public void handleClose(final Channel oldfinal IOException e) {
                    synchronized (ChannelStrategy.this) {
                        if(ChannelStrategy.this. == old) {
                            ChannelStrategy.this..handleClose(olde);
                            ChannelStrategy.this. = null;
                        }
                    }
                    .handleChannelClosed(olde);
                }
            });
            return channel;
        }
        @Override
        public void close() throws IOException {
            final Channel channel = this.;
            if(channel != null) {
                channel.close();
            }
        }
    }
New to GrepCode? Check out our FAQ X