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     */
017    package org.apache.geronimo.corba.transaction.nodistributedtransactions;
018    
019    import javax.transaction.TransactionManager;
020    
021    import org.omg.CORBA.Any;
022    import org.omg.CORBA.INTERNAL;
023    import org.omg.CosTransactions.PropagationContext;
024    import org.omg.CosTransactions.PropagationContextHelper;
025    import org.omg.CosTransactions.TransIdentity;
026    import org.omg.CosTransactions.otid_t;
027    import org.omg.IOP.Codec;
028    import org.omg.IOP.CodecPackage.InvalidTypeForEncoding;
029    import org.omg.IOP.ServiceContext;
030    import org.omg.IOP.TransactionService;
031    import org.omg.PortableInterceptor.ClientRequestInfo;
032    import org.apache.geronimo.corba.transaction.ClientTransactionPolicyConfig;
033    import org.apache.geronimo.corba.util.Util;
034    import org.apache.openejb.util.TransactionUtils;
035    
036    /**
037     * @version $Rev: 451417 $ $Date: 2006-09-29 13:13:22 -0700 (Fri, 29 Sep 2006) $
038     */
039    public class NoDTxClientTransactionPolicyConfig implements ClientTransactionPolicyConfig {
040    
041        private static final TransIdentity[] NO_PARENTS = new TransIdentity[0];
042        private static final otid_t NULL_XID = new otid_t(0, 0, new byte[0]);
043    
044        private final TransactionManager transactionManager;
045    
046        public NoDTxClientTransactionPolicyConfig(TransactionManager transactionManager) {
047            if (transactionManager == null) {
048                throw new IllegalArgumentException("transactionManager must not be null");
049            }
050            this.transactionManager = transactionManager;
051        }
052    
053        public void exportTransaction(ClientRequestInfo ri) {
054            if (TransactionUtils.isTransactionActive(transactionManager)) {
055                //19.6.2.1 (1) propagate an "empty" transaction context.
056                //but, it needs an xid!
057                TransIdentity transIdentity = new TransIdentity(null, null, NULL_XID);
058                int timeout = 0;
059                Any implementationSpecificData = Util.getORB().create_any();
060                PropagationContext propagationContext = new PropagationContext(timeout, transIdentity, NO_PARENTS, implementationSpecificData);
061                Codec codec = Util.getCodec();
062                Any any = Util.getORB().create_any();
063                PropagationContextHelper.insert(any, propagationContext);
064                byte[] encodedPropagationContext;
065                try {
066                    encodedPropagationContext = codec.encode_value(any);
067                } catch (InvalidTypeForEncoding invalidTypeForEncoding) {
068                    throw (INTERNAL)new INTERNAL("Could not encode propagationContext").initCause(invalidTypeForEncoding);
069                }
070                ServiceContext otsServiceContext = new ServiceContext(TransactionService.value, encodedPropagationContext);
071                ri.add_request_service_context(otsServiceContext, true);
072            }
073    
074        }
075    }