View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plus.jndi;
20  
21  import javax.naming.Context;
22  import javax.naming.InitialContext;
23  import javax.naming.LinkRef;
24  import javax.naming.Name;
25  import javax.naming.NameParser;
26  import javax.naming.NamingException;
27  
28  import org.eclipse.jetty.jndi.NamingUtil;
29  import org.eclipse.jetty.util.log.Logger;
30  
31  /**
32   * NamingEntry
33   * <p>
34   * Base class for all jndi related entities. Instances of
35   * subclasses of this class are declared in jetty.xml or in a 
36   * webapp's WEB-INF/jetty-env.xml file.
37   * <p>
38   * NOTE: that all NamingEntries will be bound in a single namespace.
39   *  The "global" level is just in the top level context. The "local"
40   *  level is a context specific to a webapp.
41   */
42  public abstract class NamingEntry
43  {
44      private static Logger __log = NamingUtil.__log;
45      public static final String __contextName = "__"; //all NamingEntries stored in context called "__"
46      protected final Object _scope;
47      protected final String _jndiName;  //the name representing the object associated with the NamingEntry
48      protected String _namingEntryNameString; //the name of the NamingEntry relative to the context it is stored in
49      protected String _objectNameString; //the name of the object relative to the context it is stored in
50     
51     
52      public String toString()
53      {
54          return _jndiName;
55      }
56   
57      
58      protected NamingEntry (Object scope, String jndiName)
59      throws NamingException
60      {
61          this._scope=scope;
62          this._jndiName = jndiName;
63      }
64      
65      /** 
66       * Create a NamingEntry. 
67       * A NamingEntry is a name associated with a value which can later
68       * be looked up in JNDI by a webapp.
69       * 
70       * We create the NamingEntry and put it into JNDI where it can
71       * be linked to the webapp's env-entry, resource-ref etc entries.
72       * 
73       * @param jndiName the name of the object which will eventually be in java:comp/env
74       * @throws NamingException if unable to create naming entry
75       */
76      protected NamingEntry (String jndiName)
77      throws NamingException
78      {
79          this (null, jndiName);
80      }
81  
82      
83   
84      
85      /**
86       * Add a <code>java:comp/env</code> binding for the object represented by this NamingEntry,
87       * but bind it as the name supplied
88       * @param localName the local name to bind
89       * @throws NamingException if unable to bind
90       */
91      public void bindToENC(String localName)
92      throws NamingException
93      {
94          // TODO - check on the whole overriding/non-overriding thing
95          InitialContext ic = new InitialContext();
96          Context env = (Context)ic.lookup("java:comp/env");
97          __log.debug("Binding java:comp/env/"+localName+" to "+_objectNameString);
98          NamingUtil.bind(env, localName, new LinkRef(_objectNameString));
99      }
100     
101     /**
102      * Unbind this NamingEntry from a java:comp/env
103      */
104     public void unbindENC ()
105     {
106         try
107         {
108             InitialContext ic = new InitialContext();
109             Context env = (Context)ic.lookup("java:comp/env");
110             __log.debug("Unbinding java:comp/env/"+getJndiName());
111             env.unbind(getJndiName());
112         }
113         catch (NamingException e)
114         {
115             __log.warn(e);
116         }
117     }
118     
119     /**
120      * Unbind this NamingEntry entirely
121      */
122     public void release ()
123     {
124         try
125         {
126             InitialContext ic = new InitialContext();
127             ic.unbind(_objectNameString);
128             ic.unbind(_namingEntryNameString);
129             this._namingEntryNameString=null;
130             this._objectNameString=null;
131         }
132         catch (NamingException e)
133         {
134             __log.warn(e);
135         }
136     }
137     
138     /**
139      * Get the unique name of the object
140      * relative to the scope
141      * @return the unique jndi name of the object
142      */
143     public String getJndiName ()
144     {
145         return _jndiName;
146     }
147 
148     /**
149      * Get the name of the object, fully
150      * qualified with the scope
151      * @return the name of the object, fully qualified with the scope
152      */
153     public String getJndiNameInScope ()
154     {
155         return _objectNameString;
156     }
157  
158  
159     
160     /**
161      * Save the NamingEntry for later use.
162      * <p>
163      * Saving is done by binding the NamingEntry
164      * itself, and the value it represents into
165      * JNDI. In this way, we can link to the
166      * value it represents later, but also
167      * still retrieve the NamingEntry itself too.
168      * <p>
169      * The object is bound at the jndiName passed in.
170      * This NamingEntry is bound at __/jndiName.
171      * <p>
172      * eg
173      * <pre>
174      * jdbc/foo    : DataSource
175      * __/jdbc/foo : NamingEntry
176      * </pre>
177      * 
178      * @param object the object to save 
179      * @throws NamingException if unable to save
180      */
181     protected void save (Object object)
182     throws NamingException
183     {
184         __log.debug("SAVE {} in {}",this,_scope);
185         InitialContext ic = new InitialContext();
186         NameParser parser = ic.getNameParser("");
187         Name prefix = NamingEntryUtil.getNameForScope(_scope);
188       
189         //bind the NamingEntry into the context
190         Name namingEntryName = NamingEntryUtil.makeNamingEntryName(parser, getJndiName());
191         namingEntryName.addAll(0, prefix);
192         _namingEntryNameString = namingEntryName.toString();
193         NamingUtil.bind(ic, _namingEntryNameString, this);
194                 
195         //bind the object as well
196         Name objectName = parser.parse(getJndiName());
197         objectName.addAll(0, prefix);
198         _objectNameString = objectName.toString();
199         NamingUtil.bind(ic, _objectNameString, object);
200     } 
201     
202 }