001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.tx.at;
038:
039: import java.util.Random;
040: import javax.transaction.xa.Xid;
041: import java.util.HashMap;
042: import java.util.Map;
043:
044: /**
045: * Manage mapping between WS-Atomic Transaction coordination identifier and Xid.
046: * <p/>
047: * Create Xid for imported txn. (one flowed in from different vendor)
048: * TODO: if root coordinator, try reusing AS txn id (have to export it though)
049: * <p/>
050: * <p/>
051: * Allowing setting of
052: *
053: * @author jf39279
054: */
055: public class CoordinationXid implements Xid {
056:
057: private static Map<String, Xid> coordId2Xid = new HashMap<String, Xid>();
058: private static Map<Xid, String> xid2CoordId = new HashMap<Xid, String>();
059: final private String coordId;
060:
061: public static Xid lookupOrCreate(final String coordId) {
062: Xid result = get(coordId);
063: if (result == null) {
064: result = new CoordinationXid(coordId);
065: coordId2Xid.put(coordId, result);
066: xid2CoordId.put(result, coordId);
067: }
068: return result;
069: }
070:
071: public String getCoordinationId() {
072: return coordId;
073: }
074:
075: public static Xid get(final String coordId) {
076: return coordId2Xid.get(coordId);
077: }
078:
079: private static Xid remove(final String coordId) {
080: return coordId2Xid.remove(coordId);
081: }
082:
083: private static String remove(final Xid coordId) {
084: return xid2CoordId.remove(coordId);
085: }
086:
087: /**
088: * Cleanup.
089: */
090: public static void forget(final String coordId) {
091: final Xid removed = remove(coordId);
092: if (removed != null) {
093: remove(removed);
094: }
095: }
096:
097: static private Random random = new Random();
098:
099: /**
100: * Creates a new instance of CoordinationXid representing an imported
101: * coordination id.
102: */
103: private CoordinationXid(final String coordinationXid) {
104: gtrId = new byte[16];
105: random.nextBytes(gtrId);
106: coordId = coordinationXid;
107: }
108:
109: public byte[] getGlobalTransactionId() {
110: return gtrId.clone();
111: }
112:
113: public byte[] getBranchQualifier() {
114: return BRANCH_QUALIFIER;
115: }
116:
117: private static final int FORMAT_ID = 200408; // any constant but -1 that is invalid format
118: final private byte[] gtrId;
119: static private final byte[] BRANCH_QUALIFIER = { 1 }; // no nested txn
120: private String stringForm = null; //cache
121:
122: public int getFormatId() {
123: return FORMAT_ID;
124: }
125:
126: /*
127: * returns the Transaction id of this transaction
128: */
129: public String toString() {
130: // return cache if it exists
131: if (stringForm != null) {
132: return stringForm;
133: }
134:
135: // Otherwise format the global identifier.
136: //char[] buff = new char[gtrId.length*2 + 2/*'[' and ']'*/ + 3/*bqual and ':'*/];
137: char[] buff = new char[gtrId.length * 2 + 3/*bqual and ':'*/];
138: int pos = 0;
139: //buff[pos++] = '[';
140:
141: // Convert the global transaction identifier into a string of hex digits.
142:
143: final int globalLen = gtrId.length;
144: for (int i = 0; i < globalLen; i++) {
145: final int currCharHigh = (gtrId[i] & 0xf0) >> 4;
146: final int currCharLow = gtrId[i] & 0x0f;
147: buff[pos++] = (char) (currCharHigh + (currCharHigh > 9 ? 'A' - 10
148: : '0'));
149: buff[pos++] = (char) (currCharLow + (currCharLow > 9 ? 'A' - 10
150: : '0'));
151: }
152:
153: //buff[pos++] = ':';
154: buff[pos++] = '_';
155: final int currCharHigh = (0 & 0xf0) >> 4;
156: final int currCharLow = 0 & 0x0f;
157: buff[pos++] = (char) (currCharHigh + (currCharHigh > 9 ? 'A' - 10
158: : '0'));
159: buff[pos++] = (char) (currCharLow + (currCharLow > 9 ? 'A' - 10
160: : '0'));
161: //buff[pos] = ']';
162:
163: // Cache the string form of the global identifier.
164: stringForm = new String(buff);
165: return stringForm;
166: }
167:
168: }
|