incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r589658 - in /incubator/sling/trunk: content/ jackrabbit-api/src/main/java/org/apache/sling/jcr/ jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/ jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/loader/
Date Mon, 29 Oct 2007 15:00:28 GMT
Author: cziegeler
Date: Mon Oct 29 08:00:27 2007
New Revision: 589658

URL: http://svn.apache.org/viewvc?rev=589658&view=rev
Log:
A bundle can contain a Sling-Namespaces manifest entries which contains a jcr prefix/namespace
mapping which is ensured by Sling (SLING-86)
The content bundle defines the "sling" prefix with the appropriate namespace.

Added:
    incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/NamespaceMapper.java
  (with props)
Modified:
    incubator/sling/trunk/content/pom.xml
    incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/AbstractSlingRepository.java
    incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPool.java
    incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPoolManager.java
    incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/loader/Loader.java

Modified: incubator/sling/trunk/content/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/content/pom.xml?rev=589658&r1=589657&r2=589658&view=diff
==============================================================================
--- incubator/sling/trunk/content/pom.xml (original)
+++ incubator/sling/trunk/content/pom.xml Mon Oct 29 08:00:27 2007
@@ -112,6 +112,9 @@
                         <Sling-Nodetypes>
                             SLING-INF/nodetypes/BaseContent.cnd
                         </Sling-Nodetypes>
+                        <Sling-Namespaces>
+                            sling=http://jackrabbit.apache.org/sling/1.0
+                        </Sling-Namespaces>
                     </instructions>
                 </configuration>
             </plugin>

Modified: incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/AbstractSlingRepository.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/AbstractSlingRepository.java?rev=589658&r1=589657&r2=589658&view=diff
==============================================================================
--- incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/AbstractSlingRepository.java
(original)
+++ incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/AbstractSlingRepository.java
Mon Oct 29 08:00:27 2007
@@ -134,7 +134,7 @@
             int maxActiveSessionsWait = this.getIntProperty(properties,
                 PARAM_MAX_ACTIVE_SESSIONS_WAIT);
 
-            this.poolManager = new SessionPoolManager(this.getDelegatee(),
+            this.poolManager = new SessionPoolManager(this.getDelegatee(), this.loader,
                 maxActiveSessions, maxActiveSessionsWait, maxIdleSessions);
         }
 
@@ -354,6 +354,8 @@
             case BundleEvent.UNINSTALLED:
                 this.loader.unregisterBundle(event.getBundle());
                 break;
+            case BundleEvent.UPDATED:
+                this.loader.updateBundle(event.getBundle());
         }
     }
 

Added: incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/NamespaceMapper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/NamespaceMapper.java?rev=589658&view=auto
==============================================================================
--- incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/NamespaceMapper.java
(added)
+++ incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/NamespaceMapper.java
Mon Oct 29 08:00:27 2007
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.internal;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+/**
+ * This interface is used to customize the namespace mapping of
+ * a session.
+ */
+public interface NamespaceMapper {
+
+    void defineNamespacePrefixes(Session session)
+    throws RepositoryException;
+}

Propchange: incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/NamespaceMapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/NamespaceMapper.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Modified: incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPool.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPool.java?rev=589658&r1=589657&r2=589658&view=diff
==============================================================================
--- incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPool.java
(original)
+++ incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPool.java
Mon Oct 29 08:00:27 2007
@@ -690,8 +690,7 @@
      * @param delegatee The <code>Session</code> to wrap as a pooled session.
      * @see PooledSession
      */
-    private PooledSession createPooledSession(Session delegatee) {
-
+    private PooledSession createPooledSession(Session delegatee) throws RepositoryException
{
         PooledSession pooledSession = new PooledSession(this, delegatee);
 
         // keep the pooled session
@@ -770,7 +769,7 @@
      *          <code>null</code> if no session for the workspace is available
      *          from the pool.
      */
-    private Session getFromPool(String workSpaceName) {
+    private Session getFromPool(String workSpaceName) throws RepositoryException {
         // check with pool
         for (;;) {
 

Modified: incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPoolManager.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPoolManager.java?rev=589658&r1=589657&r2=589658&view=diff
==============================================================================
--- incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPoolManager.java
(original)
+++ incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/SessionPoolManager.java
Mon Oct 29 08:00:27 2007
@@ -44,9 +44,11 @@
  */
 public class SessionPoolManager {
 
-    private Repository repository;
+    private final Repository repository;
 
-    private Map<String, SessionPool> sessionPools;
+    private final Map<String, SessionPool> sessionPools;
+
+    private final NamespaceMapper namespaceMapper;
 
     /**
      * The maximum number of active sessions per session pool, that is per user.
@@ -63,8 +65,11 @@
      */
     private int poolMaxActiveSessionsWait;
 
-    public SessionPoolManager(Repository repository, int maxActiveSessions,
-            int maxActiveSessionsWait, int maxIdleSessions) {
+    public SessionPoolManager(Repository repository,
+            NamespaceMapper mapper,
+            int maxActiveSessions,
+            int maxActiveSessionsWait,
+            int maxIdleSessions) {
 
         this.repository = repository;
         this.sessionPools = new HashMap<String, SessionPool>();
@@ -75,6 +80,8 @@
         this.poolMaxActiveSessions = maxActiveSessions;
         this.poolMaxActiveSessionsWait = maxActiveSessionsWait;
         this.poolMaxIdleSessions = maxIdleSessions;
+
+        this.namespaceMapper = mapper;
     }
 
     public void dispose() {
@@ -101,22 +108,31 @@
             throws LoginException, NoSuchWorkspaceException,
             RepositoryException {
 
+        Session result = null;
+
         // get the session pool for the credentials
         if (credentials instanceof SimpleCredentials) {
             SimpleCredentials simple = (SimpleCredentials) credentials;
             SessionPool pool = this.getPool(simple);
             if (pool != null) {
-                return pool.acquireSession(simple, workspace);
+                result = pool.acquireSession(simple, workspace);
             }
         }
 
-        // direct session, if no pool is available for the credentials
-        return this.getRepository().login(credentials, workspace);
+        if ( result == null ) {
+            // direct session, if no pool is available for the credentials
+            result = this.getRepository().login(credentials, workspace);
+        }
+        if ( result != null && this.namespaceMapper != null ) {
+            this.namespaceMapper.defineNamespacePrefixes(result);
+        }
+        return result;
     }
 
     Session impersonate(Session baseSession, Credentials credentials)
             throws LoginException, NoSuchWorkspaceException,
             RepositoryException {
+        Session result = null;
 
         // assert base session is live
         if (!baseSession.isLive()) {
@@ -127,12 +143,19 @@
         if (credentials instanceof SimpleCredentials) {
             SessionPool pool = this.getPool((SimpleCredentials) credentials);
             if (pool != null) {
-                return pool.acquireSession(baseSession, credentials);
+                result = pool.acquireSession(baseSession, credentials);
             }
         }
 
-        // no pool available for the credentials, use direct session
-        return baseSession.impersonate(credentials);
+        if ( result == null ) {
+            // no pool available for the credentials, use direct session
+            result = baseSession.impersonate(credentials);
+        }
+
+        if ( result != null && this.namespaceMapper != null ) {
+            this.namespaceMapper.defineNamespacePrefixes(result);
+        }
+        return result;
     }
 
     // ---------- Session Pooling ----------------------------------------------

Modified: incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/loader/Loader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/loader/Loader.java?rev=589658&r1=589657&r2=589658&view=diff
==============================================================================
--- incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/loader/Loader.java
(original)
+++ incubator/sling/trunk/jackrabbit-api/src/main/java/org/apache/sling/jcr/internal/loader/Loader.java
Mon Oct 29 08:00:27 2007
@@ -21,16 +21,21 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.StringTokenizer;
 
+import javax.jcr.NamespaceException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 import org.apache.sling.jcr.AbstractSlingRepository;
 import org.apache.sling.jcr.NodeTypeLoader;
+import org.apache.sling.jcr.internal.NamespaceMapper;
 import org.osgi.framework.Bundle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,10 +44,12 @@
 /**
  * The <code>Loader</code> TODO
  */
-public class Loader {
+public class Loader implements NamespaceMapper {
 
     public static final String NODETYPES_BUNDLE_HEADER = "Sling-Nodetypes";
 
+    public static final String NAMESPACES_BUNDLE_HEADER = "Sling-Namespaces";
+
     /** default log */
     private static final Logger log = LoggerFactory.getLogger(Loader.class);
 
@@ -51,6 +58,9 @@
     // bundles whose registration failed and should be retried
     private List<Bundle> delayedBundles;
 
+    /** Namespace prefix table. */
+    private final Map<Long, NamespaceEntry[]> namespaceTable = new HashMap<Long,
NamespaceEntry[]>();
+
     public Loader(AbstractSlingRepository repository) {
         this.slingRepository = repository;
         this.delayedBundles = new LinkedList<Bundle>();
@@ -65,6 +75,7 @@
     }
 
     public void registerBundle(Bundle bundle) {
+        this.registerNamespaces(bundle);
         if (this.registerBundleInternal(bundle, false)) {
             // handle delayed bundles, might help now
             int currentSize = -1;
@@ -84,11 +95,55 @@
     }
 
     public void unregisterBundle(Bundle bundle) {
+        this.unregisterNamespaces(bundle);
         if ( this.delayedBundles.contains(bundle) ) {
             this.delayedBundles.remove(bundle);
         }
     }
 
+    public void updateBundle(Bundle bundle) {
+        this.unregisterNamespaces(bundle);
+        this.registerNamespaces(bundle);
+    }
+
+    /**
+     * Register namespaces defined in the bundle in the namespace table.
+     * @param bundle The bundle.
+     */
+    protected void registerNamespaces(Bundle bundle) {
+        final String definition = (String) bundle.getHeaders().get(NAMESPACES_BUNDLE_HEADER);
+        if ( definition != null ) {
+            log.debug("registerNamespaces: Bundle {} tries to register: {}",
+                    bundle.getSymbolicName(), definition);
+            final StringTokenizer st = new StringTokenizer(definition, ",");
+            final List<NamespaceEntry>entries = new ArrayList<NamespaceEntry>();
+
+            while ( st.hasMoreTokens() ) {
+                final String token = st.nextToken().trim();
+                int pos = token.indexOf('=');
+                if ( pos == -1 ) {
+                    log.warn("registerNamespaces: Bundle {} has an invalid namespace manifest
header entry: {}",
+                            bundle.getSymbolicName(), token);
+                } else {
+                    final String prefix = token.substring(0, pos).trim();
+                    final String namespace = token.substring(pos+1).trim();
+                    entries.add(new NamespaceEntry(prefix, namespace));
+                }
+            }
+            if ( entries.size() > 0 ) {
+                this.namespaceTable.put(bundle.getBundleId(), entries.toArray(new NamespaceEntry[entries.size()]));
+            }
+        }
+    }
+
+    /**
+     * Unregister namespaces defined in the bundle.
+     * @param bundle The bundle.
+     */
+    protected void unregisterNamespaces(Bundle bundle) {
+        this.namespaceTable.remove(bundle.getBundleId());
+    }
+
     private boolean registerBundleInternal (Bundle bundle, boolean isRetry) {
         try {
             if (this.registerNodeTypes(bundle, isRetry)) {
@@ -181,6 +236,58 @@
     private void ungetSession(Session session) {
         if (session != null) {
             session.logout();
+        }
+    }
+
+    public void defineNamespacePrefixes(Session session)
+    throws RepositoryException {
+        final Iterator<NamespaceEntry[]> iter = this.namespaceTable.values().iterator();
+        while ( iter.hasNext() ) {
+            final NamespaceEntry[] entries = iter.next();
+            for(int i=0; i<entries.length; i++) {
+
+                // the namespace prefixing is a little bit tricky:
+                String mappedPrefix = null;
+                // first, we check if the namespace is registered with a prefix
+                try {
+                    mappedPrefix = session.getNamespacePrefix(entries[i].namespace);
+                } catch (NamespaceException ne) {
+                    // the namespace is not registered yet, so we should do this
+                    // can we directly use the desired prefix?
+                    mappedPrefix = entries[i].prefix + "_new";
+                    try {
+                        session.getNamespaceURI(entries[i].prefix);
+                    } catch (NamespaceException ne2) {
+                        // as an exception occured we can directly use the new prefix
+                        mappedPrefix = entries[i].prefix;
+                    }
+                    session.getWorkspace().getNamespaceRegistry().registerNamespace(mappedPrefix,
entries[i].namespace);
+                }
+                // do we have to remap?
+                if ( mappedPrefix != null && !mappedPrefix.equals(entries[i].prefix
) ) {
+                    // check if the prefix is already used?
+                    String oldUri = null;
+                    try {
+                        oldUri = session.getNamespaceURI(entries[i].prefix);
+                        session.setNamespacePrefix(entries[i].prefix + "_old", oldUri);
+                    } catch (NamespaceException ne) {
+                        // ignore: prefix is not used
+                    }
+                    // finally set prefix
+                    session.setNamespacePrefix(entries[i].prefix, entries[i].namespace);
+                }
+            }
+        }
+    }
+
+    public static class NamespaceEntry {
+
+        public final String prefix;
+        public final String namespace;
+
+        public NamespaceEntry(String p, String n) {
+            this.prefix = p;
+            this.namespace = n;
         }
     }
 }



Mime
View raw message