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     */
017    
018    package org.apache.geronimo.connector;
019    
020    import java.lang.reflect.Constructor;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.apache.geronimo.gbean.AbstractName;
025    import org.apache.geronimo.gbean.DynamicGBean;
026    import org.apache.geronimo.gbean.DynamicGBeanDelegate;
027    import org.apache.geronimo.naming.ResourceSource;
028    import org.apache.geronimo.kernel.Kernel;
029    import org.apache.geronimo.management.geronimo.JCAAdminObject;
030    
031    /**
032     * Wrapper around AdminObject that exposes its config-properties as GBeanAttributes and
033     * supplies a disconnectable proxy to bind in jndi.
034     *
035     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
036     */
037    public class AdminObjectWrapper implements DynamicGBean, JCAAdminObject, ResourceSource<RuntimeException> {
038    
039        private final String adminObjectInterface;
040        private final String adminObjectClass;
041    
042        private final DynamicGBeanDelegate delegate;
043        private final Object adminObject;
044    
045    
046        private final Kernel kernel;
047        private final AbstractName abstractName;
048        private final String objectName;
049    
050        /**
051         * Default constructor required when a class is used as a GBean Endpoint.
052         */
053        public AdminObjectWrapper() {
054            adminObjectInterface = null;
055            adminObjectClass = null;
056            adminObject = null;
057            delegate = null;
058            kernel = null;
059            abstractName = null;
060            objectName = null;
061        }
062    
063        /**
064         * Normal managed constructor.
065         *
066         * @param adminObjectInterface Interface the proxy will implement.
067         * @param adminObjectClass Class of admin object to be wrapped.
068         * @throws IllegalAccessException
069         * @throws InstantiationException
070         */
071        public AdminObjectWrapper(final String adminObjectInterface,
072                                  final String adminObjectClass,
073                                  final Kernel kernel,
074                                  final AbstractName abstractName,
075                                  final String objectName,
076                                  final ClassLoader cl) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
077            this.adminObjectInterface = adminObjectInterface;
078            this.adminObjectClass = adminObjectClass;
079            this.kernel = kernel;
080            this.abstractName = abstractName;
081            this.objectName = objectName;
082            Class clazz = cl.loadClass(adminObjectClass);
083            adminObject = clazz.newInstance();
084            delegate = new DynamicGBeanDelegate();
085            delegate.addAll(adminObject);
086        }
087    
088        public String getAdminObjectInterface() {
089            return adminObjectInterface;
090        }
091    
092        /**
093         * Returns class of wrapped AdminObject.
094         * @return class of wrapped AdminObject
095         */
096        public String getAdminObjectClass() {
097            return adminObjectClass;
098        }
099    
100        /**
101         * Returns disconnectable proxy for binding in jndi.
102         * @return proxy implementing adminObjectInterface.
103         */
104        public Object $getResource() {
105            return adminObject;
106        }
107    
108    
109        //DynamicGBean implementation
110    
111        /**
112         * Delegating DynamicGBean getAttribute method.
113         * @param name of attribute.
114         * @return attribute value.
115         * @throws Exception
116         */
117        public Object getAttribute(final String name) throws Exception {
118            return delegate.getAttribute(name);
119        }
120    
121        /**
122         * Delegating DynamicGBean setAttribute method.
123         * @param name of attribute.
124         * @param value of attribute to be set.
125         * @throws Exception
126         */
127        public void setAttribute(final String name, final Object value) throws Exception {
128            delegate.setAttribute(name, value);
129        }
130    
131        /**
132         * no-op DynamicGBean method
133         * @param name
134         * @param arguments
135         * @param types
136         * @return nothing, there are no operations.
137         * @throws Exception
138         */
139        public Object invoke(final String name, final Object[] arguments, final String[] types) throws Exception {
140            //we have no dynamic operations.
141            return null;
142        }
143    
144        /**
145         * Gets the config properties in the form of a map where the key is the
146         * property name and the value is property type (as a String not a Class).
147         */
148        public Map getConfigProperties() {
149            String[] props = delegate.getProperties();
150            Map map = new HashMap();
151            for (int i = 0; i < props.length; i++) {
152                String prop = props[i];
153                if(prop.equals("logWriter")) {
154                    continue;
155                }
156                map.put(prop, delegate.getPropertyType(prop));
157            }
158            return map;
159        }
160    
161        public void setConfigProperty(String property, Object value) throws Exception {
162            Class cls = delegate.getPropertyType(property);
163            if(value != null && value instanceof String && !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.getConstructor(new Class[]{String.class});
184                value = con.newInstance(new Object[]{value});
185            }
186            kernel.setAttribute(abstractName, property, value);
187        }
188    
189        public Object getConfigProperty(String property) throws Exception {
190            return delegate.getAttribute(property);
191        }
192    
193        public String getObjectName() {
194            return objectName;
195        }
196    
197        public boolean isStateManageable() {
198            return false;
199        }
200    
201        public boolean isStatisticsProvider() {
202            return false;
203        }
204    
205        public boolean isEventProvider() {
206            return false;
207        }
208    }