You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2007/07/10 19:33:41 UTC

svn commit: r555014 - in /roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger: business/GuiceWebloggerProvider.java business/WebloggerFactory.java business/WebloggerProvider.java config/roller.properties

Author: agilliland
Date: Tue Jul 10 10:33:38 2007
New Revision: 555014

URL: http://svn.apache.org/viewvc?view=rev&rev=555014
Log:
Make weblogger backend fully pluggable like planet is.  This adds in a WebloggerProvider interface which is used to provide instances of the Weblogger class in any way the implementor wants.  The default provider uses Guice.  Providers are pluggable through the static config file using the 'weblogger.provider.class' property.

Added:
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/GuiceWebloggerProvider.java
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerProvider.java
Modified:
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerFactory.java
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/config/roller.properties

Added: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/GuiceWebloggerProvider.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/GuiceWebloggerProvider.java?view=auto&rev=555014
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/GuiceWebloggerProvider.java (added)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/GuiceWebloggerProvider.java Tue Jul 10 10:33:38 2007
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.business;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import org.apache.roller.weblogger.config.WebloggerConfig;
+
+
+/**
+ * A Guice specific implementation of a WebloggerProvider.
+ */
+public class GuiceWebloggerProvider implements WebloggerProvider {
+    
+    // Guice injector
+    private final Injector injector;
+    
+    // maintain our own singleton instance of Weblogger
+    private Weblogger webloggerInstance = null;
+    
+    
+    /**
+     * Instantiate a new GuiceWebloggerProvider using default guice module 
+     * configured in WebloggerConfig via 'guice.backend.module' property.
+     */
+    public GuiceWebloggerProvider() {
+        
+        String moduleClassname = WebloggerConfig.getProperty("guice.backend.module");
+        if(moduleClassname == null) {
+            throw new NullPointerException("unable to lookup default guice module via property 'guice.backend.module'");
+        }
+        
+        try {
+            Class moduleClass = Class.forName(moduleClassname);
+            Module module = (Module)moduleClass.newInstance();
+            injector = Guice.createInjector(module);
+        } catch (Throwable e) {                
+            // Fatal misconfiguration, cannot recover
+            throw new RuntimeException("Error instantiating backend module " + moduleClassname, e);
+        }
+    }
+    
+    
+    /**
+     * Instantiate a new GuiceWebloggerProvider using the given Guice module.
+     *
+     * @param moduleClassname The full classname of the Guice module to use.
+     */
+    public GuiceWebloggerProvider(String moduleClassname) {
+        
+        if(moduleClassname == null) {
+            throw new NullPointerException("moduleClassname cannot be null");
+        }
+        
+        try {
+            Class moduleClass = Class.forName(moduleClassname);
+            Module module = (Module)moduleClass.newInstance();
+            injector = Guice.createInjector(module);
+        } catch (Throwable e) {                
+            // Fatal misconfiguration, cannot recover
+            throw new RuntimeException("Error instantiating backend module " + moduleClassname, e);
+        }
+    }
+    
+    
+    /**
+     * @inheritDoc
+     */
+    public void bootstrap() {
+        webloggerInstance =  injector.getInstance(Weblogger.class);
+    }
+    
+    
+    /**
+     * @inheritDoc
+     */
+    public Weblogger getWeblogger() {
+        return webloggerInstance;
+    }
+    
+}

Modified: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerFactory.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerFactory.java?view=diff&rev=555014&r1=555013&r2=555014
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerFactory.java (original)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerFactory.java Tue Jul 10 10:33:38 2007
@@ -18,9 +18,6 @@
 
 package org.apache.roller.weblogger.business;
 
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.roller.weblogger.business.startup.WebloggerStartup;
@@ -34,23 +31,8 @@
     
     private static final Log log = LogFactory.getLog(WebloggerFactory.class);
     
-    // a reference to the bootstrapped Weblogger instance
-    private static Weblogger rollerInstance = null;
-    
-    private static Injector injector = null;
-  
-    
-    static {
-        String moduleClassname = WebloggerConfig.getProperty("guice.backend.module");
-        try {
-            Class moduleClass = Class.forName(moduleClassname);
-            Module module = (Module)moduleClass.newInstance();
-            injector = Guice.createInjector(module);
-        } catch (Throwable e) {
-            // Fatal misconfiguration, cannot recover
-            throw new RuntimeException("Error instantiating backend module" + moduleClassname, e);
-        }
-    } 
+    // our configured weblogger provider
+    private static WebloggerProvider webloggerProvider = null;
     
     
     // non-instantiable
@@ -63,7 +45,7 @@
      * True if bootstrap process has been completed, False otherwise.
      */
     public static boolean isBootstrapped() {
-        return (rollerInstance != null);
+        return (webloggerProvider != null);
     }
     
     
@@ -74,43 +56,88 @@
      * @throws IllegalStateException If the app has not been properly bootstrapped yet.
      */
     public static final Weblogger getWeblogger() {
-        if (rollerInstance == null) {
+        if (webloggerProvider == null) {
             throw new IllegalStateException("Roller Weblogger has not been bootstrapped yet");
         }
         
-        return rollerInstance;
+        return webloggerProvider.getWeblogger();
     }
     
     
     /**
-     * Access to Guice injector so that developers can add new injected objects.
+     * Bootstrap the Roller Weblogger business tier, uses default WebloggerProvider.
+     *
+     * Bootstrapping the application effectively instantiates all the necessary
+     * pieces of the business tier and wires them together so that the app is 
+     * ready to run.
+     *
+     * @throws IllegalStateException If the app has not been properly prepared yet.
+     * @throws BootstrapException If an error happens during the bootstrap process.
      */
-    public static Injector getInjector() {
-        return injector;
+    public static final void bootstrap() throws BootstrapException {
+        
+        // if the app hasn't been properly started so far then bail
+        if (!WebloggerStartup.isPrepared()) {
+            throw new IllegalStateException("Cannot bootstrap until application has been properly prepared");
+        }
+        
+        // lookup our default provider and instantiate it
+        WebloggerProvider defaultProvider;
+        String providerClassname = WebloggerConfig.getProperty("weblogger.provider.class");
+        if(providerClassname != null) {
+            try {
+                Class providerClass = Class.forName(providerClassname);
+                defaultProvider = (WebloggerProvider) providerClass.newInstance();
+            } catch (Exception ex) {
+                throw new BootstrapException("Error instantiating default provider: "+providerClassname, ex);
+            }
+        } else {
+            throw new NullPointerException("No provider specified in config property 'weblogger.provider.class'");
+        }
+
+        // now just bootstrap using our default provider
+        bootstrap(defaultProvider);
     }
     
     
     /**
-     * Bootstrap the Weblogger Weblogger business tier.
-     * 
+     * Bootstrap the Roller Weblogger business tier, uses specified WebloggerProvider.
+     *
      * Bootstrapping the application effectively instantiates all the necessary
      * pieces of the business tier and wires them together so that the app is 
      * ready to run.
-     * 
+     *
+     * @param provider A WebloggerProvider to use for bootstrapping.
      * @throws IllegalStateException If the app has not been properly prepared yet.
      * @throws BootstrapException If an error happens during the bootstrap process.
      */
-    public static final void bootstrap() throws BootstrapException {
+    public static final void bootstrap(WebloggerProvider provider) 
+            throws BootstrapException {
         
         // if the app hasn't been properly started so far then bail
         if (!WebloggerStartup.isPrepared()) {
             throw new IllegalStateException("Cannot bootstrap until application has been properly prepared");
         }
         
+        if (provider == null) {
+            throw new NullPointerException("WebloggerProvider is null");
+        }
+        
         log.info("Bootstrapping Roller Weblogger business tier");
         
-        rollerInstance = injector.getInstance(Weblogger.class);
-            
+        log.info("Weblogger Provider = "+provider.getClass().getName());
+        
+        // save reference to provider
+        webloggerProvider = provider;
+        
+        // bootstrap weblogger provider
+        webloggerProvider.bootstrap();
+        
+        // make sure we are all set
+        if(webloggerProvider.getWeblogger() == null) {
+            throw new BootstrapException("Bootstrapping failed, Weblogger instance is null");
+        }
+        
         log.info("Roller Weblogger business tier successfully bootstrapped");
     }
     

Added: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerProvider.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerProvider.java?view=auto&rev=555014
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerProvider.java (added)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/business/WebloggerProvider.java Tue Jul 10 10:33:38 2007
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.business;
+
+
+/**
+ * Provides access to a Weblogger instance.
+ */
+public interface WebloggerProvider {
+    
+    /**
+     * Trigger bootstrapping.
+     */
+    public void bootstrap() throws BootstrapException;
+    
+    
+    /**
+     * Get a Weblogger instance.
+     */
+    public Weblogger getWeblogger();
+    
+}

Modified: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/config/roller.properties
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/config/roller.properties?view=diff&rev=555014&r1=555013&r2=555014
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/config/roller.properties (original)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/config/roller.properties Tue Jul 10 10:33:38 2007
@@ -532,6 +532,9 @@
 # Pluggable backend, page and editor plugins
 #-----------------------------------------------------------------------------
 
+# Backend Provider
+weblogger.provider.class=org.apache.roller.weblogger.business.GuiceWebloggerProvider
+
 # Backend Guice module
 guice.backend.module=org.apache.roller.weblogger.business.jpa.JPAWebloggerModule