KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > AdminObjectWrapper


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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
18 package org.apache.geronimo.connector;
19
20 import org.apache.geronimo.gbean.DynamicGBean;
21 import org.apache.geronimo.gbean.DynamicGBeanDelegate;
22 import org.apache.geronimo.gbean.AbstractName;
23 import org.apache.geronimo.kernel.Kernel;
24 import org.apache.geronimo.management.geronimo.JCAAdminObject;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.lang.reflect.Constructor JavaDoc;
29
30 /**
31  * Wrapper around AdminObject that exposes its config-properties as GBeanAttributes and
32  * supplies a disconnectable proxy to bind in jndi.
33  *
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class AdminObjectWrapper implements DynamicGBean, JCAAdminObject, AdminObjectSource {
37
38     private final String JavaDoc adminObjectInterface;
39     private final String JavaDoc adminObjectClass;
40
41     private final DynamicGBeanDelegate delegate;
42     private final Object JavaDoc adminObject;
43
44
45     private final Kernel kernel;
46     private final AbstractName abstractName;
47     private final String JavaDoc objectName;
48
49     /**
50      * Default constructor required when a class is used as a GBean Endpoint.
51      */

52     public AdminObjectWrapper() {
53         adminObjectInterface = null;
54         adminObjectClass = null;
55         adminObject = null;
56         delegate = null;
57         kernel = null;
58         abstractName = null;
59         objectName = null;
60     }
61
62     /**
63      * Normal managed constructor.
64      *
65      * @param adminObjectInterface Interface the proxy will implement.
66      * @param adminObjectClass Class of admin object to be wrapped.
67      * @throws IllegalAccessException
68      * @throws InstantiationException
69      */

70     public AdminObjectWrapper(final String JavaDoc adminObjectInterface,
71                               final String JavaDoc adminObjectClass,
72                               final Kernel kernel,
73                               final AbstractName abstractName,
74                               final String JavaDoc objectName,
75                               final ClassLoader JavaDoc cl) throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, ClassNotFoundException JavaDoc {
76         this.adminObjectInterface = adminObjectInterface;
77         this.adminObjectClass = adminObjectClass;
78         this.kernel = kernel;
79         this.abstractName = abstractName;
80         this.objectName = objectName;
81         Class JavaDoc clazz = cl.loadClass(adminObjectClass);
82         adminObject = clazz.newInstance();
83         delegate = new DynamicGBeanDelegate();
84         delegate.addAll(adminObject);
85     }
86
87     public String JavaDoc getAdminObjectInterface() {
88         return adminObjectInterface;
89     }
90
91     /**
92      * Returns class of wrapped AdminObject.
93      * @return class of wrapped AdminObject
94      */

95     public String JavaDoc getAdminObjectClass() {
96         return adminObjectClass;
97     }
98
99     /**
100      * Returns disconnectable proxy for binding in jndi.
101      * @return proxy implementing adminObjectInterface.
102      */

103     public Object JavaDoc $getResource() {
104         return adminObject;
105     }
106
107
108     //DynamicGBean implementation
109

110     /**
111      * Delegating DynamicGBean getAttribute method.
112      * @param name of attribute.
113      * @return attribute value.
114      * @throws Exception
115      */

116     public Object JavaDoc getAttribute(final String JavaDoc name) throws Exception JavaDoc {
117         return delegate.getAttribute(name);
118     }
119
120     /**
121      * Delegating DynamicGBean setAttribute method.
122      * @param name of attribute.
123      * @param value of attribute to be set.
124      * @throws Exception
125      */

126     public void setAttribute(final String JavaDoc name, final Object JavaDoc value) throws Exception JavaDoc {
127         delegate.setAttribute(name, value);
128     }
129
130     /**
131      * no-op DynamicGBean method
132      * @param name
133      * @param arguments
134      * @param types
135      * @return nothing, there are no operations.
136      * @throws Exception
137      */

138     public Object JavaDoc invoke(final String JavaDoc name, final Object JavaDoc[] arguments, final String JavaDoc[] types) throws Exception JavaDoc {
139         //we have no dynamic operations.
140
return null;
141     }
142
143     /**
144      * Gets the config properties in the form of a map where the key is the
145      * property name and the value is property type (as a String not a Class).
146      */

147     public Map JavaDoc getConfigProperties() {
148         String JavaDoc[] props = delegate.getProperties();
149         Map JavaDoc map = new HashMap JavaDoc();
150         for (int i = 0; i < props.length; i++) {
151             String JavaDoc prop = props[i];
152             if(prop.equals("logWriter")) {
153                 continue;
154             }
155             map.put(prop, delegate.getPropertyType(prop));
156         }
157         return map;
158     }
159
160     public void setConfigProperty(String JavaDoc property, Object JavaDoc value) throws Exception JavaDoc {
161         Class JavaDoc cls = delegate.getPropertyType(property);
162         if(value != null && value instanceof String JavaDoc && !cls.getName().equals("java.lang.String")) {
163             if(cls.isPrimitive()) {
164                 if(cls.equals(int.class)) {
165                     cls = Integer JavaDoc.class;
166                 } else if(cls.equals(boolean.class)) {
167                     cls = Boolean JavaDoc.class;
168                 } else if(cls.equals(float.class)) {
169                     cls = Float JavaDoc.class;
170                 } else if(cls.equals(double.class)) {
171                     cls = Double JavaDoc.class;
172                 } else if(cls.equals(long.class)) {
173                     cls = Long JavaDoc.class;
174                 } else if(cls.equals(short.class)) {
175                     cls = Short JavaDoc.class;
176                 } else if(cls.equals(byte.class)) {
177                     cls = Byte JavaDoc.class;
178                 } else if(cls.equals(char.class)) {
179                     cls = Character JavaDoc.class;
180                 }
181             }
182             Constructor JavaDoc con = cls.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
183             value = con.newInstance(new Object JavaDoc[]{value});
184         }
185         kernel.setAttribute(abstractName, property, value);
186     }
187
188     public Object JavaDoc getConfigProperty(String JavaDoc property) throws Exception JavaDoc {
189         return delegate.getAttribute(property);
190     }
191
192     public String JavaDoc getObjectName() {
193         return objectName;
194     }
195
196     public boolean isStateManageable() {
197         return false;
198     }
199
200     public boolean isStatisticsProvider() {
201         return false;
202     }
203
204     public boolean isEventProvider() {
205         return false;
206     }
207 }
208
Popular Tags