KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > deployment > AdminObjectFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.deployment;
23
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorManager JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34
35 import org.jboss.deployment.DeploymentException;
36 import org.jboss.logging.Logger;
37 import org.jboss.resource.metadata.AdminObjectMetaData;
38 import org.jboss.resource.metadata.ConfigPropertyMetaData;
39
40 /**
41  * An admin object factory
42  *
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 38341 $
45  */

46 public class AdminObjectFactory
47 {
48    /** The logger */
49    private static final Logger log = Logger.getLogger(AdminObjectFactory.class);
50
51    public static Object JavaDoc createAdminObject(String JavaDoc jndiName, ObjectName JavaDoc rarName, AdminObjectMetaData aomd, Properties JavaDoc properties) throws Exception JavaDoc
52    {
53       boolean trace = log.isTraceEnabled();
54       
55       // Get the current classloader
56
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
57
58       if (trace)
59          log.trace("Creating AdminObject '" + jndiName + "' metadata=" + aomd + " rar=" + rarName + " properties=" + properties + " classloader=" + cl);
60       
61       // The interface class
62
String JavaDoc interfaceName = aomd.getAdminObjectInterfaceClass();
63       // Load the interface class class
64
if (trace)
65          log.trace("AdminObject '" + jndiName + "' loading interface=" + interfaceName);
66       Class JavaDoc interfaceClass = cl.loadClass(interfaceName);
67       
68       // Determine the implementation class
69
String JavaDoc implName = aomd.getAdminObjectImplementationClass();
70       if (implName == null)
71          throw new DeploymentException("No implementation class for admin object '" + interfaceClass + "' ra=" + rarName);
72       
73       // Load the implementation class
74
if (trace)
75          log.trace("AdminObject '" + jndiName + "' loading implementation=" + implName);
76       Class JavaDoc implClass = cl.loadClass(implName);
77       if (interfaceClass.isAssignableFrom(implClass) == false)
78          throw new DeploymentException(implClass.getName() + " is not a '" + interfaceClass + "' ra=" + rarName);
79
80       Object JavaDoc result = implClass.newInstance();
81       if (trace)
82          log.trace("AdminObject '" + jndiName + "' created instance=" + result);
83       
84       // Apply values from the ra.xml
85
Collection JavaDoc raProperties = aomd.getProperties();
86       if (raProperties != null && raProperties.size() != 0)
87       {
88          for (Iterator JavaDoc i = raProperties.iterator(); i.hasNext();)
89          {
90             ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
91             String JavaDoc name = cpmd.getName();
92             String JavaDoc value = cpmd.getValue();
93             if (value != null && value.length() > 0)
94             {
95                if (properties.containsKey(name))
96                {
97                   if (trace)
98                      log.trace("AdminObject '" + jndiName + "' property=" + name + " IGNORING value=" + value +" specified in MBean properties.");
99                }
100                else
101                {
102                   // Load the property class as defined in the meta data
103
String JavaDoc typeName = cpmd.getType();
104                   if (trace)
105                      log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName);
106                   Class JavaDoc type = cl.loadClass(typeName);
107                   
108                   // Find the property editor for this class
109
PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(type);
110                   if (editor == null)
111                      throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName);
112                   editor.setAsText(value);
113                   Object JavaDoc object = editor.getValue();
114                   
115                   try
116                   {
117                      String JavaDoc setter = "set" + Character.toUpperCase(name.charAt(0));
118                      if (name.length() > 1)
119                         setter = setter.concat(name.substring(1));
120                      Method JavaDoc method = implClass.getMethod(setter, new Class JavaDoc[] { type });
121                      if (trace)
122                         log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object);
123                      method.invoke(result, new Object JavaDoc[] { object });
124                   }
125                   catch (InvocationTargetException JavaDoc e)
126                   {
127                      DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException());
128                   }
129                   catch (Throwable JavaDoc t)
130                   {
131                      DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
132                   }
133                }
134             }
135          }
136       }
137       
138       // Apply the properties
139
if (properties != null)
140       {
141          for (Iterator JavaDoc i = properties.entrySet().iterator(); i.hasNext();)
142          {
143             Map.Entry JavaDoc property = (Map.Entry JavaDoc) i.next();
144             String JavaDoc name = (String JavaDoc) property.getKey();
145             String JavaDoc value = (String JavaDoc) property.getValue();
146             if (trace)
147                log.trace("AdminObject '" + jndiName + "' property=" + name + " value=" + value);
148             
149             // Pick up the property metadata
150
ConfigPropertyMetaData cpmd = aomd.getProperty(name);
151             if (cpmd == null)
152                throw new DeploymentException("No property '" + name + "' for admin object '" + interfaceClass + "' ra=" + rarName);
153             if (trace)
154                log.trace("AdminObject '" + jndiName + "' property=" + name + " metadata=" + cpmd);
155
156             // Load the property class as defined in the meta data
157
String JavaDoc typeName = cpmd.getType();
158             if (trace)
159                log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName);
160             Class JavaDoc type = cl.loadClass(typeName);
161             
162             // Find the property editor for this class
163
PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(type);
164             if (editor == null)
165                throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName);
166             editor.setAsText(value);
167             Object JavaDoc object = editor.getValue();
168             
169             try
170             {
171                String JavaDoc setter = "set" + Character.toUpperCase(name.charAt(0));
172                if (name.length() > 1)
173                   setter = setter.concat(name.substring(1));
174                Method JavaDoc method = implClass.getMethod(setter, new Class JavaDoc[] { type });
175                if (trace)
176                   log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object);
177                method.invoke(result, new Object JavaDoc[] { object });
178             }
179             catch (InvocationTargetException JavaDoc e)
180             {
181                DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException());
182             }
183             catch (Throwable JavaDoc t)
184             {
185                DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
186             }
187          }
188       }
189       
190       return result;
191    }
192 }
193
Popular Tags