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.jee.oejb3.OpenejbJar;
020: import org.apache.openejb.jee.oejb3.EjbDeployment;
021: import org.apache.openejb.jee.oejb3.ResourceLink;
022: import org.apache.openejb.jee.EnterpriseBean;
023: import org.apache.openejb.jee.MessageDrivenBean;
024: import org.apache.openejb.jee.EjbJar;
025: import org.apache.openejb.jee.EntityBean;
026: import org.apache.openejb.jee.AssemblyDescriptor;
027: import org.apache.openejb.jee.MethodPermission;
028: import org.apache.openejb.jee.Method;
029: import org.apache.openejb.jee.ContainerTransaction;
030: import org.apache.openejb.jee.InterceptorBinding;
031: import org.apache.openejb.jee.ResourceEnvRef;
032: import org.apache.openejb.jee.MessageDestinationRef;
033: import org.apache.openejb.jee.ResourceRef;
034: import org.apache.openejb.jee.Interceptor;
035: import org.apache.openejb.jee.JndiConsumer;
036:
037: import java.util.Map;
038: import java.util.ArrayList;
039: import java.util.List;
040: import java.util.Collection;
041:
042: /**
043: * @version $Rev: 607160 $ $Date: 2007-12-27 18:22:44 -0800 $
044: */
045: class DebuggableVmHackery implements DynamicDeployer {
046:
047: public AppModule deploy(AppModule appModule)
048: throws OpenEJBException {
049:
050: for (EjbModule ejbModule : appModule.getEjbModules()) {
051: EjbJar ejbJar = ejbModule.getEjbJar();
052: OpenejbJar openejbJar = ejbModule.getOpenejbJar();
053: Map<String, EjbDeployment> deployments = openejbJar
054: .getDeploymentsByEjbName();
055:
056: ejbJar.setRelationships(null);
057:
058: List<String> removed = new ArrayList<String>();
059:
060: for (EnterpriseBean bean : ejbJar.getEnterpriseBeans()) {
061:
062: String ejbName = bean.getEjbName();
063: EjbDeployment ejbDeployment = deployments.get(ejbName);
064:
065: pruneRefs(bean, ejbDeployment);
066:
067: if (!(bean instanceof MessageDrivenBean)
068: && !(bean instanceof EntityBean)) {
069: continue;
070: }
071:
072: ejbJar.removeEnterpriseBean(ejbName);
073: openejbJar.removeEjbDeployment(ejbDeployment);
074: removed.add(ejbName);
075:
076: AssemblyDescriptor assemblyDescriptor = ejbJar
077: .getAssemblyDescriptor();
078: if (assemblyDescriptor != null) {
079: for (MethodPermission permission : copy(assemblyDescriptor
080: .getMethodPermission())) {
081: for (Method method : copy(permission
082: .getMethod())) {
083: if (method.getEjbName().equals(ejbName)) {
084: permission.getMethod().remove(method);
085: }
086: }
087: if (permission.getMethod().size() == 0) {
088: assemblyDescriptor.getMethodPermission()
089: .remove(permission);
090: }
091: }
092:
093: for (ContainerTransaction transaction : copy(assemblyDescriptor
094: .getContainerTransaction())) {
095: for (Method method : copy(transaction
096: .getMethod())) {
097: if (method.getEjbName().equals(ejbName)) {
098: transaction.getMethod().remove(method);
099: }
100: }
101: if (transaction.getMethod().size() == 0) {
102: assemblyDescriptor
103: .getContainerTransaction().remove(
104: transaction);
105: }
106: }
107:
108: for (InterceptorBinding binding : copy(assemblyDescriptor
109: .getInterceptorBinding())) {
110: if (binding.getEjbName().equals(ejbName)) {
111: assemblyDescriptor.getInterceptorBinding()
112: .remove(binding);
113: }
114: }
115: }
116: }
117:
118: // Drop any ejb ref to with an ejb-link to a removed ejb
119: for (EnterpriseBean bean : ejbJar.getEnterpriseBeans()) {
120: bean.getEjbLocalRefMap().keySet().removeAll(removed);
121: bean.getEjbRefMap().keySet().removeAll(removed);
122: }
123:
124: for (Interceptor interceptor : ejbJar.getInterceptors()) {
125: pruneRefs(interceptor, new EjbDeployment());
126: }
127:
128: }
129: return appModule;
130: }
131:
132: private void pruneRefs(JndiConsumer bean,
133: EjbDeployment ejbDeployment) {
134: for (ResourceRef ref : copy(bean.getResourceRef())) {
135: if (ref.getResType().startsWith("javax.jms.")) {
136: ResourceLink resourceLink = ejbDeployment
137: .getResourceLink(ref.getName());
138: ejbDeployment.getResourceLink().remove(resourceLink);
139: bean.getResourceRef().remove(ref);
140: }
141: }
142:
143: for (ResourceEnvRef ref : bean.getResourceEnvRef()) {
144: ResourceLink resourceLink = ejbDeployment
145: .getResourceLink(ref.getName());
146: ejbDeployment.getResourceLink().remove(resourceLink);
147: }
148: bean.getResourceEnvRef().clear();
149:
150: for (MessageDestinationRef ref : bean
151: .getMessageDestinationRef()) {
152: ResourceLink resourceLink = ejbDeployment
153: .getResourceLink(ref.getName());
154: ejbDeployment.getResourceLink().remove(resourceLink);
155: }
156: bean.getMessageDestinationRef().clear();
157:
158: bean.getPersistenceContextRef().clear();
159: bean.getPersistenceUnitRef().clear();
160: }
161:
162: public <T> List<T> copy(Collection<T> list) {
163: return new ArrayList<T>(list);
164: }
165: }
|