View Javadoc

1   /*
2    * Copyright 2004-2005 Germinus XXI
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  package com.germinus.easyconf;
17  
18  import javax.naming.InitialContext;
19  import javax.naming.NamingException;
20  
21  import org.apache.commons.configuration.Configuration;
22  import org.apache.commons.configuration.JNDIConfiguration;
23  
24  /***
25   * Represents the URL to a JNDI tree as specified in a properties file
26   * TODO: Add support for ASP applications
27   * @author  Jorge Ferrer
28   * @version $Revision: 1.2 $
29   *
30   */
31  public class JndiURL {
32      private static final String JNDI_PREFIX = Conventions.JNDI_PREFIX;
33      private static InitialContext ctx = null;
34      private String jndiPrefix;
35      private String companyId;
36      private String componentName;
37  
38      public JndiURL(String jndiPath, String companyId, String componentName) {
39          this.jndiPrefix = jndiPrefix.substring(JNDI_PREFIX.length());
40          this.companyId = companyId;
41          this.componentName = componentName;
42      }
43  
44      private synchronized InitialContext getContext() throws NamingException {
45          if (ctx == null) {
46              ctx = new InitialContext();
47          }
48          return ctx;
49      }
50  
51      public static boolean isJndi(String sourcePath) {
52          return sourcePath.startsWith(JNDI_PREFIX);
53      }
54  
55      public String getPrefix() {
56          return jndiPrefix;
57      }
58  
59      public Configuration getConfiguration() {
60          try {
61              return new JNDIConfiguration(getPrefix());
62          } catch (NamingException e) {
63              throw new ConfigurationException("Error loading JNDI configuration for " 
64                      + getPrefix());
65          }
66      }
67  
68  
69  }