/*
* $Id: NamingModule.java,v 1.9 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.core.naming;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import anvil.database.ConnectionManager;
import anvil.database.PooledConnection;
import anvil.database.NoConnectionPoolException;
import anvil.database.CannotReturnPooledConnectionException;
import anvil.script.Context;
import anvil.core.Any;
import anvil.core.Array;
import anvil.core.AnyMap;
import anvil.core.AnySequence;
import anvil.core.AnyUtils;
import anvil.Log;
import anvil.java.util.BindingEnumeration;
/// @module anvil.naming
/// Provides the functions and classes and for accessing naming services.
/// @see javax.naming
/// @see javax.naming.directory
/// @see javax.naming.ldap
/**
* class NamingModule
*
* @author: Simo Tuokko
* @author: Jani Lehtim�ki
*/
public class NamingModule
{
/// @const ADD
/// Modification attribute: add new attribute
public static final Any ADD = Any.create(DirContext.ADD_ATTRIBUTE);
/// @const REPLACE
/// Modification attribute: add or replace attribute
public static final Any REPLACE = Any.create(DirContext.REPLACE_ATTRIBUTE);
/// @const REMOVE
/// Modification attribute: remove attribute
public static final Any REMOVE = Any.create(DirContext.REMOVE_ATTRIBUTE);
/// @const OBJECT_SCOPE
/// Searching attribute: Search the named object.
public static final Any OBJECT_SCOPE = Any.create(SearchControls.OBJECT_SCOPE);
/// @const ONELEVEL_SCOPE
/// Searching attribute: Search one level of the named object.
public static final Any ONELEVEL_SCOPE = Any.create(SearchControls.ONELEVEL_SCOPE);
/// @const SUBTREE_SCOPE
/// Searching attribute: Search the entire subtree rooted at the named object.
public static final Any SUBTREE_SCOPE = Any.create(SearchControls.SUBTREE_SCOPE);
private static final PooledConnection acquireConnection(Context context, String ckey)
{
PooledConnection connImpl = null;
javax.naming.Context ctx = null;
try {
ConnectionManager manager = context.address().getZone().getManagerFor(ckey);
connImpl = manager.acquire(ckey);
return connImpl;
} catch (CannotReturnPooledConnectionException e) {
throw context.AcquireError(e.getMessage());
} catch (NoConnectionPoolException e) {
throw context.AcquireError("No such connection pool: " + ckey);
} catch (Exception e) {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e2) {
}
}
connImpl.release();
throw context.exception(e);
}
}
/// @function acquire
/// Acquires JNDI context explicitly.
/// 'poolName' has same rules as in query function.
/// @synopsis NamingContext acquire(string poolName)
/// @param poolName Configured pool name
public static final Object[] p_acquire = { null, "key"};
public static final Any acquire(Context context, String ckey)
{
PooledConnection connImpl = acquireConnection(context, ckey);
if (connImpl == null) {
return Any.NULL;
} else {
return new AnyNamingContext(connImpl);
}
}
/// @function NamingContext connect(string type, array properties)
/// Creates new NamingContext
/// @param type Type of context: ldap, directory, ...
/// @param properties Array of properties
public static final Any connect(Context context, String type, Any properties)
{
java.util.Properties props = new java.util.Properties();
if (properties.isArray()) {
BindingEnumeration e = properties.enumeration();
while(e.hasMoreElements()) {
props.setProperty(e.nextKey().toString(), e.nextElement().toString());
}
}
try {
javax.naming.Context ctx;
if (type.equalsIgnoreCase("ldap")) {
ctx = new javax.naming.ldap.InitialLdapContext(props, null);
} else if (type.equalsIgnoreCase("directory")) {
ctx = new javax.naming.directory.InitialDirContext(props);
} else {
ctx = new javax.naming.InitialContext(props);
}
return new AnyNamingContext(ctx);
} catch (NamingException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeNamespace __module__ =
new anvil.script.compiler.NativeNamespace(
"naming",
NamingModule.class,
new Class[] {
AnyAttribute.class,
AnyAttributes.class,
AnyNamingContext.class,
AnySearchResult.class,
AnyName.class,
AnyNameParser.class,
AnySearchControls.class,
},
//DOC{{
""+
" @module anvil.naming\n" +
" Provides the functions and classes and for accessing naming services. \n" +
" @see javax.naming\n" +
" @see javax.naming.directory\n" +
" @see javax.naming.ldap\n" +
" @const ADD\n" +
" Modification attribute: add new attribute\n" +
" @const REPLACE\n" +
" Modification attribute: add or replace attribute\n" +
" @const REMOVE\n" +
" Modification attribute: remove attribute\n" +
" @const OBJECT_SCOPE\n" +
" Searching attribute: Search the named object.\n" +
" @const ONELEVEL_SCOPE\n" +
" Searching attribute: Search one level of the named object.\n" +
" @const SUBTREE_SCOPE\n" +
" Searching attribute: Search the entire subtree rooted at the named object.\n" +
" @function acquire\n" +
" Acquires JNDI context explicitly.\n" +
" 'poolName' has same rules as in query function.\n" +
" @synopsis NamingContext acquire(string poolName)\n" +
" @param poolName Configured pool name\n" +
" @function NamingContext connect(string type, array properties)\n" +
" Creates new NamingContext\n" +
" @param type Type of context: ldap, directory, ...\n" +
" @param properties Array of properties\n"
//}}DOC
);
}