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

com.sun.xml.ws.tx.at.internal.WSATGatewayRM Maven / Gradle / Ivy

There is a newer version: 4.0.4
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.xml.ws.tx.at.internal;

import com.sun.istack.logging.Logger;
import com.sun.xml.ws.tx.at.localization.LocalizationMessages;
import com.sun.xml.ws.tx.at.WSATHelper;
import com.sun.xml.ws.tx.at.WSATXAResource;
import com.sun.xml.ws.tx.at.common.TransactionImportManager;
import com.sun.xml.ws.tx.dev.WSATRuntimeConfig;

import java.io.*;
import java.util.*;
import java.util.logging.Level;

import javax.transaction.*;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import javax.xml.ws.WebServiceException;


/**
 *
 * @author paulparkinson
 */

/**
 * Gateway XAResource for managing outbound WS-AT transaction branches.
 */
public class WSATGatewayRM implements XAResource, WSATRuntimeConfig.RecoveryEventListener {
  private static final Logger LOGGER = Logger.getLogger(WSATGatewayRM.class);
  private static final String WSAT = "wsat";
  private static final String OUTBOUND = "outbound";
  private static final String INBOUND = "inbound";

  private static WSATGatewayRM singleton;
  static private String resourceRegistrationName; // JTA resource registration name
  static private Map branches; // xid to Branch
  static List pendingXids; // collection of Xids
  private final Object currentXidLock = new Object();
  private Xid currentXid;
  static boolean isReadyForRecovery = false;
  public static boolean isReadyForRuntime = false;
  public static  String txlogdir;
  static String txlogdirInbound;
  private static String txlogdirOutbound;
    static boolean isStoreInit = false;
  private volatile int counter = 0;
  private Map activityXidToInternalXidMap = new HashMap();
  private Map internalXidToActivityXidMap = new HashMap();

    // package access for test instantiation only, this is a singleton
  WSATGatewayRM(String serverName) {
    resourceRegistrationName = "RM_NAME_PREFIX" + serverName;
    branches = Collections.synchronizedMap(new HashMap());
    pendingXids = Collections.synchronizedList(new ArrayList());
    singleton = this;
  }

  /**
   * called by transaction services for enlistment and used by HA delegation
   * @return
   */
  public static synchronized WSATGatewayRM getInstance() {
    if(singleton==null) {
        create("server");
    }
    return singleton;
  }

  /**
   * Called during tube/web service init
   * @return
   */
  public static synchronized WSATGatewayRM create() {
    return create("server");    
  }

    /**
     * Called as part of WSATTransactionService start
     * @param serverName this server's name
     * @return the WSATGatewayRM singleton that WSATTransactionService will call stop on during stop/shutdown
     */
  private static synchronized WSATGatewayRM create(String serverName)
  {
    if (singleton == null) {
        new WSATGatewayRM(serverName);
        isReadyForRecovery = setupRecovery();
    }
    return singleton;
  }

    private static boolean setupRecovery() {
        if (!WSATRuntimeConfig.getInstance().isWSATRecoveryEnabled()) return true;
        TransactionImportManager.getInstance().registerRecoveryResourceHandler(singleton);
        WSATRuntimeConfig.getInstance().setWSATRecoveryEventListener(singleton);
        setTxLogDirs();
        try {
            initStore();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * Called for create of WSATGatewayRM
     */
   static void initStore() throws Exception {
        if (isStoreInit) return;
        if (WSATHelper.isDebugEnabled()) debug("WSATGatewayRM.initStore path:"+txlogdirInbound);
        createFile(txlogdirInbound, true);
        if (WSATHelper.isDebugEnabled()) debug("WSATGatewayRM.initStore path:"+txlogdirOutbound);
        createFile(txlogdirOutbound, true);
        isStoreInit = true;
    }


    static private File createFile(String logFilePath, boolean isDir) throws Exception {
        File file = new File(logFilePath);
        if (!file.exists()) {
            if (isDir && !file.mkdirs()) {
                throw new Exception("Could not create directory : " + file.getAbsolutePath());
            } else if (!isDir) {
                try {
                    file.createNewFile();
                } catch (IOException ioe) {
                    Exception storeEx = new Exception("Could not create file : " + file.getAbsolutePath());
                    storeEx.initCause(ioe);
                    throw storeEx;
                }
            }
        }
        return file;
    }

    /**
     * Called for XAResource.recover
     */
   void recoverPendingBranches(String outboundRecoveryDir, String inboundRecoveryDir) {
    if (WSATHelper.isDebugEnabled()) debug("recoverPendingBranches outbound directory:"+outboundRecoveryDir);
    FileInputStream fis;
    ObjectInputStream in;
      File[] files = new File(outboundRecoveryDir).listFiles();
      if(files!=null) for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy