Author: rombert Date: Thu Aug 6 11:46:33 2015 New Revision: 1694462 URL: http://svn.apache.org/r1694462 Log: SLING-4929 - Remove configuration logic from SlingServerRepository
Task-Url: https://issues.apache.org/jira/browse/SLING-4929 Modified: sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/Activator.java sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/SlingServerRepositoryManager.java Modified: sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/Activator.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/Activator.java?rev=1694462&r1=1694461&r2=1694462&view=diff ============================================================================== --- sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/Activator.java (original) +++ sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/Activator.java Thu Aug 6 11:46:33 2015 @@ -16,33 +16,15 @@ */ package org.apache.sling.jcr.jackrabbit.server.impl; -import java.io.File; -import java.io.IOException; -import java.util.Hashtable; - -import org.apache.sling.jcr.base.AbstractSlingRepository; -import org.apache.sling.jcr.base.util.RepositoryAccessor; import org.apache.sling.jcr.jackrabbit.server.security.LoginModulePlugin; 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; import org.osgi.util.tracker.ServiceTracker; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * The <code>Activator</code> TODO */ -public class Activator implements BundleActivator, ServiceListener { - - /** default log */ - private static final Logger log = LoggerFactory.getLogger(Activator.class); +public class Activator implements BundleActivator { public static final String SERVER_REPOSITORY_FACTORY_PID = "org.apache.sling.jcr.jackrabbit.server.SlingServerRepository"; @@ -58,9 +40,6 @@ public class Activator implements Bundle */ 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 static BundleContext bundleContext; @@ -81,14 +60,6 @@ public class Activator implements Bundle private String slingContext; private static AccessManagerFactoryTracker accessManagerFactoryTracker; - protected String getRepositoryName() { - String repoName = bundleContext.getProperty("sling.repository.name"); - if (repoName != null) { - return repoName; // the repository name is set - } - return "jackrabbit"; - } - public void start(BundleContext context) { bundleContext = context; @@ -102,26 +73,7 @@ public class Activator implements Bundle if (slingContext == null) { slingContext = "default"; } - - 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); - } - } + if (accessManagerFactoryTracker == null) { accessManagerFactoryTracker = new AccessManagerFactoryTracker(bundleContext); } @@ -148,19 +100,6 @@ public class Activator implements Bundle bundleContext = null; } - // ---------- 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); - } - } - // ---------- LoginModule tracker for PluggableDefaultLoginModule private static BundleContext getBundleContext() { @@ -206,107 +145,4 @@ public class Activator implements Bundle public static AccessManagerFactoryTracker getAccessManagerFactoryTracker() { return accessManagerFactoryTracker; } - - // ---------- internal ----------------------------------------------------- - - private void verifyConfiguration(ServiceReference ref) { - ConfigurationAdmin ca = (ConfigurationAdmin) bundleContext.getService(ref); - if (ca == null) { - log.error("verifyConfiguration: Failed to get Configuration Admin Service from Service Reference"); - return; - } - - try { - // find a configuration for theses properties... - Configuration[] cfgs = ca.listConfigurations("(" - + ConfigurationAdmin.SERVICE_FACTORYPID + "=" - + SERVER_REPOSITORY_FACTORY_PID + ")"); - if (cfgs != null && cfgs.length > 0) { - log.info( - "verifyConfiguration: {} Configurations available for {}, nothing to do", - new Object[] { new Integer(cfgs.length), - SERVER_REPOSITORY_FACTORY_PID }); - return; - } - - // No config, create a default one. - Hashtable<String, String> defaultConfig = new Hashtable<String, String>(); - final String overrideUrl = bundleContext.getProperty(RepositoryAccessor.REPOSITORY_URL_OVERRIDE_PROPERTY); - if(overrideUrl != null && overrideUrl.length() > 0) { - // Ignore other parameters if override URL (SLING-254) is set - defaultConfig.put(RepositoryAccessor.REPOSITORY_URL_OVERRIDE_PROPERTY, overrideUrl); - log.info(RepositoryAccessor.REPOSITORY_URL_OVERRIDE_PROPERTY + "=" + overrideUrl + - ", using it to create the default configuration"); - - } else { - initDefaultConfig(defaultConfig, bundleContext); - } - - // create the factory and set the properties - Configuration config = ca.createFactoryConfiguration(SERVER_REPOSITORY_FACTORY_PID); - config.update(defaultConfig); - - log.info("verifyConfiguration: Created configuration {} for {}", - config.getPid(), config.getFactoryPid()); - - } catch (Throwable t) { - log.error( - "verifyConfiguration: Cannot check or define configuration", t); - } finally { - bundleContext.ungetService(ref); - } - } - - private void initDefaultConfig(Hashtable<String, String> props, BundleContext bundleContext) throws IOException { - String slingHomePath = bundleContext.getProperty("sling.home"); - String home = getHomePath(bundleContext, slingHomePath); - if (home == null) { - return; - } - - String configFileUrl = SlingServerRepositoryManager.getOrInitConfigFileUrl(bundleContext, home); - - // make home relative if inside sling.home - if (slingHomePath != null && home.startsWith(slingHomePath + "/")) { - home = home.substring(slingHomePath.length() + 1); - } - - // default config values - props.put(SLING_CONTEXT, slingContext); - if (configFileUrl != null) { - props.put(SlingServerRepositoryManager.REPOSITORY_CONFIG_URL, - configFileUrl); - } - props.put(SlingServerRepositoryManager.REPOSITORY_HOME_DIR, home); - props.put(SlingServerRepositoryManager.REPOSITORY_REGISTRATION_NAME, - this.getRepositoryName()); - - // password properties are not used any more, set to a n/a value - props.put(AbstractSlingRepository.PROPERTY_ADMIN_PASS, "not-used"); - props.put(AbstractSlingRepository.PROPERTY_ANONYMOUS_PASS, "not-used"); - } - - private String getHomePath(BundleContext bundleContext, String slingHomePath) { - File homeDir; - String repoHomePath = bundleContext.getProperty("sling.repository.home"); - if (repoHomePath != null) { - homeDir = new File(repoHomePath, getRepositoryName()); - } else if (slingHomePath != null) { - homeDir = new File(slingHomePath, getRepositoryName()); - } else { - homeDir = new File(getRepositoryName()); - } - - // make sure jackrabbit home exists - log.info("Creating default config for Jackrabbit in " + homeDir); - if (!homeDir.isDirectory()) { - if (!homeDir.mkdirs()) { - log.info("verifyConfiguration: Cannot create Jackrabbit home " - + homeDir + ", failed creating default configuration"); - return null; - } - } - - return homeDir.getPath(); - } } Modified: sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/SlingServerRepositoryManager.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/SlingServerRepositoryManager.java?rev=1694462&r1=1694461&r2=1694462&view=diff ============================================================================== --- sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/SlingServerRepositoryManager.java (original) +++ sling/trunk/bundles/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/impl/SlingServerRepositoryManager.java Thu Aug 6 11:46:33 2015 @@ -37,7 +37,6 @@ import javax.management.DynamicMBean; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.ConfigurationPolicy; import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; @@ -52,6 +51,7 @@ import org.apache.sling.jcr.api.Namespac import org.apache.sling.jcr.api.SlingRepository; import org.apache.sling.jcr.base.AbstractSlingRepository2; import org.apache.sling.jcr.base.AbstractSlingRepositoryManager; +import org.apache.sling.jcr.base.util.RepositoryAccessor; import org.apache.sling.jcr.jackrabbit.server.impl.jmx.StatisticsMBeanImpl; import org.apache.sling.serviceusermapping.ServiceUserMapper; import org.osgi.framework.Bundle; @@ -63,15 +63,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * The <code>SlingServerRepository</code> TODO + * The <code>SlingServerRepository</code> creates and configures <tt>Jackrabbit</tt> repository instances. */ @Component( label = "%repository.name", description = "%repository.description", metatype = true, name = "org.apache.sling.jcr.jackrabbit.server.SlingServerRepository", - configurationFactory = true, - policy = ConfigurationPolicy.REQUIRE) + configurationFactory = true) @Reference( name = "namespaceMapper", referenceInterface = NamespaceMapper.class, @@ -142,32 +141,29 @@ public class SlingServerRepositoryManage @Override protected Repository acquireRepository() { - - @SuppressWarnings("unchecked") - Dictionary<String, Object> environment = this.getComponentContext().getProperties(); - String configURLObj = (String) environment.get(REPOSITORY_CONFIG_URL); - String home = (String) environment.get(REPOSITORY_HOME_DIR); - - // ensure absolute home (path) - File homeFile = new File(home); - if (!homeFile.isAbsolute()) { - BundleContext context = getComponentContext().getBundleContext(); - String slingHomePath = context.getProperty("sling.home"); - if (slingHomePath != null) { - homeFile = new File(slingHomePath, home); - } else { - homeFile = homeFile.getAbsoluteFile(); - } - home = homeFile.getAbsolutePath(); + + BundleContext bundleContext = getComponentContext().getBundleContext(); + + final String overrideUrl = bundleContext.getProperty(RepositoryAccessor.REPOSITORY_URL_OVERRIDE_PROPERTY); + + // Do not configure the repository if override URL (SLING-254) is set + if ( overrideUrl != null && !overrideUrl.isEmpty() ) { + return null; } - if (!homeFile.isDirectory()) { - log.info("Creating default config for Jackrabbit in " + homeFile); - if (!homeFile.mkdirs()) { - throw new RuntimeException("Unable to create Jackrabbit home at " + home); - } + String slingHomePath = bundleContext.getProperty("sling.home"); + File homeFile; + String configURLObj; + try { + homeFile = getOrInitRepositoryHome(bundleContext, slingHomePath); + configURLObj = getOrInitConfigFileUrl(bundleContext, homeFile.getAbsolutePath()); + } catch (IOException e) { + throw new RuntimeException(getClass().getName() + " initialisation failed", e); } + // ensure absolute home (path) + String home = homeFile.getAbsolutePath(); + // somewhat dirty hack to have the derby.log file in a sensible // location, but don't overwrite anything already set if (System.getProperty("derby.stream.error.file") == null) { @@ -175,12 +171,6 @@ public class SlingServerRepositoryManage System.setProperty("derby.stream.error.file", derbyLog); } - try { - getOrInitConfigFileUrl(getComponentContext().getBundleContext(), home); - } catch (IOException e) { - throw new RuntimeException("Unable to get config file url", e); - } - InputStream ins = null; try { @@ -206,7 +196,7 @@ public class SlingServerRepositoryManage log.info("Configuration File " + configFile.getAbsolutePath() + " has been lost, trying to recreate"); - final Bundle bundle = getComponentContext().getBundleContext().getBundle(); + final Bundle bundle = bundleContext.getBundle(); SlingServerRepositoryManager.copyFile(bundle, "repository.xml", configFile); ins = new FileInputStream(configFile); @@ -242,6 +232,43 @@ public class SlingServerRepositoryManage return null; } + private File getOrInitRepositoryHome(BundleContext bundleContext, String slingHomePath) throws IOException { + + String repoHomePath = (String) getComponentContext().getProperties().get(REPOSITORY_HOME_DIR); + if ( repoHomePath == null || repoHomePath.isEmpty() ) { + repoHomePath = bundleContext.getProperty("sling.repository.home"); + } + + File homeDir; + if (repoHomePath != null && !repoHomePath.isEmpty()) { + homeDir = new File(repoHomePath, getRepositoryName(bundleContext)); + } else if (slingHomePath != null) { + homeDir = new File(slingHomePath, getRepositoryName(bundleContext)); + } else { + homeDir = new File(getRepositoryName(bundleContext)); + } + + // make sure jackrabbit home exists + if (!homeDir.isDirectory()) { + log.info("Creating default config for Jackrabbit in " + homeDir); + if (!homeDir.mkdirs()) { + throw new IOException("verifyConfiguration: Cannot create Jackrabbit home " + + homeDir + ", failed creating default configuration"); + } + } + + return homeDir; + } + + private String getRepositoryName(BundleContext bundleContext) { + String repoName = bundleContext.getProperty("sling.repository.name"); + if (repoName != null) { + return repoName; // the repository name is set + } + return "jackrabbit"; + } + + private Repository registerStatistics(Repository repository) { if (repository instanceof RepositoryImpl) { try { @@ -400,9 +427,12 @@ public class SlingServerRepositoryManage * @return the url, or null if the default location is used * @throws IOException error when getting or initialising the config file url */ - public static String getOrInitConfigFileUrl(BundleContext bundleContext, String home) throws IOException { + private String getOrInitConfigFileUrl(BundleContext bundleContext, String home) throws IOException { - String repoConfigFileUrl = bundleContext.getProperty("sling.repository.config.file.url"); + String repoConfigFileUrl = (String) getComponentContext().getProperties().get(REPOSITORY_CONFIG_URL); + if ( repoConfigFileUrl == null || repoConfigFileUrl.isEmpty() ) { + repoConfigFileUrl = bundleContext.getProperty("sling.repository.config.file.url"); + } if (repoConfigFileUrl != null) { // the repository config file is set URL configFileUrl = null; @@ -420,7 +450,7 @@ public class SlingServerRepositoryManage } } - // ensure the configuration file (inside the home Dir !) + // ensure the configuration file exists (inside the home Dir !) File configFile = new File(home, "repository.xml"); boolean copied = false;