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 534527 2007-05-02 16:10:59Z 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 className = new StringBuffer();
86  
87                  className.append(it.next());
88                  className.append('.');
89                  className.append(packageName);
90                  className.append('.');
91                  className.append(name);
92  
93                  log.debug("Trying " + className);
94  
95                  try
96                  {
97                      Class servClass = (Class) classCache.get(className);
98                      if(servClass == null)
99                      {
100                         servClass = Class.forName(className.toString());
101                         classCache.put(className, servClass);
102                     }
103                     assembler = (Assembler) servClass.newInstance();
104                     break; // for()
105                 }
106                 catch (ClassNotFoundException cnfe)
107                 {
108                     // Do this so we loop through all the packages.
109                     log.debug(className + ": Not found");
110                 }
111                 catch (NoClassDefFoundError ncdfe)
112                 {
113                     // Do this so we loop through all the packages.
114                     log.debug(className + ": No Class Definition found");
115                 }
116                 catch (ClassCastException cce)
117                 {
118                     // This means trouble!
119                     // Alternatively we can throw this exception so
120                     // that it will appear on the client browser
121                     log.error("Could not load "+className, cce);
122                     break; // for()
123                 }
124                 catch (InstantiationException ine)
125                 {
126                     // This means trouble!
127                     // Alternatively we can throw this exception so
128                     // that it will appear on the client browser
129                     log.error("Could not load "+className, ine);
130                     break; // for()
131                 }
132                 catch (IllegalAccessException ilae)
133                 {
134                     // This means trouble!
135                     // Alternatively we can throw this exception so
136                     // that it will appear on the client browser
137                     log.error("Could not load "+className, ilae);
138                     break; // for()
139                 }
140                 // With ClassCastException, InstantiationException we hit big problems
141             }
142         }
143         log.debug("Returning: " + assembler);
144 
145         return assembler;
146     }
147 }