/*
* $Id: JNDIFactory.java,v 1.7 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.database.jndi;
import anvil.server.PoolPreferences;
import anvil.database.PooledConnection;
import anvil.database.ConnectionPool;
import anvil.database.ConnectionFactory;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.ldap.InitialLdapContext;
/**
* Factory used by <code>ConnectionPool</code> to create the
* actual <code>PooledConnection</code> instances.
*
* @see PooledConnection
* @see ConnectionPool
* @version $Revision: 1.7 $
* @author Simo Tuokko
*/
public class JNDIFactory implements ConnectionFactory
{
private static final String[] NAMES = {
Context.INITIAL_CONTEXT_FACTORY, //java.naming.factory.initial
Context.OBJECT_FACTORIES, //java.naming.factory.object
Context.STATE_FACTORIES, //java.naming.factory.state
Context.URL_PKG_PREFIXES, //java.naming.factory.url.pkgs
Context.PROVIDER_URL, //java.naming.provider.url
Context.DNS_URL, //java.naming.dns.url
Context.AUTHORITATIVE, //java.naming.authoritative
Context.BATCHSIZE, //java.naming.batchsize
Context.REFERRAL, //java.naming.referral
Context.SECURITY_PROTOCOL, //java.naming.security.protocol
Context.SECURITY_AUTHENTICATION, //java.naming.security.authentication
Context.SECURITY_PRINCIPAL, //java.naming.security.principal
Context.SECURITY_CREDENTIALS, //java.naming.security.credentials
Context.LANGUAGE, //java.naming.language
Context.APPLET //java.naming.applet
};
private ConnectionPool pool;
/**
* Count of connections created.
*/
private long count = 0;
/**
* Hashtable containing environment parameters for JNDI Context
*/
private Hashtable properties;
/*
* Type of Context (normal, directory, ldap...)
*/
private String ctxclass = "normal";
/**
* Initializes new factory.
*
*/
public void initialize(ConnectionPool pool)
{
this.pool = pool;
properties = new Hashtable();
String tmp;
for (int i=0, n=NAMES.length; i<n; i++) {
tmp = (String)pool.getPreference(NAMES[i]);
if (tmp != null) {
properties.put(NAMES[i], tmp);
}
}
String type = (String)pool.getPreference("type");
if (type != null) {
ctxclass = type;
}
this.pool.getZone().log().info("JNDIFactory for pool '"+pool.getName()+"' initialized");
}
/**
* Creates new context.
*
* @throws NamingException If something goes wrong
*/
public PooledConnection create() throws Exception
{
long started = System.currentTimeMillis();
Context context;
if (ctxclass.equalsIgnoreCase("ldap")) {
context = new InitialLdapContext(properties, null);
} else if (ctxclass.equalsIgnoreCase("directory")) {
context = new InitialDirContext(properties);
} else {
context = new InitialContext(properties);
}
//this.pool.getZone().log().info("Created context: "+context+" with params: "+properties);
count++;
JNDIConnection connectionImpl =
new JNDIConnection(pool, pool.getName() + '-' + count, context);
pool.connectionCreated(connectionImpl, System.currentTimeMillis() - started);
return connectionImpl;
}
}