KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > bean > BeanWrapper


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * BeanWrapper.java
20  *
21  * This object is responsible for wrapping a bean and loading it into memory.
22  */

23
24 // package definition
25
package com.rift.coad.lib.bean;
26
27 // java imports
28
import java.lang.reflect.Proxy JavaDoc;
29 import java.lang.reflect.Constructor JavaDoc;
30
31 // logging import
32
import org.apache.log4j.Logger;
33
34 // coadunation imports
35
import com.rift.coad.lib.deployment.DeploymentLoader;
36 import com.rift.coad.lib.deployment.BeanInfo;
37 import com.rift.coad.lib.security.ThreadsPermissionContainer;
38
39 /**
40  * This object is responsible for wrapping a bean and loading it into memory.
41  *
42  * @author Brett Chaldecott
43  */

44 public class BeanWrapper {
45     
46     // the class log variable
47
protected Logger log =
48         Logger.getLogger(BeanWrapper.class.getName());
49     
50     // The classes member variables
51
private DeploymentLoader loader = null;
52     private ThreadsPermissionContainer permissions = null;
53     private String JavaDoc bindName = null;
54     private BeanHandler handler = null;
55     private Object JavaDoc proxy = null;
56     private java.rmi.Remote JavaDoc tie = null;
57     private Object JavaDoc subObject = null;
58     private Class JavaDoc interfaceRef = null;
59     
60     
61     /**
62      * Creates a new instance of BeanWrapper
63      *
64      * @param deploymentLoader The reference to the deployment loader.
65      * @param beanInfo The reference to the bean information object.
66      * @exception BeanException
67      */

68     public BeanWrapper(DeploymentLoader deploymentLoader, BeanInfo beanInfo,
69             ThreadsPermissionContainer permissions)
70         throws BeanException {
71         try {
72             this.loader = deploymentLoader;
73             this.permissions = permissions;
74             bindName = beanInfo.getBindName();
75             subObject = deploymentLoader.getClassLoader().
76                     loadClass(beanInfo.getClassName()).newInstance();
77             interfaceRef = deploymentLoader.getClassLoader().
78                     loadClass(beanInfo.getInterfaceName());
79             handler = new BeanHandler(beanInfo, subObject,
80                     beanInfo.getRole(),permissions,
81                     deploymentLoader.getClassLoader());
82             proxy = (Object JavaDoc)Proxy.newProxyInstance(
83                     deploymentLoader.getClassLoader(),
84                     new Class JavaDoc[] {deploymentLoader.getClass(
85                             beanInfo.getInterfaceName())},handler);
86             tie = loadTie(beanInfo, subObject);
87         } catch (Exception JavaDoc ex) {
88             log.error("Failed to instanciate the bean wrapper : "
89                     + ex.getMessage(),ex);
90             throw new BeanException("Failed to instanciate the bean wrapper : "
91                     + ex.getMessage(), ex);
92         }
93     }
94     
95     
96     /**
97      * The bind name that will be used to search for this proxy object or
98      * container object.
99      *
100      * @return The string containing the bind information.
101      */

102     public String JavaDoc getBindName() {
103         return bindName;
104     }
105     
106     
107     /**
108      * This method returns the reference to the proxy object.
109      *
110      * @return The reference to the proxy object
111      */

112     public Object JavaDoc getProxy() {
113         return proxy;
114     }
115     
116     
117     /**
118      * This method returns the reference to the tie class.
119      *
120      * @return The reference to the tie object.
121      */

122     public java.rmi.Remote JavaDoc getTie() {
123         return tie;
124     }
125     
126     
127     /**
128      * This method returns the reference to the sub object.
129      *
130      * @return The reference to the sub object.
131      */

132     public Object JavaDoc getSubObject() {
133         return subObject;
134     }
135     
136     
137     /**
138      * The interface this class must run as.
139      *
140      * @return The class this object must run as.
141      */

142     public Class JavaDoc getInterface() {
143         return interfaceRef;
144     }
145     
146     
147     /**
148      * This method loads the tie class.
149      *
150      * @return The reference to the loaded tie or null if it does not inherit
151      * from remote.
152      * @param beanInfo The information
153      * @param subObject The sub object uppon which all the call will be made.
154      */

155     private java.rmi.Remote JavaDoc loadTie(BeanInfo beanInfo, Object JavaDoc subObject)
156             throws BeanException {
157         if (!(subObject instanceof java.rmi.Remote JavaDoc)) {
158             return null;
159         }
160         String JavaDoc className = subObject.getClass().getName() +
161                 BeanPattern.TIE_SUFFIX;
162         try {
163             Class JavaDoc classRef = loader.getClassLoader().loadClass(className);
164             // note: Must be exact matching class when doing the constructor
165
// look up. It does not take inheritance into account.
166
Class JavaDoc[] parameterTypes = new Class JavaDoc[] {
167                     ClassLoader JavaDoc.class,
168                     subObject.getClass(), String JavaDoc.class,
169                     ThreadsPermissionContainer.class,
170                     BeanInfo.class};
171             Constructor JavaDoc constructor = classRef.getConstructor(parameterTypes);
172             return (java.rmi.Remote JavaDoc)constructor.newInstance(
173                     loader.getClassLoader(),subObject,beanInfo.getRole(),
174                     permissions,beanInfo);
175         } catch (Exception JavaDoc ex) {
176             throw new BeanException("Failed to load the class [" + className
177                     + "] because : " + ex.getMessage(),ex);
178         }
179     }
180 }
181
Popular Tags