View Javadoc

1   /***
2    * @(#)XADataSourceFactory.java
3    * 
4    * JFoxSOAF, Service-Oriented Application Framework
5    * 
6    * Copyright(c) JFoxSOAF Team
7    * 
8    * Licensed under the GNU LGPL, Version 2.1 (the "License"); 
9    * you may not use this file except in compliance with the License. 
10   * You may obtain a copy of the License at  
11   * 
12   * http://www.gnu.org/copyleft/lesser.html
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, 
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17   * See the License for the specific language governing permissions and 
18   * limitations under the License. 
19   * 
20   * For more information, please visit:
21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22   * http://www.huihoo.org/jfox/jfoxsoaf
23   */
24  
25  package org.huihoo.jfox.soaf.services.jdbc;
26  
27  import java.util.Enumeration;
28  import java.util.Hashtable;
29  import java.util.Vector;
30  
31  import javax.sql.DataSource;
32  
33  import org.apache.commons.lang.math.NumberUtils;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.enhydra.jdbc.standard.StandardXADataSource;
37  import org.huihoo.jfox.soaf.container.ServiceFactory;
38  import org.huihoo.jfox.soaf.services.transaction.JotmTransactionService;
39  
40  /***
41   * <p>
42   * XADataSource Factory.
43   * </p>
44   * 
45   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
46   * @version $Revision: 1.1 $ $Date: 2006/02/15 08:45:45 $
47   * @version Revision: 1.0
48   */
49  
50  public class XADataSourceFactory extends DataSourceFactory {
51  
52  	/***
53  	 * @param arg0
54  	 */
55  	public XADataSourceFactory() {
56  	}
57  
58  	private DataSource xads;
59  
60  	private Hashtable attributes = new Hashtable();
61  
62  	private JotmTransactionService jotmTS = (JotmTransactionService) ServiceFactory
63  			.getInstance().getService(JotmTransactionService.class);
64  
65  	private final Log logger = LogFactory.getLog(getClass());
66  
67  	public DataSource getInstance() {
68  		xads = new StandardXADataSource();
69  		try {
70  			((StandardXADataSource) xads)
71  					.setDriverName((String) getAttribute(DatabaseConstant.JDBC_DRIVER_CLASS_NAME));
72  			((StandardXADataSource) xads)
73  					.setUrl((String) getAttribute(DatabaseConstant.JDBC_URL));
74  			((StandardXADataSource) xads)
75  					.setUser((String) getAttribute(DatabaseConstant.JDBC_USERNAME));
76  			((StandardXADataSource) xads)
77  					.setPassword((String) getAttribute(DatabaseConstant.JDBC_PASSWORD));
78  			String maxConNum = (String) getAttribute(DatabaseConstant.JDBC_MAX_POOL_SIZE);
79  			String minConNum = (String) getAttribute(DatabaseConstant.JDBC_MIN_POOL_SIZE);
80  			((StandardXADataSource) xads).setMaxCon(NumberUtils
81  					.toInt(maxConNum));
82  			((StandardXADataSource) xads).setMinCon(NumberUtils
83  					.toInt(minConNum));
84  			((StandardXADataSource) xads).setTransactionManager(jotmTS
85  					.getTransactionManager());
86  		} catch (Exception e) {
87  			logger.error("Init XADataSource failed " + e.getMessage());
88  		}
89  		return xads;
90  	}
91  
92  	/***
93  	 * Return the configuration attribute with the specified name (if any), or
94  	 * <code>null</code> if there is no such attribute.
95  	 * 
96  	 * @param name
97  	 *            Name of the attribute to return
98  	 * @return attribute
99  	 */
100 	public Object getAttribute(String name) {
101 		return attributes.get(name);
102 	}
103 
104 	/***
105 	 * Return an array containing the names of all currently defined
106 	 * configuration attributes. If there are no such attributes, a zero length
107 	 * array is returned.
108 	 * 
109 	 * @return attribute names
110 	 */
111 	public String[] getAttributeNames() {
112 		Vector names = new Vector();
113 		Enumeration keys = attributes.keys();
114 		while (keys.hasMoreElements()) {
115 			names.addElement(keys.nextElement());
116 		}
117 		String results[] = new String[names.size()];
118 		for (int i = 0; i < results.length; i++) {
119 			results[i] = (String) names.elementAt(i);
120 		}
121 		return (results);
122 	}
123 
124 	/***
125 	 * Remove any configuration attribute associated with the specified name. If
126 	 * there is no such attribute, no action is taken.
127 	 * 
128 	 * @param name
129 	 *            Name of the attribute to remove
130 	 */
131 	public void removeAttribute(String name) {
132 		attributes.remove(name);
133 
134 	}
135 
136 	/***
137 	 * Set the configuration attribute with the specified name. Calling this
138 	 * with a <code>null</code> value is equivalent to calling
139 	 * <code>removeAttribute(name)</code>.
140 	 * 
141 	 * @param name
142 	 *            Name of the attribute to set
143 	 * @param value
144 	 *            Value of the attribute to set, or <code>null</code> to
145 	 *            remove any setting for this attribute
146 	 */
147 	public void setAttribute(String name, Object value) {
148 		if (value == null) {
149 			attributes.remove(name);
150 		} else {
151 			attributes.put(name, value);
152 		}
153 	}
154 }