View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2006 Beck et al. projects GmbH München (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.connector.impl;
24  
25  import java.io.PrintWriter;
26  import java.util.Iterator;
27  import java.util.Set;
28  
29  import javax.resource.NotSupportedException;
30  import javax.resource.ResourceException;
31  import javax.resource.spi.ConnectionManager;
32  import javax.resource.spi.ConnectionRequestInfo;
33  import javax.resource.spi.ManagedConnection;
34  import javax.resource.spi.ManagedConnectionFactory;
35  import javax.resource.spi.ResourceAdapter;
36  import javax.resource.spi.ResourceAdapterAssociation;
37  import javax.resource.spi.security.GenericCredential;
38  import javax.resource.spi.security.PasswordCredential;
39  import javax.security.auth.Subject;
40  
41  import de.bea.domingo.i18n.ResourceManager;
42  import de.bea.domingo.i18n.Resources;
43  
44  
45  /***
46   * @author <a href=mailto:kurt.riede@bea.de>Kurt Riede</a>
47   */
48  public final class DomingoManagedConnectionFactory implements ManagedConnectionFactory, ResourceAdapterAssociation {
49  
50      /*** serial version ID for serialization. */
51      private static final long serialVersionUID = -4991035518745150159L;
52  
53      /*** Internationalized resources. */
54      private static final Resources RESOURCES = ResourceManager.getPackageResources(DomingoConnectionFactoryImpl.class);
55  
56      /*** Reference to the associated resource adapter. */
57      private ResourceAdapter resourceAdapter;
58  
59      /*** Reference to a Domingo monitor for logging. */
60      private DomingoLogAdapter monitor;
61  
62      /*** The host for the connection to Lotus Domino. */
63      private String host;
64  
65      /*** The port for the connection to Lotus Domino. */
66      private String port;
67  
68      /***
69       * Constructor.
70       */
71      public DomingoManagedConnectionFactory() {
72          this.monitor = new DomingoLogAdapter();
73      }
74  
75      /***
76       * {@inheritDoc}
77       *
78       * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory()
79       */
80      public Object createConnectionFactory() throws ResourceException {
81          throw new NotSupportedException(RESOURCES.getString("method.not.supported.instead.1",
82                  "createConnectionFactory(ConnectionManager)"));
83      }
84  
85      /***
86       * {@inheritDoc}
87       *
88       * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager)
89       */
90      public Object createConnectionFactory(final ConnectionManager connectionManager) throws ResourceException {
91          return new DomingoConnectionFactoryImpl(this, connectionManager);
92      }
93  
94      /***
95       * {@inheritDoc}
96       *
97       * @see javax.resource.spi.ManagedConnectionFactory#createManagedConnection(javax.security.auth.Subject,
98       *      javax.resource.spi.ConnectionRequestInfo)
99       */
100     public ManagedConnection createManagedConnection(final Subject subject, final ConnectionRequestInfo cxRequestInfo)
101             throws ResourceException { // TODO connectionRequestInfo
102         DomingoManagedConnection managedConnection = null;
103         ConnectionRequestInfo cri = null;
104         if (subject == null) {
105             cri = mergeConnectionRequestParams(cxRequestInfo);
106             managedConnection = new DomingoManagedConnection(monitor, this, cri);
107         } else {
108             cri = cxRequestInfo;
109             PasswordCredential pc = getPasswordCredential(subject);
110             if (pc != null) {
111                 managedConnection = new DomingoManagedConnection(monitor, this, pc);
112             } else {
113                 GenericCredential gc = getGenericCredential(subject);
114                 if (gc != null) {
115                     managedConnection = new DomingoManagedConnection(monitor, this, gc);
116                 } else {
117                     throw new ResourceException("invalid.request.info");
118                 }
119             }
120         }
121         return managedConnection;
122     }
123 
124     /***
125      * Merges info in the CRI with the instance's property values to form a new
126      * ConnectionRequestInfo.
127      *
128      * @param cri ConnectionRequestInfo to merge with the instance's property values
129      * @return ConnectionRequestInfo reflecting CRI info merged with this instance's property values
130      * @exception ResourceException
131      */
132     private ConnectionRequestInfo mergeConnectionRequestParams(final ConnectionRequestInfo cri) throws ResourceException {
133         if (cri != null) {
134             if (!(cri instanceof DomingoConnectionSpec)) {
135                 throw new ResourceException(RESOURCES.getString("method.invalid.argument",
136                         "mergeConnectionRequestParams", cri.getClass().getName()));
137             }
138             return cri; // TODO must merge the specs here?
139         }
140         return new DomingoConnectionSpec();
141     }
142 
143     /***
144      * {@inheritDoc}
145      *
146      * @see javax.resource.spi.ManagedConnectionFactory#getLogWriter()
147      */
148     public PrintWriter getLogWriter() throws ResourceException {
149         return monitor.getWriter();
150     }
151 
152     /***
153      * {@inheritDoc}
154      *
155      * @see javax.resource.spi.ManagedConnectionFactory#matchManagedConnections(java.util.Set,
156      *      javax.security.auth.Subject,
157      *      javax.resource.spi.ConnectionRequestInfo)
158      */
159     public ManagedConnection matchManagedConnections(final Set connectionSet, final Subject subject,
160             final ConnectionRequestInfo info) throws ResourceException {
161         DomingoManagedConnection mc = null;
162         for (Iterator iter = connectionSet.iterator(); iter.hasNext();) {
163             boolean eligible = false;
164             DomingoManagedConnection temp = (DomingoManagedConnection) iter.next();
165             if (equals(temp.getManagedConnectionFactory())) {
166                 DomingoConnectionSpec cri = (DomingoConnectionSpec) info;
167                 try {
168                     eligible = matchConnection(temp, subject, cri);
169                 } catch (Exception e) {
170                     eligible = false;
171                 }
172             }
173             if (eligible) {
174                 mc = temp;
175                 break;
176             }
177         }
178         return mc;
179     }
180 
181     /***
182      * checks whether the connection is valid.
183      *
184      * @param mc the ICONManagedConnection to check
185      * @param sub the Subject to use in checking the connection
186      * @param cri the ConnectionRequestInfo to use in checking the connection
187      * @return whether the connection is valid
188      * @throws ResourceException if hte connection cannot be matched
189      */
190     protected boolean matchConnection(final DomingoManagedConnection mc, final Subject sub, final DomingoConnectionSpec cri)
191             throws ResourceException {
192          return true; // TODO check this
193     }
194 
195     /***
196      * {@inheritDoc}
197      *
198      * @see javax.resource.spi.ManagedConnectionFactory#setLogWriter(java.io.PrintWriter)
199      */
200     public void setLogWriter(final PrintWriter writer) throws ResourceException {
201         monitor.setWriter(writer);
202     }
203 
204     /***
205      * @see javax.resource.spi.ResourceAdapterAssociation#getResourceAdapter()
206      * {@inheritDoc}
207      *
208      */
209     public ResourceAdapter getResourceAdapter() {
210         return resourceAdapter;
211     }
212 
213     /***
214      * {@inheritDoc}
215      *
216      * @see javax.resource.spi.ResourceAdapterAssociation#setResourceAdapter(javax.resource.spi.ResourceAdapter)
217      */
218     public void setResourceAdapter(final ResourceAdapter adapter) throws ResourceException {
219         resourceAdapter = adapter;
220     }
221 
222     /***
223      * Returns the password credentials from a subject if exists.
224      *
225      * @param subject the subject to search in
226      * @return password credentials or <code>null</code> if not found
227      */
228     private PasswordCredential getPasswordCredential(final Subject subject) {
229         final Set theSet = subject.getPrivateCredentials(PasswordCredential.class);
230         for (Iterator iterator = theSet.iterator(); iterator.hasNext();) {
231             final PasswordCredential passwordCredential = (PasswordCredential) iterator.next();
232             if (passwordCredential.getManagedConnectionFactory().equals(this)) {
233                 return passwordCredential;
234             }
235         }
236         return null;
237     }
238 
239     /***
240      * Returns the generic credentials from a subject if exists.
241      *
242      * @param subject the subject to search in
243      * @return generic credentials or <code>null</code> if not found
244      * @throws ResourceException
245      */
246     private GenericCredential getGenericCredential(final Subject subject) throws ResourceException {
247         GenericCredential gc = null;
248         Set theSet = subject.getPrivateCredentials(GenericCredential.class);
249         if (theSet.size() == 0) {
250             theSet = subject.getPublicCredentials(GenericCredential.class);
251         }
252         int noOfEntries = 0;
253         Iterator iter = theSet.iterator();
254         if (iter.hasNext()) {
255             gc = (GenericCredential) iter.next();
256             noOfEntries++;
257         }
258         if (noOfEntries == 0) {
259             getLogWriter().println("no generic credentials found in subject");
260         } else  if (noOfEntries > 1) {
261             getLogWriter().println("more than one generic credential found in subject");
262         }
263         return gc;
264     }
265 
266     /***
267      * Returns the host of a connection to Lotus Domino.
268      *
269      * @return host name
270      */
271     public String getHost() {
272         return host;
273     }
274 
275     /***
276      * Returns the host of a connection to Lotus Domino.
277      *
278      * @return host name
279      */
280     public String getPort() {
281         return port;
282     }
283 
284     /***
285      * Sets the host name of the server for a connection.
286      *
287      * @param h name or IP-address of Lotus Domino server
288      */
289     public void setHost(final String h) {
290         this.host = h;
291     }
292 
293     /***
294      * Sets the port of the server for a connection.
295      *
296      * @param p port of Lotus Domino server
297      */
298     public void setPort(final String p) {
299         this.port = p;
300     }
301 }