KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > assemblerbroker > util > java > JavaBaseFactory


1 package org.apache.turbine.services.assemblerbroker.util.java;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.apache.turbine.Turbine;
30 import org.apache.turbine.TurbineConstants;
31 import org.apache.turbine.modules.Assembler;
32 import org.apache.turbine.modules.GenericLoader;
33 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
34 import org.apache.turbine.util.ObjectUtils;
35
36 /**
37  * A screen factory that attempts to load a java class from
38  * the module packages defined in the TurbineResource.properties.
39  *
40  * @author <a HREF="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
41  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
42  * @version $Id: JavaBaseFactory.java,v 1.8.2.4 2004/08/24 21:11:52 henning Exp $
43  */

44 public abstract class JavaBaseFactory
45     implements AssemblerFactory
46 {
47     /** A vector of packages. */
48     private static List JavaDoc packages =
49         Turbine.getConfiguration().getList(TurbineConstants.MODULE_PACKAGES);
50
51     /** Logging */
52     protected Log log = LogFactory.getLog(this.getClass());
53
54     /**
55      * A cache for previously obtained Class instances, which we keep in order
56      * to reduce the Class.forName() overhead (which can be sizable).
57      */

58     private Map JavaDoc classCache = Collections.synchronizedMap(new HashMap JavaDoc());
59     
60     static
61     {
62         ObjectUtils.addOnce(packages, GenericLoader.getBasePackage());
63     }
64
65     /**
66      * Get an Assembler.
67      *
68      * @param packageName java package name
69      * @param name name of the requested Assembler
70      * @return an Assembler
71      */

72     public Assembler getAssembler(String JavaDoc packageName, String JavaDoc name)
73     {
74         Assembler assembler = null;
75
76         log.debug("Class Fragment is " + name);
77
78         if (StringUtils.isNotEmpty(name))
79         {
80             for (Iterator JavaDoc it = packages.iterator(); it.hasNext();)
81             {
82                 StringBuffer JavaDoc className = new StringBuffer JavaDoc();
83
84                 className.append(it.next());
85                 className.append('.');
86                 className.append(packageName);
87                 className.append('.');
88                 className.append(name);
89
90                 log.debug("Trying " + className);
91
92                 try
93                 {
94                     Class JavaDoc servClass = (Class JavaDoc) classCache.get(className);
95                     if(servClass == null)
96                     {
97                         servClass = Class.forName(className.toString());
98                         classCache.put(className, servClass);
99                     }
100                     assembler = (Assembler) servClass.newInstance();
101                     break; // for()
102
}
103                 catch (ClassNotFoundException JavaDoc cnfe)
104                 {
105                     // Do this so we loop through all the packages.
106
log.debug(className + ": Not found");
107                 }
108                 catch (NoClassDefFoundError JavaDoc ncdfe)
109                 {
110                     // Do this so we loop through all the packages.
111
log.debug(className + ": No Class Definition found");
112                 }
113                 catch (ClassCastException JavaDoc cce)
114                 {
115                     // This means trouble!
116
// Alternatively we can throw this exception so
117
// that it will appear on the client browser
118
log.error("Could not load "+className, cce);
119                     break; // for()
120
}
121                 catch (InstantiationException JavaDoc ine)
122                 {
123                     // This means trouble!
124
// Alternatively we can throw this exception so
125
// that it will appear on the client browser
126
log.error("Could not load "+className, ine);
127                     break; // for()
128
}
129                 catch (IllegalAccessException JavaDoc ilae)
130                 {
131                     // This means trouble!
132
// Alternatively we can throw this exception so
133
// that it will appear on the client browser
134
log.error("Could not load "+className, ilae);
135                     break; // for()
136
}
137                 // With ClassCastException, InstantiationException we hit big problems
138
}
139         }
140         log.debug("Returning: " + assembler);
141
142         return assembler;
143     }
144 }
145
Popular Tags