KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > adapter > TestManagedConnectionFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.adapter;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.resource.ResourceException JavaDoc;
31 import javax.resource.spi.ConnectionManager JavaDoc;
32 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
33 import javax.resource.spi.ManagedConnection JavaDoc;
34 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
35 import javax.security.auth.Subject JavaDoc;
36
37 import org.jboss.logging.Logger;
38
39 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
40
41 /**
42  * ManagedConnectionFactory.java
43  *
44  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
45  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
46  * @version <tt>$Revision: 37406 $</tt>
47  */

48 public class TestManagedConnectionFactory implements ManagedConnectionFactory JavaDoc
49 {
50    /** The serialVersionUID */
51    private static final long serialVersionUID = 1L;
52
53    private Logger log = Logger.getLogger(TestManagedConnectionFactory.class);
54    
55    //number the managed connections
56
SynchronizedInt id = new SynchronizedInt(0);
57
58    String JavaDoc failure;
59
60    boolean failJoin;
61
62    long sleepInStart;
63
64    long sleepInEnd;
65    
66    Map JavaDoc xids = new WrapperMap(new HashMap JavaDoc());
67    
68    public TestManagedConnectionFactory()
69    {
70    }
71
72    public void setFailure(String JavaDoc failure)
73    {
74       this.failure = failure;
75    }
76
77    public boolean getFailJoin()
78    {
79       return failJoin;
80    }
81    
82    public void setFailJoin(boolean failJoin)
83    {
84       this.failJoin = failJoin;
85    }
86
87    public long getSleepInStart()
88    {
89       return sleepInStart;
90    }
91    
92    public void setSleepInStart(long sleep)
93    {
94       this.sleepInStart = sleep;
95    }
96
97    public long getSleepInEnd()
98    {
99       return sleepInEnd;
100    }
101    
102    public void setSleepInEnd(long sleep)
103    {
104       this.sleepInEnd = sleep;
105    }
106    
107    // implementation of javax.resource.spi.ManagedConnectionFactory interface
108

109    public int hashCode()
110    {
111      return getClass().hashCode();
112    }
113
114    public boolean equals(Object JavaDoc other)
115    {
116       return (other != null) && (other.getClass() == getClass());
117    }
118
119    public void setLogWriter(PrintWriter JavaDoc param1) throws ResourceException JavaDoc
120    {
121    }
122
123    public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
124    {
125      return null;
126    }
127
128    public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cm) throws ResourceException JavaDoc
129    {
130      return new TestConnectionFactory(cm, this);
131    }
132
133    public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc
134    {
135       throw new ResourceException JavaDoc("not yet implemented");
136    }
137
138    public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cri) throws ResourceException JavaDoc
139    {
140       if (failure != null && failure.equals("createManagedConnectionResource"))
141          throw new ResourceException JavaDoc("");
142       if (failure != null && failure.equals("createManagedConnectionRuntime"))
143          throw new RuntimeException JavaDoc("");
144       return new TestManagedConnection(this, subject, (TestConnectionRequestInfo)cri, id.increment());
145    }
146
147    public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc candidates, Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cri) throws ResourceException JavaDoc
148    {
149       if (failure != null && failure.equals("matchManagedConnectionResource"))
150          throw new ResourceException JavaDoc("");
151       if (failure != null && failure.equals("matchManagedConnectionRuntime"))
152          throw new RuntimeException JavaDoc("");
153       if (candidates.isEmpty())
154          return null;
155       return (ManagedConnection JavaDoc)candidates.iterator().next();
156    }
157
158    Integer JavaDoc integerProperty;
159
160    public Integer JavaDoc getIntegerProperty()
161    {
162       return integerProperty;
163    }
164
165    /**
166     * Set the IntegerProperty value.
167     *
168     * @param newIntegerProperty The new IntegerProperty value.
169     */

170    public void setIntegerProperty(Integer JavaDoc integerProperty)
171    {
172       this.integerProperty = integerProperty;
173    }
174
175    Integer JavaDoc defaultIntegerProperty;
176
177    /**
178     * Get the DefaultIntegerProperty value.
179     *
180     * @return the DefaultIntegerProperty value.
181     */

182    public Integer JavaDoc getDefaultIntegerProperty()
183    {
184       return defaultIntegerProperty;
185    }
186
187    /**
188     * Set the DefaultIntegerProperty value.
189     *
190     * @param newDefaultIntegerProperty The new DefaultIntegerProperty value.
191     */

192    public void setDefaultIntegerProperty(Integer JavaDoc defaultIntegerProperty)
193    {
194       this.defaultIntegerProperty = defaultIntegerProperty;
195    }
196
197    Boolean JavaDoc booleanProperty;
198
199    /**
200     * Get the BooleanProperty value.
201     *
202     * @return the BooleanProperty value.
203     */

204    public Boolean JavaDoc getBooleanProperty()
205    {
206       return booleanProperty;
207    }
208
209    /**
210     * Set the BooleanProperty value.
211     *
212     * @param newBooleanProperty The new BooleanProperty value.
213     */

214    public void setBooleanProperty(Boolean JavaDoc booleanProperty)
215    {
216       this.booleanProperty = booleanProperty;
217    }
218
219    Long JavaDoc longProperty;
220
221    /**
222     * Get the LongProperty value.
223     *
224     * @return the LongProperty value.
225     */

226    public Long JavaDoc getLongProperty()
227    {
228       return longProperty;
229    }
230
231    /**
232     * Set the LongProperty value.
233     *
234     * @param newLongProperty The new LongProperty value.
235     */

236    public void setLongProperty(Long JavaDoc longProperty)
237    {
238       this.longProperty = longProperty;
239    }
240
241    Double JavaDoc doubleProperty;
242
243    /**
244     * Get the DoubleProperty value.
245     *
246     * @return the DoubleProperty value.
247     */

248    public Double JavaDoc getDoubleProperty()
249    {
250       return doubleProperty;
251    }
252
253    /**
254     * Set the DoubleProperty value.
255     *
256     * @param newDoubleProperty The new DoubleProperty value.
257     */

258    public void setDoubleProperty(Double JavaDoc doubleProperty)
259    {
260       this.doubleProperty = doubleProperty;
261    }
262
263    URL JavaDoc urlProperty;
264
265    /**
266     * Get the UrlProperty value. (this is a jboss specific property editor)
267     *
268     * @return the UrlProperty value.
269     */

270    public URL JavaDoc getUrlProperty()
271    {
272       return urlProperty;
273    }
274
275    /**
276     * Set the UrlProperty value.
277     *
278     * @param newUrlProperty The new UrlProperty value.
279     */

280    public void setUrlProperty(URL JavaDoc urlProperty)
281    {
282       this.urlProperty = urlProperty;
283    }
284    
285    Map JavaDoc getXids()
286    {
287       return xids;
288    }
289    
290    public class WrapperMap implements Map JavaDoc
291    {
292       Map JavaDoc delegate;
293       
294       public WrapperMap(Map JavaDoc delegate)
295       {
296          this.delegate = delegate;
297       }
298       
299       public void clear()
300       {
301          delegate.clear();
302       }
303
304       public boolean containsKey(Object JavaDoc key)
305       {
306          return delegate.containsKey(key);
307       }
308
309       public boolean containsValue(Object JavaDoc value)
310       {
311          return delegate.containsValue(value);
312       }
313
314       public Set JavaDoc entrySet()
315       {
316          return delegate.entrySet();
317       }
318
319       public Object JavaDoc get(Object JavaDoc key)
320       {
321          return delegate.get(key);
322       }
323
324       public boolean isEmpty()
325       {
326          return delegate.isEmpty();
327       }
328
329       public Set JavaDoc keySet()
330       {
331          return delegate.keySet();
332       }
333
334       public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
335       {
336          Object JavaDoc result = delegate.put(key, value);
337          log.info("Change xid=" + key + " from " + result + " to " + value);
338          return result;
339       }
340
341       public void putAll(Map JavaDoc t)
342       {
343          delegate.putAll(t);
344       }
345
346       public Object JavaDoc remove(Object JavaDoc key)
347       {
348          return delegate.remove(key);
349       }
350
351       public int size()
352       {
353          return delegate.size();
354       }
355
356       public Collection JavaDoc values()
357       {
358          return delegate.values();
359       }
360    }
361 }
362
Popular Tags