Home » geronimo-2.2-source-release » org.apache.geronimo.openejb.cluster.infra » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.geronimo.openejb.cluster.infra;
   21   
   22   import java.net.URI;
   23   import java.net.URISyntaxException;
   24   import java.util.Collection;
   25   import java.util.HashSet;
   26   import java.util.Set;
   27   
   28   import org.slf4j.Logger;
   29   import org.slf4j.LoggerFactory;
   30   import org.apache.geronimo.clustering.Node;
   31   import org.apache.geronimo.clustering.SessionManagerListener;
   32   import org.apache.geronimo.clustering.wadi.RemoteNode;
   33   import org.apache.geronimo.clustering.wadi.WADISessionManager;
   34   import org.apache.geronimo.gbean.GBeanInfo;
   35   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   36   import org.apache.geronimo.gbean.GBeanLifecycle;
   37   import org.apache.geronimo.gbean.ReferenceCollection;
   38   import org.apache.geronimo.gbean.ReferenceCollectionEvent;
   39   import org.apache.geronimo.gbean.ReferenceCollectionListener;
   40   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   41   import org.apache.geronimo.management.geronimo.NetworkConnector;
   42   import org.apache.geronimo.openejb.EjbDeploymentIdAccessor;
   43   import org.codehaus.wadi.group.Peer;
   44   import org.codehaus.wadi.servicespace.InvocationMetaData;
   45   import org.codehaus.wadi.servicespace.ServiceNotAvailableException;
   46   import org.codehaus.wadi.servicespace.ServiceNotFoundException;
   47   import org.codehaus.wadi.servicespace.ServiceProxy;
   48   import org.codehaus.wadi.servicespace.ServiceProxyFactory;
   49   import org.codehaus.wadi.servicespace.ServiceRegistry;
   50   import org.codehaus.wadi.servicespace.ServiceSpace;
   51   
   52   
   53   /**
   54    *
   55    * @version $Rev:$ $Date:$
   56    */
   57   public class NetworkConnectorMonitor implements GBeanLifecycle {
   58       private static final Logger log = LoggerFactory.getLogger(NetworkConnectorMonitor.class);
   59       
   60       private final Collection<NetworkConnector> connectors;
   61       private final Collection<EjbDeploymentIdAccessor> idAccessors;
   62       private final WADISessionManager sessionManager;
   63       private final String nodeName;
   64       private final Set<URI> locations;
   65       private NetworkConnectorTracker localTracker;
   66       private ServiceProxyFactory trackerProxyFactory;
   67       
   68       public NetworkConnectorMonitor(Collection<NetworkConnector> connectors,
   69           Collection<EjbDeploymentIdAccessor> idAccessors,
   70           WADISessionManager sessionManager) {
   71           if (null == connectors && !(connectors instanceof ReferenceCollection)) {
   72               throw new IllegalArgumentException("connectors must be a [" + ReferenceCollection.class + "]");
   73           } else if (null == idAccessors && !(idAccessors instanceof ReferenceCollection)) {
   74               throw new IllegalArgumentException("idAccessors must be a [" + ReferenceCollection.class + "]");
   75           } else if (null == sessionManager) {
   76               throw new IllegalArgumentException("sessionManager is required");
   77           }
   78           this.connectors = connectors;
   79           this.idAccessors = idAccessors;
   80           this.sessionManager = sessionManager;
   81           
   82           nodeName = sessionManager.getCluster().getLocalNode().getName();
   83           
   84           locations = new HashSet<URI>();
   85   
   86           registerListenerForMembershipUpdates(sessionManager);
   87           registerListenerForConnectorUpdates((ReferenceCollection) connectors);
   88           registerListenerForDeploymentUpdates((ReferenceCollection) idAccessors);
   89       }
   90   
   91       protected void registerListenerForMembershipUpdates(WADISessionManager sessionManager) {
   92           sessionManager.registerSessionManagerListener(new SessionManagerListener() {
   93               public void onJoin(Node joiningNode, Set<Node> newHostingNodes) {
   94                   Set<URI> clonedLocations;
   95                   synchronized (locations) {
   96                       clonedLocations = new HashSet<URI>(locations);
   97                   }
   98                   for (EjbDeploymentIdAccessor deploymentIdAccessor : NetworkConnectorMonitor.this.idAccessors) {
   99                       String deploymentId = deploymentIdAccessor.getDeploymentId();
  100                       ServiceProxy proxy = trackerProxyFactory.getProxy();
  101                       RemoteNode remoteNode = (RemoteNode) joiningNode;
  102                       proxy.getInvocationMetaData().setTargets(new Peer[] {remoteNode.getPeer()});
  103                       NetworkConnectorTracker tracker = (NetworkConnectorTracker)  proxy;
  104                       tracker.registerNetworkConnectorLocations(deploymentId, nodeName, clonedLocations);
  105                   }
  106               }
  107   
  108               public void onLeave(Node leavingNode, Set<Node> newHostingNodes) {
  109                   String leavingNodeName = leavingNode.getName();
  110                   localTracker.unregisterNetworkConnectorLocations(leavingNodeName);
  111               }
  112           });
  113       }
  114   
  115       protected void registerListenerForDeploymentUpdates(ReferenceCollection deploymentIdAccessors) {
  116           deploymentIdAccessors.addReferenceCollectionListener(new ReferenceCollectionListener() {
  117               public void memberAdded(ReferenceCollectionEvent event) {
  118                   EjbDeploymentIdAccessor idAccessor = (EjbDeploymentIdAccessor) event.getMember();
  119                   String deploymentId = idAccessor.getDeploymentId();
  120                   updateTracker(deploymentId);
  121               }
  122   
  123               public void memberRemoved(ReferenceCollectionEvent event) {
  124                   EjbDeploymentIdAccessor idAccessor = (EjbDeploymentIdAccessor) event.getMember();
  125                   String deploymentId = idAccessor.getDeploymentId();
  126                   removeTracker(deploymentId);
  127               }
  128           });
  129       }
  130   
  131       protected void registerListenerForConnectorUpdates(ReferenceCollection connectors) {
  132           connectors.addReferenceCollectionListener(new ReferenceCollectionListener() {
  133               public void memberAdded(ReferenceCollectionEvent event) {
  134                   URI uri = buildURI(event);
  135                   synchronized (locations) {
  136                       locations.add(uri);
  137                   }
  138                   Set<URI> uris = new HashSet<URI>();
  139                   uris.add(uri);
  140                   for (EjbDeploymentIdAccessor deploymentIdAccessor : idAccessors) {
  141                       String deploymentId = deploymentIdAccessor.getDeploymentId();
  142                       updateTracker(deploymentId, uris);
  143                   }
  144               }
  145   
  146               public void memberRemoved(ReferenceCollectionEvent event) {
  147                   URI uri = buildURI(event);
  148                   synchronized (locations) {
  149                       locations.remove(uri);
  150                   }
  151                   Set<URI> uris = new HashSet<URI>();
  152                   uris.add(uri);
  153                   for (EjbDeploymentIdAccessor deploymentIdAccessor : idAccessors) {
  154                       String deploymentId = deploymentIdAccessor.getDeploymentId();
  155                       removeTracker(deploymentId, uris);
  156                   }
  157               }
  158   
  159               protected URI buildURI(ReferenceCollectionEvent event) {
  160                   NetworkConnector connector = (NetworkConnector) event.getMember();
  161                   return NetworkConnectorMonitor.this.buildURI(connector);
  162               }
  163           });
  164       }
  165   
  166       public void doFail() {
  167           try {
  168               doStop();
  169           } catch (Exception e) {
  170               log.error("See nested", e);
  171           }
  172       }
  173   
  174       public void doStart() throws Exception {
  175           trackerProxyFactory = newNetworkConnectorTrackerProxy();
  176           
  177           localTracker = locateLocalNetworkConnectorTracker();
  178   
  179           for (NetworkConnector connector : connectors) {
  180               URI uri = buildURI(connector);
  181               synchronized (locations) {
  182                   locations.add(uri);
  183               }
  184           }
  185   
  186           for (EjbDeploymentIdAccessor deploymentIdAccessor : idAccessors) {
  187               String deploymentId = deploymentIdAccessor.getDeploymentId();
  188               updateTracker(deploymentId);
  189           }
  190       }
  191   
  192       public void doStop() throws Exception {
  193           synchronized (locations) {
  194               locations.clear();
  195           }
  196       }
  197   
  198       protected NetworkConnectorTracker locateLocalNetworkConnectorTracker() {
  199           ServiceSpace serviceSpace = sessionManager.getServiceSpace();
  200           ServiceRegistry serviceRegistry = serviceSpace.getServiceRegistry();
  201           try {
  202               return (NetworkConnectorTracker) serviceRegistry.getStartedService(NetworkConnectorTracker.NAME);
  203           } catch (Exception e) {
  204               throw new IllegalStateException(e);
  205           }
  206       }
  207       
  208       protected URI buildURI(NetworkConnector connector) {
  209           String uriAsString = connector.getProtocol() + "://" + connector.getHost() + ":" + connector.getPort();
  210           URI uri;
  211           try {
  212               uri = new URI(uriAsString);
  213           } catch (URISyntaxException e) {
  214               throw new IllegalStateException("URI [" + uriAsString + "] is invalid", e);
  215           }
  216           return uri;
  217       }
  218   
  219       protected ServiceProxyFactory newNetworkConnectorTrackerProxy() {
  220           ServiceSpace serviceSpace = sessionManager.getServiceSpace();
  221           ServiceProxyFactory proxyFactory = serviceSpace.getServiceProxyFactory(NetworkConnectorTracker.NAME,
  222               new Class[] { NetworkConnectorTracker.class });
  223           InvocationMetaData invocationMetaData = proxyFactory.getInvocationMetaData();
  224           invocationMetaData.setOneWay(true);
  225           return proxyFactory;
  226       }
  227   
  228       protected void updateTrackerForAllDeploymentIds() {
  229           for (EjbDeploymentIdAccessor deploymentIdAccessor : idAccessors) {
  230               String deploymentId = deploymentIdAccessor.getDeploymentId();
  231               updateTracker(deploymentId);
  232           }
  233       }
  234   
  235       protected void updateTracker(Object deploymentId) {
  236           Set<URI> clonedLocations;
  237           synchronized (locations) {
  238               clonedLocations = new HashSet<URI>(locations);
  239           }
  240           updateTracker(deploymentId, clonedLocations);
  241       }
  242       
  243       protected void updateTracker(Object deploymentId, Set<URI> locationsToRegister) {
  244           NetworkConnectorTracker tracker = (NetworkConnectorTracker) trackerProxyFactory.getProxy();
  245           tracker.registerNetworkConnectorLocations(deploymentId, nodeName, locationsToRegister);
  246       }
  247       
  248       protected void removeTracker(Object deploymentId) {
  249           Set<URI> clonedLocations;
  250           synchronized (locations) {
  251               clonedLocations = new HashSet<URI>(locations);
  252           }
  253           removeTracker(deploymentId, clonedLocations);
  254       }
  255   
  256       protected void removeTracker(Object deploymentId, Set<URI> locationsToUnregister) {
  257           NetworkConnectorTracker tracker = (NetworkConnectorTracker) trackerProxyFactory.getProxy();
  258           tracker.unregisterNetworkConnectorLocations(deploymentId, nodeName, locationsToUnregister);
  259       }
  260       
  261       public static final GBeanInfo GBEAN_INFO;
  262   
  263       public static final String GBEAN_REF_NETWORK_CONNECTORS = "NetworkConnectors";
  264       public static final String GBEAN_REF_EJB_DEP_ID_ACCESSOR = "EjbDeploymentIdAccessor";
  265       public static final String GBEAN_REF_WADI_SESSION_MANAGER = "WADISessionManager";
  266   
  267       static {
  268           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("NetworkConnectorMonitor",
  269               NetworkConnectorMonitor.class,
  270               GBeanInfoBuilder.DEFAULT_J2EE_TYPE);
  271   
  272           infoBuilder.addReference(GBEAN_REF_NETWORK_CONNECTORS, NetworkConnector.class, GBeanInfoBuilder.DEFAULT_J2EE_TYPE);
  273           infoBuilder.addReference(GBEAN_REF_EJB_DEP_ID_ACCESSOR, EjbDeploymentIdAccessor.class, GBeanInfoBuilder.DEFAULT_J2EE_TYPE);
  274           infoBuilder.addReference(GBEAN_REF_WADI_SESSION_MANAGER, WADISessionManager.class, GBeanInfoBuilder.DEFAULT_J2EE_TYPE);
  275   
  276           infoBuilder.setConstructor(new String[] {GBEAN_REF_NETWORK_CONNECTORS,
  277               GBEAN_REF_EJB_DEP_ID_ACCESSOR,
  278               GBEAN_REF_WADI_SESSION_MANAGER});
  279   
  280           GBEAN_INFO = infoBuilder.getBeanInfo();
  281       }
  282   
  283       public static GBeanInfo getGBeanInfo() {
  284           return GBEAN_INFO;
  285       }
  286   
  287   }

Home » geronimo-2.2-source-release » org.apache.geronimo.openejb.cluster.infra » [javadoc | source]