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.openejb.config;
017:
018: import org.apache.openejb.OpenEJBException;
019: import org.apache.openejb.util.Logger;
020: import org.apache.openejb.util.LogCategory;
021: import org.apache.openejb.jee.EnvEntry;
022: import org.apache.openejb.jee.EnterpriseBean;
023: import org.apache.openejb.jee.JndiConsumer;
024:
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.net.URL;
028: import java.util.Collections;
029: import java.util.HashMap;
030: import java.util.Map;
031: import java.util.Properties;
032:
033: /**
034: * Read in all the properties for an app's META-INF/env-entries.properties file
035: * and create <env-entry> tags for each in the component's enc.
036: *
037: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
038: */
039: public class EnvEntriesPropertiesDeployer implements DynamicDeployer {
040:
041: private static final Logger log = Logger.getInstance(
042: LogCategory.OPENEJB_STARTUP_CONFIG,
043: EnvEntriesPropertiesDeployer.class);
044:
045: public AppModule deploy(AppModule appModule)
046: throws OpenEJBException {
047:
048: // ApplicationClient META-INF/env-entries.properties
049: for (ClientModule module : appModule.getClientModules()) {
050: for (Map.Entry<String, String> entry : getEnvEntries(module)
051: .entrySet()) {
052: EnvEntry envEntry = new EnvEntry(entry.getKey(),
053: "java.lang.String", entry.getValue());
054: apply(module.getApplicationClient(), envEntry,
055: "AppClient");
056: }
057: }
058:
059: // WebModule META-INF/env-entries.properties
060: for (WebModule webModule : appModule.getWebModules()) {
061: deploy(webModule);
062: }
063:
064: // Resource Adapters do not have an ENC
065:
066: // EjbJar META-INF/env-entries.properties
067: for (EjbModule module : appModule.getEjbModules()) {
068: for (Map.Entry<String, String> entry : getEnvEntries(module)
069: .entrySet()) {
070: EnvEntry envEntry = new EnvEntry(entry.getKey(),
071: "java.lang.String", entry.getValue());
072:
073: // To override a specific ejb only
074: // the following format is used:
075: // <ejb-name>.name = value
076: if (envEntry.getName().contains(".")) {
077: String name = envEntry.getName();
078: String ejbName = name.substring(0, name
079: .indexOf('.'));
080: name = name.substring(name.indexOf('.') + 1);
081: EnterpriseBean bean = module.getEjbJar()
082: .getEnterpriseBean(ejbName);
083: if (bean != null) {
084: // Set the new property name without the <ejb-name>. prefix
085: envEntry.setName(name);
086: apply(bean, envEntry, bean.getEjbName());
087: continue;
088: }
089: }
090:
091: for (EnterpriseBean bean : module.getEjbJar()
092: .getEnterpriseBeans()) {
093: apply(bean, envEntry, bean.getEjbName());
094: }
095: }
096: }
097:
098: return appModule;
099: }
100:
101: public WebModule deploy(WebModule webModule) {
102: for (Map.Entry<String, String> entry : getEnvEntries(webModule)
103: .entrySet()) {
104: EnvEntry envEntry = new EnvEntry(entry.getKey(),
105: "java.lang.String", entry.getValue());
106: apply(webModule.getWebApp(), envEntry, "WebApp");
107: }
108: return webModule;
109: }
110:
111: private void apply(JndiConsumer bean, EnvEntry newEntry,
112: String componentName) {
113: EnvEntry entry = bean.getEnvEntryMap().get(newEntry.getName());
114: if (entry != null) {
115: log.debug("envprops.override", componentName, entry
116: .getName(), entry.getEnvEntryValue(), newEntry
117: .getEnvEntryValue());
118: entry.setEnvEntryValue(newEntry.getEnvEntryValue());
119: return;
120: }
121:
122: // Must not be an override, just add the new entry
123: log.debug("envprops.add", componentName, newEntry.getName(),
124: newEntry.getEnvEntryValue());
125: bean.getEnvEntry().add(newEntry);
126: }
127:
128: @SuppressWarnings({"unchecked"})
129: private Map<String, String> getEnvEntries(DeploymentModule module) {
130: URL propsUrl = (URL) module.getAltDDs().get(
131: "env-entries.properties");
132: if (propsUrl == null)
133: return Collections.emptyMap();
134: try {
135:
136: InputStream in = propsUrl.openStream();
137: Properties envEntriesProps = new Properties();
138: envEntriesProps.load(in);
139:
140: return new HashMap(envEntriesProps);
141: } catch (IOException e) {
142: log.error("envprops.notLoaded", e, module.getModuleId(),
143: propsUrl.toExternalForm());
144: return Collections.emptyMap();
145: }
146: }
147: }
|