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 org.codehaus.plexus.PlexusContainer;
20  import org.codehaus.plexus.classworlds.realm.ClassRealm;
21  import org.codehaus.plexus.component.factory.AbstractComponentFactory;
22  import org.codehaus.plexus.component.factory.ComponentInstantiationException;
23  import org.codehaus.plexus.component.repository.ComponentDescriptor;
24  
25  import java.lang.reflect.Modifier;
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   * @version $Id$
33   */
34  public class JavaComponentFactory
35      extends AbstractComponentFactory
36  {
37      public String getId()
38      {
39          return "java";
40      }
41  
42      public Object newInstance( ComponentDescriptor componentDescriptor,
43                                 ClassRealm classRealm,
44                                 PlexusContainer container )
45          throws ComponentInstantiationException
46      {
47          Class implementationClass = null;
48  
49          try
50          {
51              String implementation = componentDescriptor.getImplementation();
52  
53              implementationClass = classRealm.loadClass( implementation );
54  
55              int modifiers = implementationClass.getModifiers();
56  
57              if ( Modifier.isInterface( modifiers ) )
58              {
59                  throw new ComponentInstantiationException(
60                      "Cannot instantiate implementation '" + implementation + "' because the class is a interface." );
61              }
62  
63              if ( Modifier.isAbstract( modifiers ) )
64              {
65                  throw new ComponentInstantiationException(
66                      "Cannot instantiate implementation '" + implementation + "' because the class is abstract." );
67              }
68  
69              Object instance = implementationClass.newInstance();
70  
71              return instance;
72          }
73          catch ( InstantiationException e )
74          {
75              //PLXAPI: most probably cause of this is the implementation class not having
76              //        a default constructor.
77              throw makeException( classRealm, componentDescriptor, implementationClass, e );
78          }
79          catch ( ClassNotFoundException e )
80          {
81              throw makeException( classRealm, componentDescriptor, implementationClass, e );
82          }
83          catch ( IllegalAccessException e )
84          {
85              throw makeException( classRealm, componentDescriptor, implementationClass, e );
86          }
87          catch ( LinkageError e )
88          {
89              throw makeException( classRealm, componentDescriptor, implementationClass, e );
90          }
91      }
92  
93      private ComponentInstantiationException makeException( ClassRealm componentClassRealm,
94                                                             ComponentDescriptor componentDescriptor,
95                                                             Class implementationClass,
96                                                             Throwable e )
97      {
98          // ----------------------------------------------------------------------
99          // Display the realm when there is an error, We should probably return a string here so we
100         // can incorporate this into the error message for easy debugging.
101         // ----------------------------------------------------------------------
102 
103         String msg;
104 
105         if ( componentClassRealm == null )
106         {
107             msg = "classRealm is null for " + componentDescriptor;
108         }
109         else
110         {
111             msg = "Could not instantiate component: " + componentDescriptor.getHumanReadableKey() + " realm: "
112                 + componentClassRealm.getId();
113         }
114 
115         return new ComponentInstantiationException( msg, e );
116     }
117 }