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