File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbcx\JdbcDataSourceFactory.java.html

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