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.assembler.classic;
017:
018: import static org.apache.openejb.assembler.classic.MethodInfoUtil.resolveAttributes;
019: import org.apache.openejb.core.CoreDeploymentInfo;
020: import org.apache.openejb.OpenEJBException;
021: import org.apache.openejb.DeploymentInfo;
022: import org.apache.openejb.util.Logger;
023: import org.apache.openejb.util.LogCategory;
024:
025: import java.util.List;
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.ArrayList;
029: import java.util.Collections;
030: import java.lang.reflect.Method;
031:
032: /**
033: * @version $Rev: 638255 $ $Date: 2008-03-18 01:02:11 -0700 $
034: */
035: public class MethodTransactionBuilder {
036:
037: public static final Logger logger = Logger
038: .getInstance(LogCategory.OPENEJB_STARTUP,
039: MethodTransactionBuilder.class);
040:
041: public void build(HashMap<String, DeploymentInfo> deployments,
042: List<MethodTransactionInfo> methodTransactions)
043: throws OpenEJBException {
044: for (DeploymentInfo deploymentInfo : deployments.values()) {
045: applyTransactionAttributes(
046: (CoreDeploymentInfo) deploymentInfo,
047: methodTransactions);
048: }
049: }
050:
051: public static void applyTransactionAttributes(
052: CoreDeploymentInfo deploymentInfo,
053: List<MethodTransactionInfo> methodTransactionInfos)
054: throws OpenEJBException {
055:
056: methodTransactionInfos = normalize(methodTransactionInfos);
057:
058: Map<Method, MethodAttributeInfo> attributes = resolveAttributes(
059: methodTransactionInfos, deploymentInfo);
060:
061: for (Map.Entry<Method, MethodAttributeInfo> entry : attributes
062: .entrySet()) {
063: MethodTransactionInfo value = (MethodTransactionInfo) entry
064: .getValue();
065:
066: // logger.info(entry.getKey().toString() +" "+ value.transAttribute);
067: deploymentInfo.setMethodTransactionAttribute(
068: entry.getKey(), value.transAttribute);
069: }
070: }
071:
072: /**
073: * This method splits the MethodTransactionInfo objects so that there is
074: * exactly one MethodInfo per MethodTransactionInfo. A single MethodTransactionInfo
075: * with three MethodInfos would be expanded into three MethodTransactionInfo with
076: * one MethodInfo each.
077: *
078: * The MethodTransactionInfo list is then sorted from least to most specific.
079: *
080: * @param infos
081: * @return a normalized list of new MethodTransactionInfo objects
082: */
083: public static List<MethodTransactionInfo> normalize(
084: List<MethodTransactionInfo> infos) {
085: List<MethodTransactionInfo> normalized = new ArrayList<MethodTransactionInfo>();
086: for (MethodTransactionInfo oldInfo : infos) {
087: for (MethodInfo methodInfo : oldInfo.methods) {
088: MethodTransactionInfo newInfo = new MethodTransactionInfo();
089: newInfo.description = oldInfo.description;
090: newInfo.methods.add(methodInfo);
091: newInfo.transAttribute = oldInfo.transAttribute;
092:
093: normalized.add(newInfo);
094: }
095: }
096:
097: Collections.reverse(normalized);
098: Collections.sort(normalized, new MethodTransactionComparator());
099:
100: return normalized;
101: }
102:
103: public static class MethodTransactionComparator extends
104: MethodInfoUtil.BaseComparator<MethodTransactionInfo> {
105: public int compare(MethodTransactionInfo a,
106: MethodTransactionInfo b) {
107: return compare(a.methods.get(0), b.methods.get(0));
108: }
109: }
110: }
|