001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.connector;
017:
018: import java.lang.reflect.Constructor;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.geronimo.gbean.AbstractName;
023: import org.apache.geronimo.gbean.DynamicGBean;
024: import org.apache.geronimo.gbean.DynamicGBeanDelegate;
025: import org.apache.geronimo.naming.ResourceSource;
026: import org.apache.geronimo.kernel.Kernel;
027: import org.apache.geronimo.management.geronimo.JCAAdminObject;
028:
029: /**
030: * Wrapper around AdminObject that exposes its config-properties as GBeanAttributes and
031: * supplies a disconnectable proxy to bind in jndi.
032: *
033: * @version $Rev: 607943 $ $Date: 2008-01-01 15:07:17 -0800 (Tue, 01 Jan 2008) $
034: */
035: public class AdminObjectWrapper implements DynamicGBean,
036: JCAAdminObject, ResourceSource<RuntimeException> {
037:
038: private final String adminObjectInterface;
039: private final String adminObjectClass;
040:
041: private final DynamicGBeanDelegate delegate;
042: private final Object adminObject;
043:
044: private final Kernel kernel;
045: private final AbstractName abstractName;
046: private final String objectName;
047:
048: /**
049: * Default constructor required when a class is used as a GBean Endpoint.
050: */
051: public AdminObjectWrapper() {
052: adminObjectInterface = null;
053: adminObjectClass = null;
054: adminObject = null;
055: delegate = null;
056: kernel = null;
057: abstractName = null;
058: objectName = null;
059: }
060:
061: /**
062: * Normal managed constructor.
063: *
064: * @param adminObjectInterface Interface the proxy will implement.
065: * @param adminObjectClass Class of admin object to be wrapped.
066: * @throws IllegalAccessException
067: * @throws InstantiationException
068: */
069: public AdminObjectWrapper(final String adminObjectInterface,
070: final String adminObjectClass, final Kernel kernel,
071: final AbstractName abstractName, final String objectName,
072: final ClassLoader cl) throws IllegalAccessException,
073: InstantiationException, ClassNotFoundException {
074: this .adminObjectInterface = adminObjectInterface;
075: this .adminObjectClass = adminObjectClass;
076: this .kernel = kernel;
077: this .abstractName = abstractName;
078: this .objectName = objectName;
079: Class clazz = cl.loadClass(adminObjectClass);
080: adminObject = clazz.newInstance();
081: delegate = new DynamicGBeanDelegate();
082: delegate.addAll(adminObject);
083: }
084:
085: public String getAdminObjectInterface() {
086: return adminObjectInterface;
087: }
088:
089: /**
090: * Returns class of wrapped AdminObject.
091: * @return class of wrapped AdminObject
092: */
093: public String getAdminObjectClass() {
094: return adminObjectClass;
095: }
096:
097: /**
098: * Returns disconnectable proxy for binding in jndi.
099: * @return proxy implementing adminObjectInterface.
100: */
101: public Object $getResource() {
102: return adminObject;
103: }
104:
105: //DynamicGBean implementation
106:
107: /**
108: * Delegating DynamicGBean getAttribute method.
109: * @param name of attribute.
110: * @return attribute value.
111: * @throws Exception
112: */
113: public Object getAttribute(final String name) throws Exception {
114: return delegate.getAttribute(name);
115: }
116:
117: /**
118: * Delegating DynamicGBean setAttribute method.
119: * @param name of attribute.
120: * @param value of attribute to be set.
121: * @throws Exception
122: */
123: public void setAttribute(final String name, final Object value)
124: throws Exception {
125: delegate.setAttribute(name, value);
126: }
127:
128: /**
129: * no-op DynamicGBean method
130: * @param name
131: * @param arguments
132: * @param types
133: * @return nothing, there are no operations.
134: * @throws Exception
135: */
136: public Object invoke(final String name, final Object[] arguments,
137: final String[] types) throws Exception {
138: //we have no dynamic operations.
139: return null;
140: }
141:
142: /**
143: * Gets the config properties in the form of a map where the key is the
144: * property name and the value is property type (as a String not a Class).
145: */
146: public Map getConfigProperties() {
147: String[] props = delegate.getProperties();
148: Map map = new HashMap();
149: for (int i = 0; i < props.length; i++) {
150: String prop = props[i];
151: if (prop.equals("logWriter")) {
152: continue;
153: }
154: map.put(prop, delegate.getPropertyType(prop));
155: }
156: return map;
157: }
158:
159: public void setConfigProperty(String property, Object value)
160: throws Exception {
161: Class cls = delegate.getPropertyType(property);
162: if (value != null && value instanceof String
163: && !cls.getName().equals("java.lang.String")) {
164: if (cls.isPrimitive()) {
165: if (cls.equals(int.class)) {
166: cls = Integer.class;
167: } else if (cls.equals(boolean.class)) {
168: cls = Boolean.class;
169: } else if (cls.equals(float.class)) {
170: cls = Float.class;
171: } else if (cls.equals(double.class)) {
172: cls = Double.class;
173: } else if (cls.equals(long.class)) {
174: cls = Long.class;
175: } else if (cls.equals(short.class)) {
176: cls = Short.class;
177: } else if (cls.equals(byte.class)) {
178: cls = Byte.class;
179: } else if (cls.equals(char.class)) {
180: cls = Character.class;
181: }
182: }
183: Constructor con = cls
184: .getConstructor(new Class[] { String.class });
185: value = con.newInstance(new Object[] { value });
186: }
187: kernel.setAttribute(abstractName, property, value);
188: }
189:
190: public Object getConfigProperty(String property) throws Exception {
191: return delegate.getAttribute(property);
192: }
193:
194: public String getObjectName() {
195: return objectName;
196: }
197:
198: public boolean isStateManageable() {
199: return false;
200: }
201:
202: public boolean isStatisticsProvider() {
203: return false;
204: }
205:
206: public boolean isEventProvider() {
207: return false;
208: }
209: }
|