public void handleEvent(Event event) { log.trace("handleEvent"); refreshCache(); }
@SuppressWarnings("squid:S1149") private ServiceRegistration registerService(String id, Resource config) { Hashtable<String, Object> props = new Hashtable<String, Object>(); props.put(SERVICE_OWNER_KEY, getClass().getCanonicalName()); props.put(CONFIGURATION_ID_KEY, id); ServiceRegistration serviceRegistration = registerServiceObject(config, props); registeredServices.put(id, serviceRegistration); log.debug("Automatic Package Replication job {} successfully updated with service {}", id, serviceRegistration.getReference().getProperty(Constants.SERVICE_ID)); return serviceRegistration; }
@Deactivate public void deactivate(ComponentContext context) throws LoginException { log.trace("deactivate"); for (String id : registeredServices.keySet()) { unregisterService(id); } log.trace("Deactivation successful!"); }
log.trace("refreshCache"); try ( ResourceResolver resolver = getResourceResolver()) { Resource aprRoot = resolver.getResource(getRootPath()); List<String> configuredIds = new ArrayList<String>(); for (Resource child : aprRoot.getChildren()) { if (!JcrConstants.JCR_CONTENT.equals(child.getName())) { log.debug("Updating service for configuration {}", child.getPath()); updateJobService(child.getPath(), child.getChild(JcrConstants.JCR_CONTENT)); configuredIds.add(child.getPath()); if (!configuredIds.contains(configurationId)) { log.debug("Unregistering service for configuration {}", configurationId); this.unregisterService(configurationId);
private void updateJobService(String id, Resource resource) { log.debug("Registering job: {}", id); try { String filter = "(&(" + SERVICE_OWNER_KEY + "=" + getClass().getCanonicalName() + ")" + "(" + CONFIGURATION_ID_KEY + "=" + id + "))"; ServiceReference[] serviceReferences = (ServiceReference[]) ArrayUtils.addAll( bctx.getServiceReferences(Runnable.class.getCanonicalName(), filter), bctx.getServiceReferences(EventHandler.class.getCanonicalName(), filter)); if (serviceReferences != null && serviceReferences.length > 0) { ServiceReference sr = serviceReferences[0]; if (isServiceUpdated(resource, sr)) { log.debug("Service for {} up to date, no changes necessary", id); } else { log.warn("Unbinding ServiceReference for {}", id); unregisterService(id); registerService(id, resource); } } else { registerService(id, resource); } } catch (Exception e) { log.error("Failed to register job " + id, e); } }
@Override public void execute(String id) { AutomaticPackageReplicatorJob job = (AutomaticPackageReplicatorJob) getBundleContext() .getService(super.getRegisteredServices().get(id).getReference()); job.run(); }
@Override protected ServiceRegistration registerServiceObject(Resource config, Hashtable<String, Object> props) { AutomaticPackageReplicatorModel model = new AutomaticPackageReplicatorModel(config); AutomaticPackageReplicatorJob job = new AutomaticPackageReplicatorJob(resourceResolverFactory, replicator, eventAdmin, model.getPackagePath()); ServiceRegistration serviceRegistration = null; props.put(TRIGGER_KEY, model.getTrigger().name()); if (AutomaticPackageReplicatorModel.TRIGGER.cron == model.getTrigger()) { if(StringUtils.isEmpty(model.getCronTrigger())){ throw new IllegalArgumentException("No cron trigger specified"); } props.put(Scheduler.PROPERTY_SCHEDULER_EXPRESSION, model.getCronTrigger()); log.debug("Registering cron runner with: {}", props); serviceRegistration = super.getBundleContext().registerService(Runnable.class.getCanonicalName(), job, props); } else { if(StringUtils.isEmpty(model.getEventTopic())){ throw new IllegalArgumentException("No event topic specified"); } props.put(EventConstants.EVENT_TOPIC, new String[] { model.getEventTopic() }); if (StringUtils.isNotEmpty(model.getEventFilter())) { props.put(EventConstants.EVENT_FILTER, model.getEventFilter()); } log.debug("Registering event handler runner with: {}", props); serviceRegistration = super.getBundleContext().registerService(EventHandler.class.getCanonicalName(), job, props); } return serviceRegistration; } }
@Activate public void activate(ComponentContext context) throws LoginException { log.trace("activate"); bctx = context.getBundleContext(); refreshCache(); log.trace("Activation successful!"); }