View Javadoc
1   package org.codehaus.plexus.component.factory.java;
2   
3   /*
4    * Copyright 2001-2006 Codehaus 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.lang.reflect.Modifier;
20  
21  import org.codehaus.plexus.PlexusContainer;
22  import org.codehaus.plexus.classworlds.realm.ClassRealm;
23  import org.codehaus.plexus.component.factory.AbstractComponentFactory;
24  import org.codehaus.plexus.component.factory.ComponentInstantiationException;
25  import org.codehaus.plexus.component.repository.ComponentDescriptor;
26  
27  /**
28   * Component Factory for components written in Java Language which have default no parameter constructor
29   *
30   * @author Jason van Zyl
31   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
32   */
33  public class JavaComponentFactory extends AbstractComponentFactory {
34      public String getId() {
35          return "java";
36      }
37  
38      public Object newInstance(ComponentDescriptor componentDescriptor, ClassRealm classRealm, PlexusContainer container)
39              throws ComponentInstantiationException {
40          Class implementationClass = null;
41  
42          try {
43              String implementation = componentDescriptor.getImplementation();
44  
45              implementationClass = classRealm.loadClass(implementation);
46  
47              int modifiers = implementationClass.getModifiers();
48  
49              if (Modifier.isInterface(modifiers)) {
50                  throw new ComponentInstantiationException(
51                          "Cannot instantiate implementation '" + implementation + "' because the class is a interface.");
52              }
53  
54              if (Modifier.isAbstract(modifiers)) {
55                  throw new ComponentInstantiationException(
56                          "Cannot instantiate implementation '" + implementation + "' because the class is abstract.");
57              }
58  
59              Object instance = implementationClass.newInstance();
60  
61              return instance;
62          } catch (InstantiationException e) {
63              // PLXAPI: most probably cause of this is the implementation class not having
64              //        a default constructor.
65              throw makeException(classRealm, componentDescriptor, implementationClass, e);
66          } catch (ClassNotFoundException e) {
67              throw makeException(classRealm, componentDescriptor, implementationClass, e);
68          } catch (IllegalAccessException e) {
69              throw makeException(classRealm, componentDescriptor, implementationClass, e);
70          } catch (LinkageError e) {
71              throw makeException(classRealm, componentDescriptor, implementationClass, e);
72          }
73      }
74  
75      private ComponentInstantiationException makeException(
76              ClassRealm componentClassRealm,
77              ComponentDescriptor componentDescriptor,
78              Class implementationClass,
79              Throwable e) {
80          // ----------------------------------------------------------------------
81          // Display the realm when there is an error, We should probably return a string here so we
82          // can incorporate this into the error message for easy debugging.
83          // ----------------------------------------------------------------------
84  
85          String msg;
86  
87          if (componentClassRealm == null) {
88              msg = "classRealm is null for " + componentDescriptor;
89          } else {
90              msg = "Could not instantiate component: " + componentDescriptor.getHumanReadableKey() + " realm: "
91                      + componentClassRealm.getId();
92          }
93  
94          return new ComponentInstantiationException(msg, e);
95      }
96  }