KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > pluggable > PluggableFeatureFactoryBaseImpl


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 package com.sun.enterprise.pluggable;
24
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 /**
32  * An abstract base class for implementing a PluggableFeatureFactory interface i * using dynamic proxies. This class does not directly implement the interface
33  * PluggableFeatureFactory, but an proxy instance implementing the interface
34  * can be obtained by a call to the static method getInstance(). In reality,
35  * this class implements InvocationHnalder interface used to handle method
36  * invocations on a dynamic proxy object.
37  */

38 public abstract class PluggableFeatureFactoryBaseImpl implements InvocationHandler JavaDoc {
39
40     /**
41      * Reference to a logger used to log exceptions.
42      */

43     private Logger JavaDoc _logger;
44
45     /**
46      * A property object that keeps interface names and the names of
47      * corresponding implementation classes.
48      */

49     private Properties JavaDoc _featureImplClasses;
50
51     /**
52      * Private constructor. The public instances of this object are not
53      * available. The instance of this class is however used as invocation
54      * handler for dynamic proxy returned by static method getInstance().
55      */

56     protected PluggableFeatureFactoryBaseImpl(Logger JavaDoc logger) {
57         _logger = logger;
58     }
59
60     /**
61      * Handle a invocation on a proxy object. This implementation looks for
62      * a property key matching the return type of the method, then takes the
63      * value of the property, assumes it to be a class name, assumes that the
64      * class has a default public constructor and then creates an instance of
65      * that class and returns it.
66      */

67     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
68             throws Throwable JavaDoc {
69         String JavaDoc featureName = findFeatureFromMethod(method);
70         String JavaDoc className = _featureImplClasses.getProperty(featureName);
71         Object JavaDoc featureImpl = Class.forName(className).newInstance();
72         return featureImpl;
73     }
74
75     protected abstract String JavaDoc getDefaultFeatureFactoryPropertyName();
76
77     /**
78      * Get an instance of a pluggable feature factory using the system
79      * property com.sun.appserv.pluggable.features. The value of the system
80      * property is expected to be the name of a class that extends
81      * java.util.Properties and defines one property for every supported
82      * pluggable feature. This method calls getInstance(String) if the
83      * value of com.sun.appserv.pluggable.features is not null.
84      *
85      * The property com.sun.appserv.pluggable.features is defined as a
86      * constant in the interface PluggableFeatureFactory and the implementation
87      * uses that (PluggableFeatureFactory.PLUGGABLE_FEATURES_PROPERTY_NAME)
88      *
89      * @return the return value from call to getInstance(String) if the
90      * system property is defined, null otherwise.
91      * @see getInstance(String propClassName)
92      */

93     public Object JavaDoc getInstance() {
94         String JavaDoc propClassName = System.getProperty(
95                 getDefaultFeatureFactoryPropertyName());
96         return getInstance(propClassName);
97     }
98
99     /**
100      * Get an instance of a pluggable feature factory using specified property
101      * class name. The method expects name of a class that extends
102      * java.util.Properties and has a default (null or no argument) constructor.
103      * This method will create an instance of specified class and then
104      * call getInstance(Properties). If an instance of specified class name can
105      * not be created, the method logs the exception to the logger specified
106      * by setLogger() method, or to System.err (if no logger was set).
107      *
108      * @param propClassName name of a class that extends Properties and contains
109      * a property for every supported pluggable feature.
110      *
111      * @return the return value from getInstance(Properties) if propClassName
112      * is not null and an instance of the class represented by propClassName
113      * was successfully created, null otherwise.
114      * @see getInstance(java.util.Properties props)
115      */

116     public Object JavaDoc getInstance(String JavaDoc propClassName) {
117         if (propClassName == null) {
118             return null;
119         }
120         Properties JavaDoc props = null;
121         try {
122             props = (Properties JavaDoc)Class.forName(propClassName).newInstance();
123         } catch (Exception JavaDoc ex) {
124             String JavaDoc msg = "Error loading pluggable features class "
125                     + propClassName;
126             if (_logger != null) {
127                 _logger.log(Level.WARNING, msg, ex);
128             } else {
129                 System.err.println(msg + "\nStack Trace:");
130                 ex.printStackTrace();
131             }
132         }
133         return getInstance(props);
134     }
135
136     /**
137      * Get an instance of a pluggable feature factory. The method expects
138      * a property object as parameter that has one property for every supported
139      * pluggable feature. The property name is the name of the interface
140      * (without package name) and property value is the fully qualified name of
141      * the class that implements the interface. The implementing class must
142      * have a default public constructor.
143      *
144      * @param props properties defining name of feature and implementing
145      * classes. Name of the feature is the class name of the interface
146      * defining the feature.
147      * @return a proxy object implementing the interface
148      * PluggableFeatureFactory. If specified parameter props is null,
149      * the method returns null.
150      */

151     protected abstract Object JavaDoc createFeatureFactory(InvocationHandler JavaDoc handler);
152
153     public Object JavaDoc getInstance(Properties JavaDoc props) {
154         if (props == null) {
155             return null;
156         }
157         _featureImplClasses = props;
158         return createFeatureFactory(this);
159     }
160
161     /**
162      * Find feature name from a method. For the interface
163      * PluggableFeatureFactory, a feature name is name of the interface
164      * defining the feature. The return type of any method in the interface
165      * PluggableFeatureFactory is the interface defining the feature (Note
166      * that the interface only contains getter methods). For example, if one
167      * of the pluggable features is defined by the interface
168      * com.sun.enterprise.server.pluggable.CoolStuff, the corresponding
169      * feature name is CoolStuff.
170      */

171     private String JavaDoc findFeatureFromMethod(Method JavaDoc method) {
172         Class JavaDoc returnType = method.getReturnType();
173         return Utils.getNQClassName(returnType);
174     }
175
176 }
177
Popular Tags