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 java.util.Map;
019: import java.util.HashMap;
020:
021: import org.apache.openejb.util.LogCategory;
022: import org.apache.openejb.util.Messages;
023: import org.apache.openejb.util.Logger;
024: import org.apache.openejb.loader.SystemInstance;
025: import org.apache.openejb.OpenEJBException;
026: import org.apache.openejb.jee.oejb3.OpenejbJar;
027: import org.apache.openejb.jee.oejb3.EjbDeployment;
028: import org.apache.openejb.jee.EnterpriseBean;
029: import org.codehaus.swizzle.stream.StringTemplate;
030:
031: public class InitEjbDeployments implements DynamicDeployer {
032: public static Messages messages = new Messages(
033: "org.apache.openejb.util.resources");
034: public static Logger logger = Logger.getInstance(
035: LogCategory.OPENEJB, "org.apache.openejb.util.resources");
036:
037: private final StringTemplate deploymentIdTemplate;
038: private static final String DEPLOYMENT_ID_FORMAT = "openejb.deploymentId.format";
039:
040: public InitEjbDeployments() {
041: String format = SystemInstance.get().getProperty(
042: DEPLOYMENT_ID_FORMAT, "{ejbName}");
043: this .deploymentIdTemplate = new StringTemplate(format);
044: }
045:
046: public synchronized AppModule deploy(AppModule appModule)
047: throws OpenEJBException {
048: Map<String, String> contextData = new HashMap<String, String>();
049: contextData.put("appId", appModule.getModuleId());
050: for (EjbModule ejbModule : appModule.getEjbModules()) {
051: contextData.put("ejbJarId", ejbModule.getModuleId());
052: deploy(ejbModule, contextData);
053: }
054: contextData.clear();
055: return appModule;
056: }
057:
058: public EjbModule deploy(EjbModule ejbModule,
059: Map<String, String> contextData) throws OpenEJBException {
060: contextData.put("moduleId", ejbModule.getModuleId());
061:
062: OpenejbJar openejbJar;
063: if (ejbModule.getOpenejbJar() != null) {
064: openejbJar = ejbModule.getOpenejbJar();
065: } else {
066: openejbJar = new OpenejbJar();
067: ejbModule.setOpenejbJar(openejbJar);
068: }
069:
070: StringTemplate deploymentIdTemplate = this .deploymentIdTemplate;
071: if (openejbJar.getProperties()
072: .containsKey(DEPLOYMENT_ID_FORMAT)) {
073: String format = openejbJar.getProperties().getProperty(
074: DEPLOYMENT_ID_FORMAT);
075: logger.info("Using " + DEPLOYMENT_ID_FORMAT + " '" + format
076: + "'");
077: deploymentIdTemplate = new StringTemplate(format);
078: }
079:
080: for (EnterpriseBean bean : ejbModule.getEjbJar()
081: .getEnterpriseBeans()) {
082: EjbDeployment ejbDeployment = openejbJar
083: .getDeploymentsByEjbName().get(bean.getEjbName());
084: if (ejbDeployment == null) {
085:
086: ejbDeployment = new EjbDeployment();
087:
088: ejbDeployment.setEjbName(bean.getEjbName());
089: ejbDeployment.setDeploymentId(autoAssignDeploymentId(
090: bean, contextData, deploymentIdTemplate));
091:
092: logger.info("Auto-deploying ejb " + bean.getEjbName()
093: + ": EjbDeployment(deployment-id="
094: + ejbDeployment.getDeploymentId() + ")");
095: openejbJar.getEjbDeployment().add(ejbDeployment);
096: } else {
097: if (ejbDeployment.getDeploymentId() == null) {
098: ejbDeployment
099: .setDeploymentId(autoAssignDeploymentId(
100: bean, contextData,
101: deploymentIdTemplate));
102: logger.info("Auto-assigning deployment-id for ejb "
103: + bean.getEjbName()
104: + ": EjbDeployment(deployment-id="
105: + ejbDeployment.getDeploymentId() + ")");
106: }
107: }
108: }
109:
110: return ejbModule;
111: }
112:
113: private String autoAssignDeploymentId(EnterpriseBean bean,
114: Map<String, String> contextData, StringTemplate template) {
115: contextData.put("ejbType", bean.getClass().getSimpleName());
116: contextData.put("ejbClass", bean.getClass().getName());
117: contextData.put("ejbClass.simpleName", bean.getClass()
118: .getSimpleName());
119: contextData.put("ejbName", bean.getEjbName());
120: return template.apply(contextData);
121: }
122: }
|