001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: ManagedConnectionSupport.java,v 1.1 2007/05/17 21:52:59 mlipp Exp $
021: *
022: * $Log: ManagedConnectionSupport.java,v $
023: * Revision 1.1 2007/05/17 21:52:59 mlipp
024: * Refactored resource adaptors.
025: *
026: */
027: package de.danet.an.util.ra;
028:
029: import java.io.PrintWriter;
030: import java.security.Principal;
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.HashSet;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Set;
037:
038: import javax.resource.NotSupportedException;
039: import javax.resource.ResourceException;
040: import javax.resource.spi.ConnectionEvent;
041: import javax.resource.spi.ConnectionEventListener;
042: import javax.resource.spi.ConnectionRequestInfo;
043: import javax.resource.spi.LocalTransaction;
044: import javax.resource.spi.ManagedConnection;
045: import javax.resource.spi.ManagedConnectionMetaData;
046: import javax.security.auth.Subject;
047: import javax.transaction.xa.XAResource;
048:
049: /**
050: * This class provides a base for implementing managed connections.
051: *
052: * @author mnl
053: *
054: */
055: public abstract class ManagedConnectionSupport implements
056: ManagedConnection {
057:
058: private Set connections = Collections
059: .synchronizedSet(new HashSet());
060: private List listeners = Collections
061: .synchronizedList(new ArrayList());
062: private PrintWriter logWriter;
063: private ManagedConnectionMetaData metaData;
064:
065: /**
066: * Create a new instance with all attributes initialized
067: * to defaults or the given values.
068: * @param the managed connection factory
069: * @param subject security context as JAAS subject
070: *
071: */
072: public ManagedConnectionSupport(
073: ManagedConnectionFactorySupport mcf, Subject subject) {
074: try {
075: this .logWriter = mcf.getLogWriter();
076: } catch (ResourceException e) {
077: throw (IllegalArgumentException) (new IllegalArgumentException(
078: "Cannot get log writer: " + e.getMessage()))
079: .initCause(e);
080: }
081: String userName = null;
082: if (subject != null && subject.getPrincipals() != null
083: && subject.getPrincipals().size() > 0) {
084: userName = ((Principal) subject.getPrincipals().iterator()
085: .next()).getName();
086: }
087: metaData = mcf.createMetaData(userName);
088: }
089:
090: /* (non-Javadoc)
091: * @see javax.resource.spi.ManagedConnection#destroy()
092: */
093: public final void destroy() throws ResourceException {
094: cleanup();
095: connections = null;
096: listeners = null;
097: doDestroy();
098: }
099:
100: /**
101: * Destroy any resources allocated by the derived class. This is
102: * invoked after the cleanup done by <code>destroy</code> for this
103: * class.
104: */
105: protected abstract void doDestroy();
106:
107: /* (non-Javadoc)
108: * @see javax.resource.spi.ManagedConnection#getConnection
109: */
110: public Object getConnection(Subject subject,
111: ConnectionRequestInfo cxRequestInfo)
112: throws ResourceException {
113: ConnectionSupport connection = createConnection(subject,
114: cxRequestInfo);
115: connection.setManagedConnection(this );
116: connections.add(connection);
117: return connection;
118: }
119:
120: /**
121: * Create a connection. This method must be implemented by derived
122: * classes.
123: *
124: * @param subject security context as JAAS subject
125: * @param cxRequestInfo ConnectionRequestInfo instance
126: * @return the connection implementation object
127: * @see javax.resource.spi.ManagedConnection#getConnection
128: */
129: protected abstract ConnectionSupport createConnection(
130: Subject subject, ConnectionRequestInfo cxRequestInfo);
131:
132: /**
133: * Close the managed connection.
134: */
135: public void close(ConnectionSupport connection) {
136: ConnectionEvent event = new ConnectionEvent(this ,
137: ConnectionEvent.CONNECTION_CLOSED);
138: event.setConnectionHandle(connection);
139: for (Iterator i = listeners.iterator(); i.hasNext();) {
140: ((ConnectionEventListener) i.next())
141: .connectionClosed(event);
142: }
143: connection.setManagedConnection(null);
144: connections.remove(connection);
145: }
146:
147: /* (non-Javadoc)
148: * @see javax.resource.spi.ManagedConnection#associateConnection(java.lang.Object)
149: */
150: public void associateConnection(Object newConnection)
151: throws ResourceException {
152: ((ConnectionSupport) newConnection).setManagedConnection(this );
153: connections.add(newConnection);
154: }
155:
156: /* (non-Javadoc)
157: * @see javax.resource.spi.ManagedConnection#cleanup()
158: */
159: public void cleanup() throws ResourceException {
160: for (Iterator i = connections.iterator(); i.hasNext();) {
161: ConnectionSupport con = (ConnectionSupport) i.next();
162: con.setManagedConnection(null);
163: }
164: connections.clear();
165: }
166:
167: /* (non-Javadoc)
168: * @see javax.resource.spi.ManagedConnection#getLocalTransaction()
169: */
170: public LocalTransaction getLocalTransaction()
171: throws ResourceException {
172: throw new NotSupportedException("Not supported.");
173: }
174:
175: /* (non-Javadoc)
176: * @see javax.resource.spi.ManagedConnection#getXAResource()
177: */
178: public XAResource getXAResource() throws ResourceException {
179: throw new NotSupportedException("Not supported.");
180: }
181:
182: /* (non-Javadoc)
183: * @see javax.resource.spi.ManagedConnection#setLogWriter(java.io.PrintWriter)
184: */
185: public void setLogWriter(PrintWriter logWriter)
186: throws ResourceException {
187: this .logWriter = logWriter;
188: }
189:
190: /* (non-Javadoc)
191: * @see javax.resource.spi.ManagedConnection#getLogWriter()
192: */
193: public PrintWriter getLogWriter() throws ResourceException {
194: return logWriter;
195: }
196:
197: /* (non-Javadoc)
198: * @see javax.resource.spi.ManagedConnection#getMetaData()
199: */
200: public ManagedConnectionMetaData getMetaData()
201: throws ResourceException {
202: return metaData;
203: }
204:
205: /* (non-Javadoc)
206: * @see javax.resource.spi.ManagedConnection#addConnectionEventListener(javax.resource.spi.ConnectionEventListener)
207: */
208: public void addConnectionEventListener(
209: ConnectionEventListener listener) {
210: listeners.add(listener);
211: }
212:
213: /* (non-Javadoc)
214: * @see javax.resource.spi.ManagedConnection#removeConnectionEventListener(javax.resource.spi.ConnectionEventListener)
215: */
216: public void removeConnectionEventListener(
217: ConnectionEventListener listener) {
218: listeners.remove(listener);
219: }
220:
221: }
|