use of org.apache.sling.distribution.trigger.DistributionTrigger in project sling by apache.
the class AbstractDistributionAgentFactory method deactivate.
void deactivate(BundleContext context) {
if (componentReg != null) {
ServiceReference reference = componentReg.getReference();
Object service = context.getService(reference);
if (service instanceof SimpleDistributionAgent) {
SimpleDistributionAgent agent = (SimpleDistributionAgent) service;
for (DistributionTrigger trigger : triggers) {
agent.disableTrigger(trigger);
}
triggers.clear();
triggersEnabled = false;
agent.disable();
}
if (safeUnregister(componentReg)) {
componentReg = null;
}
agent = null;
}
if (safeUnregister(mbeanServiceRegistration)) {
mbeanServiceRegistration = null;
}
log.info("deactivated agent {}", agentName);
}
use of org.apache.sling.distribution.trigger.DistributionTrigger in project sling by apache.
the class AbstractDistributionAgentFactory method activate.
void activate(BundleContext context, Map<String, Object> config) {
log.info("activating with config {}", OsgiUtils.osgiPropertyMapToString(config));
// inject configuration
Dictionary<String, Object> props = new Hashtable<String, Object>();
boolean enabled = PropertiesUtil.toBoolean(config.get(ENABLED), true);
String triggersTarget = SettingsUtils.removeEmptyEntry(PropertiesUtil.toString(config.get(TRIGGERS_TARGET), null));
triggersEnabled = triggersTarget != null && triggersTarget.trim().length() > 0;
agentName = PropertiesUtil.toString(config.get(NAME), null);
if (enabled && agentName != null) {
for (Map.Entry<String, Object> entry : config.entrySet()) {
// skip service and component related properties
if (entry.getKey().startsWith("service.") || entry.getKey().startsWith("component.")) {
continue;
}
props.put(entry.getKey(), entry.getValue());
}
if (componentReg == null) {
DefaultDistributionLog distributionLog = null;
try {
String logLevel = PropertiesUtil.toString(config.get(LOG_LEVEL), DefaultDistributionLog.LogLevel.INFO.name());
DefaultDistributionLog.LogLevel level = DefaultDistributionLog.LogLevel.valueOf(logLevel.trim().toUpperCase());
if (level == null) {
level = DefaultDistributionLog.LogLevel.INFO;
}
distributionLog = new DefaultDistributionLog(DistributionComponentKind.AGENT, agentName, SimpleDistributionAgent.class, level);
agent = createAgent(agentName, context, config, distributionLog);
} catch (Throwable t) {
if (distributionLog != null) {
distributionLog.error("Cannot create agent", t);
}
log.error("Cannot create agent {}", OsgiUtils.osgiPropertyMapToString(config), t);
}
if (agent != null) {
// register agent service
componentReg = context.registerService(DistributionAgent.class.getName(), agent, props);
agent.enable();
if (triggersEnabled) {
for (DistributionTrigger trigger : triggers) {
agent.enableTrigger(trigger);
}
}
Dictionary<String, String> mbeanProps = new Hashtable<String, String>();
mbeanProps.put("jmx.objectname", "org.apache.sling.distribution:type=agent,id=" + ObjectName.quote(agentName));
DistributionAgentMBeanType mbean = createMBeanAgent(agent, config);
mbeanServiceRegistration = context.registerService(distributionAgentMBeanType.getName(), mbean, mbeanProps);
}
log.info("activated agent {}", agentName);
}
}
}
use of org.apache.sling.distribution.trigger.DistributionTrigger in project sling by apache.
the class DistributionTriggerServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String secondsParameter = request.getParameter("sec");
int seconds = secondsParameter != null && secondsParameter.length() > 0 ? Integer.parseInt(secondsParameter) : DEFAULT_NUMBER_OF_SECONDS;
if (seconds > MAX_NUMBER_OF_SECONDS) {
seconds = MAX_NUMBER_OF_SECONDS;
} else if (seconds < 0) {
seconds = DEFAULT_NUMBER_OF_SECONDS;
}
DistributionTrigger distributionTrigger = request.getResource().adaptTo(DistributionTrigger.class);
// setup SSE headers
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
// needed to allow e.g. the JavaScript EventSource API to make a call from author to this server and listen for the events
// response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // allowed origins should be explicitly configured
// response.setHeader("Access-Control-Allow-Credentials", "true");
final PrintWriter writer = response.getWriter();
DistributionRequestHandler distributionRequestHandler = new DistributionRequestHandler() {
public void handle(@Nullable ResourceResolver resourceResolver, @Nonnull DistributionRequest request) {
writeEvent(writer, request);
}
};
try {
distributionTrigger.register(distributionRequestHandler);
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
log.error("thread interrupted", e);
}
distributionTrigger.unregister(distributionRequestHandler);
} catch (DistributionException e) {
response.setStatus(400);
response.getWriter().write("error while (un)registering trigger " + e.toString());
}
}
Aggregations