01: /*
02: * Copyright 2004 Clinton Begin
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
21: import javax.naming.InitialContext;
22: import javax.naming.NamingException;
23: import javax.sql.DataSource;
24: import java.util.Hashtable;
25: import java.util.Iterator;
26: import java.util.Map;
27: import java.util.Properties;
28:
29: /**
30: * DataSourceFactory implementation for JNDI
31: */
32: public class JndiDataSourceFactory implements DataSourceFactory {
33:
34: private DataSource dataSource;
35:
36: public void initialize(Map properties) {
37: try {
38: InitialContext initCtx = null;
39: Hashtable context = getContextProperties(properties);
40:
41: if (context == null) {
42: initCtx = new InitialContext();
43: } else {
44: initCtx = new InitialContext(context);
45: }
46:
47: if (properties.containsKey("DataSource")) {
48: dataSource = (DataSource) initCtx
49: .lookup((String) properties.get("DataSource"));
50: } else if (properties.containsKey("DBJndiContext")) { // LEGACY --Backward compatibility
51: dataSource = (DataSource) initCtx
52: .lookup((String) properties
53: .get("DBJndiContext"));
54: } else if (properties.containsKey("DBFullJndiContext")) { // LEGACY --Backward compatibility
55: dataSource = (DataSource) initCtx
56: .lookup((String) properties
57: .get("DBFullJndiContext"));
58: } else if (properties.containsKey("DBInitialContext")
59: && properties.containsKey("DBLookup")) { // LEGACY --Backward compatibility
60: Context ctx = (Context) initCtx
61: .lookup((String) properties
62: .get("DBInitialContext"));
63: dataSource = (DataSource) ctx
64: .lookup((String) properties.get("DBLookup"));
65: }
66:
67: } catch (NamingException e) {
68: throw new SqlMapException(
69: "There was an error configuring JndiDataSourceTransactionPool. Cause: "
70: + e, e);
71: }
72: }
73:
74: public DataSource getDataSource() {
75: return dataSource;
76: }
77:
78: private static Hashtable getContextProperties(Map allProps) {
79: final String PREFIX = "context.";
80: Hashtable contextProperties = null;
81: Iterator keys = allProps.keySet().iterator();
82: while (keys.hasNext()) {
83: String key = (String) keys.next();
84: String value = (String) allProps.get(key);
85: if (key.startsWith(PREFIX)) {
86: if (contextProperties == null) {
87: contextProperties = new Properties();
88: }
89: contextProperties.put(key.substring(PREFIX.length()),
90: value);
91: }
92: }
93: return contextProperties;
94: }
95:
96: }
|