KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > model > RollerFactory


1 package org.roller.model;
2
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.roller.RollerException;
7 import org.roller.config.RollerConfig;
8 import org.roller.util.StringUtils;
9
10 /**
11  * This is primarily a static holder for whatever Roller implementation is
12  * being used. RollerContext should call setRoller(ServletContext) during its
13  * initialization so that the Roller implementation can be instantiated.
14  * Likewise, RollerContext.getRoller() should be replaced with calls to
15  * RollerFactory.getRoller(). This should prevent us any direct ties to
16  * Castor and permit for experimentation* with other persistence frameworks.
17  *
18  * @author llavandowska
19  * @author Allen Gilliland
20  */

21 public abstract class RollerFactory
22 {
23     private static final String JavaDoc DEFAULT_IMPL =
24         "org.roller.business.hibernate.HibernateRollerImpl";
25     
26     private static Roller rollerInstance = null;
27
28     private static Log mLogger =
29         LogFactory.getFactory().getInstance(RollerFactory.class);
30             
31     /**
32      * Let's just be doubling certain this class cannot be instantiated.
33      * @see java.lang.Object#Object()
34      */

35     private RollerFactory()
36     {
37         // hello all you beautiful people
38
}
39     
40     /**
41      * Static accessor for the instance of Roller
42      * held by this class.
43      *
44      * @return Roller
45      */

46     public static Roller getRoller()
47     {
48         // check to see if we need to instantiate
49
if(rollerInstance == null) {
50             // lookup value for the roller classname to use
51
String JavaDoc roller_classname =
52                     RollerConfig.getProperty("persistence.roller.classname");
53             
54             // now call setRoller ourselves
55
RollerFactory.setRoller(roller_classname);
56         }
57         
58         return rollerInstance;
59     }
60     
61     
62     /**
63      * Construct the actual Roller implemenation for this instance.
64      *
65      * Use reflection to call the implementation's instantiate() method.
66      * Should this fail (either no implementation is specified or
67      * instantiate() throws an Exception) then the DEFAULT_IMPL will be tried.
68      * If this should fail then we are in trouble :/
69      *
70      * @param roller_classname The name of the Roller implementation class
71      * to instantiate.
72      */

73     public static void setRoller(String JavaDoc roller_classname)
74     {
75         
76         if (StringUtils.isEmpty( roller_classname ))
77             roller_classname = DEFAULT_IMPL;
78         
79         try
80         {
81             Class JavaDoc rollerClass = Class.forName(roller_classname);
82             java.lang.reflect.Method JavaDoc instanceMethod =
83                     rollerClass.getMethod("instantiate", (Class JavaDoc[])null);
84             
85             // do the invocation
86
rollerInstance = (Roller)
87                 instanceMethod.invoke(rollerClass, (Class JavaDoc[])null);
88             
89             mLogger.info("Using Roller Impl: " + roller_classname);
90         }
91         catch (Exception JavaDoc e)
92         {
93             // uh oh
94
mLogger.error("Error instantiating " + roller_classname, e);
95             try
96             {
97                 // if we didn't already try DEFAULT_IMPL then try it now
98
if( ! DEFAULT_IMPL.equals(roller_classname))
99                 {
100                     mLogger.info("** Trying DEFAULT_IMPL "+DEFAULT_IMPL+" **");
101                     
102                     Class JavaDoc rollerClass = Class.forName(DEFAULT_IMPL);
103                     java.lang.reflect.Method JavaDoc instanceMethod =
104                             rollerClass.getMethod("instantiate", (Class JavaDoc[])null);
105                     
106                     // do the invocation
107
rollerInstance = (Roller)
108                         instanceMethod.invoke(rollerClass, (Class JavaDoc[])null);
109                 }
110                 else
111                 {
112                     // we just do this so that the logger gets the message
113
throw new Exception JavaDoc("Doh! Couldn't instantiate a roller class");
114                 }
115                 
116             }
117             catch (Exception JavaDoc re)
118             {
119                 mLogger.fatal("Failed to instantiate fallback roller impl", re);
120             }
121         }
122         
123     }
124     
125     
126     /**
127      * Set Roller to be returned by factory.
128      */

129     public static void setRoller(Roller roller)
130     {
131         if (roller != null) rollerInstance = roller;
132     }
133 }
134
Popular Tags