/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: SetInitialContextTag.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.2 2005/09/05 09:41:48 drmlipp
* Synchronized with 1.3.2.
*
* Revision 1.1.2.2 2005/08/30 14:37:43 drmlipp
* LDAP query workging.
*
* Revision 1.1.2.1 2005/08/29 15:21:44 drmlipp
* Started Jelly LDAP taglib.
*
*/
package de.danet.an.util.jellytags.ldap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
/**
* This class provides the tag used to get an LDAP directory initial
* context.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 1607 $
*/
public class SetInitialContextTag extends TagSupport {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory
.getLog(SetInitialContextTag.class);
/** The variable name to export. */
private String var;
/** The variable scope to export */
private String scope;
/** The provider URL. */
private String providerUrl = null;
/** The initial context factory. */
private String initialContextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
/** The securiyt principal. */
private String securityPrincipal = null;
/** The security credentials */
private String securityCredentials = null;
/** The environment. */
private Hashtable environment = new Hashtable ();
/**
* Creates an instance of <code>SetInitialContextTag</code>
* with all attributes initialized to default values.
*/
public SetInitialContextTag () {
}
/**
* Sets the name of the variable exported by this tag.
* @param var the variable's name
*/
public void setVar(String var) {
this.var = var;
}
/**
* Sets the variable scope for this variable. For example setting
* this value to 'parent' will set this value in the parent
* scope. When Jelly is run from inside a Servlet environment then
* other scopes will be available such as 'request', 'session' or
* 'application'.<P>
*
* Other applications may implement their own custom scopes.
* @param scope the scope
*/
public void setScope(String scope) {
this.scope = scope;
}
/**
* @param initialContextFactory The initialContextFactory to set.
*/
public void setInitialContextFactory(String initialContextFactory) {
this.initialContextFactory = initialContextFactory;
}
/**
* @param providerUrl The providerUrl to set.
*/
public void setProviderUrl(String providerUrl) {
this.providerUrl = providerUrl;
}
/**
* @param securityPrincipal The securityPrincipal to set.
*/
public void setSecurityPrincipal(String securityPrincipal) {
this.securityPrincipal = securityPrincipal;
}
/**
* @param securtityCredentials The securtityCredentials to set.
*/
public void setSecurityCredentials(String securtityCredentials) {
this.securityCredentials = securtityCredentials;
}
/**
* Adds an environment entry from a nested <environmentEntry>.
* @param name the entry's name
* @param value the entry's value
*/
public void addEnvironmentEntry (String name, String value) {
environment.put (name, value);
}
/**
* Execute the tag.
* @param output not used
* @throws JellyTagException if an error occurs
*/
public void doTag(XMLOutput output) throws JellyTagException {
if (initialContextFactory != null) {
environment.put
(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
}
if (providerUrl != null) {
environment.put (Context.PROVIDER_URL, providerUrl);
}
if (securityPrincipal != null) {
environment.put (Context.SECURITY_PRINCIPAL, securityPrincipal);
}
if (securityCredentials != null) {
environment.put (Context.SECURITY_CREDENTIALS, securityCredentials);
}
invokeBody (output);
try {
LdapContext lctx = null;
lctx = new InitialLdapContext(environment, null);
if (scope != null) {
context.setVariable(var, scope, lctx);
} else {
context.setVariable(var, lctx);
}
} catch (NamingException e) {
if (logger.isDebugEnabled()) {
logger.debug("Cannot access LDAP server: " + e.getMessage(), e);
}
throw new JellyTagException (e);
}
}
}