KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > main > AbstractBuilder


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

17
18 package org.apache.avalon.repository.main ;
19
20 import java.lang.reflect.Constructor JavaDoc;
21
22 import org.apache.avalon.repository.RepositoryException;
23 import org.apache.avalon.repository.provider.Factory;
24 import org.apache.avalon.repository.provider.InitialContext;
25
26
27 /**
28  * Application and component bootstrapper used to instantiate, and or invoke
29  * Classes and their methods within newly constructed Repository ClassLoaders.
30  *
31  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
32  * @version $Revision: 1.9 $
33  */

34 public abstract class AbstractBuilder
35 {
36    /**
37     * Load a factory class using a supplied classloader and factory classname.
38     * @param classloader the classloader to load the class from
39     * @param factory the factory classname
40     * @return the factory class
41     * @exception RepositoryException if a factory class loading error occurs
42     */

43     protected Class JavaDoc loadFactoryClass( ClassLoader JavaDoc classloader, String JavaDoc factory )
44         throws RepositoryException
45     {
46         try
47         {
48             return classloader.loadClass( factory );
49         }
50         catch( ClassNotFoundException JavaDoc e )
51         {
52             final String JavaDoc error =
53               "Could not find the factory class: " + factory;
54             throw new RepositoryException( error, e );
55         }
56         catch( Throwable JavaDoc e )
57         {
58             final String JavaDoc error =
59               "Unable to load factory class: ["
60               + factory
61               + "].";
62             throw new RepositoryException( error, e );
63         }
64     }
65
66    /**
67     * <p>Create a factory delegate using a supplied class and command line arguemnts.
68     * The implementation will conduct an ordered search for a constructor matching
69     * one of the four standard constructor patterns.</p>
70     * <ul>
71     * <li>[FactoryClass]( InitialContext context, ClassLoader loader )</li>
72     * <li>[FactoryClass]( InitialContext context )</li>
73     * <li>[FactoryClass]( ClassLoader loader )</li>
74     * <li>[FactoryClass]( )</li>
75     * </ul>
76     *
77     * @param classloader the classloader
78     * @param clazz the the factory class
79     * @param context the inital repository context
80     * @return the instantiated factory
81     * @exception RepositoryException if a factory creation error occurs
82     */

83     protected Factory createDelegate(
84       ClassLoader JavaDoc classloader, Class JavaDoc clazz, InitialContext context )
85       throws RepositoryException
86     {
87         if( null == classloader ) throw new NullPointerException JavaDoc( "classloader" );
88         if( null == clazz ) throw new NullPointerException JavaDoc( "clazz" );
89         if( null == context ) throw new NullPointerException JavaDoc( "context" );
90
91         try
92         {
93             Constructor JavaDoc constructor =
94               clazz.getConstructor(
95                 new Class JavaDoc[]{ InitialContext.class, ClassLoader JavaDoc.class } );
96             return createFactory(
97               constructor, new Object JavaDoc[]{ context, classloader } );
98         }
99         catch( NoSuchMethodException JavaDoc e )
100         {
101             try
102             {
103                 Constructor JavaDoc constructor =
104                   clazz.getConstructor(
105                     new Class JavaDoc[]{ InitialContext.class } );
106                 return createFactory(
107                   constructor, new Object JavaDoc[]{ context } );
108             }
109             catch( NoSuchMethodException JavaDoc ee )
110             {
111                 try
112                 {
113                     Constructor JavaDoc constructor =
114                       clazz.getConstructor(
115                         new Class JavaDoc[]{ ClassLoader JavaDoc.class } );
116                     return createFactory(
117                       constructor, new Object JavaDoc[]{ classloader } );
118                 }
119                 catch( NoSuchMethodException JavaDoc eee )
120                 {
121                     try
122                     {
123                         Constructor JavaDoc constructor =
124                           clazz.getConstructor(
125                             new Class JavaDoc[0] );
126                         return createFactory(
127                           constructor, new Object JavaDoc[0] );
128                     }
129                     catch( NoSuchMethodException JavaDoc eeee )
130                     {
131                         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
132                         buffer.append( "Supplied factory class [" );
133                         buffer.append( clazz.getName() );
134                         buffer.append(
135                           " ] does not implement a recognized constructor." );
136                         throw new RepositoryException( buffer.toString() );
137                     }
138                 }
139             }
140         }
141     }
142
143    /**
144     * Instantiation of a factory instance using a supplied constructor
145     * and arguments.
146     *
147     * @param constructor the factory constructor
148     * @param args the constructor arguments
149     * @return the factory instance
150     * @exception RepositoryException if an instantiation error occurs
151     */

152     private Factory createFactory(
153       Constructor JavaDoc constructor, Object JavaDoc[] args )
154       throws RepositoryException
155     {
156         Class JavaDoc clazz = constructor.getDeclaringClass();
157         try
158         {
159             return (Factory) constructor.newInstance( args );
160         }
161         catch( Throwable JavaDoc e )
162         {
163             final String JavaDoc error =
164               "Error while attempting to instantiate the factory: ["
165               + clazz.getName()
166               + "].";
167             throw new RepositoryException( error, e );
168         }
169     }
170 }
171
Popular Tags