KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > api > bean > Controls


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

19
20 import org.apache.beehive.controls.api.properties.PropertyMap;
21 import org.apache.beehive.controls.api.bean.ControlBean;
22 import org.apache.beehive.controls.api.context.ControlBeanContext;
23 import org.apache.beehive.controls.api.ControlException;
24
25 import org.apache.beehive.controls.spi.bean.ControlFactory;
26 import org.apache.beehive.controls.spi.bean.JavaControlFactory;
27
28 import org.apache.commons.discovery.tools.DiscoverClass;
29
30 import java.lang.reflect.Constructor JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.InvocationTargetException JavaDoc;
33
34 /**
35  * Helper class for using controls. Includes static methods to help instantiate controls, and initialize
36  * declarative control clients.
37  */

38 public class Controls
39 {
40     final private static String JavaDoc DEFAULT_FACTORY_CLASS = JavaControlFactory.class.getName();
41
42     /**
43      * Factory method for instantiating controls. Controls instantiated using this method will be associated with the
44      * current thread-local ControlBeanContext (possibly none), and have an auto-generated ID.
45      *
46      * @param cl the classloader used to load the ControlBean. If null, the system classloader will be used.
47      * @param beanName the fully qualified name of the ControlBean class.
48      * @param props an optional PropertyMap containing initial property values for the control. May be null.
49      * @return an instance of the specified ControlBean.
50      * @throws ClassNotFoundException
51      */

52     public static ControlBean instantiate( ClassLoader JavaDoc cl,
53                                            String JavaDoc beanName,
54                                            PropertyMap props )
55         throws ClassNotFoundException JavaDoc
56     {
57         return instantiate( cl, beanName, props, null, null );
58     }
59
60     /**
61      * Factory method for instantiating controls.
62      *
63      * @param cl the classloader used to load the ControlBean. If null, the system classloader will be used.
64      * @param beanName the fully qualified name of the ControlBean class.
65      * @param props an optional PropertyMap containing initial property values for the control. May be null.
66      * @param cbc the ControlBeanContext that will nest the created control. If null, the thread-local context
67      * (possibly none) will be used.
68      * @param id a unique ID for the created control. If null, an ID will be auto-generated.
69      * @return an instance of the specified ControlBean.
70      * @throws ClassNotFoundException
71      */

72     public static ControlBean instantiate( ClassLoader JavaDoc cl,
73                                            String JavaDoc beanName,
74                                            PropertyMap props,
75                                            ControlBeanContext cbc,
76                                            String JavaDoc id )
77         throws ClassNotFoundException JavaDoc
78     {
79         Class JavaDoc beanClass = ( cl == null ) ? Class.forName( beanName ) : cl.loadClass( beanName );
80         return instantiate(beanClass, props, cbc, id);
81     }
82
83     /**
84      * Factory method for instantiating controls.
85      *
86      * @param beanClass the ControlBean class to instantiate
87      * @param props an optional PropertyMap containing initial property values for the control.
88      * may be null.
89      * @param context the ControlBeanContext that will nest the created control. If null, the
90      * thread-local context (possibly none) will be used.
91      * @param id a unique ID for the created control. If null, an ID will be auto-generated.
92      * @return an instance of the specified ControlBean.
93      */

94     public static <T extends ControlBean> T instantiate( Class JavaDoc<T> beanClass,
95                                                          PropertyMap props,
96                                                          ControlBeanContext context,
97                                                          String JavaDoc id )
98     {
99         try
100         {
101             DiscoverClass discoverer = new DiscoverClass();
102             Class JavaDoc factoryClass = discoverer.find( ControlFactory.class, DEFAULT_FACTORY_CLASS );
103             ControlFactory factory = (ControlFactory)factoryClass.newInstance();
104             return factory.instantiate( beanClass, props, context, id );
105         }
106         catch ( Exception JavaDoc e )
107         {
108             throw new ControlException( "Exception creating ControlBean", e );
109         }
110     }
111
112     /**
113      * Helper method for initializing instances of declarative control clients (objects that use controls via @Control
114      * and @EventHandler annotations). This method runs the client-specific generated ClientInitializer class to do
115      * its initialization work.
116      *
117      * @param cl the classloader used to load the ClientInitializer. If null, defaults to the classloader used to
118      * load the client object being initialized.
119      * @param client the client object being initialized.
120      * @param cbc the ControlBeanContext to be associated with the client object (that will nest the controls the client
121      * defines). If null, the thread-local context (possibly none) will be used.
122      * @throws ControlException
123      * @throws ClassNotFoundException
124      */

125     public static void initializeClient( ClassLoader JavaDoc cl, Object JavaDoc client, ControlBeanContext cbc )
126         throws ClassNotFoundException JavaDoc
127     {
128         Class JavaDoc clientClass = client.getClass();
129         String JavaDoc clientName = clientClass.getName();
130
131         if ( cl == null )
132             cl = clientClass.getClassLoader();
133
134         String JavaDoc initName = clientName + "ClientInitializer";
135         Class JavaDoc initClass = cl.loadClass( initName );
136
137         try
138         {
139             Method JavaDoc m = initClass.getMethod( "initialize", ControlBeanContext.class, clientClass );
140             m.invoke(null, cbc, client );
141         }
142         catch ( Throwable JavaDoc e )
143         {
144             if ( e instanceof InvocationTargetException JavaDoc )
145             {
146                 if ( e.getCause() != null )
147                 {
148                     e = e.getCause();
149                 }
150             }
151                 
152             throw new ControlException( "Exception trying to run client initializer: " + e.getClass().getName() + ", " +
153                                         e.getMessage(), e );
154         }
155     }
156 }
157
Popular Tags