001: /*
002: * $Id: MuleTransactionConfig.java 11278 2008-03-07 20:26:34Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transaction;
012:
013: import org.mule.api.transaction.TransactionConfig;
014: import org.mule.api.transaction.TransactionFactory;
015: import org.mule.transaction.constraints.ConstraintFilter;
016: import org.mule.util.ClassUtils;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: /**
022: * <p/> <code>MuleTransactionConfig</code> defines transaction configuration for a
023: * transactional endpoint.
024: */
025: public class MuleTransactionConfig implements TransactionConfig {
026: /**
027: * logger used by this class
028: */
029: protected static final Log logger = LogFactory
030: .getLog(MuleTransactionConfig.class);
031:
032: public static final String ACTION_NONE_STRING = "NONE";
033: public static final String ACTION_ALWAYS_BEGIN_STRING = "ALWAYS_BEGIN";
034: public static final String ACTION_BEGIN_OR_JOIN_STRING = "BEGIN_OR_JOIN";
035: public static final String ACTION_ALWAYS_JOIN_STRING = "ALWAYS_JOIN";
036: public static final String ACTION_JOIN_IF_POSSIBLE_STRING = "JOIN_IF_POSSIBLE";
037:
038: private TransactionFactory factory;
039:
040: private byte action = ACTION_NONE;
041:
042: private ConstraintFilter constraint = null;
043:
044: private int timeout;
045:
046: public MuleTransactionConfig() {
047: // todo timeout = RegistryContext.getConfiguration().getDefaultTransactionTimeout();
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.mule.umo.UMOTransactionConfig#getFactory()
054: */
055: public TransactionFactory getFactory() {
056: return factory;
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see org.mule.umo.UMOTransactionConfig#setFactory(org.mule.umo.UMOTransactionFactory)
063: */
064: public void setFactory(TransactionFactory factory) {
065: if (factory == null) {
066: throw new IllegalArgumentException(
067: "Transaction Factory cannot be null");
068: }
069: this .factory = factory;
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see org.mule.umo.UMOTransactionConfig#getAction()
076: */
077: public byte getAction() {
078: return action;
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.mule.umo.UMOTransactionConfig#setAction(byte)
085: */
086: public void setAction(byte action) {
087: this .action = action;
088:
089: }
090:
091: public void setActionAsString(String action) {
092: if (ACTION_ALWAYS_BEGIN_STRING.equals(action)) {
093: this .action = ACTION_ALWAYS_BEGIN;
094: } else if (ACTION_BEGIN_OR_JOIN_STRING.equals(action)) {
095: this .action = ACTION_BEGIN_OR_JOIN;
096: } else if (ACTION_ALWAYS_JOIN_STRING.equals(action)) {
097: this .action = ACTION_ALWAYS_JOIN;
098: } else if (ACTION_JOIN_IF_POSSIBLE_STRING.equals(action)) {
099: this .action = ACTION_JOIN_IF_POSSIBLE;
100: } else if (ACTION_NONE_STRING.equals(action)) {
101: this .action = ACTION_NONE;
102: } else {
103: throw new IllegalArgumentException("Action " + action
104: + " is not recognised as a begin action.");
105: }
106: }
107:
108: public String getActionAsString() {
109: switch (action) {
110: case ACTION_ALWAYS_BEGIN:
111: return ACTION_ALWAYS_BEGIN_STRING;
112: case ACTION_BEGIN_OR_JOIN:
113: return ACTION_BEGIN_OR_JOIN_STRING;
114: case ACTION_ALWAYS_JOIN:
115: return ACTION_ALWAYS_JOIN_STRING;
116: case ACTION_JOIN_IF_POSSIBLE:
117: return ACTION_JOIN_IF_POSSIBLE_STRING;
118: default:
119: return ACTION_NONE_STRING;
120: }
121: }
122:
123: public boolean isTransacted() {
124: return factory != null && factory.isTransacted()
125: && action != ACTION_NONE;
126: }
127:
128: public ConstraintFilter getConstraint() {
129: if (constraint == null) {
130: return null;
131: }
132: try {
133: return (ConstraintFilter) constraint.clone();
134: } catch (CloneNotSupportedException e) {
135: logger.fatal("Failed to clone ContraintFilter: "
136: + e.getMessage(), e);
137: return constraint;
138: }
139: }
140:
141: public void setConstraint(ConstraintFilter constraint) {
142: this .constraint = constraint;
143: }
144:
145: public int getTimeout() {
146: return timeout;
147: }
148:
149: public void setTimeout(int timeout) {
150: this .timeout = timeout;
151: }
152:
153: public String toString() {
154: StringBuffer buf = new StringBuffer();
155: buf.append("Transaction{factory=").append(factory).append(
156: ", action=").append(getActionAsString()).append(
157: ", timeout=").append(timeout).append("}");
158: return buf.toString();
159: }
160:
161: public int hashCode() {
162: return ClassUtils.hash(new Object[] { factory,
163: new Byte(action), constraint, new Integer(timeout) });
164: }
165:
166: public boolean equals(Object obj) {
167: if (this == obj)
168: return true;
169: if (obj == null || getClass() != obj.getClass())
170: return false;
171:
172: final MuleTransactionConfig other = (MuleTransactionConfig) obj;
173: return ClassUtils.equal(factory, other.factory)
174: && ClassUtils.equal(new Byte(action), new Byte(
175: other.action))
176: && ClassUtils.equal(constraint, other.constraint)
177: && ClassUtils.equal(new Integer(timeout), new Integer(
178: other.timeout));
179:
180: }
181: }
|