Skip to content

Instantly share code, notes, and snippets.

@nateyolles
Created October 15, 2015 18:54
Show Gist options
  • Save nateyolles/d0a547d814376e66f7aa to your computer and use it in GitHub Desktop.
Save nateyolles/d0a547d814376e66f7aa to your computer and use it in GitHub Desktop.
Example servlet reading and updated OSGi configs in Adobe Experience Manager
package com.nateyolles.aem;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.Configuration;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.cm.ConfigurationAdmin;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
@SlingServlet(paths = "/bin/bar")
public class OsgiConfigExampleServlet extends SlingAllMethodsServlet {
/** Service to get OSGi configurations */
@Reference
private ConfigurationAdmin configAdmin;
private static final String LOGGER_FACTORY_PID = "org.apache.sling.commons.log.LogManager.factory.config";
private static final String CUSTOM_LOGGER_PID = "org.apache.sling.commons.log.LogManager.factory.config.278b07c1-7eb9-43a8-8d84-8fed5ee6b0a4";
private static final String HTML_LIBRARY_MANAGER_PID = "com.day.cq.widget.impl.HtmlLibraryManagerImpl";
private static final String MINIFY_PROPERTY = "htmllibmanager.minify";
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
/* Managed Service and Manage Service Factory configs */
Configuration loggerFactoryConfig = configAdmin.getConfiguration(LOGGER_FACTORY_PID);
Configuration loggerConfig = configAdmin.getConfiguration(CUSTOM_LOGGER_PID);
/* returns true */
boolean isFactoryPid = loggerConfig.getFactoryPid().equals(loggerFactoryConfig.getPid());
/* Get all configs from the factory */
try {
/* Java filter syntax*/
String filter = '(' + ConfigurationAdmin.SERVICE_FACTORYPID + '=' + LOGGER_FACTORY_PID + ')';
Configuration[] allLoggerConfigs = configAdmin.listConfigurations(filter);
} catch (InvalidSyntaxException e) {
// TODO Auto-generated catch block
}
/*********************************************/
/* Get properties */
Configuration htmlLibraryManangerConfig = configAdmin.getConfiguration(HTML_LIBRARY_MANAGER_PID);
Dictionary<String, Object> properties = htmlLibraryManangerConfig.getProperties();
boolean isMinify = PropertiesUtil.toBoolean(properties.get(MINIFY_PROPERTY), false);
/*********************************************/
/* Set properties */
if (properties == null) {
properties = new Hashtable<String, Object>();
}
/* Remember HashTables don't accept null values. */
properties.put(MINIFY_PROPERTY, true);
htmlLibraryManangerConfig.update(properties);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment