Author: rombert Date: Wed Aug 5 14:30:32 2015 New Revision: 1694230 URL: http://svn.apache.org/r1694230 Log: SLING-4927 - Unable to configure the SlingServerRepository using the provisioning model
Allow the SlingServerRepositoryManager to handle creation of the home directory for Jackrabbit and of the configuration file. 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=1694230&r1=1694229&r2=1694230&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 Wed Aug 5 14:30:32 2015 @@ -18,9 +18,6 @@ package org.apache.sling.jcr.jackrabbit. import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; import java.util.Hashtable; import org.apache.sling.jcr.base.AbstractSlingRepository; @@ -267,7 +264,7 @@ public class Activator implements Bundle return; } - String configFileUrl = getConfigFileUrl(bundleContext, home); + String configFileUrl = SlingServerRepositoryManager.getOrInitConfigFileUrl(bundleContext, home); // make home relative if inside sling.home if (slingHomePath != null && home.startsWith(slingHomePath + "/")) { @@ -312,45 +309,4 @@ public class Activator implements Bundle return homeDir.getPath(); } - - private String getConfigFileUrl(BundleContext bundleContext, String home) throws IOException { - String repoConfigFileUrl = bundleContext.getProperty("sling.repository.config.file.url"); - if (repoConfigFileUrl != null) { - // the repository config file is set - URL configFileUrl = null; - try { - // verify it is a good url - configFileUrl = new URL(repoConfigFileUrl); - return repoConfigFileUrl; - } catch (MalformedURLException e) { - // this not an url, trying with "file:" - configFileUrl = new URL("file:///" + repoConfigFileUrl); - File configFile = new File(configFileUrl.getFile()); - if (configFile.canRead()) { - return configFileUrl.toString(); - } - } - } - - // ensure the configuration file (inside the home Dir !) - File configFile = new File(home, "repository.xml"); - boolean copied = false; - - try { - URL contextConfigURL = new URL("context:repository.xml"); - InputStream contextConfigStream = contextConfigURL.openStream(); - if (contextConfigStream != null) { - SlingServerRepositoryManager.copyStream(contextConfigStream, configFile); - copied = true; - } - } catch (Exception e) {} - - if (!copied) { - SlingServerRepositoryManager.copyFile(bundleContext.getBundle(), "repository.xml", configFile); - } - - // config file is repository.xml (default) in homeDir - return null; - } - } 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=1694230&r1=1694229&r2=1694230&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 Wed Aug 5 14:30:32 2015 @@ -160,6 +160,13 @@ public class SlingServerRepositoryManage } home = homeFile.getAbsolutePath(); } + + 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); + } + } // somewhat dirty hack to have the derby.log file in a sensible // location, but don't overwrite anything already set @@ -167,6 +174,12 @@ public class SlingServerRepositoryManage String derbyLog = home + "/derby.log"; 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 { @@ -228,7 +241,7 @@ public class SlingServerRepositoryManage // got no repository .... return null; } - + private Repository registerStatistics(Repository repository) { if (repository instanceof RepositoryImpl) { try { @@ -379,7 +392,56 @@ public class SlingServerRepositoryManage // ---------- Helper ------------------------------------------------------- - public static void copyFile(Bundle bundle, String entryPath, File destFile) throws FileNotFoundException, + /** + * Attempts to retrieve the URL of the repository configuration file, creating a default one is no URL is configured + * + * @param bundleContext the bundle context + * @param home the repository home + * @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 { + + String repoConfigFileUrl = bundleContext.getProperty("sling.repository.config.file.url"); + if (repoConfigFileUrl != null) { + // the repository config file is set + URL configFileUrl = null; + try { + // verify it is a good url + configFileUrl = new URL(repoConfigFileUrl); + return repoConfigFileUrl; + } catch (MalformedURLException e) { + // this not an url, trying with "file:" + configFileUrl = new URL("file:///" + repoConfigFileUrl); + File configFile = new File(configFileUrl.getFile()); + if (configFile.canRead()) { + return configFileUrl.toString(); + } + } + } + + // ensure the configuration file (inside the home Dir !) + File configFile = new File(home, "repository.xml"); + boolean copied = false; + + try { + URL contextConfigURL = new URL("context:repository.xml"); + InputStream contextConfigStream = contextConfigURL.openStream(); + if (contextConfigStream != null) { + SlingServerRepositoryManager.copyStream(contextConfigStream, configFile); + copied = true; + } + } catch (Exception e) {} + + if (!copied) { + SlingServerRepositoryManager.copyFile(bundleContext.getBundle(), "repository.xml", configFile); + } + + // config file is repository.xml (default) in homeDir + return null; + } + + private static void copyFile(Bundle bundle, String entryPath, File destFile) throws FileNotFoundException, IOException { if (destFile.canRead()) { // nothing to do, file exists @@ -397,7 +459,7 @@ public class SlingServerRepositoryManage copyStream(source, destFile); } - public static void copyStream(InputStream source, File destFile) throws FileNotFoundException, IOException { + private static void copyStream(InputStream source, File destFile) throws FileNotFoundException, IOException { OutputStream dest = null; try {