public AllActiveEnginesChain(BundleContext context, String name) { if(context == null){ throw new IllegalArgumentException("The parsed BundleContext MUST NOT be NULL!"); } if(name == null || name.isEmpty()){ throw new IllegalArgumentException("The parsed Chain name MUST NOT be NULL!"); } this.context = context; this.name = name; Set<String> trackAll = Collections.emptySet(); this.tracker = new EnginesTracker(context, trackAll, //empty set to track all engines this); this.tracker.open(); }
/** * This internally used an {@link EnginesTracker} to track currently active * {@link EnhancementEngine}. This will {@link EnginesTracker#close() close} * this tracker and also clear other member variables */ public void close(){ synchronized (lock) { this.executionPlan = null; this.engineNames = null; } this.tracker.close(); this.tracker = null; this.name = null; } @Override
/** * Creates a new {@link EnginesTracker} for the parsed {@link BundleContext} * and engine names. * Examples: * <code><pre> * //Track all active engines with a customiser * new EnginesTracker(context,null,customiser); * * //Track all engines referenced by a Chain and use the customiser * //to react on changes * new EnginesTracker(context,chain.getEngineNames(),customiser); * </pre></code> * @param context the bundle context used to track engines * @param engineNames the names of the engines to track. Parse <code>null</code> * or an {@link Collections#emptySet()} to track all engines * @param customizer the {@link ServiceTrackerCustomizer} used with this tracker. */ public EnginesTracker(BundleContext context, Set<String> engineNames, ServiceTrackerCustomizer customizer){ if(context == null){ throw new IllegalArgumentException("The parsed BundleContext MUST NOT be NULL!"); } initEngineTracker(context,engineNames,customizer); }
private void update() throws ChainException { Set<String> activeEngineNames = new HashSet<String>(tracker.getActiveEngineNames()); if(activeEngineNames.isEmpty()){ throw new ChainException("Currently there are no active EnhancementEngines available"); } List<EnhancementEngine> activeEngines = new ArrayList<EnhancementEngine>(activeEngineNames.size()); Iterator<String> names = activeEngineNames.iterator(); while(names.hasNext()){ String name = names.next(); EnhancementEngine engine = tracker.getEngine(name); if(engine != null){ activeEngines.add(engine); } else { //looks like the config has changed in the meantime names.remove(); } } Set<String> emptySet = Collections.emptySet(); executionPlan = calculateExecutionPlan( getName(),activeEngines, emptySet,//this Chain does not support optional engines emptySet); //only active meaning that no engines are missing engineNames = Collections.unmodifiableSet(activeEngineNames); }
/** * Creates a new execution plan based on the configured {@link #chain} and * the currently available {@link EnhancementEngine}s. If required * {@link EnhancementEngine}s are missing a {@link ChainException} will be * thrown. * @return the execution plan * @throws ChainException if a required {@link EnhancementEngine} of the * configured {@link #chain} is not active. */ private ImmutableGraph createExecutionPlan() throws ChainException { List<EnhancementEngine> availableEngines = new ArrayList<EnhancementEngine>(chain.size()); Set<String> optionalEngines = new HashSet<String>(); Set<String> missingEngines = new HashSet<String>(); for(Entry<String,Map<String,List<String>>> entry : chain.entrySet()){ boolean optional = getState(entry.getValue(),"optional"); EnhancementEngine engine = tracker.getEngine(entry.getKey()); if(engine != null){ availableEngines.add(engine); } else { missingEngines.add(entry.getKey()); } if(optional){ optionalEngines.add(entry.getKey()); } } return calculateExecutionPlan(getName(),availableEngines,optionalEngines, missingEngines,chainScopedEnhProps); }
@Override public EnhancementEngine getEngine(String name){ ServiceReference ref = getReference(name); return ref == null ? null : (EnhancementEngine)nameTracker.getService(ref); } /*
@Override public EnhancementEngine getEngine(String name){ ServiceReference ref = getReference(name); return ref == null ? null : (EnhancementEngine)nameTracker.getService(ref); } /*
@Activate protected void activate(ComponentContext ctx) { final BundleContext bc = ctx.getBundleContext(); engineTracker = new EnginesTracker(bc, Collections.<String>emptySet(), new ServiceTrackerCustomizer() { @Override public Object addingService(ServiceReference reference) { Object service = bc.getService(reference); if(service != null){ _enginesCache = null; //rebuild the cache on the next call } return service; } @Override public void modifiedService(ServiceReference reference, Object service) { _enginesCache = null; //rebuild the cache on the next call } @Override public void removedService(ServiceReference reference, Object service) { if(reference != null){ bc.ungetService(reference); _enginesCache = null; //rebuild the cache on the next call } } }); engineTracker.open(); }
@Deactivate protected void deactivate(ComponentContext ctx){ if(engineTracker != null){ engineTracker.close(); engineTracker = null; } } /**
/** * Creates a new {@link EnginesTracker} for the parsed {@link BundleContext} * and engine names. * Examples: * <code><pre> * //Track all active engines with a customiser * new EnginesTracker(context,null,customiser); * * //Track all engines referenced by a Chain and use the customiser * //to react on changes * new EnginesTracker(context,chain.getEngineNames(),customiser); * </pre></code> * @param context the bundle context used to track engines * @param engineNames the names of the engines to track. Parse <code>null</code> * or an {@link Collections#emptySet()} to track all engines * @param customizer the {@link ServiceTrackerCustomizer} used with this tracker. */ public EnginesTracker(BundleContext context, Set<String> engineNames, ServiceTrackerCustomizer customizer){ if(context == null){ throw new IllegalArgumentException("The parsed BundleContext MUST NOT be NULL!"); } initEngineTracker(context,engineNames,customizer); }
@Activate protected void activate(ComponentContext ctx) { final BundleContext bc = ctx.getBundleContext(); engineTracker = new EnginesTracker(bc, Collections.<String>emptySet(), new ServiceTrackerCustomizer() { @Override public Object addingService(ServiceReference reference) { Object service = bc.getService(reference); if(service != null){ _enginesCache = null; //rebuild the cache on the next call } return service; } @Override public void modifiedService(ServiceReference reference, Object service) { _enginesCache = null; //rebuild the cache on the next call } @Override public void removedService(ServiceReference reference, Object service) { if(reference != null){ bc.ungetService(reference); _enginesCache = null; //rebuild the cache on the next call } } }); engineTracker.open(); }
@Deactivate protected void deactivate(ComponentContext ctx){ if(engineTracker != null){ engineTracker.close(); engineTracker = null; } } /**
protected EnginesTracker(){/* nothing to do here */ } /** * Creates a new {@link EnginesTracker} for the parsed {@link BundleContext} * and engine names. * Examples: * <code><pre> * //Track all active engines * new EnginesTracker(context); * * //Track only the langId engine * new EnginesTracker(context,langId); * </pre></code> * @param context The bundle context used to track engines * @param engineNames the name of the engines to track. If empty * all engines are tracked. */ public EnginesTracker(BundleContext context, String...engineNames){ if(context == null){ throw new IllegalArgumentException("The parsed BundleContext MUST NOT be NULL!"); } final Set<String> names; if(engineNames == null){ names = Collections.emptySet(); } else { names = new HashSet<String>(Arrays.asList(engineNames)); } initEngineTracker(context,names,null); } /**
tracker = new EnginesTracker(ctx.getBundleContext(), chain.keySet(),this); tracker.open();
@Override protected void deactivate(ComponentContext ctx) { tracker.close(); tracker = null; chainScopedEnhProps = null; chain = null; super.deactivate(ctx); } @Override
protected EnginesTracker(){/* nothing to do here */ } /** * Creates a new {@link EnginesTracker} for the parsed {@link BundleContext} * and engine names. * Examples: * <code><pre> * //Track all active engines * new EnginesTracker(context); * * //Track only the langId engine * new EnginesTracker(context,langId); * </pre></code> * @param context The bundle context used to track engines * @param engineNames the name of the engines to track. If empty * all engines are tracked. */ public EnginesTracker(BundleContext context, String...engineNames){ if(context == null){ throw new IllegalArgumentException("The parsed BundleContext MUST NOT be NULL!"); } final Set<String> names; if(engineNames == null){ names = Collections.emptySet(); } else { names = new HashSet<String>(Arrays.asList(engineNames)); } initEngineTracker(context,names,null); } /**