001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.deployment;
023:
024: import java.beans.IntrospectionException;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.Properties;
028:
029: import javax.management.ObjectName;
030: import javax.resource.spi.ActivationSpec;
031:
032: import org.jboss.deployment.DeploymentException;
033: import org.jboss.logging.Logger;
034: import org.jboss.metadata.ActivationConfigPropertyMetaData;
035: import org.jboss.resource.metadata.MessageListenerMetaData;
036: import org.jboss.resource.metadata.RequiredConfigPropertyMetaData;
037: import org.jboss.util.propertyeditor.PropertyEditors;
038:
039: /**
040: * An activation spec factory
041: *
042: * @author <a href="adrian@jboss.com">Adrian Brock</a>
043: * @version $Revision: 57189 $
044: */
045: public class ActivationSpecFactory {
046: /** The logger */
047: private static final Logger log = Logger
048: .getLogger(ActivationSpecFactory.class);
049:
050: public static ActivationSpec createActivationSpec(
051: ObjectName rarName, String messagingType,
052: Collection activationConfig, MessageListenerMetaData mlmd)
053: throws Exception {
054: boolean trace = log.isTraceEnabled();
055:
056: if (trace)
057: log.trace("Create ActivationSpec rar=" + rarName
058: + " messagingType=" + messagingType
059: + " activationConfig=" + activationConfig
060: + " messageListner=" + mlmd);
061:
062: // Check we have all the required properties
063: for (Iterator i = mlmd.getRequiredConfigProperties().iterator(); i
064: .hasNext();) {
065: RequiredConfigPropertyMetaData rcpmd = (RequiredConfigPropertyMetaData) i
066: .next();
067:
068: String rcp = rcpmd.getName();
069: String rcpName = rcp.substring(0, 1).toUpperCase();
070: if (rcp.length() > 1)
071: rcpName = rcpName.concat(rcp.substring(1));
072: if (trace)
073: log.trace("Checking required config " + rcpName);
074:
075: boolean found = false;
076: for (Iterator j = activationConfig.iterator(); j.hasNext();) {
077: ActivationConfigPropertyMetaData acpmd = (ActivationConfigPropertyMetaData) j
078: .next();
079:
080: String acp = acpmd.getName();
081: String acpName = acp.substring(0, 1).toUpperCase();
082: if (acp.length() > 1)
083: acpName = acpName.concat(acp.substring(1));
084:
085: if (trace)
086: log.trace("Checking required config " + rcpName
087: + " against " + acpName + " result="
088: + rcpName.equals(acpName));
089:
090: if (rcpName.equals(acpName)) {
091: if (trace)
092: log.trace("Found required config " + rcp + " "
093: + acpmd);
094: found = true;
095: break;
096: }
097: }
098: if (found == false)
099: throw new DeploymentException(
100: "Required config property " + rcpmd
101: + " for messagingType '"
102: + messagingType
103: + "' not found in activation config "
104: + activationConfig + " ra=" + rarName);
105: }
106:
107: // Determine the activation spec class
108: String className = mlmd.getActivationSpecType();
109: if (className == null)
110: throw new DeploymentException(
111: "No activation spec type for messagingType '"
112: + messagingType + "' ra=" + rarName);
113:
114: // Load the class
115: if (trace)
116: log.trace("Loading ActivationSpec class=" + className);
117: Class asClass = Thread.currentThread().getContextClassLoader()
118: .loadClass(className);
119: if (ActivationSpec.class.isAssignableFrom(asClass) == false)
120: throw new DeploymentException(asClass.getName()
121: + " is not an activation spec class '"
122: + messagingType + "' ra=" + rarName);
123: ActivationSpec result = (ActivationSpec) asClass.newInstance();
124: if (trace)
125: log.trace("Instantiated ActivationSpec class=" + result);
126:
127: /* Apply the properties to the ActivationSpec java bean using the util
128: PropertyEditors.mapJavaBeanProperties method.
129: */
130: Properties beanProps = new Properties();
131: for (Iterator i = activationConfig.iterator(); i.hasNext();) {
132: ActivationConfigPropertyMetaData acpmd = (ActivationConfigPropertyMetaData) i
133: .next();
134: String name = acpmd.getName();
135: String value = acpmd.getValue();
136: beanProps.setProperty(name, value);
137: }
138: if (trace)
139: log.trace("Configuring ActivationSpec properties="
140: + beanProps);
141: try {
142: PropertyEditors.mapJavaBeanProperties(result, beanProps);
143: } catch (IntrospectionException e) {
144: String msg = "Error for ActivationSpec class "
145: + asClass.getName() + " as JavaBean";
146: DeploymentException.rethrowAsDeploymentException(msg, e);
147: }
148:
149: // Validate the activation spec
150: try {
151: if (trace)
152: log
153: .trace("Trying to validate ActivationSpec "
154: + result);
155: result.validate();
156: } catch (UnsupportedOperationException e) {
157: log
158: .debug("Validation is not supported for ActivationSpec: "
159: + className);
160: }
161:
162: return result;
163: }
164: }
|