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