01: package org.geotools.data.jdbc.datasource;
02:
03: import java.io.IOException;
04: import java.util.Map;
05:
06: import javax.sql.DataSource;
07:
08: import org.geotools.data.DataSourceException;
09: import org.geotools.data.DataStoreFactorySpi.Param;
10: import org.geotools.factory.GeoTools;
11:
12: /**
13: * A datasource factory SPI doing JDNI lookups
14: * @author Administrator
15: *
16: */
17: public class JNDIDataSourceFactory extends AbstractDataSourceFactorySpi {
18:
19: public static final Param DSTYPE = new Param("dstype",
20: String.class, "Must be JNDI", false);
21:
22: public static final Param JNDI_REFNAME = new Param(
23: "jdniReferenceName", String.class,
24: "The path where the connection pool must be located", true);
25:
26: private static final Param[] PARAMS = new Param[] { DSTYPE,
27: JNDI_REFNAME };
28:
29: public DataSource createDataSource(Map params) throws IOException {
30: return createNewDataSource(params);
31: }
32:
33: public boolean canProcess(Map params) {
34: return super .canProcess(params)
35: && "JNDI".equals(params.get("dstype"));
36: }
37:
38: public DataSource createNewDataSource(Map params)
39: throws IOException {
40: String refName = (String) JNDI_REFNAME.lookUp(params);
41: try {
42: return (DataSource) GeoTools.getInitialContext(
43: GeoTools.getDefaultHints()).lookup(refName);
44: } catch (Exception e) {
45: throw new DataSourceException(
46: "Could not find the specified data source in JNDI",
47: e);
48: }
49: }
50:
51: public String getDescription() {
52: return "A JNDI based DataSource locator. Provide the JDNI location of a DataSource object in order to make it work";
53: }
54:
55: public Param[] getParametersInfo() {
56: return PARAMS;
57: }
58:
59: /**
60: * Make sure a JNDI context is available
61: */
62: public boolean isAvailable() {
63: try {
64: GeoTools.getInitialContext(GeoTools.getDefaultHints());
65: return true;
66: } catch (Exception e) {
67: return false;
68: }
69: }
70:
71: }
|