All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.jotm.ots.OTSServerTransactionInterceptor Maven / Gradle / Ivy

There is a newer version: 2.3.1-M1
Show newest version
/*
 * @(#) OTSServerTransactionInterceptor.java	1.0 02/07/15
 *
 * JOTM: Java Open Transaction Manager
 *
 * This module was originally developed by
 *  - INRIA inside the ObjectWeb Consortium(http://www.objectweb.org)
 *
 * The original code and portions created by INRIA are
 * Copyright (C) 2002 - INRIA (www.inria.fr)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * -Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * --------------------------------------------------------------------------
 * $Id: OTSServerTransactionInterceptor.java 1085 2009-12-14 15:32:09Z durieuxp $
 * --------------------------------------------------------------------------
 */
package org.objectweb.jotm.ots;

// java import
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.omg.IOP.ServiceContext;
import org.omg.PortableInterceptor.ForwardRequest;
import org.omg.PortableInterceptor.ORBInitInfo;
import org.omg.PortableInterceptor.ServerRequestInfo;
import org.omg.PortableInterceptor.ServerRequestInterceptor;
import org.objectweb.jotm.Current;
import org.objectweb.jotm.InternalTransactionContext;
import org.objectweb.jotm.TransactionContext;

/**
 * Class OTSServerTransactionInterceptor is a Server Interceptor for OTS Java Server
 * of JOTM. This Interceptor translate the Standart OTS Propagation Context to a Internal JOTM
 * Transaction context
 *
 * @author  Guillaume Riviere ([email protected])
 * @version 1.0, 13/09/2002
 */
public class OTSServerTransactionInterceptor extends OTSInterceptor implements ServerRequestInterceptor {

    /**
     * current object
     */
    private static Current current = null;

    /**
     * Transaction context
     */
    private Map contexts = Collections.synchronizedMap(new HashMap());
    // private TransactionContext txCtx = null;

    /**
     * interceptor name
     */
    private String interceptorName = "OTSServerTransactionInteceptor";

    /**
     * constructor
     */
    public OTSServerTransactionInterceptor(ORBInitInfo info) {
    super(info);
    }

    /**
     * get the name of this interceptor
     * @return name
     */
    public String name() {
    return interceptorName;
    }

    /**
     * Receive request context
     * @param jri JServerRequestInfo the server request information
     * @exception IOException if an exception occur with the ObjectOutput
     */
    public void receive_request_service_contexts(ServerRequestInfo jri) throws ForwardRequest {
        try {
            TransactionContext txCtx = decodeCorbaPropagationContext(jri.get_request_service_context(TX_CTX_ID)) ;

            if ( txCtx != null ) {
                Object key = new Integer(jri.request_id());
                contexts.put (key, txCtx);
            }
        } catch (org.omg.CORBA.BAD_PARAM b) {
            // else we do nothing -> no transaction context for this call
        } catch (Exception e) {
            throw new ForwardRequest();
        }
    }

    /**
     * Receive request
     * @param jri JServerRequestInfo the server request information
     * @exception IOException if an exception occur with the ObjectOutput
     */
    public void receive_request(ServerRequestInfo jri) throws ForwardRequest {
        if (current == null) {
            current = Current.getCurrent();
        }

        if (current != null) {
            Object key = new Integer(jri.request_id());
            InternalTransactionContext txCtx = (InternalTransactionContext) contexts.remove(key);

            // put into the the Current object (false for the server side context)
            current.setPropagationContext(txCtx, false);
        }
    }

    /**
     * send reply with context
     * @param jri JServerRequestInfo the server request information
     * @exception IOException if an exception occur with the ObjectOutput
     */
    public void send_reply(ServerRequestInfo jri)  {

        if (current == null) {
            current = Current.getCurrent();
        }

        if (current != null) {
            try {
                // get the Transaction Context (null if there is no transaction)
                TransactionContext txCtx = current.getPropagationContext(false);
                ServiceContext pContext = null ;

                if (txCtx != null) {

                    // get the TransactionContext and build the Corba PropagtionContext
                    pContext = buildCorbaPropagationContext(txCtx);
                    jri.add_reply_service_context(pContext, true);
                    current.setPropagationContext(null, false);
                } else {

                  // if no active global transaction, the container does not include a tx context
                  // in the request message
                }
            } catch (Exception e) {
            }
        }
    }

    public void send_exception(ServerRequestInfo jri) throws ForwardRequest {
        if (current == null) {
            current = Current.getCurrent();
        }

        if (current != null) {
            try {
                // get the Transaction Context (null if there is no transaction)
                TransactionContext txCtx = current.getPropagationContext(false);
                ServiceContext pContext = null ;

                if (txCtx != null) {

                    // get the TransactionContext and build the Corba PropagtionContext
                    pContext = buildCorbaPropagationContext(txCtx);
                    jri.add_reply_service_context(pContext, true);
                    current.setPropagationContext(null, false);
                } else {

                    // if no active global transaction, the container does not include a tx context
                    // in the request message
                }
            } catch (Exception e) {
            }
        }
    }

    public void send_other(ServerRequestInfo jri) throws ForwardRequest {
    }

    public void destroy() {
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy