001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.corba;
022    
023    import java.io.Serializable;
024    import java.util.Map;
025    import java.util.HashMap;
026    import java.util.Iterator;
027    import java.lang.reflect.Method;
028    
029    import org.apache.geronimo.corba.util.Util;
030    import org.apache.geronimo.corba.transaction.ServerTransactionPolicyConfig;
031    import org.apache.geronimo.corba.transaction.OperationTxPolicy;
032    import org.apache.geronimo.corba.transaction.MappedServerTransactionPolicyConfig;
033    import org.apache.geronimo.corba.transaction.nodistributedtransactions.NoDTxServerTransactionPolicies;
034    import org.apache.geronimo.gbean.GBeanLifecycle;
035    import org.apache.geronimo.openejb.EjbDeployment;
036    
037    import org.omg.CORBA.Policy;
038    
039    /**
040     * @version $Rev: 497125 $ $Date: 2007-01-17 10:51:30 -0800 (Wed, 17 Jan 2007) $
041     */
042    public class TSSLink implements GBeanLifecycle {
043        private final TSSBean tssBean;
044        private final EjbDeployment ejb;
045        private final String[] jndiNames;
046    
047        public TSSLink() {
048            tssBean = null;
049            ejb = null;
050            jndiNames = null;
051        }
052    
053        public TSSLink(String[] jndiNames, TSSBean tssBean, EjbDeployment ejb) {
054            if (tssBean == null) {
055                throw new NullPointerException("No TSSBean supplied");
056            }
057            if (ejb == null) {
058                throw new NullPointerException("No ejb supplied");
059            }
060            this.jndiNames = jndiNames;
061            this.tssBean = tssBean;
062            this.ejb = ejb;
063        }
064    
065        public void doStart() throws Exception {
066            if (tssBean != null) {
067                tssBean.registerContainer(this);
068            }
069        }
070    
071        public void doStop() throws Exception {
072            destroy();
073        }
074    
075        public void doFail() {
076            destroy();
077        }
078    
079        protected void destroy() {
080            if (tssBean != null) {
081                tssBean.unregisterContainer(this);
082            }
083        }
084    
085        public EjbDeployment getDeployment() {
086            return ejb;
087        }
088    
089        public String getContainerId() {
090            return ejb.getDeploymentId();
091        }
092    
093        public String[] getJndiNames() {
094            return jndiNames;
095        }
096    
097        /**
098         * CORBA home transaction import policy configuration
099         * @return home transaction import policy
100         */
101        public Serializable getHomeTxPolicyConfig() {
102            if (ejb.getHomeInterface() == null) {
103                return null;
104            }
105            Serializable policy = buildTransactionImportPolicy(ejb.getHomeInterface());
106            return policy;
107        }
108    
109        /**
110         * CORBA remote transaction import policy configuration
111         * @return remote transaction import policy
112         */
113        public Serializable getRemoteTxPolicyConfig() {
114            if (ejb.getRemoteInterface() == null) {
115                return null;
116            }
117            Serializable policy = buildTransactionImportPolicy(ejb.getRemoteInterface());
118            return policy;
119        }
120    
121        private Serializable buildTransactionImportPolicy(Class intf) {
122    
123            Map policies = new HashMap();
124    
125            Map methodToOperation = Util.mapMethodToOperation(intf);
126            for (Iterator iterator = methodToOperation.entrySet().iterator(); iterator.hasNext();) {
127                Map.Entry entry = (Map.Entry) iterator.next();
128                Method method = (Method) entry.getKey();
129                String operation = (String) entry.getValue();
130    
131                if (!ejb.isBeanManagedTransaction()) {
132                    byte transactionAttribute = ejb.getTransactionAttribute(method);
133                    OperationTxPolicy operationTxPolicy = NoDTxServerTransactionPolicies.getContainerTransactionPolicy(transactionAttribute);
134                    policies.put(operation, operationTxPolicy);
135                } else {
136                    OperationTxPolicy operationTxPolicy = NoDTxServerTransactionPolicies.getBeanTransactionPolicy();
137                    policies.put(operation, operationTxPolicy);
138                }
139            }
140            ServerTransactionPolicyConfig serverTransactionPolicyConfig = new MappedServerTransactionPolicyConfig(policies);
141    
142            return serverTransactionPolicyConfig;
143        }
144    
145        /**
146         * Add the policy overrides (if any) to the list 
147         * of policies used to create a POA instance.
148         * 
149         * @param policies The base set of policies.
150         * 
151         * @return A new Policy array with the overrides added.  Returns
152         *         the same array if no overrides are required.
153         */
154        public Policy[] addPolicyOverrides(Policy[] policies) {
155            return tssBean.addPolicyOverrides(policies);
156        }
157    }