From a430449adf43f6cc6103d9fb90e8f15225f0a96b Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 4 Sep 2012 14:35:59 +0000 Subject: [PATCH] Fix loading of CND via testing classpath. Introduce explicit admin credentials git-svn-id: https://svn.argeo.org/commons/trunk@5550 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../argeo/jackrabbit/JackrabbitWrapper.java | 84 ++++++++++++------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitWrapper.java b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitWrapper.java index 4635ef2c2..c4d131d6e 100644 --- a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitWrapper.java +++ b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitWrapper.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.jcr.Credentials; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.Repository; @@ -75,6 +76,13 @@ public class JackrabbitWrapper extends JcrRepositoryWrapper implements private BundleContext bundleContext; + /** + * Explicitly set admin credentials used in initialization. Useful for + * testing, in real applications authentication is rather dealt with + * externally + */ + private Credentials adminCredentials = null; + /** * Empty constructor, {@link #init()} should be called after properties have * been set @@ -100,10 +108,10 @@ public class JackrabbitWrapper extends JcrRepositoryWrapper implements if ((cndFiles == null || cndFiles.size() == 0) && (namespaces == null || namespaces.size() == 0)) return; - + Session session = null; try { - session = login(); + session = login(adminCredentials); // register namespaces if (namespaces.size() > 0) { NamespaceHelper namespaceHelper = new NamespaceHelper(session); @@ -112,42 +120,50 @@ public class JackrabbitWrapper extends JcrRepositoryWrapper implements // load CND files from classpath or as URL for (String resUrl : cndFiles) { - boolean classpath; - // normalize URL - if (resUrl.startsWith("classpath:")) { - resUrl = resUrl.substring("classpath:".length()); - classpath = true; - } else if (resUrl.indexOf(':') < 0) { - if (!resUrl.startsWith("/")) { - resUrl = "/" + resUrl; - log.warn("Classpath should start with '/'"); - } - // resUrl = "classpath:" + resUrl; - classpath = true; - } else { - classpath = false; - } - URL url; Bundle dataModelBundle = null; - if (classpath) { - if (bundleContext != null) { - Bundle currentBundle = bundleContext.getBundle(); - url = currentBundle.getResource(resUrl); - if (url != null) {// found - dataModelBundle = findDataModelBundle(resUrl); + + {// TODO put this in a separate method + boolean classpath; + // normalize URL + if (bundleContext != null + && resUrl.startsWith("classpath:")) { + resUrl = resUrl.substring("classpath:".length()); + classpath = true; + } else if (resUrl.indexOf(':') < 0) { + if (!resUrl.startsWith("/")) { + resUrl = "/" + resUrl; + log.warn("Classpath should start with '/'"); } + // resUrl = "classpath:" + resUrl; + classpath = true; } else { - url = getClass().getClassLoader().getResource(resUrl); - // if (url == null) - // url = Thread.currentThread() - // .getContextClassLoader() - // .getResource(resUrl); + classpath = false; } - } else { - url = new URL(resUrl); - } + if (classpath) { + if (bundleContext != null) { + Bundle currentBundle = bundleContext.getBundle(); + url = currentBundle.getResource(resUrl); + if (url != null) {// found + dataModelBundle = findDataModelBundle(resUrl); + } + } else { + resUrl = "classpath:" + resUrl; + url = null; + // url = + // getClass().getClassLoader().getResource(resUrl); + // if (url == null) + // url = Thread.currentThread() + // .getContextClassLoader() + // .getResource(resUrl); + } + } else if (!resUrl.startsWith("classpath:")) { + url = new URL(resUrl); + } else { + url = null; + } + } // check existing data model nodes new NamespaceHelper(session).registerNamespace( ArgeoNames.ARGEO, ArgeoNames.ARGEO_NAMESPACE); @@ -318,4 +334,8 @@ public class JackrabbitWrapper extends JcrRepositoryWrapper implements this.resourceLoader = resourceLoader; } + public void setAdminCredentials(Credentials adminCredentials) { + this.adminCredentials = adminCredentials; + } + } -- 2.30.2