View Javadoc

1   package org.apache.turbine.services.assemblerbroker.util.java;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import org.apache.turbine.Turbine;
33  import org.apache.turbine.TurbineConstants;
34  import org.apache.turbine.modules.Assembler;
35  import org.apache.turbine.modules.GenericLoader;
36  import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
37  import org.apache.turbine.util.ObjectUtils;
38  
39  /***
40   * A screen factory that attempts to load a java class from
41   * the module packages defined in the TurbineResource.properties.
42   *
43   * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
44   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
45   * @version $Id: JavaBaseFactory.java 677309 2008-07-16 15:35:24Z tv $
46   */
47  public abstract class JavaBaseFactory
48      implements AssemblerFactory
49  {
50      /**</package-summary/html">A vector of packages/ *//package-summary.html">em>* A vector of packages. */
51      privateong> static List packages =
52          Turbine.getConfiguration().getList(TurbineConstants.MODULE_PACKAGES);
53  
54      /*** Logging */
55      protected Log log = LogFactory.getLog(this.getClass());
56  
57      /***
58       * A cache for previously obtained Class instances, which we keep in order
59       * to reduce the Class.forName() overhead (which can be sizable).
60       */
61      private Map classCache = Collections.synchronizedMap(new HashMap());
62  
63      static
64      {
65          ObjectUtils.addOnce(packages, GenericLoader.getBasePackage());
66      }
67  
68      /***
69       * Get an Assembler.
70       *
71       * @param packageName java package name
72       * @param name name of the requested Assembler
73       * @return an Assembler
74       */
75      publicAssembler getAssembler(String packageName, String name)/package-summary.html">ong> Assembler getAssembler(String packageName, String name)
76      {
77          Assembler assembler = null;
78  
79          log.debug("Class Fragment is " + name);
80  
81          if (StringUtils.isNotEmpty(name))
82          {
83              for (Iterator it = packages.iterator(); it.hasNext();)
84              {
85                  StringBuffer sb = new StringBuffer();
86  
87                  sb.append(it.next()).append('.').append(packageName).append('.').append(name);
88                  
89                  String className = sb.toString();
90  
91                  log.debug("Trying " + className);
92  
93                  try
94                  {
95                      Class servClass = (Class) classCache.get(className);
96                      if(servClass == null)
97                      {
98                          servClass = Class.forName(className.toString());
99                          classCache.put(className, servClass);
100                     }
101                     assembler = (Assembler) servClass.newInstance();
102                     break; // for()
103                 }
104                 catch (ClassNotFoundException cnfe)
105                 {
106                     // Do this so we loop through all the packages.
107                     log.debug(className + ": Not found");
108                 }
109                 catch (NoClassDefFoundError ncdfe)
110                 {
111                     // Do this so we loop through all the packages.
112                     log.debug(className + ": No Class Definition found");
113                 }
114                 catch (ClassCastException cce)
115                 {
116                     // This means trouble!
117                     // Alternatively we can throw this exception so
118                     // that it will appear on the client browser
119                     log.error("Could not load "+className, cce);
120                     break; // for()
121                 }
122                 catch (InstantiationException ine)
123                 {
124                     // This means trouble!
125                     // Alternatively we can throw this exception so
126                     // that it will appear on the client browser
127                     log.error("Could not load "+className, ine);
128                     break; // for()
129                 }
130                 catch (IllegalAccessException ilae)
131                 {
132                     // This means trouble!
133                     // Alternatively we can throw this exception so
134                     // that it will appear on the client browser
135                     log.error("Could not load "+className, ilae);
136                     break; // for()
137                 }
138                 // With ClassCastException, InstantiationException we hit big problems
139             }
140         }
141         log.debug("Returning: " + assembler);
142 
143         return assembler;
144     }
145 }