KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > spi > bean > JavaControlFactory


1 package org.apache.beehive.controls.spi.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 java.lang.reflect.Constructor JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.util.concurrent.ConcurrentHashMap JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import org.apache.beehive.controls.api.properties.PropertyMap;
28 import org.apache.beehive.controls.api.properties.BeanPropertyMap;
29 import org.apache.beehive.controls.api.properties.PropertyKey;
30 import org.apache.beehive.controls.api.bean.ControlBean;
31 import org.apache.beehive.controls.api.context.ControlBeanContext;
32 import org.apache.beehive.controls.api.ControlException;
33
34 /**
35  * The SimpleControlFactory class is a default implementation of the
36  * <code>org.apache.beehive.controls.api.bean.ControlFactory</code> interface. It
37  * uses Java reflection to create new control instances.
38  *
39  * @see org.apache.beehive.controls.api.bean.Controls#instantiate
40  * @see org.apache.beehive.controls.spi.bean.ControlFactory
41  */

42 public class JavaControlFactory implements ControlFactory
43 {
44     private static ConcurrentHashMap JavaDoc<Class JavaDoc, Constructor JavaDoc> _constructors =
45         new ConcurrentHashMap JavaDoc<Class JavaDoc, Constructor JavaDoc>();
46
47     private static final Properties JavaDoc _extImplBindings = new Properties JavaDoc();
48
49     private static final String JavaDoc EXT_IMPL_BINDING_CONFIG = "controlbindings.properties";
50
51     static
52     {
53         InputStream JavaDoc is = JavaControlFactory.class.getClassLoader().
54                            getResourceAsStream( EXT_IMPL_BINDING_CONFIG );
55
56         if ( is != null )
57         {
58             try
59             {
60                 _extImplBindings.load( is );
61             }
62             catch ( IOException JavaDoc ie ) { }
63         }
64     }
65
66     /**
67      * Instantiates a new ControlBean of the requested class, using mechanisms provided
68      * by a provider-specific JavaBeans framework.
69      *
70      * @param beanClass the ControlBean class to instantiate
71      * @param props an initial set of client-specified properties to associate with the
72      * bean instance. May be null.
73      * @param context the containing ControlBeanContext for the bean, if nested inside of
74      * a container or other control. May be null to use the current active
75      * execution context.
76      * @param id the bean control ID. Must be unique within the containing context. If
77      * null, a unique identifier will be auto-generated.
78      * @return a new ControlBean instance of the requested class.
79      */

80     public <T extends ControlBean> T instantiate(Class JavaDoc<T> beanClass,
81                                                  PropertyMap props,
82                                                  ControlBeanContext context,
83                                                  String JavaDoc id)
84     {
85         String JavaDoc beanClassName = beanClass.getName();
86
87         String JavaDoc extImplBinding = _extImplBindings.getProperty( beanClassName + "_" + id );
88         if ( extImplBinding == null )
89             extImplBinding = _extImplBindings.getProperty( beanClassName );
90
91         if ( extImplBinding != null )
92         {
93             BeanPropertyMap bpm = props == null ? new BeanPropertyMap( beanClass ) : new BeanPropertyMap( props );
94             PropertyKey propKey = new PropertyKey( org.apache.beehive.controls.api.properties.BaseProperties.class,
95                                                    "controlImplementation" );
96
97             bpm.setProperty( propKey, extImplBinding );
98             props = bpm;
99         }
100
101         T ret = null;
102         try
103         {
104             Constructor JavaDoc<T> ctor = _constructors.get(beanClass);
105             if (ctor == null)
106             {
107                 ctor = beanClass.getConstructor(ControlBeanContext.class, String JavaDoc.class,
108                                                 PropertyMap.class);
109                 _constructors.put(beanClass, ctor);
110             }
111             ret = ctor.newInstance(context, id, props);
112         }
113         catch (InvocationTargetException JavaDoc ite)
114         {
115             Throwable JavaDoc t = ite.getCause();
116             throw new ControlException("ControlBean constructor exception", t);
117         }
118         catch (Exception JavaDoc e)
119         {
120             throw new ControlException("Exception creating ControlBean", e);
121         }
122
123         return ret;
124     }
125 }
126
Popular Tags