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 */
017package org.apache.activemq.util;
018
019import static javax.transaction.xa.XAResource.TMENDRSCAN;
020import static javax.transaction.xa.XAResource.TMFAIL;
021import static javax.transaction.xa.XAResource.TMJOIN;
022import static javax.transaction.xa.XAResource.TMNOFLAGS;
023import static javax.transaction.xa.XAResource.TMONEPHASE;
024import static javax.transaction.xa.XAResource.TMRESUME;
025import static javax.transaction.xa.XAResource.TMSTARTRSCAN;
026import static javax.transaction.xa.XAResource.TMSUCCESS;
027import static javax.transaction.xa.XAResource.TMSUSPEND;
028
029public class XASupport {
030
031    public static String toString(int flags) {
032        if (flags == TMNOFLAGS) {
033            return "TMNOFLAGS";
034        }
035
036        StringBuilder result = new StringBuilder();
037        if (hasFlag(flags, TMENDRSCAN)) {
038            add(result, "TMENDRSCAN");
039        }
040        if (hasFlag(flags, TMFAIL)) {
041            add(result, "TMFAIL");
042        }
043        if (hasFlag(flags, TMJOIN)) {
044            add(result, "TMJOIN");
045        }
046        if (hasFlag(flags, TMONEPHASE)) {
047            add(result, "TMONEPHASE");
048        }
049        if (hasFlag(flags, TMRESUME)) {
050            add(result, "TMRESUME");
051        }
052        if (hasFlag(flags, TMSTARTRSCAN)) {
053            add(result, "TMSTARTRSCAN");
054        }
055        if (hasFlag(flags, TMSUCCESS)) {
056            add(result, "TMSUCCESS");
057        }
058        if (hasFlag(flags, TMSUSPEND)) {
059            add(result, "TMSUSPEND");
060        }
061
062        int nonStandardFlags = flags
063                & ~TMENDRSCAN
064                & ~TMFAIL
065                & ~TMJOIN
066                & ~TMONEPHASE
067                & ~TMRESUME
068                & ~TMSTARTRSCAN
069                & ~TMSUCCESS
070                & ~TMSUSPEND;
071
072        if (nonStandardFlags != 0) {
073            add(result, String.format("0x%08x", nonStandardFlags));
074        }
075
076        return result.toString();
077    }
078
079    private static boolean hasFlag(int flags, int flag) {
080        return (flags & flag) == flag;
081    }
082
083    private static void add(StringBuilder result, String string) {
084        if (result.length() > 0) {
085            result.append(" | ");
086        }
087        result.append(string);
088    }
089}