001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: */package org.apache.geronimo.naming.deployment;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Method;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.HashMap;
023:
024: import javax.annotation.Resource;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.geronimo.common.DeploymentException;
029: import org.apache.geronimo.gbean.GBeanInfo;
030: import org.apache.geronimo.gbean.GBeanInfoBuilder;
031: import org.apache.geronimo.gbean.GBeanLifecycle;
032: import org.apache.geronimo.j2ee.deployment.Module;
033: import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedApp;
034: import org.apache.geronimo.j2ee.deployment.annotation.ResourceAnnotationHelper;
035: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
036: import org.apache.geronimo.naming.reference.KernelReference;
037: import org.apache.geronimo.xbeans.javaee.DescriptionType;
038: import org.apache.geronimo.xbeans.javaee.EnvEntryType;
039: import org.apache.geronimo.xbeans.javaee.EnvEntryTypeValuesType;
040: import org.apache.geronimo.xbeans.javaee.InjectionTargetType;
041: import org.apache.geronimo.xbeans.javaee.JndiNameType;
042: import org.apache.geronimo.xbeans.javaee.XsdStringType;
043: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
044: import org.apache.xmlbeans.QNameSet;
045: import org.apache.xmlbeans.XmlObject;
046:
047: /**
048: * @version $Rev: 610760 $ $Date: 2008-01-10 03:07:04 -0800 (Thu, 10 Jan 2008) $
049: */
050: public class EnvironmentEntryBuilder extends AbstractNamingBuilder
051: implements GBeanLifecycle {
052:
053: private static final Log log = LogFactory
054: .getLog(EnvironmentEntryBuilder.class);
055: private static final Map<String, String> NAMESPACE_UPDATES = new HashMap<String, String>();
056: static {
057: NAMESPACE_UPDATES.put(
058: "http://geronimo.apache.org/xml/ns/naming",
059: "http://geronimo.apache.org/xml/ns/naming-1.2");
060: NAMESPACE_UPDATES.put(
061: "http://geronimo.apache.org/xml/ns/naming-1.1",
062: "http://geronimo.apache.org/xml/ns/naming-1.2");
063: }
064:
065: private final QNameSet envEntryQNameSet;
066:
067: public EnvironmentEntryBuilder(String[] eeNamespaces) {
068: envEntryQNameSet = buildQNameSet(eeNamespaces, "env-entry");
069: }
070:
071: public void doStart() throws Exception {
072: XmlBeansUtil.registerNamespaceUpdates(NAMESPACE_UPDATES);
073: }
074:
075: public void doStop() {
076: XmlBeansUtil.unregisterNamespaceUpdates(NAMESPACE_UPDATES);
077: }
078:
079: public void doFail() {
080: doStop();
081: }
082:
083: public void buildNaming(XmlObject specDD, XmlObject plan,
084: Module module, Map componentContext)
085: throws DeploymentException {
086:
087: // Discover and process any @Resource annotations (if !metadata-complete)
088: if ((module != null) && (module.getClassFinder() != null)) {
089:
090: // Process all the annotations for this naming builder type
091: try {
092: ResourceAnnotationHelper.processAnnotations(module
093: .getAnnotatedApp(), module.getClassFinder(),
094: EnvEntryRefProcessor.INSTANCE);
095: } catch (Exception e) {
096: log.warn(
097: "Unable to process @Resource annotations for module"
098: + module.getName(), e);
099: }
100: }
101:
102: List<EnvEntryType> envEntriesUntyped = convert(specDD
103: .selectChildren(envEntryQNameSet), JEE_CONVERTER,
104: EnvEntryType.class, EnvEntryType.type);
105: for (EnvEntryType envEntry : envEntriesUntyped) {
106: String name = getStringValue(envEntry.getEnvEntryName());
107: addInjections(name, envEntry.getInjectionTargetArray(),
108: componentContext);
109: String type = getStringValue(envEntry.getEnvEntryType());
110: String text = getStringValue(envEntry.getEnvEntryValue());
111: try {
112: Object value;
113: if (text == null) {
114: if ("org.apache.geronimo.kernel.Kernel"
115: .equals(type)) {
116: value = new KernelReference();
117: } else {
118: value = null;
119: }
120: } else if ("java.lang.String".equals(type)) {
121: value = text;
122: } else if ("java.lang.Character".equals(type)) {
123: value = text.charAt(0);
124: } else if ("java.lang.Boolean".equals(type)) {
125: value = Boolean.valueOf(text);
126: } else if ("java.lang.Byte".equals(type)) {
127: value = Byte.valueOf(text);
128: } else if ("java.lang.Short".equals(type)) {
129: value = Short.valueOf(text);
130: } else if ("java.lang.Integer".equals(type)) {
131: value = Integer.valueOf(text);
132: } else if ("java.lang.Long".equals(type)) {
133: value = Long.valueOf(text);
134: } else if ("java.lang.Float".equals(type)) {
135: value = Float.valueOf(text);
136: } else if ("java.lang.Double".equals(type)) {
137: value = Double.valueOf(text);
138: } else {
139: throw new DeploymentException("unrecognized type: "
140: + type);
141: }
142: getJndiContextMap(componentContext).put(ENV + name,
143: value);
144: } catch (NumberFormatException e) {
145: throw new DeploymentException(
146: "Invalid env-entry value for name: " + name, e);
147: }
148: }
149:
150: }
151:
152: public QNameSet getSpecQNameSet() {
153: return envEntryQNameSet;
154: }
155:
156: public QNameSet getPlanQNameSet() {
157: return QNameSet.EMPTY;
158: }
159:
160: public static class EnvEntryRefProcessor extends
161: ResourceAnnotationHelper.ResourceProcessor {
162:
163: public static final EnvEntryRefProcessor INSTANCE = new EnvEntryRefProcessor();
164:
165: private EnvEntryRefProcessor() {
166: }
167:
168: public boolean processResource(AnnotatedApp annotatedApp,
169: Resource annotation, Class cls, Method method,
170: Field field) {
171: String resourceName = getResourceName(annotation, method,
172: field);
173: String resourceType = getResourceType(annotation, method,
174: field);
175: if (resourceType.equals("java.lang.String")
176: || resourceType.equals("java.lang.Character")
177: || resourceType.equals("java.lang.Integer")
178: || resourceType.equals("java.lang.Boolean")
179: || resourceType.equals("java.lang.Double")
180: || resourceType.equals("java.lang.Byte")
181: || resourceType.equals("java.lang.Short")
182: || resourceType.equals("java.lang.Long")
183: || resourceType.equals("java.lang.Float")) {
184:
185: log.debug("addResource(): <env-entry> found");
186:
187: boolean exists = false;
188: EnvEntryType[] envEntries = annotatedApp
189: .getEnvEntryArray();
190: for (EnvEntryType envEntry : envEntries) {
191: if (getStringValue(envEntry.getEnvEntryName())
192: .equals(resourceName)) {
193: exists = true;
194: if (method != null || field != null) {
195: InjectionTargetType[] targets = envEntry
196: .getInjectionTargetArray();
197: if (!hasTarget(method, field, targets)) {
198: configureInjectionTarget(envEntry
199: .addNewInjectionTarget(),
200: method, field);
201: }
202: }
203: break;
204: }
205: }
206: if (!exists) {
207: try {
208:
209: log
210: .debug("addResource(): Does not exist in DD: "
211: + resourceName);
212:
213: // Doesn't exist in deployment descriptor -- add new
214: EnvEntryType envEntry = annotatedApp
215: .addNewEnvEntry();
216:
217: //------------------------------------------------------------------------------
218: // <env-entry> required elements:
219: //------------------------------------------------------------------------------
220:
221: // env-entry-name
222: JndiNameType envEntryName = envEntry
223: .addNewEnvEntryName();
224: envEntryName.setStringValue(resourceName);
225:
226: if (!resourceType.equals("")) {
227: // env-entry-type
228: EnvEntryTypeValuesType envEntryType = envEntry
229: .addNewEnvEntryType();
230: envEntryType.setStringValue(resourceType);
231: } else if (method != null || field != null) {
232: // injectionTarget
233: InjectionTargetType injectionTarget = envEntry
234: .addNewInjectionTarget();
235: configureInjectionTarget(injectionTarget,
236: method, field);
237: }
238:
239: // env-entry-value
240: String mappdedNameAnnotation = annotation
241: .mappedName();
242: if (!mappdedNameAnnotation.equals("")) {
243: XsdStringType value = envEntry
244: .addNewEnvEntryValue();
245: value.setStringValue(mappdedNameAnnotation);
246: envEntry.setMappedName(value);
247: }
248:
249: //------------------------------------------------------------------------------
250: // <env-entry> optional elements:
251: //------------------------------------------------------------------------------
252:
253: // description
254: String descriptionAnnotation = annotation
255: .description();
256: if (!descriptionAnnotation.equals("")) {
257: DescriptionType description = envEntry
258: .addNewDescription();
259: description
260: .setStringValue(descriptionAnnotation);
261: }
262:
263: } catch (Exception anyException) {
264: log
265: .debug("ResourceAnnotationHelper: Exception caught while processing <env-entry>");
266: }
267: }
268: }
269: return false;
270: }
271: }
272:
273: public static final GBeanInfo GBEAN_INFO;
274:
275: static {
276: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
277: EnvironmentEntryBuilder.class,
278: NameFactory.MODULE_BUILDER);
279: infoBuilder.addAttribute("eeNamespaces", String[].class, true,
280: true);
281: infoBuilder.setConstructor(new String[] { "eeNamespaces" });
282:
283: GBEAN_INFO = infoBuilder.getBeanInfo();
284: }
285:
286: public static GBeanInfo getGBeanInfo() {
287: return GBEAN_INFO;
288: }
289:
290: }
|