You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/01/15 09:30:32 UTC
svn commit: r612037 - in /incubator/sling/trunk/jcr:
jackrabbit-client/src/main/java/org/apache/sling/jcr/jackrabbit/client/Activator.java
jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/Activator.java
Author: fmeschbe
Date: Tue Jan 15 00:30:26 2008
New Revision: 612037
URL: http://svn.apache.org/viewvc?rev=612037&view=rev
Log:
SLING-155 Register ServiceListener if Configuration Admin is not available on
bundle startup. This ensure configuration verification as soon as the
service becomes registered
Modified:
incubator/sling/trunk/jcr/jackrabbit-client/src/main/java/org/apache/sling/jcr/jackrabbit/client/Activator.java
incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/Activator.java
Modified: incubator/sling/trunk/jcr/jackrabbit-client/src/main/java/org/apache/sling/jcr/jackrabbit/client/Activator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/jackrabbit-client/src/main/java/org/apache/sling/jcr/jackrabbit/client/Activator.java?rev=612037&r1=612036&r2=612037&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/jackrabbit-client/src/main/java/org/apache/sling/jcr/jackrabbit/client/Activator.java (original)
+++ incubator/sling/trunk/jcr/jackrabbit-client/src/main/java/org/apache/sling/jcr/jackrabbit/client/Activator.java Tue Jan 15 00:30:26 2008
@@ -24,6 +24,10 @@
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
@@ -33,7 +37,7 @@
/**
* The <code>Activator</code> TODO
*/
-public class Activator implements BundleActivator {
+public class Activator implements BundleActivator, ServiceListener {
/** default log */
private static final Logger log = LoggerFactory.getLogger(Activator.class);
@@ -52,26 +56,69 @@
*/
public static final String SLING_CONTEXT_DEFAULT = "sling.context.default";
- /* (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
- */
- public void start(BundleContext context) throws Exception {
+ // The name of the Configuration Admin Service
+ private static final String CONFIG_ADMIN_NAME = ConfigurationAdmin.class.getName();
+
+ // this bundle's context, used by verifyConfiguration
+ private BundleContext bundleContext;
+
+ // the name of the default sling context
+ private String slingContext;
+
+ public void start(BundleContext context) {
+
+ this.bundleContext = context;
// check the name of the default context, nothing to do if none
- String slingContext = context.getProperty(SLING_CONTEXT_DEFAULT);
+ slingContext = context.getProperty(SLING_CONTEXT_DEFAULT);
if (slingContext == null) {
- return;
+ slingContext = "default";
}
- ServiceReference sr = context.getServiceReference(ConfigurationAdmin.class.getName());
- if (sr == null) {
- log.info("Activator: Need ConfigurationAdmin Service to ensure configuration");
- return;
+ ServiceReference sr = context.getServiceReference(CONFIG_ADMIN_NAME);
+ if (sr != null) {
+
+ // immediately verify the configuration as the service is here
+ verifyConfiguration(sr);
+
+ } else {
+
+ // register as service listener for Configuration Admin to verify
+ // the configuration when the service is registered
+ try {
+ bundleContext.addServiceListener(this, "("
+ + Constants.OBJECTCLASS + "=" + CONFIG_ADMIN_NAME + ")");
+ } catch (InvalidSyntaxException ise) {
+ log.error(
+ "start: Failed to register for Configuration Admin Service, will not verify configuration",
+ ise);
+ }
}
+ }
- ConfigurationAdmin ca = (ConfigurationAdmin) context.getService(sr);
+ public void stop(BundleContext arg0) {
+ // nothing to do
+ }
+
+ // ---------- ServiceListener ----------------------------------------------
+
+ public void serviceChanged(ServiceEvent event) {
+ if (event.getType() == ServiceEvent.REGISTERED) {
+
+ // verify the configuration with the newly registered service
+ verifyConfiguration(event.getServiceReference());
+
+ // don't care for any more service state changes
+ bundleContext.removeServiceListener(this);
+ }
+ }
+
+ // ---------- internal -----------------------------------------------------
+
+ private void verifyConfiguration(ServiceReference ref) {
+ ConfigurationAdmin ca = (ConfigurationAdmin) bundleContext.getService(ref);
if (ca == null) {
- log.info("Activator: Need ConfigurationAdmin Service to ensure configuration (has gone ?)");
+ log.error("verifyConfiguration: Failed to get Configuration Admin Service from Service Reference");
return;
}
@@ -81,7 +128,8 @@
+ ConfigurationAdmin.SERVICE_FACTORYPID + "="
+ CLIENT_REPOSITORY_FACTORY_PID + ")");
if (cfgs != null && cfgs.length > 0) {
- log.info("Activator: {} Configurations available for {}, nothing to do",
+ log.info(
+ "verifyConfiguration: {} Configurations available for {}, nothing to do",
new Object[] { new Integer(cfgs.length),
CLIENT_REPOSITORY_FACTORY_PID });
return;
@@ -92,22 +140,21 @@
props.put(SLING_CONTEXT, slingContext);
props.put(SlingClientRepository.REPOSITORY_NAME, "crx");
props.put(Context.PROVIDER_URL, "http://jcr.day.com");
- props.put(Context.INITIAL_CONTEXT_FACTORY, "com.day.util.jndi.provider.MemoryInitialContextFactory");
+ props.put(Context.INITIAL_CONTEXT_FACTORY,
+ "com.day.util.jndi.provider.MemoryInitialContextFactory");
// create the factory and set the properties
- ca.createFactoryConfiguration(CLIENT_REPOSITORY_FACTORY_PID).update(props);
+ Configuration config = ca.createFactoryConfiguration(CLIENT_REPOSITORY_FACTORY_PID);
+ config.update(props);
+
+ log.debug("verifyConfiguration: Created configuration {} for {}",
+ config.getPid(), config.getFactoryPid());
} catch (Throwable t) {
- log.error("Activator: Cannot check or define configuration", t);
+ log.error(
+ "verifyConfiguration: Cannot check or define configuration", t);
} finally {
- context.ungetService(sr);
+ bundleContext.ungetService(ref);
}
- }
-
- /* (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
- */
- public void stop(BundleContext arg0) throws Exception {
- // nothing to do
}
}
Modified: incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/Activator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/Activator.java?rev=612037&r1=612036&r2=612037&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/Activator.java (original)
+++ incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/Activator.java Tue Jan 15 00:30:26 2008
@@ -21,6 +21,10 @@
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
@@ -30,7 +34,7 @@
/**
* The <code>Activator</code> TODO
*/
-public class Activator implements BundleActivator {
+public class Activator implements BundleActivator, ServiceListener {
/** default log */
private static final Logger log = LoggerFactory.getLogger(Activator.class);
@@ -49,30 +53,73 @@
*/
public static final String SLING_CONTEXT_DEFAULT = "sling.context.default";
+ // The name of the Configuration Admin Service
+ private static final String CONFIG_ADMIN_NAME = ConfigurationAdmin.class.getName();
+
+ // this bundle's context, used by verifyConfiguration
+ private BundleContext bundleContext;
+
+ // the name of the default sling context
+ private String slingContext;
+
protected String getRepositoryName() {
return "jackrabbit";
}
- /* (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
- */
- public void start(BundleContext context) throws Exception {
+ public void start(BundleContext context) {
+
+ this.bundleContext = context;
// check the name of the default context, nothing to do if none
- String slingContext = context.getProperty(SLING_CONTEXT_DEFAULT);
+ slingContext = context.getProperty(SLING_CONTEXT_DEFAULT);
if (slingContext == null) {
- return;
+ slingContext = "default";
}
- ServiceReference sr = context.getServiceReference(ConfigurationAdmin.class.getName());
- if (sr == null) {
- log.info("Activator: Need ConfigurationAdmin Service to ensure configuration");
- return;
+ ServiceReference sr = context.getServiceReference(CONFIG_ADMIN_NAME);
+ if (sr != null) {
+
+ // immediately verify the configuration as the service is here
+ verifyConfiguration(sr);
+
+ } else {
+
+ // register as service listener for Configuration Admin to verify
+ // the configuration when the service is registered
+ try {
+ bundleContext.addServiceListener(this, "("
+ + Constants.OBJECTCLASS + "=" + CONFIG_ADMIN_NAME + ")");
+ } catch (InvalidSyntaxException ise) {
+ log.error(
+ "start: Failed to register for Configuration Admin Service, will not verify configuration",
+ ise);
+ }
}
+ }
- ConfigurationAdmin ca = (ConfigurationAdmin) context.getService(sr);
+ public void stop(BundleContext arg0) {
+ // nothing to do
+ }
+
+ // ---------- ServiceListener ----------------------------------------------
+
+ public void serviceChanged(ServiceEvent event) {
+ if (event.getType() == ServiceEvent.REGISTERED) {
+
+ // verify the configuration with the newly registered service
+ verifyConfiguration(event.getServiceReference());
+
+ // don't care for any more service state changes
+ bundleContext.removeServiceListener(this);
+ }
+ }
+
+ // ---------- internal -----------------------------------------------------
+
+ private void verifyConfiguration(ServiceReference ref) {
+ ConfigurationAdmin ca = (ConfigurationAdmin) bundleContext.getService(ref);
if (ca == null) {
- log.info("Activator: Need ConfigurationAdmin Service to ensure configuration (has gone ?)");
+ log.error("verifyConfiguration: Failed to get Configuration Admin Service from Service Reference");
return;
}
@@ -82,49 +129,52 @@
+ ConfigurationAdmin.SERVICE_FACTORYPID + "="
+ SERVER_REPOSITORY_FACTORY_PID + ")");
if (cfgs != null && cfgs.length > 0) {
- log.info("Activator: {} Configurations available for {}, nothing to do",
+ log.info(
+ "verifyConfiguration: {} Configurations available for {}, nothing to do",
new Object[] { new Integer(cfgs.length),
SERVER_REPOSITORY_FACTORY_PID });
return;
}
- String slingHome = context.getProperty("sling.home");
+ String slingHome = bundleContext.getProperty("sling.home");
// make sure jackrabbit home exists
File homeDir = new File(slingHome, this.getRepositoryName());
if (!homeDir.isDirectory()) {
if (!homeDir.mkdirs()) {
- log.info("Activator: Cannot create Jackrabbit home " + homeDir
- + ", failed creating default configuration");
+ log.info("verifyConfiguration: Cannot create Jackrabbit home "
+ + homeDir + ", failed creating default configuration");
return;
}
}
// ensure the configuration file
File configFile = new File(slingHome, "repository.xml");
- SlingServerRepository.copyFile(context.getBundle(), "repository.xml", configFile);
+ SlingServerRepository.copyFile(bundleContext.getBundle(),
+ "repository.xml", configFile);
// we have no configuration, create from default settings
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(SLING_CONTEXT, slingContext);
- props.put(SlingServerRepository.REPOSITORY_CONFIG_URL, configFile.getPath());
- props.put(SlingServerRepository.REPOSITORY_HOME_DIR, homeDir.getPath());
- props.put(SlingServerRepository.REPOSITORY_REGISTRATION_NAME, this.getRepositoryName());
+ props.put(SlingServerRepository.REPOSITORY_CONFIG_URL,
+ configFile.getPath());
+ props.put(SlingServerRepository.REPOSITORY_HOME_DIR,
+ homeDir.getPath());
+ props.put(SlingServerRepository.REPOSITORY_REGISTRATION_NAME,
+ this.getRepositoryName());
// create the factory and set the properties
- ca.createFactoryConfiguration(SERVER_REPOSITORY_FACTORY_PID).update(props);
+ Configuration config = ca.createFactoryConfiguration(SERVER_REPOSITORY_FACTORY_PID);
+ config.update(props);
+
+ log.debug("verifyConfiguration: Created configuration {} for {}",
+ config.getPid(), config.getFactoryPid());
} catch (Throwable t) {
- log.error("Activator: Cannot check or define configuration", t);
+ log.error(
+ "verifyConfiguration: Cannot check or define configuration", t);
} finally {
- context.ungetService(sr);
+ bundleContext.ungetService(ref);
}
- }
-
- /* (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
- */
- public void stop(BundleContext arg0) throws Exception {
- // nothing to do
}
}