8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.naming.spi;
27
28 import java.util.Enumeration;
29 import java.util.Hashtable;
30 import java.util.StringTokenizer;
31 import java.net.MalformedURLException;
32
33 import javax.naming.*;
34 import com.sun.naming.internal.VersionHelper;
35 import com.sun.naming.internal.ResourceManager;
36 import com.sun.naming.internal.FactoryEnumeration;
37
38 /**
39 * This class contains methods for creating context objects
40 * and objects referred to by location information in the naming
41 * or directory service.
42 *<p>
43 * This class cannot be instantiated. It has only static methods.
44 *<p>
45 * The mention of URL in the documentation for this class refers to
46 * a URL string as defined by RFC 1738 and its related RFCs. It is
47 * any string that conforms to the syntax described therein, and
48 * may not always have corresponding support in the java.net.URL
49 * class or Web browsers.
50 *<p>
608 }
609
610 }
611
612
613 // ------------ Initial Context Factory Stuff
614 private static InitialContextFactoryBuilder initctx_factory_builder = null;
615
616 /**
617 * Use this method for accessing initctx_factory_builder while
618 * inside an unsynchronized method.
619 */
620 private static synchronized InitialContextFactoryBuilder
621 getInitialContextFactoryBuilder() {
622 return initctx_factory_builder;
623 }
624
625 /**
626 * Creates an initial context using the specified environment
627 * properties.
628 *<p>
629 * If an InitialContextFactoryBuilder has been installed,
630 * it is used to create the factory for creating the initial context.
631 * Otherwise, the class specified in the
632 * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> environment property is used.
633 * Note that an initial context factory (an object that implements the
634 * InitialContextFactory interface) must be public and must have a
635 * public constructor that accepts no arguments.
636 *
637 * @param env The possibly null environment properties used when
638 * creating the context.
639 * @return A non-null initial context.
640 * @exception NoInitialContextException If the
641 * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> property
642 * is not found or names a nonexistent
643 * class or a class that cannot be instantiated,
644 * or if the initial context could not be created for some other
645 * reason.
646 * @exception NamingException If some other naming exception was encountered.
647 * @see javax.naming.InitialContext
648 * @see javax.naming.directory.InitialDirContext
649 */
650 public static Context getInitialContext(Hashtable<?,?> env)
651 throws NamingException {
652 InitialContextFactory factory;
653
654 InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder();
655 if (builder == null) {
656 // No factory installed, use property
657 // Get initial context factory class name
658
659 String className = env != null ?
660 (String)env.get(Context.INITIAL_CONTEXT_FACTORY) : null;
661 if (className == null) {
662 NoInitialContextException ne = new NoInitialContextException(
663 "Need to specify class name in environment or system " +
664 "property, or in an application resource file: " +
665 Context.INITIAL_CONTEXT_FACTORY);
666 throw ne;
667 }
668
669 try {
670 factory = (InitialContextFactory)
671 helper.loadClass(className).newInstance();
672 } catch(Exception e) {
673 NoInitialContextException ne =
674 new NoInitialContextException(
675 "Cannot instantiate class: " + className);
676 ne.setRootCause(e);
677 throw ne;
678 }
679 } else {
680 factory = builder.createInitialContextFactory(env);
681 }
682
683 return factory.getInitialContext(env);
684 }
685
686
687 /**
688 * Sets the InitialContextFactory builder to be builder.
689 *
690 *<p>
691 * The builder can only be installed if the executing thread is allowed by
692 * the security manager to do so. Once installed, the builder cannot
693 * be replaced.
694 * @param builder The initial context factory builder to install. If null,
695 * no builder is set.
696 * @exception SecurityException builder cannot be installed for security
697 * reasons.
698 * @exception NamingException builder cannot be installed for
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.naming.spi;
27
28 import java.util.*;
29 import java.net.MalformedURLException;
30
31 import javax.naming.*;
32 import com.sun.naming.internal.VersionHelper;
33 import com.sun.naming.internal.ResourceManager;
34 import com.sun.naming.internal.FactoryEnumeration;
35
36 /**
37 * This class contains methods for creating context objects
38 * and objects referred to by location information in the naming
39 * or directory service.
40 *<p>
41 * This class cannot be instantiated. It has only static methods.
42 *<p>
43 * The mention of URL in the documentation for this class refers to
44 * a URL string as defined by RFC 1738 and its related RFCs. It is
45 * any string that conforms to the syntax described therein, and
46 * may not always have corresponding support in the java.net.URL
47 * class or Web browsers.
48 *<p>
606 }
607
608 }
609
610
611 // ------------ Initial Context Factory Stuff
612 private static InitialContextFactoryBuilder initctx_factory_builder = null;
613
614 /**
615 * Use this method for accessing initctx_factory_builder while
616 * inside an unsynchronized method.
617 */
618 private static synchronized InitialContextFactoryBuilder
619 getInitialContextFactoryBuilder() {
620 return initctx_factory_builder;
621 }
622
623 /**
624 * Creates an initial context using the specified environment
625 * properties.
626 * <p>
627 * This is done as follows:
628 * <ul>
629 * <li>If an InitialContextFactoryBuilder has been installed,
630 * it is used to create the factory for creating the initial
631 * context</li>
632 * <li>Otherwise, the class specified in the
633 * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> environment property
634 * is used
635 * <ul>
636 * <li>First, the {@linkplain java.util.ServiceLoader ServiceLoader}
637 * mechanism tries to locate an {@code InitialContextFactory}
638 * provider with system classloader</li>
639 * <li>Failing that, this implementation tries to locate a suitable
640 * {@code InitialContextFactory} using a built-in mechanism
641 * <br>
642 * (Note that an initial context factory (an object that implements
643 * the InitialContextFactory interface) must be public and must have
644 * a public constructor that accepts no arguments)</li>
645 * </ul>
646 * </li>
647 * </ul>
648 * @param env The possibly null environment properties used when
649 * creating the context.
650 * @return A non-null initial context.
651 * @exception NoInitialContextException If the
652 * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> property
653 * is not found or names a nonexistent
654 * class or a class that cannot be instantiated,
655 * or if the initial context could not be created for some other
656 * reason.
657 * @exception NamingException If some other naming exception was encountered.
658 * @see javax.naming.InitialContext
659 * @see javax.naming.directory.InitialDirContext
660 */
661 public static Context getInitialContext(Hashtable<?,?> env)
662 throws NamingException {
663 InitialContextFactory factory = null;
664
665 InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder();
666 if (builder == null) {
667 // No builder installed, use property
668 // Get initial context factory class name
669
670 String className = env != null ?
671 (String)env.get(Context.INITIAL_CONTEXT_FACTORY) : null;
672 if (className == null) {
673 NoInitialContextException ne = new NoInitialContextException(
674 "Need to specify class name in environment or system " +
675 "property, or in an application resource file: " +
676 Context.INITIAL_CONTEXT_FACTORY);
677 throw ne;
678 }
679
680 ServiceLoader<InitialContextFactory> loader =
681 ServiceLoader.load(InitialContextFactory.class,
682 ClassLoader.getSystemClassLoader());
683
684 Iterator<InitialContextFactory> iterator = loader.iterator();
685 try {
686 while (iterator.hasNext()) {
687 InitialContextFactory f = iterator.next();
688 if (f.getClass().getName().equals(className)) {
689 factory = f;
690 break;
691 }
692 }
693 } catch (ServiceConfigurationError e) {
694 NoInitialContextException ne =
695 new NoInitialContextException(
696 "Cannot load initial context factory "
697 + "'" + className + "'");
698 ne.setRootCause(e);
699 throw ne;
700 }
701
702 if (factory == null) {
703 try {
704 factory = (InitialContextFactory)
705 helper.loadClass(className).newInstance();
706 } catch (Exception e) {
707 NoInitialContextException ne =
708 new NoInitialContextException(
709 "Cannot instantiate class: " + className);
710 ne.setRootCause(e);
711 throw ne;
712 }
713 }
714 } else {
715 factory = builder.createInitialContextFactory(env);
716 }
717
718 return factory.getInitialContext(env);
719 }
720
721
722 /**
723 * Sets the InitialContextFactory builder to be builder.
724 *
725 *<p>
726 * The builder can only be installed if the executing thread is allowed by
727 * the security manager to do so. Once installed, the builder cannot
728 * be replaced.
729 * @param builder The initial context factory builder to install. If null,
730 * no builder is set.
731 * @exception SecurityException builder cannot be installed for security
732 * reasons.
733 * @exception NamingException builder cannot be installed for
|