KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > JNDIDataSourceFactory


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conf;
21
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import javax.sql.DataSource JavaDoc;
26
27 import org.apache.cayenne.access.QueryLogger;
28 import org.apache.cayenne.util.Util;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Looks up DataSource objects via JNDI.
34  *
35  * @author Andrus Adamchik
36  */

37 public class JNDIDataSourceFactory implements DataSourceFactory {
38
39     private static final Log logger = LogFactory.getLog(JNDIDataSourceFactory.class);
40
41     protected Configuration parentConfig;
42
43     public void initializeWithParentConfiguration(Configuration conf) {
44         this.parentConfig = conf;
45     }
46
47     /**
48      * Attempts to load DataSource using JNDI. In case of failure tries to get the
49      * DataSource with the same name from CayenneModeler preferences.
50      */

51     public DataSource JavaDoc getDataSource(String JavaDoc location) throws Exception JavaDoc {
52
53         try {
54             return loadViaJNDI(location);
55         }
56         catch (Exception JavaDoc ex) {
57
58             logger.info("failed JNDI lookup, attempt to load "
59                     + "from local preferences. Location key:"
60                     + location);
61
62             // failover to preferences loader to allow local development
63
try {
64                 return loadFromPreferences(location);
65             }
66             catch (Exception JavaDoc preferencesException) {
67
68                 logger.info("failed loading from local preferences", Util
69                         .unwindException(preferencesException));
70
71                 // giving up ... rethrow original exception...
72
QueryLogger.logConnectFailure(ex);
73                 throw ex;
74             }
75         }
76     }
77
78     DataSource JavaDoc loadViaJNDI(String JavaDoc location) throws NamingException JavaDoc {
79         QueryLogger.logConnect(location);
80
81         Context JavaDoc initCtx = new InitialContext JavaDoc();
82         DataSource JavaDoc ds;
83         try {
84             Context JavaDoc envCtx = (Context JavaDoc) initCtx.lookup("java:comp/env");
85             ds = (DataSource JavaDoc) envCtx.lookup(location);
86         }
87         catch (NamingException JavaDoc namingEx) {
88             // try looking up the location directly...
89
ds = (DataSource JavaDoc) initCtx.lookup(location);
90         }
91
92         QueryLogger.logConnectSuccess();
93         return ds;
94     }
95
96     DataSource JavaDoc loadFromPreferences(String JavaDoc location) throws Exception JavaDoc {
97         // as we don't want compile dependencies on the Modeler, instantiate factory via
98
// reflection ...
99

100         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
101         DataSourceFactory prefsFactory = (DataSourceFactory) Class
102                 .forName("org.apache.cayenne.modeler.pref.PreferencesDataSourceFactory", true, loader)
103                 .newInstance();
104
105         prefsFactory.initializeWithParentConfiguration(parentConfig);
106         return prefsFactory.getDataSource(location);
107     }
108 }
109
Popular Tags