View Javadoc

1   /**
2    *
3    * Copyright 2003-2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
27  import java.util.HashMap;
28  import java.lang.reflect.Constructor;
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: 396206 $ $Date: 2006-04-22 19:55:45 -0700 (Sat, 22 Apr 2006) $
35   */
36  public class AdminObjectWrapper implements DynamicGBean, JCAAdminObject, AdminObjectSource {
37  
38      private final String adminObjectInterface;
39      private final String adminObjectClass;
40  
41      private final DynamicGBeanDelegate delegate;
42      private final Object adminObject;
43  
44  
45      private final Kernel kernel;
46      private final AbstractName abstractName;
47      private final String 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 adminObjectInterface,
71                                final String adminObjectClass,
72                                final Kernel kernel,
73                                final AbstractName abstractName,
74                                final String objectName,
75                                final ClassLoader cl) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
76          this.adminObjectInterface = adminObjectInterface;
77          this.adminObjectClass = adminObjectClass;
78          this.kernel = kernel;
79          this.abstractName = abstractName;
80          this.objectName = objectName;
81          Class clazz = cl.loadClass(adminObjectClass);
82          adminObject = clazz.newInstance();
83          delegate = new DynamicGBeanDelegate();
84          delegate.addAll(adminObject);
85      }
86  
87      public String getAdminObjectInterface() {
88          return adminObjectInterface;
89      }
90  
91      /**
92       * Returns class of wrapped AdminObject.
93       * @return class of wrapped AdminObject
94       */
95      public String getAdminObjectClass() {
96          return adminObjectClass;
97      }
98  
99      /**
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 }