01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.jdbcx;
07:
08: import java.util.Hashtable;
09:
10: //#ifdef JDK14
11: import javax.naming.Context;
12: import javax.naming.Name;
13: import javax.naming.Reference;
14: import javax.naming.spi.ObjectFactory; //#endif
15:
16: import org.h2.constant.SysProperties;
17: import org.h2.engine.Constants;
18: import org.h2.message.Trace;
19: import org.h2.message.TraceSystem;
20:
21: /**
22: * This class is used to create new DataSource objects.
23: * An application should not use this class directly.
24: */
25: public class JdbcDataSourceFactory
26: //#ifdef JDK14
27: implements ObjectFactory
28: //#endif
29: {
30:
31: private static TraceSystem traceSystem;
32: private Trace trace;
33:
34: static {
35: org.h2.Driver.load();
36: traceSystem = new TraceSystem(
37: SysProperties.CLIENT_TRACE_DIRECTORY + "h2datasource"
38: + Constants.SUFFIX_TRACE_FILE, false);
39: traceSystem.setLevelFile(SysProperties.DATASOURCE_TRACE_LEVEL);
40: }
41:
42: /**
43: * The public constructor to create new factory objects.
44: */
45: public JdbcDataSourceFactory() {
46: trace = traceSystem.getTrace("JDBCX");
47: }
48:
49: /**
50: * Creates a new object using the specified location or reference
51: * information.
52: *
53: * @param obj the reference (this factory only supports objects of type
54: * javax.naming.Reference)
55: * @param name unused
56: * @param nameCtx unused
57: * @param environment unused
58: * @return the new JdbcDataSource, or null if the reference class name is
59: * not JdbcDataSource.
60: */
61: //#ifdef JDK14
62: public synchronized Object getObjectInstance(Object obj, Name name,
63: Context nameCtx, Hashtable environment) throws Exception {
64: trace
65: .debug("getObjectInstance obj=" + obj + " name=" + name
66: + " nameCtx=" + nameCtx + " environment="
67: + environment);
68: Reference ref = (Reference) obj;
69: if (ref.getClassName().equals(JdbcDataSource.class.getName())) {
70: JdbcDataSource dataSource = new JdbcDataSource();
71: dataSource.setURL((String) ref.get("url").getContent());
72: dataSource.setUser((String) ref.get("user").getContent());
73: dataSource.setPassword((String) ref.get("password")
74: .getContent());
75: String s = (String) ref.get("loginTimeout").getContent();
76: dataSource.setLoginTimeout(Integer.parseInt(s));
77: return dataSource;
78: }
79: return null;
80: }
81:
82: //#endif
83:
84: TraceSystem getTraceSystem() {
85: return traceSystem;
86: }
87:
88: Trace getTrace() {
89: return trace;
90: }
91:
92: }
|