001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.adapter;
023:
024: import java.io.PrintWriter;
025: import java.net.URL;
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Set;
030: import javax.resource.ResourceException;
031: import javax.resource.spi.ConnectionManager;
032: import javax.resource.spi.ConnectionRequestInfo;
033: import javax.resource.spi.ManagedConnection;
034: import javax.resource.spi.ManagedConnectionFactory;
035: import javax.resource.spi.ValidatingManagedConnectionFactory;
036: import javax.security.auth.Subject;
037:
038: import org.jboss.logging.Logger;
039:
040: import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
041:
042: /**
043: * ManagedConnectionFactory.java
044: *
045: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
046: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
047: * @version <tt>$Revision: 57211 $</tt>
048: */
049: public class TestManagedConnectionFactory implements
050: ManagedConnectionFactory {
051: /** The serialVersionUID */
052: private static final long serialVersionUID = 1L;
053:
054: private Logger log = Logger
055: .getLogger(TestManagedConnectionFactory.class);
056:
057: //number the managed connections
058: SynchronizedInt id = new SynchronizedInt(0);
059:
060: String failure;
061:
062: boolean failJoin;
063:
064: long sleepInStart;
065:
066: long sleepInEnd;
067:
068: Map xids = new WrapperMap(new HashMap());
069:
070: public TestManagedConnectionFactory() {
071: }
072:
073: public void setFailure(String failure) {
074: this .failure = failure;
075: }
076:
077: public boolean getFailJoin() {
078: return failJoin;
079: }
080:
081: public void setFailJoin(boolean failJoin) {
082: this .failJoin = failJoin;
083: }
084:
085: public long getSleepInStart() {
086: return sleepInStart;
087: }
088:
089: public void setSleepInStart(long sleep) {
090: this .sleepInStart = sleep;
091: }
092:
093: public long getSleepInEnd() {
094: return sleepInEnd;
095: }
096:
097: public void setSleepInEnd(long sleep) {
098: this .sleepInEnd = sleep;
099: }
100:
101: // implementation of javax.resource.spi.ManagedConnectionFactory interface
102:
103: public int hashCode() {
104: return getClass().hashCode();
105: }
106:
107: public boolean equals(Object other) {
108: return (other != null) && (other.getClass() == getClass());
109: }
110:
111: public void setLogWriter(PrintWriter param1)
112: throws ResourceException {
113: }
114:
115: public PrintWriter getLogWriter() throws ResourceException {
116: return null;
117: }
118:
119: public Object createConnectionFactory(ConnectionManager cm)
120: throws ResourceException {
121: return new TestConnectionFactory(cm, this );
122: }
123:
124: public Object createConnectionFactory() throws ResourceException {
125: throw new ResourceException("not yet implemented");
126: }
127:
128: public ManagedConnection createManagedConnection(Subject subject,
129: ConnectionRequestInfo cri) throws ResourceException {
130: if (failure != null
131: && failure.equals("createManagedConnectionResource"))
132: throw new ResourceException("");
133: if (failure != null
134: && failure.equals("createManagedConnectionRuntime"))
135: throw new RuntimeException("");
136: return new TestManagedConnection(this , subject,
137: (TestConnectionRequestInfo) cri, id.increment());
138: }
139:
140: public ManagedConnection matchManagedConnections(Set candidates,
141: Subject subject, ConnectionRequestInfo cri)
142: throws ResourceException {
143: if (failure != null
144: && failure.equals("matchManagedConnectionResource"))
145: throw new ResourceException("");
146: if (failure != null
147: && failure.equals("matchManagedConnectionRuntime"))
148: throw new RuntimeException("");
149: if (candidates.isEmpty())
150: return null;
151: return (ManagedConnection) candidates.iterator().next();
152: }
153:
154: Integer integerProperty;
155:
156: public Integer getIntegerProperty() {
157: return integerProperty;
158: }
159:
160: /**
161: * Set the IntegerProperty value.
162: *
163: * @param newIntegerProperty The new IntegerProperty value.
164: */
165: public void setIntegerProperty(Integer integerProperty) {
166: this .integerProperty = integerProperty;
167: }
168:
169: Integer defaultIntegerProperty;
170:
171: /**
172: * Get the DefaultIntegerProperty value.
173: *
174: * @return the DefaultIntegerProperty value.
175: */
176: public Integer getDefaultIntegerProperty() {
177: return defaultIntegerProperty;
178: }
179:
180: /**
181: * Set the DefaultIntegerProperty value.
182: *
183: * @param newDefaultIntegerProperty The new DefaultIntegerProperty value.
184: */
185: public void setDefaultIntegerProperty(Integer defaultIntegerProperty) {
186: this .defaultIntegerProperty = defaultIntegerProperty;
187: }
188:
189: Boolean booleanProperty;
190:
191: /**
192: * Get the BooleanProperty value.
193: *
194: * @return the BooleanProperty value.
195: */
196: public Boolean getBooleanProperty() {
197: return booleanProperty;
198: }
199:
200: /**
201: * Set the BooleanProperty value.
202: *
203: * @param newBooleanProperty The new BooleanProperty value.
204: */
205: public void setBooleanProperty(Boolean booleanProperty) {
206: this .booleanProperty = booleanProperty;
207: }
208:
209: Long longProperty;
210:
211: /**
212: * Get the LongProperty value.
213: *
214: * @return the LongProperty value.
215: */
216: public Long getLongProperty() {
217: return longProperty;
218: }
219:
220: /**
221: * Set the LongProperty value.
222: *
223: * @param newLongProperty The new LongProperty value.
224: */
225: public void setLongProperty(Long longProperty) {
226: this .longProperty = longProperty;
227: }
228:
229: Double doubleProperty;
230:
231: /**
232: * Get the DoubleProperty value.
233: *
234: * @return the DoubleProperty value.
235: */
236: public Double getDoubleProperty() {
237: return doubleProperty;
238: }
239:
240: /**
241: * Set the DoubleProperty value.
242: *
243: * @param newDoubleProperty The new DoubleProperty value.
244: */
245: public void setDoubleProperty(Double doubleProperty) {
246: this .doubleProperty = doubleProperty;
247: }
248:
249: URL urlProperty;
250:
251: /**
252: * Get the UrlProperty value. (this is a jboss specific property editor)
253: *
254: * @return the UrlProperty value.
255: */
256: public URL getUrlProperty() {
257: return urlProperty;
258: }
259:
260: /**
261: * Set the UrlProperty value.
262: *
263: * @param newUrlProperty The new UrlProperty value.
264: */
265: public void setUrlProperty(URL urlProperty) {
266: this .urlProperty = urlProperty;
267: }
268:
269: Map getXids() {
270: return xids;
271: }
272:
273: public class WrapperMap implements Map {
274: Map delegate;
275:
276: public WrapperMap(Map delegate) {
277: this .delegate = delegate;
278: }
279:
280: public void clear() {
281: delegate.clear();
282: }
283:
284: public boolean containsKey(Object key) {
285: return delegate.containsKey(key);
286: }
287:
288: public boolean containsValue(Object value) {
289: return delegate.containsValue(value);
290: }
291:
292: public Set entrySet() {
293: return delegate.entrySet();
294: }
295:
296: public Object get(Object key) {
297: return delegate.get(key);
298: }
299:
300: public boolean isEmpty() {
301: return delegate.isEmpty();
302: }
303:
304: public Set keySet() {
305: return delegate.keySet();
306: }
307:
308: public Object put(Object key, Object value) {
309: Object result = delegate.put(key, value);
310: log.info("Change xid=" + key + " from " + result + " to "
311: + value);
312: return result;
313: }
314:
315: public void putAll(Map t) {
316: delegate.putAll(t);
317: }
318:
319: public Object remove(Object key) {
320: return delegate.remove(key);
321: }
322:
323: public int size() {
324: return delegate.size();
325: }
326:
327: public Collection values() {
328: return delegate.values();
329: }
330: }
331: }
|