001    /**
002     *
003     * Copyright 2003-2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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 org.apache.geronimo.gbean.DynamicGBean;
021    import org.apache.geronimo.gbean.DynamicGBeanDelegate;
022    import org.apache.geronimo.gbean.AbstractName;
023    import org.apache.geronimo.kernel.Kernel;
024    import org.apache.geronimo.management.geronimo.JCAAdminObject;
025    
026    import java.util.Map;
027    import java.util.HashMap;
028    import java.lang.reflect.Constructor;
029    
030    /**
031     * Wrapper around AdminObject that exposes its config-properties as GBeanAttributes and
032     * supplies a disconnectable proxy to bind in jndi.
033     *
034     * @version $Rev: 396206 $ $Date: 2006-04-22 19:55:45 -0700 (Sat, 22 Apr 2006) $
035     */
036    public class AdminObjectWrapper implements DynamicGBean, JCAAdminObject, AdminObjectSource {
037    
038        private final String adminObjectInterface;
039        private final String adminObjectClass;
040    
041        private final DynamicGBeanDelegate delegate;
042        private final Object adminObject;
043    
044    
045        private final Kernel kernel;
046        private final AbstractName abstractName;
047        private final String objectName;
048    
049        /**
050         * Default constructor required when a class is used as a GBean Endpoint.
051         */
052        public AdminObjectWrapper() {
053            adminObjectInterface = null;
054            adminObjectClass = null;
055            adminObject = null;
056            delegate = null;
057            kernel = null;
058            abstractName = null;
059            objectName = null;
060        }
061    
062        /**
063         * Normal managed constructor.
064         *
065         * @param adminObjectInterface Interface the proxy will implement.
066         * @param adminObjectClass Class of admin object to be wrapped.
067         * @throws IllegalAccessException
068         * @throws InstantiationException
069         */
070        public AdminObjectWrapper(final String adminObjectInterface,
071                                  final String adminObjectClass,
072                                  final Kernel kernel,
073                                  final AbstractName abstractName,
074                                  final String objectName,
075                                  final ClassLoader cl) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
076            this.adminObjectInterface = adminObjectInterface;
077            this.adminObjectClass = adminObjectClass;
078            this.kernel = kernel;
079            this.abstractName = abstractName;
080            this.objectName = objectName;
081            Class clazz = cl.loadClass(adminObjectClass);
082            adminObject = clazz.newInstance();
083            delegate = new DynamicGBeanDelegate();
084            delegate.addAll(adminObject);
085        }
086    
087        public String getAdminObjectInterface() {
088            return adminObjectInterface;
089        }
090    
091        /**
092         * Returns class of wrapped AdminObject.
093         * @return class of wrapped AdminObject
094         */
095        public String getAdminObjectClass() {
096            return adminObjectClass;
097        }
098    
099        /**
100         * Returns disconnectable proxy for binding in jndi.
101         * @return proxy implementing adminObjectInterface.
102         */
103        public Object $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 getAttribute(final String name) throws Exception {
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 name, final Object value) throws Exception {
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 invoke(final String name, final Object[] arguments, final String[] types) throws Exception {
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 getConfigProperties() {
148            String[] props = delegate.getProperties();
149            Map map = new HashMap();
150            for (int i = 0; i < props.length; i++) {
151                String 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 property, Object value) throws Exception {
161            Class cls = delegate.getPropertyType(property);
162            if(value != null && value instanceof String && !cls.getName().equals("java.lang.String")) {
163                if(cls.isPrimitive()) {
164                    if(cls.equals(int.class)) {
165                        cls = Integer.class;
166                    } else if(cls.equals(boolean.class)) {
167                        cls = Boolean.class;
168                    } else if(cls.equals(float.class)) {
169                        cls = Float.class;
170                    } else if(cls.equals(double.class)) {
171                        cls = Double.class;
172                    } else if(cls.equals(long.class)) {
173                        cls = Long.class;
174                    } else if(cls.equals(short.class)) {
175                        cls = Short.class;
176                    } else if(cls.equals(byte.class)) {
177                        cls = Byte.class;
178                    } else if(cls.equals(char.class)) {
179                        cls = Character.class;
180                    }
181                }
182                Constructor con = cls.getConstructor(new Class[]{String.class});
183                value = con.newInstance(new Object[]{value});
184            }
185            kernel.setAttribute(abstractName, property, value);
186        }
187    
188        public Object getConfigProperty(String property) throws Exception {
189            return delegate.getAttribute(property);
190        }
191    
192        public String 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    }