jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1383976 - /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/
Date Wed, 12 Sep 2012 14:40:59 GMT
Author: mduerig
Date: Wed Sep 12 14:40:58 2012
New Revision: 1383976

URL: http://svn.apache.org/viewvc?rev=1383976&view=rev
Log:
JCR-3420: Improving Jackrabbit integration within OSGi and other managed environment
delegate instance creation to a bean factory 
Thanks Chetan Mehrotra for the patch

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfigVisitor.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanFactory.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/NoOpConfigVisitor.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfigurationParser.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java?rev=1383976&r1=1383975&r2=1383976&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
Wed Sep 12 14:40:58 2012
@@ -68,6 +68,11 @@ public class BeanConfig {
         BeanConfig.class.getClassLoader();
 
     /**
+     * Factory to create instance from Bean className
+     */
+    private BeanFactory instanceFactory = new SimpleBeanFactory();
+
+    /**
      * The current class loader used by this instance to create instances of
      * configured classes.
      */
@@ -148,6 +153,14 @@ public class BeanConfig {
     }
 
     /**
+     *
+     * @param instanceFactory the {@link BeanFactory} to use to create bean instance
+     */
+    public void setInstanceFactory(BeanFactory instanceFactory) {
+        this.instanceFactory = instanceFactory;
+    }
+
+    /**
      * Returns the class name of the configured bean.
      *
      * @return class name of the bean
@@ -174,59 +187,35 @@ public class BeanConfig {
     @SuppressWarnings("unchecked")
     public <T> T newInstance(Class<T> klass) throws ConfigurationException {
         String cname = getClassName();
-        try {
-            Class<?> objectClass = Class.forName(cname, true, getClassLoader());
-            if (!klass.isAssignableFrom(objectClass)) {
+        // Instantiate the object using the default constructor
+        Object instance = instanceFactory.newInstance(klass,this);
+        Class<?> objectClass = instance.getClass();
+
+        // Set all configured bean properties
+        Map<String, Method> setters = getSetters(objectClass);
+        Enumeration<?> enumeration = properties.propertyNames();
+        while (enumeration.hasMoreElements()) {
+            String name = enumeration.nextElement().toString();
+            Method setter = setters.get(name);
+            if (setter != null) {
+                if (setter.getAnnotation(Deprecated.class) != null) {
+                    log.warn("Parameter {} of {} has been deprecated",
+                            name, cname);
+                }
+                String value = properties.getProperty(name);
+                setProperty(instance, name, setter, value);
+            } else if (validate) {
                 throw new ConfigurationException(
                         "Configured class " + cname
-                        + " does not implement " + klass.getName()
-                        + ". Please fix the repository configuration.");
-            }
-            if (objectClass.getAnnotation(Deprecated.class) != null) {
-                log.warn("{} has been deprecated", cname);
-            }
-
-            // Instantiate the object using the default constructor
-            Object instance = objectClass.newInstance();
-
-            // Set all configured bean properties
-            Map<String, Method> setters = getSetters(objectClass);
-            Enumeration<?> enumeration = properties.propertyNames();
-            while (enumeration.hasMoreElements()) {
-                String name = enumeration.nextElement().toString();
-                Method setter = setters.get(name);
-                if (setter != null) {
-                    if (setter.getAnnotation(Deprecated.class) != null) {
-                        log.warn("Parameter {} of {} has been deprecated",
-                                name, cname);
-                    }
-                    String value = properties.getProperty(name);
-                    setProperty(instance, name, setter, value);
-                } else if (validate) {
-                    throw new ConfigurationException(
-                            "Configured class " + cname
-                            + " does not contain a property named " + name);
-                }
-            }
-
-            if (instance instanceof DatabaseAware) {
-                ((DatabaseAware) instance).setConnectionFactory(connectionFactory);
+                        + " does not contain a property named " + name);
             }
+        }
 
-            return (T) instance;
-        } catch (ClassNotFoundException e) {
-            throw new ConfigurationException(
-                    "Configured bean implementation class " + cname
-                    + " was not found.", e);
-        } catch (InstantiationException e) {
-            throw new ConfigurationException(
-                    "Configured bean implementation class " + cname
-                    + " can not be instantiated.", e);
-        } catch (IllegalAccessException e) {
-            throw new ConfigurationException(
-                    "Configured bean implementation class " + cname
-                    + " is protected.", e);
+        if (instance instanceof DatabaseAware) {
+            ((DatabaseAware) instance).setConnectionFactory(connectionFactory);
         }
+
+        return (T) instance;
     }
 
     private Map<String, Method> getSetters(Class<?> klass) {

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfigVisitor.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfigVisitor.java?rev=1383976&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfigVisitor.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfigVisitor.java
Wed Sep 12 14:40:58 2012
@@ -0,0 +1,25 @@
+/*
+ * 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.jackrabbit.core.config;
+
+/**
+ * A BeanConfig visitor which is invoked upon creation of BeanConfig before any
+ * instance is created from that bean configuration
+ */
+public interface BeanConfigVisitor {
+    void visit(BeanConfig config);
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfigVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfigVisitor.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanFactory.java?rev=1383976&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanFactory.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanFactory.java
Wed Sep 12 14:40:58 2012
@@ -0,0 +1,22 @@
+/*
+ * 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.jackrabbit.core.config;
+
+
+public interface BeanFactory {
+    Object newInstance(Class<?> klass, BeanConfig config) throws ConfigurationException;
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanFactory.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java?rev=1383976&r1=1383975&r2=1383976&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java
Wed Sep 12 14:40:58 2012
@@ -109,13 +109,7 @@ public class ConfigurationParser {
         // Bean configuration element
         Element element = getElement(parent, name);
 
-        // Bean implementation class
-        String className = getAttribute(element, CLASS_ATTRIBUTE);
-
-        // Bean properties
-        Properties properties = parseParameters(element);
-
-        return new BeanConfig(className, properties);
+        return parseBeanConfig(element);
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/NoOpConfigVisitor.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/NoOpConfigVisitor.java?rev=1383976&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/NoOpConfigVisitor.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/NoOpConfigVisitor.java
Wed Sep 12 14:40:58 2012
@@ -0,0 +1,24 @@
+/*
+ * 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.jackrabbit.core.config;
+
+public class NoOpConfigVisitor implements BeanConfigVisitor{
+    @Override
+    public void visit(BeanConfig config) {
+
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/NoOpConfigVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/NoOpConfigVisitor.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfigurationParser.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfigurationParser.java?rev=1383976&r1=1383975&r2=1383976&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfigurationParser.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfigurationParser.java
Wed Sep 12 14:40:58 2012
@@ -203,6 +203,10 @@ public class RepositoryConfigurationPars
      */
     protected final ConnectionFactory connectionFactory;
 
+    protected BeanFactory beanFactory = new SimpleBeanFactory();
+
+    protected BeanConfigVisitor configVisitor = new NoOpConfigVisitor();
+
     /**
      * Element specifying the class of principals used to retrieve the userID
      * in the 'class' attribute.
@@ -355,6 +359,8 @@ public class RepositoryConfigurationPars
     protected BeanConfig parseBeanConfig(Element parent, String name) throws ConfigurationException
{
         BeanConfig cfg = super.parseBeanConfig(parent, name);
         cfg.setConnectionFactory(connectionFactory);
+        cfg.setInstanceFactory(beanFactory);
+        configVisitor.visit(cfg);
         return cfg;
     }
 
@@ -365,6 +371,8 @@ public class RepositoryConfigurationPars
     protected BeanConfig parseBeanConfig(Element element) throws ConfigurationException {
         BeanConfig cfg = super.parseBeanConfig(element);
         cfg.setConnectionFactory(connectionFactory);
+        cfg.setInstanceFactory(beanFactory);
+        configVisitor.visit(cfg);
         return cfg;
     }
 
@@ -724,8 +732,7 @@ public class RepositoryConfigurationPars
                     if (IMPORT_PNI_ELEMENT.equals(child.getNodeName()) ||
                             IMPORT_PPI_ELEMENT.equals(child.getNodeName()) ||
                             IMPORT_PII_ELEMENT.equals(child.getNodeName())) {
-                        String className = getAttribute((Element) child, CLASS_ATTRIBUTE);
-                        BeanConfig bc = new BeanConfig(className, parseParameters((Element)
child));
+                        BeanConfig bc = parseBeanConfig((Element) child);
                         bc.setValidate(false);
                         protectedItemImporters.add(bc);
                     } // else: some other entry -> ignore.
@@ -1085,4 +1092,12 @@ public class RepositoryConfigurationPars
         };
     }
 
+
+    public void setBeanFactory(BeanFactory beanFactory) {
+        this.beanFactory = beanFactory;
+    }
+
+    public void setConfigVisitor(BeanConfigVisitor configVisitor) {
+        this.configVisitor = configVisitor;
+    }
 }

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java?rev=1383976&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java
Wed Sep 12 14:40:58 2012
@@ -0,0 +1,57 @@
+/*
+ * 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.jackrabbit.core.config;
+
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SimpleBeanFactory implements BeanFactory {
+    private Logger log = LoggerFactory.getLogger(getClass());
+
+    @Override
+    public Object newInstance(Class<?> klass, BeanConfig config) throws ConfigurationException{
+        String cname = config.getClassName();
+        try {
+            Class<?> objectClass = Class.forName(cname, true, config.getClassLoader());
+            if (!klass.isAssignableFrom(objectClass)) {
+                throw new ConfigurationException(
+                        "Configured class " + cname
+                                + " does not implement " + klass.getName()
+                                + ". Please fix the repository configuration.");
+            }
+            if (objectClass.getAnnotation(Deprecated.class) != null) {
+                log.warn("{} has been deprecated", cname);
+            }
+
+            // Instantiate the object using the default constructor
+            return objectClass.newInstance();
+        } catch (ClassNotFoundException e) {
+            throw new ConfigurationException(
+                    "Configured bean implementation class " + cname
+                            + " was not found.", e);
+        } catch (InstantiationException e) {
+            throw new ConfigurationException(
+                    "Configured bean implementation class " + cname
+                            + " can not be instantiated.", e);
+        } catch (IllegalAccessException e) {
+            throw new ConfigurationException(
+                    "Configured bean implementation class " + cname
+                            + " is protected.", e);
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message