Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.servicemix.nmr.management;
 
 import java.util.Map;
 
 import  org.osgi.framework.BundleContext;
 import  org.osgi.framework.ServiceReference;
 import  org.osgi.util.tracker.ServiceTracker;
 
 public class ManagementEndpointRegistry implements ExchangeListener {
 
     private final Logger logger = LoggerFactory.getLogger(ManagementEndpointRegistry.class);
 
     private BundleContext bundleContext;
     private final Map<StringInternalEndpointinternalEndpoints;
     private final Map<StringManagedEndpointendpoints;
     private ServiceTracker managementStrategyTracker;
     private ServiceTracker endpointTracker;
 
     public ManagementEndpointRegistry() {
          = new ConcurrentHashMap<StringManagedEndpoint>();
     }
 
     public void setBundleContext(BundleContext bundleContext) {
         this. = bundleContext;
     }
 
     public void init() {
         if ( == null) {
             throw new IllegalArgumentException("bundleContext must be set");
         }
          = new ServiceTracker(ManagementStrategy.class.getName(), null) {
             @Override
             public Object addingService(ServiceReference reference) {
                 ManagementStrategy newMs = (ManagementStrategysuper.addingService(reference);
                 if (getService() == null) {
                     bindManagementStrategy(newMs);
                 }
                 return newMs;
             }
 
             @Override
             public void removedService(ServiceReference referenceObject service) {
                 ManagementStrategy newMs = (ManagementStrategygetService();
                 bindManagementStrategy(newMs);
                 super.removedService(referenceservice);
             }
         };
         .open();
          = new ServiceTracker(InternalEndpoint.class.getName(), null) {
             @Override
             public Object addingService(ServiceReference reference) {
                 InternalEndpoint endpoint = (InternalEndpointsuper.addingService(reference);
                 register(endpoint);
                 return endpoint;
             }
 
             @Override
             public void removedService(ServiceReference referenceObject service) {
                 InternalEndpoint endpoint = (InternalEndpointservice;
                 unregister(endpoint);
                 super.removedService(referenceservice);
             }
         };
         .open();
     }
 
     public void destroy() {
         unregisterAll();
         .close();
        .close();
    }
    public void bindManagementStrategy(ManagementStrategy ms) {
        .debug("Using new management strategy: {}"ms);
        unregisterAll();
         = ms;
        registerAll();
    }
    protected void registerAll() {
        if ( != null) {
            for (String id : .keySet()) {
                registerEndpoint(.get(id));
            }
        }
    }
    protected void unregisterAll() {
        if ( != null) {
            for (String id : .keySet()) {
                unregisterEndpoint(.get(id));
            }
        }
    }
    protected void registerEndpoint(InternalEndpoint iep) {
        if ( != null) {
            try {
                .info("Registering endpoint: {} with properties {}"iepiep.getMetaData());
                ManagedEndpoint ep = new ManagedEndpoint(iep);
                .put(iep.getId(), ep);
                .manageObject(ep);
            } catch (Exception e) {
                .warn("Unable to register managed endpoint."e);
            }
        }
    }
    private void unregisterEndpoint(InternalEndpoint iep) {
        if ( != null) {
            try {
                .info("Unregistering endpoint: {} with properties {}"iepiep.getMetaData());
                ManagedEndpoint ep = .remove(iep.getId());
                .unmanageObject(ep);
            } catch (Exception e) {
                .warn("Unable to unregister managed endpoint."e);
            }
        }
    }
    public void register(InternalEndpoint endpoint) {
        .put(endpoint.getId(), endpoint);
        registerEndpoint(endpoint);
    }
    public void unregister(InternalEndpoint endpoint) {
        .remove(endpoint.getId());
        unregisterEndpoint(endpoint);
    }
    public void exchangeSent(Exchange exchange) {
        try {
            .trace("Sending exchange: {}"exchange);
            if (exchange.getStatus() == . &&
                    exchange.getRole() == . &&
                    exchange.getOut(false) == null &&
                    exchange.getFault(false) == null &&
                    exchange instanceof InternalExchange) {
                String id = ((InternalExchangeexchange).getSource().getId();
                .trace("Source endpoint: {} (known endpoints: {})"id);
                ManagedEndpoint me = .get(id);
                if (me == null) {
                    .trace("No managed endpoint registered with id: {}"id);
                  
                } else {
                    me.incrementOutbound();
                }
            }
        } catch (Throwable t) {
            .warn("Caught exception while processing exchange."t);
        }
    }
    public void exchangeDelivered(Exchange exchange) {
        try {
            .trace("Receiving exchange: {}"exchange);
            if (exchange.getStatus() == . &&
                    exchange.getRole() == . &&
                    exchange.getOut(false) == null &&
                    exchange.getFault(false) == null &&
                    exchange instanceof InternalExchange) {
                String id = ((InternalExchangeexchange).getDestination().getId();
                .trace("Dest endpoint: {} (known endpoints: {})"id);
                ManagedEndpoint me = .get(id);
                if (me == null) {
                    .warn("No managed endpoint registered with id: {}"id);
                } else {
                    me.incrementInbound();
                }
            }
        } catch (Throwable t) {
            .warn("Caught exception while processing exchange."t);
        }
    }
    public void exchangeFailed(Exchange exchange) {
        ExchangeFailedEvent event = new ExchangeFailedEvent(exchange);
        try {
            .notify(event);
        } catch (Exception ex) {
            .warn("ExchangeFailedEvent notification failed"ex);
        }
    }
New to GrepCode? Check out our FAQ X