Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008, 2010 VMware Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: VMware Inc. - initial contribution /
 
 
 package org.eclipse.virgo.nano.core.internal.blueprint;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.osgi.service.event.Event;
 import  org.osgi.service.event.EventConstants;
 import  org.osgi.service.event.EventHandler;
 
ApplicationContextDependencyMonitor is a class that tracks the satisfaction of service dependencies needed during the creation of application contexts and issues log messages for delayed service dependencies.

Concurrent Semantics
This class is thread safe.

 
 public final class ApplicationContextDependencyMonitor implements EventHandler {
 
     private static final String TOPIC_BLUEPRINT_EVENTS = "org/osgi/service/blueprint/container/";
 
     private static final String EVENT_WAITING =  + "WAITING";
 
     private static final String EVENT_GRACE_PERIOD =  + "GRACE_PERIOD";
 
     private static final String EVENT_FAILURE =  + "FAILURE";
 
     private static final String EVENT_CREATED =  + "CREATED";
 
     private static final int MAXIMUM_WARNING_INTERVAL = 60 * 1000;
 
     private static final int WARNING_INTERVAL_INCREASE_RATE_PERCENT = 200;
 
     private static final int INITIAL_WARNING_INTERVAL = 5 * 1000;
 
     private static final int SLOW_WARNING_INTERVAL = 5 * 60 * 1000;
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     private final EventLogger eventLogger;
 
 
     private final Map<BundleMap<ServiceDependencyTicker>> tickers = new HashMap<BundleMap<ServiceDependencyTicker>>();
 
     private final Object monitor = new Object();

    
Construct a ApplicationContextDependencyMonitor which uses the given java.util.concurrent.ScheduledExecutorService to schedule its warning messages.

Parameters:
scheduledExecutorService the java.util.concurrent.ScheduledExecutorService for scheduling warning messages
eventLogger
 
     public ApplicationContextDependencyMonitor(ScheduledExecutorService scheduledExecutorServiceEventLogger eventLogger) {
         this. = scheduledExecutorService;
         this. = eventLogger;
     }

    
 
     public void handleEvent(Event event) {
         synchronized (this.) {
             Bundle bundle = (Bundleevent.getProperty(EventConstants.BUNDLE);
 
             if (.equals(event.getTopic())) {
                 List<ServiceDependencyserviceDependencies = createServiceDependencies(event);
                 if (serviceDependencies != null) {
                     for (ServiceDependency serviceDependency : serviceDependencies) {
                         addServiceDependencyTicker(serviceDependencybundle);
                     }
                 }
             } else if (.equals(event.getTopic())) {
                List<ServiceDependencyremainingUnsatisfiedDependencies = createServiceDependencies(event);
                if (remainingUnsatisfiedDependencies != null) {
                    changeInUnsatisfiedDependencies(remainingUnsatisfiedDependenciesbundle);
                }
            } else if (.equals(event.getTopic())) {
                String[] dependenciesArray = (String[]) event.getProperty("dependencies");
                if (dependenciesArray != null) {
                    List<ServiceDependencyserviceDependencies = createServiceDependencies(event);
                    if (serviceDependencies != null) {
                        serviceDependenciesTimedOut(serviceDependenciesbundle);
                    }
                } else {
                    containerCreationFailed(bundle);
                }
            } else if (.equals(event.getTopic())) {
                containerCreated(bundle);
            }
        }
    }
    private void serviceDependenciesTimedOut(List<ServiceDependencytimedOutDependenciesBundle bundle) {
        Map<ServiceDependencyTickerbundlesTickers = this..get(bundle);
        if (bundlesTickers != null) {
            for (ServiceDependency timedOutDependency : timedOutDependencies) {
                Ticker ticker = bundlesTickers.remove(timedOutDependency);
                if (ticker != null) {
                    dependencyTimedOut(timedOutDependencytickerbundle);
                }
            }
        }
    }
    private void containerCreationFailed(Bundle bundle) {
        Map<ServiceDependencyTickertickers = this..remove(bundle);
        if (tickers != null) {
            for (Entry<ServiceDependencyTickerticker : tickers.entrySet()) {
                ticker.getValue().cancel();
            }
        }
    }
    private void containerCreated(Bundle bundle) {
        Map<ServiceDependencyTickerbundlesTickers = this..remove(bundle);
        if (bundlesTickers != null) {
            for (Entry<ServiceDependencyTickerentry : bundlesTickers.entrySet()) {
                dependencySatisfied(entry.getKey(), entry.getValue(), bundle);
            }
        }
    }
    private void changeInUnsatisfiedDependencies(List<ServiceDependencyremainingUnsatisfiedDependenciesBundle bundle) {
        Map<ServiceDependencyTickertickers = this..get(bundle);
        if (tickers != null) {
            Iterator<Entry<ServiceDependencyTicker>> entries = tickers.entrySet().iterator();
            while (entries.hasNext()) {
                Entry<ServiceDependencyTickerentry = entries.next();
                if (!remainingUnsatisfiedDependencies.contains(entry.getKey())) {
                    dependencySatisfied(entry.getKey(), entry.getValue(), bundle);
                    entries.remove();
                }
            }
        }
    }
    private void dependencySatisfied(ServiceDependency serviceDependencyTicker tickerBundle bundle) {
        .info("Service dependency '{}' has been satisfied"serviceDependency);
        handleRemovedTicker(tickerserviceDependencybundletrue);
    }
    private void dependencyTimedOut(ServiceDependency serviceDependencyTicker tickerBundle bundle) {
        .info("Service dependency '{}' has timed out"serviceDependency);
        handleRemovedTicker(tickerserviceDependencybundlefalse);
    }
    private void handleRemovedTicker(Ticker tickerServiceDependency serviceDependencyBundle bundleboolean satisfied) {
        boolean hasTicked = ticker.cancel();
        if (hasTicked) {
            if (satisfied) {
                this..log(.serviceDependency.getBeanName(),
                    bundle.getSymbolicName(), bundle.getVersion(), serviceDependency.getFilter());
            } else {
                this..log(.serviceDependency.getBeanName(),
                    bundle.getSymbolicName(), bundle.getVersion(), serviceDependency.getFilter());
            }
        }
    }

    
Add a service dependency ticker for the given application context, given associated bundle, and given service dependency.

Parameters:
applicationContext the partially constructed application context which needs the service dependency
serviceDependency the service dependency
bundle the org.osgi.framework.Bundle associated with the given application context
    private void addServiceDependencyTicker(final ServiceDependency serviceDependencyfinal Bundle bundle) {
        Map<ServiceDependencyTickerserviceDependencyTickers = getServiceDependencyTickers(bundle);
        if (serviceDependencyTickers.containsKey(serviceDependency)) {
            .warn("Service dependency '{}' already being waited upon"serviceDependency);
        } else {
            // Services which are flagged as likely to be slow to be published are given a longer initial warning
            // interval.
            boolean slowService = serviceDependency.getFilter().contains("(org.eclipse.virgo.server.slowservice=true)");
            serviceDependencyTickers.put(serviceDependency, StandardTicker.createExponentialTicker(slowService ? 
                new Callable<Void>() {
                    public Void call() throws Exception {
                        synchronized (ApplicationContextDependencyMonitor.this.) {
                            if (bundle.getState() == .) {
                                ApplicationContextDependencyMonitor.this.containerCreationFailed(bundle);
                            } else {
                                .log(.serviceDependency.getBeanName(),
                                    bundle.getSymbolicName(), bundle.getVersion(), serviceDependency.getFilter());
                            }
                            return null;
                        }
                    }
                }, this.));
        }
    }

    
Get the possibly empty map of service dependency tickers for the given Bundle.

Parameters:
bundle the Bundle whose application context's service dependencies are required
Returns:
a map of service dependency tickers
        Map<ServiceDependencyTickertickers = this..get(bundle);
        if (tickers == null) {
            tickers = new HashMap<ServiceDependencyTicker>();
            this..put(bundletickers);
        }
        return tickers;
    }
    public void stop() {
        this..shutdown();
    }
    private List<ServiceDependencycreateServiceDependencies(Event event) {
        String[] filters = (String[]) event.getProperty("dependencies");
        String[] beanNames = (String[]) event.getProperty("bean.name");
        List<ServiceDependencyserviceDependencies = new ArrayList<ServiceDependency>();
        if (filters != null && beanNames != null) {
            for (int i = 0; i < filters.lengthi++) {
                serviceDependencies.add(new ServiceDependency(filters[i], beanNames[i]));
            }
            return serviceDependencies;
        }
        /*
         * Return null when filters is non-null and beanNames is null. Blueprint events sometimes lack this information,
         * but a corresponding event including bean names is posted by
         * BlueprintEventPostingOsgiBundleApplicationContextListener on receipt of the underlying Spring DM event. A
         * return value of null indicates that the caller should ignore this event.
         */
        return filters == null ? serviceDependencies : null;
    }
    private static final class ServiceDependency {
        private final String filter;
        private final String beanName;
        private ServiceDependency(String filterString beanName) {
            this. = filter;
            this. = beanName;
        }
        public String getFilter() {
            return ;
        }
        public String getBeanName() {
            return ;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + .hashCode();
            result = prime * result + .hashCode();
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            ServiceDependency other = (ServiceDependencyobj;
            if (!.equals(other.beanName))
                return false;
            if (!.equals(other.filter))
                return false;
            return true;
        }
        public String toString() {
            return this. + " " + this.;
        }
    }
New to GrepCode? Check out our FAQ X