KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntrospectionException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.management.ObjectName JavaDoc;
30 import javax.resource.spi.ActivationSpec JavaDoc;
31
32 import org.jboss.deployment.DeploymentException;
33 import org.jboss.logging.Logger;
34 import org.jboss.metadata.ActivationConfigPropertyMetaData;
35 import org.jboss.resource.metadata.MessageListenerMetaData;
36 import org.jboss.resource.metadata.RequiredConfigPropertyMetaData;
37 import org.jboss.util.propertyeditor.PropertyEditors;
38
39 /**
40  * An activation spec factory
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @version $Revision: 38341 $
44  */

45 public class ActivationSpecFactory
46 {
47    /** The logger */
48    private static final Logger log = Logger.getLogger(ActivationSpecFactory.class);
49
50    public static ActivationSpec JavaDoc createActivationSpec(ObjectName JavaDoc rarName,
51       String JavaDoc messagingType, Collection JavaDoc activationConfig,
52       MessageListenerMetaData mlmd)
53       throws Exception JavaDoc
54    {
55       boolean trace = log.isTraceEnabled();
56       
57       if (trace)
58          log.trace("Create ActivationSpec rar=" + rarName + " messagingType=" + messagingType +
59             " activationConfig=" + activationConfig + " messageListner=" + mlmd);
60       
61       // Check we have all the required properties
62
for (Iterator JavaDoc i = mlmd.getRequiredConfigProperties().iterator(); i.hasNext();)
63       {
64          RequiredConfigPropertyMetaData rcpmd = (RequiredConfigPropertyMetaData) i.next();
65
66          String JavaDoc rcp = rcpmd.getName();
67          String JavaDoc rcpName = rcp.substring(0, 1).toUpperCase();
68          if (rcp.length() > 1)
69             rcpName = rcpName.concat(rcp.substring(1));
70          if (trace)
71             log.trace("Checking required config " + rcpName);
72
73          boolean found = false;
74          for (Iterator JavaDoc j = activationConfig.iterator(); j.hasNext();)
75          {
76             ActivationConfigPropertyMetaData acpmd = (ActivationConfigPropertyMetaData) j.next();
77             
78             String JavaDoc acp = acpmd.getName();
79             String JavaDoc acpName = acp.substring(0, 1).toUpperCase();
80             if (acp.length() > 1)
81                acpName = acpName.concat(acp.substring(1));
82
83             if (trace)
84                log.trace("Checking required config " + rcpName + " against " + acpName + " result=" + rcpName.equals(acpName));
85             
86             if (rcpName.equals(acpName))
87             {
88                if (trace)
89                   log.trace("Found required config " + rcp + " " + acpmd);
90                found = true;
91                break;
92             }
93          }
94          if (found == false)
95             throw new DeploymentException("Required config property " + rcpmd + " for messagingType '" + messagingType +
96                "' not found in activation config " + activationConfig + " ra=" + rarName);
97       }
98
99       // Determine the activation spec class
100
String JavaDoc className = mlmd.getActivationSpecType();
101       if (className == null)
102          throw new DeploymentException("No activation spec type for messagingType '" + messagingType + "' ra=" + rarName);
103       
104       // Load the class
105
if (trace)
106          log.trace("Loading ActivationSpec class=" + className);
107       Class JavaDoc asClass = Thread.currentThread().getContextClassLoader().loadClass(className);
108       if (ActivationSpec JavaDoc.class.isAssignableFrom(asClass) == false)
109          throw new DeploymentException(asClass.getName() + " is not an activation spec class '" + messagingType + "' ra=" + rarName);
110       ActivationSpec JavaDoc result = (ActivationSpec JavaDoc) asClass.newInstance();
111       if (trace)
112          log.trace("Instantiated ActivationSpec class=" + result);
113
114       /* Apply the properties to the ActivationSpec java bean using the util
115       PropertyEditors.mapJavaBeanProperties method.
116       */

117       Properties JavaDoc beanProps = new Properties JavaDoc();
118       for (Iterator JavaDoc i = activationConfig.iterator(); i.hasNext();)
119       {
120          ActivationConfigPropertyMetaData acpmd = (ActivationConfigPropertyMetaData) i.next();
121          String JavaDoc name = acpmd.getName();
122          String JavaDoc value = acpmd.getValue();
123          beanProps.setProperty(name, value);
124       }
125       if (trace)
126          log.trace("Configuring ActivationSpec properties=" + beanProps);
127       try
128       {
129          PropertyEditors.mapJavaBeanProperties(result, beanProps);
130       }
131       catch(IntrospectionException JavaDoc e)
132       {
133          String JavaDoc msg = "Error for ActivationSpec class " + asClass.getName()
134             + " as JavaBean";
135          DeploymentException.rethrowAsDeploymentException(msg, e);
136       }
137
138       // Validate the activation spec
139
try
140       {
141          if (trace)
142             log.trace("Trying to validate ActivationSpec " + result);
143          result.validate();
144       }
145       catch (UnsupportedOperationException JavaDoc e)
146       {
147          log.debug("Validation is not supported for ActivationSpec: " + className);
148       }
149       
150       return result;
151    }
152 }
153
Popular Tags