KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > ant > JndiProperties


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.ant;
18
19 import javax.naming.InitialContext JavaDoc;
20
21 import org.apache.tools.ant.PropertyHelper;
22 import org.apache.tools.ant.Task;
23
24
25 /**
26  * Dynamic properties from a JNDI context. Use ${jndi:NAME} syntax.
27  * You may need to use <jndiEnv> to set up jndi properties and drivers,
28  * and eventually different context-specific tasks.
29  *
30  * @author Costin Manolache
31  */

32 public class JndiProperties extends Task {
33     public static String JavaDoc PREFIX="jndi:";
34     private static org.apache.commons.logging.Log log=
35         org.apache.commons.logging.LogFactory.getLog( JndiProperties.class );
36     private JndiHelper helper=new JndiHelper();
37
38     public JndiProperties() {
39         initNaming();
40     }
41
42     static boolean initialized=false;
43     static void initNaming() {
44         if( initialized ) return;
45         initialized=true;
46         Thread.currentThread().setContextClassLoader( JndiProperties.class.getClassLoader() );
47 // System.setProperty( "java.naming.factory.initial", "org.apache.naming.memory.MemoryInitialContextFactory" );
48
}
49     
50     class JndiHelper extends PropertyHelper {
51         public boolean setPropertyHook( String JavaDoc ns, String JavaDoc name, Object JavaDoc v, boolean inh,
52                                         boolean user, boolean isNew)
53         {
54             if( ! name.startsWith(PREFIX) ) {
55                 // pass to next
56
return super.setPropertyHook(ns, name, v, inh, user, isNew);
57             }
58             name=name.substring( PREFIX.length() );
59
60             // XXX later
61

62             return true;
63         }
64
65         public Object JavaDoc getPropertyHook( String JavaDoc ns, String JavaDoc name , boolean user) {
66             if( ! name.startsWith(PREFIX) ) {
67                 // pass to next
68
return super.getPropertyHook(ns, name, user);
69             }
70
71             Object JavaDoc o=null;
72             name=name.substring( PREFIX.length() );
73             try {
74                 InitialContext JavaDoc ic=new InitialContext JavaDoc();
75                 // XXX lookup attribute in DirContext ?
76
o=ic.lookup( name );
77                 if( log.isDebugEnabled() ) log.debug( "getProperty jndi: " + name + " " + o);
78             } catch( Exception JavaDoc ex ) {
79                 log.error("getProperty " + name , ex);
80                 o=null;
81             }
82             return o;
83         }
84
85     }
86
87
88     public void execute() {
89         PropertyHelper phelper=PropertyHelper.getPropertyHelper( project );
90         helper.setProject( project );
91         helper.setNext( phelper.getNext() );
92         phelper.setNext( helper );
93
94         project.addReference( "jndiProperties", this );
95     }
96     
97 }
98
Popular Tags