From: Mathieu Baudier Date: Fri, 4 Nov 2011 19:47:30 +0000 (+0000) Subject: Load JCR CND files with OSGi bundle context X-Git-Tag: argeo-commons-2.1.30~1083 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=0f82096aecc32f674336405cb52f7796ed29d23c;p=lgpl%2Fargeo-commons.git Load JCR CND files with OSGi bundle context git-svn-id: https://svn.argeo.org/commons/trunk@4879 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/eclipse/runtime/org.argeo.eclipse.ui/pom.xml b/eclipse/runtime/org.argeo.eclipse.ui/pom.xml index d8c7f1435..cda0a6beb 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui/pom.xml +++ b/eclipse/runtime/org.argeo.eclipse.ui/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.commons.eclipse @@ -29,6 +30,7 @@ ${version.maven-bundle-plugin} + ${project.artifactID};singleton:=true lazy org.argeo.eclipse.ui.ArgeoUiPlugin org.eclipse.ui;resolution:=optional,org.eclipse.rap.ui;resolution:=optional,org.eclipse.core.runtime diff --git a/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java b/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java index 816edc1be..7a5a71e39 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java +++ b/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java @@ -115,8 +115,9 @@ class ApplicationContextTracker { if (contributorBundle.getState() != Bundle.ACTIVE && contributorBundle.getState() != Bundle.STARTING) { try { - log.info("Starting bundle: " - + contributorBundle.getSymbolicName()); + if (log.isTraceEnabled()) + log.trace("Starting bundle: " + + contributorBundle.getSymbolicName()); contributorBundle.start(); } catch (BundleException e) { e.printStackTrace(); diff --git a/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java b/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java index 73fef7812..c0394fff1 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java +++ b/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java @@ -22,84 +22,45 @@ import org.eclipse.core.runtime.ILogListener; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.ui.plugin.AbstractUIPlugin; -import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; /** * The activator class controls the plug-in life cycle */ public class ArgeoUiPlugin extends AbstractUIPlugin implements ILogListener { - - // The plug-in ID public static final String PLUGIN_ID = "org.argeo.eclipse.ui"; - - private final static String SPRING_OSGI_EXTENDER = "org.springframework.osgi.extender"; - private final static Log log = LogFactory.getLog(ArgeoUiPlugin.class); - // The shared instance private static ArgeoUiPlugin plugin; - private BundleContext bundleContext; - - /** - * The constructor - */ - public ArgeoUiPlugin() { - } - - /* - * (non-Javadoc) - * - * @see - * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext - * ) - */ public void start(BundleContext context) throws Exception { super.start(context); - plugin = this; - bundleContext = context; - - Platform.addLogListener(this); - log.debug("Eclipse logging now directed to standard logging"); - - // Make sure that the Spring OSGi extender is started - Bundle osgiExtBundle = Platform.getBundle(SPRING_OSGI_EXTENDER); - if (osgiExtBundle != null) - osgiExtBundle.start(); - else - log.error("Spring OSGi Extender not found"); - + // weirdly, the start method is called twice... + if (plugin == null) { + plugin = this; + Platform.addLogListener(this); + log.debug("Eclipse logging now directed to standard logging"); + } } - /* - * (non-Javadoc) - * - * @see - * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext - * ) - */ public void stop(BundleContext context) throws Exception { - Platform.removeLogListener(this); - log.debug("Eclipse logging not directed anymore to standard logging"); - - plugin = null; - super.stop(context); + try { + // weirdly, the stop method is called twice... + if (plugin != null) { + Platform.removeLogListener(this); + log.debug("Eclipse logging not directed anymore to standard logging"); + plugin = null; + } + } finally { + super.stop(context); + } } - /** - * Returns the shared instance - * - * @return the shared instance - */ + /** Returns the shared instance */ public static ArgeoUiPlugin getDefault() { return plugin; } - public BundleContext getBundleContext() { - return bundleContext; - } - public void logging(IStatus status, String plugin) { Log pluginLog = LogFactory.getLog(plugin); Integer severity = status.getSeverity(); diff --git a/security/modules/org.argeo.security.dao.ldap/META-INF/spring/security-ldap-jcr.xml b/security/modules/org.argeo.security.dao.ldap/META-INF/spring/security-ldap-jcr.xml index 376a8e914..ef18e0419 100644 --- a/security/modules/org.argeo.security.dao.ldap/META-INF/spring/security-ldap-jcr.xml +++ b/security/modules/org.argeo.security.dao.ldap/META-INF/spring/security-ldap-jcr.xml @@ -17,6 +17,7 @@ + - - + + \ No newline at end of file diff --git a/security/modules/org.argeo.security.dao.os/META-INF/spring/security-os.xml b/security/modules/org.argeo.security.dao.os/META-INF/spring/security-os.xml index b33721883..33b4be32f 100644 --- a/security/modules/org.argeo.security.dao.os/META-INF/spring/security-os.xml +++ b/security/modules/org.argeo.security.dao.os/META-INF/spring/security-os.xml @@ -20,6 +20,7 @@ + @@ -38,8 +39,8 @@ - + init-method="init" destroy-method="destroy" depends-on="argeoDataModel"> + org.argeo.security.dao.os Commons Security DAO OS + + + + org.apache.felix + maven-bundle-plugin + + + + *, + org.argeo.jcr, + + + + + + \ No newline at end of file diff --git a/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java b/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java index f898d9df3..ce2511a57 100644 --- a/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java +++ b/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java @@ -78,12 +78,12 @@ public abstract class AbstractSecureApplication implements IApplication { .getName(); if (log.isDebugEnabled()) log.debug(username + " logged in"); - display.disposeExec(new Runnable() { - public void run() { - log.debug("Display disposed"); - logout(loginContext, username); - } - }); +// display.disposeExec(new Runnable() { +// public void run() { +// log.debug("Display disposed"); +// logout(loginContext, username); +// } +// }); try { PrivilegedAction privilegedAction = new PrivilegedAction() { diff --git a/server/modules/org.argeo.node.repofactory.jackrabbit/META-INF/spring/repofactory.xml b/server/modules/org.argeo.node.repofactory.jackrabbit/META-INF/spring/repofactory.xml index 73c9a9fc7..266f2673f 100644 --- a/server/modules/org.argeo.node.repofactory.jackrabbit/META-INF/spring/repofactory.xml +++ b/server/modules/org.argeo.node.repofactory.jackrabbit/META-INF/spring/repofactory.xml @@ -3,6 +3,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> - + + + \ No newline at end of file diff --git a/server/runtime/org.argeo.server.jackrabbit/pom.xml b/server/runtime/org.argeo.server.jackrabbit/pom.xml index dc02e48d9..62056ecf7 100644 --- a/server/runtime/org.argeo.server.jackrabbit/pom.xml +++ b/server/runtime/org.argeo.server.jackrabbit/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.commons.server @@ -75,6 +76,13 @@ 0.3.4-SNAPSHOT + + + org.eclipse.osgi + org.eclipse.osgi + provided + + org.springframework diff --git a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitContainer.java b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitContainer.java index 10c7af1d0..70bc300a1 100644 --- a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitContainer.java +++ b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitContainer.java @@ -58,6 +58,11 @@ import org.argeo.ArgeoException; import org.argeo.jcr.ArgeoNames; import org.argeo.jcr.JcrUtils; import org.argeo.security.SystemAuthentication; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.packageadmin.ExportedPackage; +import org.osgi.service.packageadmin.PackageAdmin; import org.springframework.core.io.Resource; import org.springframework.security.Authentication; import org.springframework.security.context.SecurityContextHolder; @@ -98,7 +103,7 @@ public class JackrabbitContainer implements Repository { private Boolean autocreateWorkspaces = false; - private Executor systemExecutor; + private BundleContext bundleContext; /** * Empty constructor, {@link #init()} should be called after properties have @@ -356,56 +361,133 @@ public class JackrabbitContainer implements Repository { if (isRemote()) return; - Runnable action = new Runnable() { - public void run() { - Session session = null; - try { - session = login(); - // register namespaces - if (namespaces.size() > 0) { - NamespaceHelper namespaceHelper = new NamespaceHelper( - session); - namespaceHelper.registerNamespaces(namespaces); + Session session = null; + try { + session = login(); + // register namespaces + if (namespaces.size() > 0) { + NamespaceHelper namespaceHelper = new NamespaceHelper(session); + namespaceHelper.registerNamespaces(namespaces); + } + // load CND files from classpath or as URL + for (String resUrl : cndFiles) { + boolean classpath; + // 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; + // } + + 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 '/'"); } - // load CND files from classpath or as URL - for (String resUrl : cndFiles) { - boolean classpath; - if (resUrl.startsWith("classpath:")) { - resUrl = resUrl.substring("classpath:".length()); - classpath = true; - } else if (resUrl.indexOf(':') < 0) { - classpath = true; - } else { - classpath = false; - } + // resUrl = "classpath:" + resUrl; + classpath = true; + } else { + classpath = false; + } - URL url = classpath ? getClass().getClassLoader() - .getResource(resUrl) : new URL(resUrl); + // Resource resource = + // resourceLoader.getResource(resUrl); - Reader reader = null; - try { - reader = new InputStreamReader(url.openStream()); - CndImporter - .registerNodeTypes(reader, session, true); - } finally { - IOUtils.closeQuietly(reader); + // = classpath ? new ClassPathResource(resUrl) : new + // UrlResource(resUrl); + + 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); } + } else { + url = getClass().getClassLoader().getResource(resUrl); } - } catch (Exception e) { - log.error( - "Cannot import node type definitions " + cndFiles, - e); - JcrUtils.discardQuietly(session); + if (url == null) + throw new ArgeoException("No " + resUrl + + " in the classpath," + + " make sure the containing" + + " package is visible."); + + } else { + url = new URL(resUrl); + } + + Reader reader = null; + try { + reader = new InputStreamReader(url.openStream()); + CndImporter.registerNodeTypes(reader, session, true); } finally { - JcrUtils.logoutQuietly(session); + IOUtils.closeQuietly(reader); } + + if (log.isDebugEnabled()) + log.debug("Data model " + + resUrl + + (dataModelBundle != null ? ", version " + + dataModelBundle.getVersion() + + ", bundle " + + dataModelBundle.getSymbolicName() : "")); } - }; + } catch (Exception e) { + JcrUtils.discardQuietly(session); + throw new ArgeoException("Cannot import node type definitions " + + cndFiles, e); + } finally { + JcrUtils.logoutQuietly(session); + } + + } + + /** Find which OSGi bundle provided the data model resource */ + protected Bundle findDataModelBundle(String resUrl) { + if (resUrl.startsWith("/")) + resUrl = resUrl.substring(1); + String pkg = resUrl.substring(0, resUrl.lastIndexOf('/')).replace('/', + '.'); + ServiceReference paSr = bundleContext + .getServiceReference(PackageAdmin.class.getName()); + PackageAdmin packageAdmin = (PackageAdmin) bundleContext + .getService(paSr); + + // find exported package + ExportedPackage exportedPackage = null; + ExportedPackage[] exportedPackages = packageAdmin + .getExportedPackages(pkg); + if (exportedPackages == null) + throw new ArgeoException("No exported package found for " + pkg); + for (ExportedPackage ep : exportedPackages) { + for (Bundle b : ep.getImportingBundles()) { + if (b.getBundleId() == bundleContext.getBundle().getBundleId()) { + exportedPackage = ep; + break; + } + } + } - if (systemExecutor != null) - systemExecutor.execute(action); - else - action.run(); + Bundle exportingBundle = null; + if (exportedPackage != null) { + exportingBundle = exportedPackage.getExportingBundle(); + } else { + throw new ArgeoException("No OSGi exporting package found for " + + resUrl); + } + return exportingBundle; } /* @@ -554,7 +636,11 @@ public class JackrabbitContainer implements Repository { } public void setSystemExecutor(Executor systemExecutor) { - this.systemExecutor = systemExecutor; + throw new IllegalArgumentException( + "systemExecutor is not supported anymore, use:\n" + + "\n" + + "\t\n" + + ""); } public void setRepository(Repository repository) { @@ -565,4 +651,8 @@ public class JackrabbitContainer implements Repository { Set dataModelMigrations) { this.dataModelMigrations = dataModelMigrations; } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } } diff --git a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java index 6fe5bf340..5be370fe1 100644 --- a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java +++ b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java @@ -2,6 +2,7 @@ package org.argeo.jackrabbit; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import javax.jcr.Repository; import javax.jcr.RepositoryException; @@ -14,6 +15,7 @@ import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory; import org.argeo.ArgeoException; import org.argeo.jcr.ArgeoJcrConstants; import org.argeo.jcr.DefaultRepositoryFactory; +import org.osgi.framework.BundleContext; /** Repository factory which can access remote Jackrabbit repositories */ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory @@ -21,7 +23,9 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory private final static Log log = LogFactory .getLog(JackrabbitRepositoryFactory.class); - @SuppressWarnings("rawtypes") + private BundleContext bundleContext; + + @SuppressWarnings({ "rawtypes", "unchecked" }) public Repository getRepository(Map parameters) throws RepositoryException { Repository repository = super.getRepository(parameters); if (repository != null) @@ -41,9 +45,24 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory if (repository == null) throw new ArgeoException("Remote Davex repository " + uri + " not found"); - log.info("Initialized remote Jackrabbit repository " + repository - + " from uri " + uri); + log.info("Initialized remote Jackrabbit repository from uri " + uri); + + if (parameters.containsKey(JCR_REPOSITORY_ALIAS) + && bundleContext != null) { + Properties properties = new Properties(); + properties.putAll(parameters); + bundleContext.registerService(Repository.class.getName(), + repository, properties); + log.info("Registered under alias '" + + parameters.get(JCR_REPOSITORY_ALIAS) + + "' the remote JCR repository from uri " + uri); + } return repository; } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + } diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java index fc9bcaa86..4c8ee9010 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java @@ -134,8 +134,8 @@ public abstract class ThreadBoundJcrSessionFactory { if (activeSessions.size() == 0) return; - if (log.isDebugEnabled()) - log.debug("Cleaning up " + activeSessions.size() + if (log.isTraceEnabled()) + log.trace("Cleaning up " + activeSessions.size() + " active JCR sessions..."); deactivate(); @@ -172,8 +172,8 @@ public abstract class ThreadBoundJcrSessionFactory { Session session = activeSessions.get(thread.getId()); activeSessions.remove(thread.getId()); session.logout(); - if (log.isDebugEnabled()) - log.debug("Cleaned up JCR session (userID=" + if (log.isTraceEnabled()) + log.trace("Cleaned up JCR session (userID=" + session.getUserID() + ") from dead thread " + thread.getId()); }