cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r240335 - in /cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr: AbstractRepository.java JNDIRepository.java JackrabbitRepository.java
Date Fri, 26 Aug 2005 20:26:29 GMT
Author: vgritsenko
Date: Fri Aug 26 13:26:24 2005
New Revision: 240335

URL: http://svn.apache.org/viewcvs?rev=240335&view=rev
Log:
release resources

Modified:
    cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/AbstractRepository.java
    cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JNDIRepository.java
    cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JackrabbitRepository.java

Modified: cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/AbstractRepository.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/AbstractRepository.java?rev=240335&r1=240334&r2=240335&view=diff
==============================================================================
--- cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/AbstractRepository.java (original)
+++ cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/AbstractRepository.java Fri Aug 26
13:26:24 2005
@@ -15,17 +15,8 @@
  */
 package org.apache.cocoon.jcr;
 
-import java.util.Map;
-
-import javax.jcr.Credentials;
-import javax.jcr.LoginException;
-import javax.jcr.NoSuchWorkspaceException;
-import javax.jcr.Repository;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-import javax.jcr.SimpleCredentials;
-
 import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.component.Component;
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
@@ -37,10 +28,7 @@
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.thread.ThreadSafe;
-import org.apache.excalibur.source.SourceResolver;
-import org.apache.excalibur.source.impl.FileSource;
-// import org.apache.cocoon.components.variables.VariableResolver;
-// import org.apache.cocoon.components.variables.VariableResolverFactory;
+
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
@@ -48,31 +36,44 @@
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.sitemap.PatternException;
 
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.excalibur.source.impl.FileSource;
+
+import javax.jcr.Credentials;
+import javax.jcr.LoginException;
+import javax.jcr.NoSuchWorkspaceException;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+import java.util.Map;
+
 /**
  * Base class for JCR (aka <a
- * href="http://www.jcp.org/en/jsr/detail?id=170">JSR-170</a>) repositories as
- * Cocoon components. The main purpose of this class is to allow repository
+ * href="http://www.jcp.org/en/jsr/detail?id=170">JSR-170</a>) repository as
+ * a Cocoon component. The main purpose of this class is to allow repository
  * credentials to be specified in the component's configuration, so that the
  * application code just has to call <code>repository.login()</code>.
+ *
  * <p>
  * There is no Cocoon-specific role for this component: "<code>javax.jcr.Repository</code>"
  * should be used.
+ *
  * <p>
  * The configuration of this class, inherited by its subclasses, is as follows:
  *
  * <pre>
- *
  *    &lt;jcr-repository&gt;
  *      &lt;jaas src="context://samples/jaas.config"/&gt;
  *      &lt;credentials login="<i>expression</i>" password="<i>expression</i>"/&gt;
  *      ... other specific configuration...
  *    &lt;/jcr-repository&gt;
- *
  * </pre>
  *
  * Login and password can be specified using the sitemap expression language,
  * thus allowing the use of input modules to compute their values, e.g.
- * <code>password="{session-attr:jcr-password}"</code>
+ * <code>password="{session-attr:jcr-password}"</code>.
+ *
  * <p>
  * <code>&lt;credentials&gt;</code> is optional. If not specified, the
  * application must explicitely supply credentials when calling
@@ -80,7 +81,19 @@
  *
  * @version $Id$
  */
-public class AbstractRepository extends AbstractLogEnabled implements Repository, ThreadSafe,
Contextualizable, Serviceable, Configurable, Disposable {
+public abstract class AbstractRepository extends AbstractLogEnabled
+                                         implements Repository, ThreadSafe, Contextualizable,
Serviceable,
+                                                    Configurable, Disposable, Component {
+
+    /**
+     * Role which shall be used for JCR repository implementations.
+     */
+    public static final String ROLE = "javax.jcr.Repository";
+
+    /**
+     * The request attribute in which the JCR session is stored
+     */
+    public static final String JCR_SESSION_REQUEST_ATTRIBUTE = "jcr-session";
 
     protected ServiceManager manager;
 
@@ -91,14 +104,10 @@
     // Defined by the portal block :-(
     // protected VariableResolverFactory variableFactory;
 
-    protected VariableResolver loginResolver = null;
+    protected VariableResolver loginResolver;
 
-    protected VariableResolver passwordResolver = null;
+    protected VariableResolver passwordResolver;
 
-    /**
-     * The request attribute in which the JCR session is stored
-     */
-    public static final String JCR_SESSION_REQUEST_ATTRIBUTE = "jcr-session";
 
     // =============================================================================================
     // Avalon lifecycle
@@ -110,25 +119,30 @@
 
     public void service(ServiceManager manager) throws ServiceException {
         this.manager = manager;
-        // this.variableFactory =
-        // (VariableResolverFactory)manager.lookup(VariableResolverFactory.ROLE);
     }
 
     public void configure(Configuration config) throws ConfigurationException {
+        // FIXME FIXME FIXME Hack setting system jaas property
         Configuration jaas = config.getChild("jaas", false);
         if (jaas != null) {
             String jaasURI = jaas.getAttribute("src");
+            SourceResolver resolver = null;
             FileSource jaasSrc = null;
             try {
-                SourceResolver resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+                resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
                 jaasSrc = (FileSource) resolver.resolveURI(jaasURI);
+                if (System.getProperty("java.security.auth.login.config") == null) {
+                    System.setProperty("java.security.auth.login.config", jaasSrc.getFile().getAbsolutePath());
+                } else {
+                    // WARNING: java.security.auth.login.config has already been set
+                }
             } catch (Exception e) {
                 throw new ConfigurationException("Cannot resolve jaas URI: " + jaasURI);
-            }
-            if (System.getProperty("java.security.auth.login.config") == null) {
-                System.setProperty("java.security.auth.login.config", jaasSrc.getFile().getAbsolutePath());
-            } else {
-                // WARNING: java.security.auth.login.config has already been set
+            } finally {
+                if (jaasSrc != null) {
+                    resolver.release(jaasSrc);
+                }
+                this.manager.release(resolver);
             }
         } else {
             throw new ConfigurationException("Path of jaas.config has not been configured");
@@ -147,22 +161,28 @@
             try {
                 this.passwordResolver = VariableResolverFactory.getResolver(password, this.manager);
             } catch (PatternException e) {
-                if (this.loginResolver instanceof Disposable)
+                if (this.loginResolver instanceof Disposable) {
                     ((Disposable) this.loginResolver).dispose();
-                // this.variableFactory.release(this.loginResolver);
+                }
+                this.loginResolver = null;
                 throw new ConfigurationException("Invalid expression for 'password' at "
+ credentials.getLocation(), e);
             }
         }
     }
 
     public void dispose() {
-        if (this.loginResolver instanceof Disposable)
+        this.context = null;
+        this.delegate = null;
+
+        if (this.loginResolver instanceof Disposable) {
             ((Disposable) this.loginResolver).dispose();
-        if (this.passwordResolver instanceof Disposable)
+        }
+        this.loginResolver = null;
+        if (this.passwordResolver instanceof Disposable) {
             ((Disposable) this.passwordResolver).dispose();
-        // this.variableFactory.release(this.loginResolver);
-        // this.variableFactory.release(this.passwordResolver);
-        // this.manager.release(this.variableFactory);
+        }
+        this.passwordResolver = null;
+        this.manager = null;
     }
 
     // =============================================================================================
@@ -177,7 +197,8 @@
         return delegate.getDescriptorKeys();
     }
 
-    public Session login() throws LoginException, NoSuchWorkspaceException, RepositoryException
{
+    public Session login()
+    throws LoginException, NoSuchWorkspaceException, RepositoryException {
         Session session = getCachedSession(null);
 
         if (session == null) {
@@ -189,7 +210,8 @@
         return session;
     }
 
-    public Session login(Credentials creds) throws LoginException, NoSuchWorkspaceException,
RepositoryException {
+    public Session login(Credentials creds)
+    throws LoginException, NoSuchWorkspaceException, RepositoryException {
         Session session = getCachedSession(null);
 
         if (session == null) {
@@ -200,7 +222,8 @@
         return session;
     }
 
-    public Session login(Credentials creds, String workspace) throws LoginException, NoSuchWorkspaceException,
RepositoryException {
+    public Session login(Credentials creds, String workspace)
+    throws LoginException, NoSuchWorkspaceException, RepositoryException {
         Session session = getCachedSession(workspace);
 
         if (session == null) {
@@ -211,7 +234,8 @@
         return session;
     }
 
-    public Session login(String workspace) throws LoginException, NoSuchWorkspaceException,
RepositoryException {
+    public Session login(String workspace)
+    throws LoginException, NoSuchWorkspaceException, RepositoryException {
         Session session = getCachedSession(workspace);
 
         if (session == null) {

Modified: cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JNDIRepository.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JNDIRepository.java?rev=240335&r1=240334&r2=240335&view=diff
==============================================================================
--- cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JNDIRepository.java (original)
+++ cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JNDIRepository.java Fri Aug 26 13:26:24
2005
@@ -29,14 +29,25 @@
 
     public void configure(Configuration config) throws ConfigurationException {
         super.configure(config);
+
         Configuration rsrcConfig = config.getChild("jndi-resource");
         String name = rsrcConfig.getAttribute("name");
 
+        InitialContext ctx = null;
         try {
-            InitialContext ctx = new InitialContext();
-            this.delegate = (Repository)ctx.lookup(name);
+            ctx = new InitialContext();
+            this.delegate = (Repository) ctx.lookup(name);
         } catch (NamingException e) {
             throw new ConfigurationException("Cannot lookup JNDI entry '" + name + "'", e);
+        } finally {
+            if (ctx != null) {
+                try {
+                    ctx.close();
+                } catch (NamingException ignored) {
+                    // Ignored
+                }
+            }
         }
     }
+
 }

Modified: cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JackrabbitRepository.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JackrabbitRepository.java?rev=240335&r1=240334&r2=240335&view=diff
==============================================================================
--- cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JackrabbitRepository.java (original)
+++ cocoon/blocks/jcr/trunk/java/org/apache/cocoon/jcr/JackrabbitRepository.java Fri Aug 26
13:26:24 2005
@@ -28,17 +28,16 @@
 /**
  * JackrabbitRepository is a JCR repository component based on <a
  * href="http://incubator.apache.org/jackrabbit">Jackrabbit</a>
+ *
  * <p>
  * The configuration is as follows:
  *
  * <pre>
- *
  *    &lt;jcr-repository&gt;
  *      &lt;credentials login="<i>expression</i>" password="<i>expression</i>"/&gt;
  *      &lt;home src="file://path/to/repository"/&gt;
  *      &lt;configuration src="resource://your/application/jcr/repository.xml"/&gt;
  *    &lt;/jcr-repository&gt;
- *
  * </pre>
  *
  * The <code>home</code> URI points to the base location of the repository,
@@ -51,7 +50,6 @@
 public class JackrabbitRepository extends AbstractRepository {
 
     public void configure(Configuration config) throws ConfigurationException {
-
         super.configure(config);
 
         String homeURI = config.getChild("home").getAttribute("src");
@@ -59,38 +57,41 @@
         String configURI = config.getChild("configuration").getAttribute("src");
 
         // having to release sources is a major PITA...
-
+        SourceResolver resolver = null;
         try {
-            SourceResolver resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
 
             // Ensure home uri is a file and absolutize it
             Source homeSrc = resolver.resolveURI(homeURI);
-            if (homeSrc instanceof FileSource) {
+            try {
+                if (!(homeSrc instanceof FileSource)) {
+                    throw new ConfigurationException("Home path '" + homeURI + "' should
map to a file, at " +
+                                                     config.getChild("home").getLocation());
+                }
                 homePath = ((FileSource) homeSrc).getFile().getAbsolutePath();
+            } finally {
                 resolver.release(homeSrc);
-            } else {
-                resolver.release(homeSrc);
-                throw new ConfigurationException("Home path '" + homeURI + "' should map
to a file, at " + config.getChild("home").getLocation());
             }
 
-            // Load the config
+            // Load repository configuration
             Source configSrc = resolver.resolveURI(configURI);
-            InputSource is = SourceUtil.getInputSource(configSrc);
-
             RepositoryConfig repoConfig;
             try {
+                InputSource is = SourceUtil.getInputSource(configSrc);
                 repoConfig = RepositoryConfig.create(is, homePath);
             } finally {
                 resolver.release(configSrc);
             }
 
-            // And create the repo
+            // And create the repository
             this.delegate = RepositoryImpl.create(repoConfig);
 
         } catch (ConfigurationException ce) {
             throw ce;
         } catch (Exception e) {
             throw new ConfigurationException("Cannot access configuration information at
" + config.getLocation(), e);
+        } finally {
+            this.manager.release(resolver);
         }
     }
 }



Mime
View raw message