KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > datasource > JndiDataSourceFactory


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.datasource;
17
18 import com.ibatis.sqlmap.client.SqlMapException;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.sql.DataSource JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * DataSourceFactory implementation for JNDI
31  */

32 public class JndiDataSourceFactory implements DataSourceFactory {
33
34   private DataSource JavaDoc dataSource;
35
36   public void initialize(Map JavaDoc properties) {
37     try {
38       InitialContext JavaDoc initCtx = null;
39       Hashtable JavaDoc context = getContextProperties(properties);
40
41       if (context == null) {
42         initCtx = new InitialContext JavaDoc();
43       } else {
44         initCtx = new InitialContext JavaDoc(context);
45       }
46
47       if (properties.containsKey("DataSource")) {
48         dataSource = (DataSource JavaDoc) initCtx.lookup((String JavaDoc) properties.get("DataSource"));
49       } else if (properties.containsKey("DBJndiContext")) { // LEGACY --Backward compatibility
50
dataSource = (DataSource JavaDoc) initCtx.lookup((String JavaDoc) properties.get("DBJndiContext"));
51       } else if (properties.containsKey("DBFullJndiContext")) { // LEGACY --Backward compatibility
52
dataSource = (DataSource JavaDoc) initCtx.lookup((String JavaDoc) properties.get("DBFullJndiContext"));
53       } else if (properties.containsKey("DBInitialContext")
54           && properties.containsKey("DBLookup")) { // LEGACY --Backward compatibility
55
Context JavaDoc ctx = (Context JavaDoc) initCtx.lookup((String JavaDoc) properties.get("DBInitialContext"));
56         dataSource = (DataSource JavaDoc) ctx.lookup((String JavaDoc) properties.get("DBLookup"));
57       }
58
59     } catch (NamingException JavaDoc e) {
60       throw new SqlMapException("There was an error configuring JndiDataSourceDaoTransactionPool. Cause: " + e, e);
61     }
62   }
63
64   public DataSource JavaDoc getDataSource() {
65     return dataSource;
66   }
67
68   private static Hashtable JavaDoc getContextProperties(Map JavaDoc allProps) {
69     final String JavaDoc PREFIX = "context.";
70     Hashtable JavaDoc contextProperties = null;
71     Iterator JavaDoc keys = allProps.keySet().iterator();
72     while (keys.hasNext()) {
73       String JavaDoc key = (String JavaDoc) keys.next();
74       String JavaDoc value = (String JavaDoc) allProps.get(key);
75       if (key.startsWith(PREFIX)) {
76         if (contextProperties == null) {
77           contextProperties = new Properties JavaDoc();
78         }
79         contextProperties.put(key.substring(PREFIX.length()), value);
80       }
81     }
82     return contextProperties;
83   }
84
85 }
86
87
Popular Tags