~~ ~~ Licensed to the Apache Software Foundation (ASF) under one or more ~~ contributor license agreements. See the NOTICE file distributed with ~~ this work for additional information regarding copyright ownership. ~~ The ASF licenses this file to You under the Apache License, Version 2.0 ~~ (the "License"); you may not use this file except in compliance with ~~ the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, software ~~ distributed under the License is distributed on an "AS IS" BASIS, ~~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~~ See the License for the specific language governing permissions and ~~ limitations under the License. ~~ ===== Usage ===== Usage * {{{Creating an InitialContext}}} * {{{Configuring the context contents}}} * Creating an InitialContext As is always the case with JNDI, you start by creating an InitialContext. This is done by invoking <<>> with a suitable set of properties. In particular, two properties are required: +---------------------------+---------------------------+ |Property name |Description | +---------------------------+---------------------------+ |java.naming.factory.initial|Contains the class name of the| | |InitialContextFactory. In the case | |of Commons JNDI, this is | |{{{apidocs/org/apache/commons/jndi/CommonsJNDIContextFactory} | |org.apache.commons.jndi.CommonsJndiContextFactory}}. +---------------------------+----------------------------+ |java.naming.provider.url |An URL in the form | | |<<>>>, where url| | |specifies the URL of a file,| | |from which to load the | | |context contents. | +---------------------------+----------------------------+ +-------------------------------------------------------------------- java.util.Properties props = new java.util.Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.commons.jndi.CommonsJndiContextFactory"); java.net.URL url = new File("/etc/myproject/jndi.properties").toURI().toURL(); props.setProperty(Context.PROVIDER_URL, "mem:" + url); final InitialContext ctx; try { ctx = javax.naming.spi.NamingManager.getInitialContext(props); } catch (NamingException e) { // Log the error and throw a RuntimeException. throw new java.lang.reflect.UndeclaredThrowableException(e, e.getMessage()); } +-------------------------------------------------------------------- * Configuring the context contents The initial context contents are read from a resource file. This resource file is configured through the property "java.naming.provider.url", given by the constant <<>>. The URL takes the form mem:\, where <<<\>>> is any valid java.net.URL, or "resource:\", if you want to load the resource from the classpath. A so-called {{{./apidocs/org/apache/commons/jndi/reader/StreamContextReader.java}context reader}} is instantiated to read the resource file, depending on the URL ending. If the URL ends with ".properties.xml", then it is assumed that the resource file is an XML file matching the {{{http://java.sun.com/dtd/properties.dtd}DTD for Java properties. In that case the context reader is an instance of {{{apidocs//commons-jndi/src/main/java/org/apache/commons/jndi/reader/XmlPropertyReader}XmlPropertyReader}}. Otherwise, if the resource URL ends with <<<.xml>>>, then an {{{apidocs//commons-jndi/src/main/java/org/apache/commons/jndi/reader/XmlReader}XmlReader}} is used and the resource file must meet the {{{./resources/CommonsJNDI.xsd}Commons JNDI XSD}}. In all other cases, the standard format of Java properties is assumed, and a {{{apidocs//commons-jndi/src/main/java/org/apache/commons/jndi/reader/PropertyReader}PropertyReader}} is used to read the resource file. * {{{./readers/PropertyReader.html}}Standard Java property files}} * {{{./readers/XmlPropertyReader.html}}Property files in XML format}} * {{{./readers/XmlReader.html}}Files in Commons JNDI XML format}}