001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.ant;
018:
019: import javax.naming.InitialContext;
020:
021: import org.apache.tools.ant.PropertyHelper;
022: import org.apache.tools.ant.Task;
023:
024: /**
025: * Dynamic properties from a JNDI context. Use ${jndi:NAME} syntax.
026: * You may need to use <jndiEnv> to set up jndi properties and drivers,
027: * and eventually different context-specific tasks.
028: *
029: * @author Costin Manolache
030: */
031: public class JndiProperties extends Task {
032: public static String PREFIX = "jndi:";
033: private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
034: .getLog(JndiProperties.class);
035: private JndiHelper helper = new JndiHelper();
036:
037: public JndiProperties() {
038: initNaming();
039: }
040:
041: static boolean initialized = false;
042:
043: static void initNaming() {
044: if (initialized)
045: return;
046: initialized = true;
047: Thread.currentThread().setContextClassLoader(
048: JndiProperties.class.getClassLoader());
049: // System.setProperty( "java.naming.factory.initial", "org.apache.naming.memory.MemoryInitialContextFactory" );
050: }
051:
052: class JndiHelper extends PropertyHelper {
053: public boolean setPropertyHook(String ns, String name,
054: Object v, boolean inh, boolean user, boolean isNew) {
055: if (!name.startsWith(PREFIX)) {
056: // pass to next
057: return super .setPropertyHook(ns, name, v, inh, user,
058: isNew);
059: }
060: name = name.substring(PREFIX.length());
061:
062: // XXX later
063:
064: return true;
065: }
066:
067: public Object getPropertyHook(String ns, String name,
068: boolean user) {
069: if (!name.startsWith(PREFIX)) {
070: // pass to next
071: return super .getPropertyHook(ns, name, user);
072: }
073:
074: Object o = null;
075: name = name.substring(PREFIX.length());
076: try {
077: InitialContext ic = new InitialContext();
078: // XXX lookup attribute in DirContext ?
079: o = ic.lookup(name);
080: if (log.isDebugEnabled())
081: log.debug("getProperty jndi: " + name + " " + o);
082: } catch (Exception ex) {
083: log.error("getProperty " + name, ex);
084: o = null;
085: }
086: return o;
087: }
088:
089: }
090:
091: public void execute() {
092: PropertyHelper phelper = PropertyHelper
093: .getPropertyHelper(project);
094: helper.setProject(project);
095: helper.setNext(phelper.getNext());
096: phelper.setNext(helper);
097:
098: project.addReference("jndiProperties", this);
099: }
100:
101: }
|