@Override
    public Association addServerAssociation(String peerAddress, int peerPort, String serverName, String assocName,
            IpChannelType ipChannelType) throws Exception {
        if (!this.started) {
            throw new Exception(String.format("Management=%s not started", this.name));
        }

        if (peerAddress == null) {
            throw new Exception("Peer address cannot be null");
        }

        if (peerPort < 0) {
            throw new Exception("Peer port cannot be less than 0");
        }

        if (serverName == null) {
            throw new Exception("Server name cannot be null");
        }

        if (assocName == null) {
            throw new Exception("Association name cannot be null");
        }

        synchronized (this) {
            if (this.associations.get(assocName) != null) {
                throw new Exception(String.format("Already has association=%s", assocName));
            }

            Server server = null;

            for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
                Server serverTemp = n.getValue();
                if (serverTemp.getName().equals(serverName)) {
                    server = serverTemp;
                }
            }

            if (server == null) {
                throw new Exception(String.format("No Server found for name=%s", serverName));
            }

            for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n
                    .getNext()) != end;) {
                Association associationTemp = n.getValue();
                
                if (associationTemp.getServerName().equals(server.getName()) && peerAddress.equals(associationTemp.getPeerAddress()) && associationTemp.getPeerPort() == peerPort) {
                    throw new Exception(String.format("Already has association=%s with same peer address=%s and port=%d",
                            associationTemp.getName(), peerAddress, peerPort));
                }
            }

            if (server.getIpChannelType() != ipChannelType)
                throw new Exception(String.format("Server and Accociation has different IP channel type"));

            NettyAssociationImpl association = new NettyAssociationImpl(peerAddress, peerPort, serverName, assocName,
                    ipChannelType);
            association.setManagement(this);

            NettyAssociationMap<String, Association> newAssociations = new NettyAssociationMap<String, Association>();
            newAssociations.putAll(this.associations);
            newAssociations.put(assocName, association);
            this.associations = newAssociations;
            // this.associations.put(assocName, association);

            FastList<String> newAssociations2 = new FastList<String>();
            newAssociations2.addAll(((NettyServerImpl) server).associations);
            newAssociations2.add(assocName);
            ((NettyServerImpl) server).associations = newAssociations2;
            // ((ServerImpl) server).associations.add(assocName);

            this.store();

            for (ManagementEventListener lstr : managementEventListeners) {
                try {
                    lstr.onAssociationAdded(association);
                } catch (Throwable ee) {
                    logger.error("Exception while invoking onAssociationAdded", ee);
                }
            }

            if (logger.isInfoEnabled()) {
                logger.info(String.format("Added Associoation=%s of type=%s", association.getName(),
                        association.getAssociationType()));
            }

            return association;
        }
    }
@ Override public Association addServerAssociation ( String peerAddress , int peerPort , String serverName , String assocName , IpChannelType ipChannelType ) throws Exception { if ( ! this . started ) { throw new Exception ( String . format ( "Management=%s not started" , this . name ) ) ; } if ( peerAddress == null ) { throw new Exception ( "Peer address cannot be null" ) ; } if ( peerPort < 0 ) { throw new Exception ( "Peer port cannot be less than 0" ) ; } if ( serverName == null ) { throw new Exception ( "Server name cannot be null" ) ; } if ( assocName == null ) { throw new Exception ( "Association name cannot be null" ) ; } synchronized ( this ) { if ( this . associations . get ( assocName ) != null ) { throw new Exception ( String . format ( "Already has association=%s" , assocName ) ) ; } Server server = null ; for ( FastList . Node < Server > n = this . servers . head ( ) , end = this . servers . tail ( ) ; ( n = n . getNext ( ) ) != end ; ) { Server serverTemp = n . getValue ( ) ; if ( serverTemp . getName ( ) . equals ( serverName ) ) { server = serverTemp ; } } if ( server == null ) { throw new Exception ( String . format ( "No Server found for name=%s" , serverName ) ) ; } for ( FastMap . Entry < String , Association > n = this . associations . head ( ) , end = this . associations . tail ( ) ; ( n = n . getNext ( ) ) != end ; ) { Association associationTemp = n . getValue ( ) ; if ( associationTemp . getServerName ( ) . equals ( server . getName ( ) ) && peerAddress . equals ( associationTemp . getPeerAddress ( ) ) && associationTemp . getPeerPort ( ) == peerPort ) { throw new Exception ( String . format ( "Already has association=%s with same peer address=%s and port=%d" , associationTemp . getName ( ) , peerAddress , peerPort ) ) ; } } if ( server . getIpChannelType ( ) != ipChannelType ) throw new Exception ( String . format ( "Server and Accociation has different IP channel type" ) ) ; NettyAssociationImpl association = new NettyAssociationImpl ( peerAddress , peerPort , serverName , assocName , ipChannelType ) ; association . setManagement ( this ) ; NettyAssociationMap < String , Association > newAssociations = new NettyAssociationMap < String , Association > ( ) ; newAssociations . putAll ( this . associations ) ; newAssociations . put ( assocName , association ) ; this . associations = newAssociations ; FastList < String > newAssociations2 = new FastList < String > ( ) ; newAssociations2 . addAll ( ( ( NettyServerImpl ) server ) . associations ) ; newAssociations2 . add ( assocName ) ; ( ( NettyServerImpl ) server ) . associations = newAssociations2 ; this . store ( ) ; for ( ManagementEventListener lstr : managementEventListeners ) { try { lstr . onAssociationAdded ( association ) ; } catch ( Throwable ee ) { logger . error ( "Exception while invoking onAssociationAdded" , ee ) ; } } if ( logger . isInfoEnabled ( ) ) { logger . info ( String . format ( "Added Associoation=%s of type=%s" , association . getName ( ) , association . getAssociationType ( ) ) ) ; } return association ; } }
/*
(non-Javadoc)

@see org.mobicents.protocols.api.Management#addServerAssociation(java.lang .String, int, java.lang.String,
java.lang.String, org.mobicents.protocols.api.IpChannelType)