KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.model;
19
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.roller.RollerException;
24 import org.apache.roller.config.RollerConfig;
25 import org.apache.commons.lang.StringUtils;
26
27 /**
28  * This is primarily a static holder for whatever Roller implementation is
29  * being used. RollerContext should call setRoller(ServletContext) during its
30  * initialization so that the Roller implementation can be instantiated.
31  * Likewise, RollerContext.getRoller() should be replaced with calls to
32  * RollerFactory.getRoller(). This should prevent us any direct ties to
33  * Castor and permit for experimentation* with other persistence frameworks.
34  *
35  * @author llavandowska
36  * @author Allen Gilliland
37  */

38 public abstract class RollerFactory
39 {
40     private static final String JavaDoc DEFAULT_IMPL =
41         "org.apache.roller.business.hibernate.HibernateRollerImpl";
42     
43     private static Roller rollerInstance = null;
44
45     private static Log mLogger =
46         LogFactory.getFactory().getInstance(RollerFactory.class);
47             
48     /**
49      * Let's just be doubling certain this class cannot be instantiated.
50      * @see java.lang.Object#Object()
51      */

52     private RollerFactory()
53     {
54         // hello all you beautiful people
55
}
56     
57     /**
58      * Static accessor for the instance of Roller
59      * held by this class.
60      *
61      * @return Roller
62      */

63     public static Roller getRoller()
64     {
65         // check to see if we need to instantiate
66
if(rollerInstance == null) {
67             // lookup value for the roller classname to use
68
String JavaDoc roller_classname =
69                     RollerConfig.getProperty("persistence.roller.classname");
70             
71             // now call setRoller ourselves
72
RollerFactory.setRoller(roller_classname);
73         }
74         
75         return rollerInstance;
76     }
77     
78     
79     /**
80      * Construct the actual Roller implemenation for this instance.
81      *
82      * Use reflection to call the implementation's instantiate() method.
83      * Should this fail (either no implementation is specified or
84      * instantiate() throws an Exception) then the DEFAULT_IMPL will be tried.
85      * If this should fail then we are in trouble :/
86      *
87      * @param roller_classname The name of the Roller implementation class
88      * to instantiate.
89      */

90     public static void setRoller(String JavaDoc roller_classname)
91     {
92         
93         if (StringUtils.isEmpty( roller_classname ))
94             roller_classname = DEFAULT_IMPL;
95         
96         try
97         {
98             Class JavaDoc rollerClass = Class.forName(roller_classname);
99             java.lang.reflect.Method JavaDoc instanceMethod =
100                     rollerClass.getMethod("instantiate", (Class JavaDoc[])null);
101             
102             // do the invocation
103
rollerInstance = (Roller)
104                 instanceMethod.invoke(rollerClass, (Object JavaDoc[])null);
105             
106             mLogger.info("Using Roller Impl: " + roller_classname);
107         }
108         catch (Exception JavaDoc e)
109         {
110             // uh oh
111
mLogger.error("Error instantiating " + roller_classname, e);
112             try
113             {
114                 // if we didn't already try DEFAULT_IMPL then try it now
115
if( ! DEFAULT_IMPL.equals(roller_classname))
116                 {
117                     mLogger.info("** Trying DEFAULT_IMPL "+DEFAULT_IMPL+" **");
118                     
119                     Class JavaDoc rollerClass = Class.forName(DEFAULT_IMPL);
120                     java.lang.reflect.Method JavaDoc instanceMethod =
121                             rollerClass.getMethod("instantiate", (Class JavaDoc[])null);
122                     
123                     // do the invocation
124
rollerInstance = (Roller)
125                         instanceMethod.invoke(rollerClass, (Object JavaDoc[])null);
126                 }
127                 else
128                 {
129                     // we just do this so that the logger gets the message
130
throw new Exception JavaDoc("Doh! Couldn't instantiate a roller class");
131                 }
132                 
133             }
134             catch (Exception JavaDoc re)
135             {
136                 mLogger.fatal("Failed to instantiate fallback roller impl", re);
137             }
138         }
139         
140     }
141     
142     
143     /**
144      * Set Roller to be returned by factory.
145      */

146     public static void setRoller(Roller roller)
147     {
148         if (roller != null) rollerInstance = roller;
149     }
150 }
151
Popular Tags