001: /*
002: * $Id: MuleManagedConnectionFactory.java 10789 2008-02-12 20:04:43Z 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.module.jca;
012:
013: import java.beans.PropertyChangeListener;
014: import java.beans.PropertyChangeSupport;
015: import java.io.IOException;
016: import java.io.ObjectInputStream;
017: import java.io.PrintWriter;
018: import java.util.Iterator;
019: import java.util.Set;
020:
021: import javax.resource.ResourceException;
022: import javax.resource.spi.ConnectionManager;
023: import javax.resource.spi.ConnectionRequestInfo;
024: import javax.resource.spi.ManagedConnection;
025: import javax.resource.spi.ManagedConnectionFactory;
026: import javax.resource.spi.security.PasswordCredential;
027: import javax.security.auth.Subject;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * <code>MuleManagedConnectionFactory</code> TODO
034: */
035:
036: public class MuleManagedConnectionFactory implements
037: ManagedConnectionFactory {
038: /**
039: * Serial version
040: */
041: private static final long serialVersionUID = -1460847590293644271L;
042:
043: private transient PrintWriter out;
044: private transient PropertyChangeSupport changes = new PropertyChangeSupport(
045: this );
046:
047: // userName property value
048: private String username = null;
049:
050: // password property value
051: private String password = null;
052:
053: /**
054: * logger used by this class
055: */
056: protected transient Log logger = LogFactory.getLog(this .getClass());
057:
058: public MuleManagedConnectionFactory() {
059: super ();
060: }
061:
062: private void readObject(ObjectInputStream in) throws IOException,
063: ClassNotFoundException {
064: in.defaultReadObject();
065: this .logger = LogFactory.getLog(this .getClass());
066: this .changes = new PropertyChangeSupport(this );
067: this .out = null;
068: }
069:
070: public int hashCode() {
071: final int PRIME = 31;
072: int result = 1;
073: result = PRIME * result
074: + ((password == null) ? 0 : password.hashCode());
075: return PRIME * result
076: + ((username == null) ? 0 : username.hashCode());
077: }
078:
079: public boolean equals(Object obj) {
080: if (this == obj) {
081: return true;
082: }
083:
084: if (obj == null) {
085: return false;
086: }
087:
088: if (this .getClass() != obj.getClass()) {
089: return false;
090: }
091:
092: final MuleManagedConnectionFactory other = (MuleManagedConnectionFactory) obj;
093:
094: if (password == null) {
095: if (other.password != null) {
096: return false;
097: }
098: } else if (!password.equals(other.password)) {
099: return false;
100: }
101:
102: if (username == null) {
103: if (other.username != null) {
104: return false;
105: }
106: } else if (!username.equals(other.username)) {
107: return false;
108: }
109:
110: return true;
111: }
112:
113: /**
114: * Creates a Connection Factory instance. The ConnectionFactory instance is
115: * initialized with the passed ConnectionManager. In the managed scenario,
116: * ConnectionManager is provided by the application server.
117: *
118: * @param cxManager ConnectionManager to be associated with created EIS
119: * connection factory instance
120: * @return EIS-specific Connection Factory instance
121: * @throws javax.resource.ResourceException if the attempt to create a connection
122: * factory fails
123: */
124:
125: public Object createConnectionFactory(ConnectionManager cxManager)
126: throws ResourceException {
127: try {
128: return new DefaultMuleConnectionFactory(this , cxManager,
129: null);
130: } catch (Exception e) {
131: throw new ResourceException(e);
132: }
133: }
134:
135: /**
136: * Creates a Connection Factory instance. The Connection Factory instance is
137: * initialized with a default ConnectionManager. In the non-managed scenario, the
138: * ConnectionManager is provided by the resource adapter.
139: *
140: * @return EIS-specific Connection Factory instance
141: * @throws ResourceException if the attempt to create a connection factory fails
142: */
143:
144: public Object createConnectionFactory() throws ResourceException {
145: return new DefaultMuleConnectionFactory(this , null, null);
146: }
147:
148: /**
149: * ManagedConnectionFactory uses the security information (passed as Subject) and
150: * additional ConnectionRequestInfo (which is specific to ResourceAdapter and
151: * opaque to application server) to create this new connection.
152: *
153: * @param subject caller's security information
154: * @param cxRequestInfo additional resource adapter specific connection request
155: * information
156: * @return ManagedConnection instance
157: * @throws ResourceException if the attempt to create a connection fails
158: */
159:
160: public ManagedConnection createManagedConnection(Subject subject,
161: ConnectionRequestInfo cxRequestInfo)
162: throws ResourceException {
163: return new MuleManagedConnection(this , subject, cxRequestInfo);
164: }
165:
166: /**
167: * Returns a matched managed connection from the candidate set of connections.
168: * ManagedConnectionFactory uses the security info (as in Subject) and
169: * information provided through ConnectionRequestInfo and additional Resource
170: * Adapter specific criteria to do matching. A MC that has the requested store is
171: * returned as a match
172: *
173: * @param connectionSet candidate connection set
174: * @param subject caller's security information
175: * @param cxRequestInfo additional resource adapter specific connection request
176: * information
177: * @return ManagedConnection if resource adapter finds an acceptable match,
178: * otherwise null
179: * @throws ResourceException if the match fails
180: */
181:
182: public ManagedConnection matchManagedConnections(Set connectionSet,
183: Subject subject, ConnectionRequestInfo cxRequestInfo)
184: throws ResourceException {
185: PasswordCredential pc = RaHelper.getPasswordCredential(this ,
186: subject, cxRequestInfo);
187:
188: Iterator it = connectionSet.iterator();
189: while (it.hasNext()) {
190: Object obj = it.next();
191: if (obj instanceof MuleManagedConnection) {
192: MuleManagedConnection mc = (MuleManagedConnection) obj;
193: PasswordCredential mcpc = mc.getPasswordCredential();
194: if (mcpc != null && pc != null && mcpc.equals(pc)) {
195: return mc;
196: }
197: }
198: }
199: return null;
200: }
201:
202: /**
203: * Sets the log writer for this ManagedConnectionFactory instance. The log writer
204: * is a character output stream to which all logging and tracing messages for
205: * this ManagedConnectionfactory instance will be printed.
206: *
207: * @param out an output stream for error logging and tracing
208: * @throws ResourceException if the method fails
209: */
210:
211: public void setLogWriter(PrintWriter out) throws ResourceException {
212: this .out = out;
213: }
214:
215: /**
216: * Gets the log writer for this ManagedConnectionFactory instance.
217: *
218: * @return PrintWriter an output stream for error logging and tracing
219: * @throws ResourceException if the method fails
220: */
221:
222: public PrintWriter getLogWriter() throws ResourceException {
223: return this .out;
224: }
225:
226: /**
227: * Associate PropertyChangeListener with the ManagedConnectionFactory, in order
228: * to notify about properties changes.
229: *
230: * @param lis the PropertyChangeListener to be associated with the
231: * ManagedConnectionFactory
232: */
233:
234: public void addPropertyChangeListener(PropertyChangeListener lis) {
235: changes.addPropertyChangeListener(lis);
236: }
237:
238: /**
239: * Delete association of PropertyChangeListener with the
240: * ManagedConnectionFactory.
241: *
242: * @param lis the PropertyChangeListener to be removed
243: */
244:
245: public void removePropertyChangeListener(PropertyChangeListener lis) {
246: changes.removePropertyChangeListener(lis);
247: }
248:
249: /**
250: * Returns the value of the userName property.
251: *
252: * @return the value of the userName property
253: */
254:
255: public String getUsername() {
256: return this .username;
257: }
258:
259: /**
260: * Sets the value of the userName property.
261: *
262: * @param username String containing the value to be assigned to userName
263: */
264:
265: public void setUsername(String username) {
266: this .username = username;
267: }
268:
269: /**
270: * Returns the value of the password property.
271: *
272: * @return the value of the password property
273: */
274:
275: public String getPassword() {
276: return this .password;
277: }
278:
279: /**
280: * Sets the value of the password property.
281: *
282: * @param password String containing the value to be assigned to password
283: */
284:
285: public void setPassword(String password) {
286: this.password = password;
287: }
288: }
|