Return-Path: Delivered-To: apmail-incubator-roller-commits-archive@www.apache.org Received: (qmail 61463 invoked from network); 22 Jan 2007 18:43:42 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 22 Jan 2007 18:43:42 -0000 Received: (qmail 73940 invoked by uid 500); 22 Jan 2007 18:43:49 -0000 Delivered-To: apmail-incubator-roller-commits-archive@incubator.apache.org Received: (qmail 73911 invoked by uid 500); 22 Jan 2007 18:43:49 -0000 Mailing-List: contact roller-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: roller-dev@incubator.apache.org Delivered-To: mailing list roller-commits@incubator.apache.org Received: (qmail 73900 invoked by uid 99); 22 Jan 2007 18:43:48 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Jan 2007 10:43:48 -0800 X-ASF-Spam-Status: No, hits=-8.6 required=10.0 tests=ALL_TRUSTED,INFO_TLD,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Jan 2007 10:43:42 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 607AE1A981A; Mon, 22 Jan 2007 10:42:35 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: roller-commits@incubator.apache.org From: agilliland@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070122184235.607AE1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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; + } + }