KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > apiscan > classfile > ClassFileLoaderFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * ClassFileLoaderFactory.java
26  *
27  * Created on August 24, 2004, 5:56 PM
28  */

29
30 package com.sun.enterprise.tools.verifier.apiscan.classfile;
31
32 import java.lang.reflect.Constructor JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * A factory for ClassFileLoader so that we can control the creation of
38  * ClassFileLoaders. More over, this factory can be dynamically configured by
39  * setting the Java class name of the actual ClassFileLoader type in the system
40  * property apiscan.ClassFileLoader. See newInstance() method.
41  *
42  * @author Sanjeeb.Sahoo@Sun.COM
43  */

44 public class ClassFileLoaderFactory {
45     private static String JavaDoc resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings";
46     private static Logger JavaDoc logger = Logger.getLogger("apiscan.classfile", resourceBundleName); // NOI18N
47
private final static String JavaDoc myClassName = "ClassFileLoaderFactory"; // NOI18N
48
/**
49      * a factory method to create ClassFileLoader instance. It decides which
50      * kind of loader class to instantioate based on the class name supplied by
51      * the system property ClassFileLoader. If there is no such property set, it
52      * defaults to {@link BCELClassFileLoader}
53      *
54      * @param args Search path to be used by the ClassFileLoader. Depending on
55      * the type of the ClassFileLoader requested, the semantics of
56      * this argument varies.
57      * @throws RuntimeException If it could not instantiate the loader type
58      * requested. The actual error is wrapped in this
59      * exception.
60      */

61     public static ClassFileLoader newInstance(Object JavaDoc[] args)
62             throws RuntimeException JavaDoc {
63         logger.entering(myClassName, "newInstance", args); // NOI18N
64
String JavaDoc loaderClassName = System.getProperty("apiscan.ClassFileLoader");
65         if (loaderClassName == null) {
66             loaderClassName =
67                     com.sun.enterprise.tools.verifier.apiscan.classfile.BCELClassFileLoader.class.getName();
68             logger.logp(Level.FINE, myClassName, "newInstance", // NOI18N
69
"System Property apiscan.ClassFileLoader is null, so defaulting to " + // NOI18N
70
loaderClassName);
71         }
72         try {
73             Class JavaDoc clazz = Class.forName(loaderClassName);
74             Object JavaDoc o = null;
75             Constructor JavaDoc[] constrs = clazz.getConstructors();
76             for (int i = 0; i < constrs.length; ++i) {
77                 try {
78                     o = constrs[i].newInstance(args);
79                 } catch (IllegalArgumentException JavaDoc e) {
80                  //try another constr as this argument did not match the reqd types for this constr.
81
}
82             }
83             if (o == null) throw new Exception JavaDoc(
84                     "Could not find a suitable constructor for this argument types.");
85             logger.exiting(myClassName, "<init>", o); // NOI18N
86
return (ClassFileLoader) o;
87         } catch (Exception JavaDoc e) {
88             logger.log(Level.SEVERE, "com.sun.enterprise.tools.verifier.apiscan.classfile.ClassFileLoaderFactory.exception1", e);
89             throw new RuntimeException JavaDoc("Unable to instantiate a loader.", e);
90         }
91     }
92 }
93
Popular Tags