roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r498741 - /incubator/roller/trunk/apps/planet/src/java/org/apache/roller/planet/business/PlanetFactory.java
Date Mon, 22 Jan 2007 18:42:35 GMT
Author: agilliland
Date: Mon Jan 22 10:42:34 2007
New Revision: 498741

URL: http://svn.apache.org/viewvc?view=rev&rev=498741
Log:
fix up PlanetFactory to ensure there is no possible double instantiation of Planet implemenation.


Modified:
    incubator/roller/trunk/apps/planet/src/java/org/apache/roller/planet/business/PlanetFactory.java

Modified: incubator/roller/trunk/apps/planet/src/java/org/apache/roller/planet/business/PlanetFactory.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/apps/planet/src/java/org/apache/roller/planet/business/PlanetFactory.java?view=diff&rev=498741&r1=498740&r2=498741
==============================================================================
--- incubator/roller/trunk/apps/planet/src/java/org/apache/roller/planet/business/PlanetFactory.java
(original)
+++ incubator/roller/trunk/apps/planet/src/java/org/apache/roller/planet/business/PlanetFactory.java
Mon Jan 22 10:42:34 2007
@@ -15,69 +15,38 @@
  * copyright in this work, please see the NOTICE file in the top level
  * directory of this distribution.
  */
-package org.apache.roller.planet.business;
 
+package org.apache.roller.planet.business;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.roller.planet.config.PlanetConfig; 
 import org.apache.commons.lang.StringUtils;
 
+
 /**
  * Instantiates planet implementation.
  */
 public abstract class PlanetFactory {
+    
     private static final String DEFAULT_IMPL =
         "org.apache.roller.planet.business.hibernate.HibernatePlanetImpl";
     
     private static Planet planetInstance = null;
     
-    private static Log mLogger =
-            LogFactory.getFactory().getInstance(PlanetFactory.class);
+    private static Log log = LogFactory.getLog(PlanetFactory.class);
     
-    /**
-     * Let's just be doubling certain this class cannot be instantiated.
-     * @see java.lang.Object#Object()
-     */
-    private PlanetFactory() {
-        // hello planetary citizens
-    }
     
     /**
-     * Static accessor for the instance of Planet
-     * held by this class.
-     *
-     * @return Planet
+     * We instantiate the Planet implementation statically at class loading time
+     * to make absolutely sure that there is no way for our singleton to get
+     * instantiated twice.
      */
-    public static Planet getPlanet() {
-        // check to see if we need to instantiate
-        if(planetInstance == null) {
-            // lookup value for the planet classname to use
-            String planet_classname =
-                    PlanetConfig.getProperty("persistence.planet.classname");
-            
-            // now call setPlanet ourselves
-            PlanetFactory.setPlanet(planet_classname);
-        }
-        
-        return planetInstance;
-    }
-    
-    
-    /**
-     * Construct the actual Planet implemenation for this instance.
-     *
-     * Use reflection to call the implementation's instantiate() method.
-     * Should this fail (either no implementation is specified or
-     * instantiate() throws an Exception) then the DEFAULT_IMPL will be tried.
-     * If this should fail then we are in trouble :/
-     *
-     * @param planet_classname The name of the Planet implementation class
-     * to instantiate.
-     */
-    public static void setPlanet(String planet_classname) {
-        
-        if (StringUtils.isEmpty( planet_classname ))
+    static {
+        // lookup value for the roller classname to use
+        String planet_classname =
+                PlanetConfig.getProperty("persistence.planet.classname");
+        if(planet_classname == null || planet_classname.trim().length() < 1)
             planet_classname = DEFAULT_IMPL;
         
         try {
@@ -86,42 +55,50 @@
                     planetClass.getMethod("instantiate", (Class[])null);
             
             // do the invocation
-            planetInstance = (Planet)
-                instanceMethod.invoke(planetClass, (Object[])null);
+            planetInstance = (Planet) instanceMethod.invoke(planetClass, (Object[])null);
+            
+            log.info("Using Planet Impl: " + planet_classname);
+            
+        } catch (Throwable e) {
             
-            mLogger.info("Using Planet Impl: " + planet_classname);
-        } catch (Exception e) {
             // uh oh
-            mLogger.error("Error instantiating " + planet_classname, e);
+            log.error("Error instantiating " + planet_classname, e);
+            
             try {
                 // if we didn't already try DEFAULT_IMPL then try it now
                 if( ! DEFAULT_IMPL.equals(planet_classname)) {
-                    mLogger.info("** Trying DEFAULT_IMPL "+DEFAULT_IMPL+" **");
                     
-                    Class planetClass = Class.forName(DEFAULT_IMPL);
+                    log.info("** Trying DEFAULT_IMPL "+DEFAULT_IMPL+" **");
+                    
+                    Class rollerClass = Class.forName(DEFAULT_IMPL);
                     java.lang.reflect.Method instanceMethod =
-                            planetClass.getMethod("instantiate", (Class[])null);
+                            rollerClass.getMethod("instantiate", (Class[])null);
                     
                     // do the invocation
-                    planetInstance = (Planet)
-                        instanceMethod.invoke(planetClass, (Object[])null);
+                    planetInstance = (Planet) instanceMethod.invoke(rollerClass, (Object[])null);
                 } else {
                     // we just do this so that the logger gets the message
-                    throw new Exception("Doh! Couldn't instantiate the planet class");
+                    throw new Exception("Doh! Couldn't instantiate a planet class");
                 }
                 
             } catch (Exception re) {
-                mLogger.fatal("Failed to instantiate fallback planet impl", re);
+                log.fatal("Failed to instantiate fallback planet impl", re);
             }
         }
-        
     }
     
     
     /**
-     * Set Planet to be returned by factory.
+     * Let's just be doubling certain this class cannot be instantiated.
+     * @see java.lang.Object#Object()
      */
-    public static void setPlanet(Planet planet) {
-        if (planet != null) planetInstance = planet;
+    private PlanetFactory() {
+        // hello planetary citizens
     }
+    
+    
+    public static Planet getPlanet() {
+        return planetInstance;
+    }
+    
 }



Mime
View raw message