Module java.naming
Package javax.naming.spi

Class NamingManager

java.lang.Object
javax.naming.spi.NamingManager
直接已知的子类:
DirectoryManager

public class NamingManager extends Object
该类包含用于在命名或目录服务中创建上下文对象和位置信息引用的对象的方法。

该类不能被实例化。它只有静态方法。

文档中提到的URL指的是RFC 1738及其相关RFC中定义的URL字符串。它是符合其中描述的语法的任何字符串,并且可能并非总是在java.net.URL类或Web浏览器中具有相应的支持。

NamingManager可以安全地被多个线程并发访问。

除非另有说明,传递给任何方法的Name或环境参数由调用者拥有。实现不会修改对象或保留对其的引用,尽管它可能保留对克隆或副本的引用。

自版本:
1.3
  • Field Details

    • CPE

      public static final String CPE
      持有环境属性的名称,getContinuationContext()将其CannotProceedException参数的值存储在其中。此属性由继续上下文继承,并且可能被该上下文的服务提供者用于检查异常的字段。

      此常量的值为"java.naming.spi.CannotProceedException"。

      自版本:
      1.3
      另请参阅:
  • Method Details

    • setObjectFactoryBuilder

      public static void setObjectFactoryBuilder(ObjectFactoryBuilder builder) throws NamingException
      ObjectFactoryBuilder确定尝试加载对象工厂时使用的策略。请参阅getObjectInstance()和ObjectFactory类以了解默认策略的描述。setObjectFactoryBuilder()通过安装ObjectFactoryBuilder覆盖此默认策略。随后加载和创建的对象工厂将使用已安装的构建器。

      如果执行线程被允许(通过安全管理器的checkSetFactory()方法)安装构建器,则可以安装构建器。安装后,无法替换构建器。

      参数:
      builder - 要安装的工厂构建器。如果为null,则不安装任何构建器。
      抛出:
      SecurityException - 由于安全原因无法安装构建器。
      NamingException - 由于非安全相关原因无法安装构建器。
      IllegalStateException - 如果已安装工厂。
      另请参阅:
    • getObjectInstance

      public static Object getObjectInstance(Object refInfo, Name name, Context nameCtx, Hashtable<?,?> environment) throws Exception
      Creates an instance of an object for the specified object and environment.

      If an object factory builder has been installed, it is used to create a factory for creating the object. Otherwise, the following rules are used to create the object:

      1. If refInfo is a Reference or Referenceable containing a factory class name, use the named factory to create the object. Return refInfo if the factory cannot be created. Under JDK 1.1, if the factory class must be loaded from a location specified in the reference, a SecurityManager must have been installed or the factory creation will fail. If an exception is encountered while creating the factory, it is passed up to the caller.
      2. If refInfo is a Reference or Referenceable with no factory class name, and the address or addresses are StringRefAddrs with address type "URL", try the URL context factory corresponding to each URL's scheme id to create the object (see getURLContext()). If that fails, continue to the next step.
      3. Use the object factories specified in the Context.OBJECT_FACTORIES property of the environment, and of the provider resource file associated with nameCtx, in that order. The value of this property is a colon-separated list of factory class names that are tried in order, and the first one that succeeds in creating an object is the one used. If none of the factories can be loaded, return refInfo. If an exception is encountered while creating the object, the exception is passed up to the caller.

      Service providers that implement the DirContext interface should use DirectoryManager.getObjectInstance(), not this method. Service providers that implement only the Context interface should use this method.

      Note that an object factory (an object that implements the ObjectFactory interface) must be public and must have a public constructor that accepts no arguments. In cases where the factory is in a named module then it must be in a package which is exported by that module to the java.naming module.

      The name and nameCtx parameters may optionally be used to specify the name of the object being created. name is the name of the object, relative to context nameCtx. This information could be useful to the object factory or to the object implementation. If there are several possible contexts from which the object could be named -- as will often be the case -- it is up to the caller to select one. A good rule of thumb is to select the "deepest" context available. If nameCtx is null, name is relative to the default initial context. If no name is being specified, the name parameter should be null.

      Parameters:
      refInfo - The possibly null object for which to create an object.
      name - The name of this object relative to nameCtx. Specifying a name is optional; if it is omitted, name should be null.
      nameCtx - The context relative to which the name parameter is specified. If null, name is relative to the default initial context.
      environment - The possibly null environment to be used in the creation of the object factory and the object.
      Returns:
      An object created using refInfo; or refInfo if an object cannot be created using the algorithm described above.
      Throws:
      NamingException - if a naming exception was encountered while attempting to get a URL context, or if one of the factories accessed throws a NamingException.
      Exception - if one of the factories accessed throws an exception, or if an error was encountered while loading and instantiating the factory and object classes. A factory should only throw an exception if it does not want other factories to be used in an attempt to create an object. See ObjectFactory.getObjectInstance().
      See Also:
    • getURLContext

      public static Context getURLContext(String scheme, Hashtable<?,?> environment) throws NamingException
      创建给定URL方案ID的上下文。

      生成的上下文用于解析方案scheme的URL。生成的上下文不与特定URL绑定。它能够处理具有指定方案的任意URL。

      创建生成上下文的工厂的类名遵循命名约定scheme-idURLContextFactory(例如,对于“ftp”方案ID,“ftpURLContextFactory”),在指定的包中如下所示。 Context.URL_PKG_PREFIXES环境属性(可能包含从系统属性或应用程序资源文件中获取的值)包含以冒号分隔的包前缀列表。属性中的每个包前缀按指定的顺序尝试加载工厂类。默认包前缀是“com.sun.jndi.url”(如果没有指定的包有效,则尝试此默认值)。完整的包名称由包前缀构造,与方案ID连接而成。

      例如,如果方案ID是“ldap”,并且Context.URL_PKG_PREFIXES属性包含“com.widget:com.wiz.jndi”,则命名管理器将尝试加载以下类,直到成功实例化一个为止:

      • com.widget.ldap.ldapURLContextFactory
      • com.wiz.jndi.ldap.ldapURLContextFactory
      • com.sun.jndi.url.ldap.ldapURLContextFactory
      如果没有包前缀有效,则返回null。

      如果实例化了工厂,则使用以下参数调用它以生成生成的上下文。

      factory.getObjectInstance(null, environment);

      例如,在LDAP URL上下文工厂上调用如上所示的getObjectInstance()将返回一个可以解析LDAP URL的上下文(例如,“ldap://ldap.wiz.com/o=wiz,c=us”,“ldap://ldap.umich.edu/o=umich,c=us”,...)。

      java.naming模块的包中。

      参数:
      scheme - 上下文支持的URL的非空方案ID。
      environment - 在创建对象工厂和上下文时要使用的可能为null的环境属性。
      返回:
      用于解析具有方案IDscheme的URL的上下文;如果未找到用于创建上下文的工厂,则返回null
      抛出:
      NamingException - 在创建上下文时发生命名异常。
      参见:
    • getInitialContext

      public static Context getInitialContext(Hashtable<?,?> env) throws NamingException
      使用指定的环境属性创建初始上下文。

      • 如果已安装InitialContextFactoryBuilder,则将其用于创建初始上下文的工厂
      • 否则,使用Context.INITIAL_CONTEXT_FACTORY环境属性中指定的类
        • 首先,ServiceLoader机制尝试使用当前线程的上下文类加载器定位InitialContextFactory提供程序
        • 如果失败,此实现将使用内置机制定位合适的InitialContextFactory
          (请注意,初始上下文工厂(实现InitialContextFactory接口的对象)必须是public,并且必须具有不接受参数的公共构造函数。在工厂位于命名模块中的情况下,它必须位于由该模块导出到java.naming模块的包中。)
      参数:
      env - 创建上下文时使用的可能为null的环境属性。
      返回:
      非空初始上下文。
      抛出:
      NoInitialContextException - 如果未找到Context.INITIAL_CONTEXT_FACTORY属性,或者命名不存在的类或无法实例化类,或者由于其他原因无法创建初始上下文。
      NamingException - 如果遇到其他命名异常。
      参见:
    • setInitialContextFactoryBuilder

      public static void setInitialContextFactoryBuilder(InitialContextFactoryBuilder builder) throws NamingException
      将InitialContextFactory构建器设置为builder。

      参数:
      builder - 要安装的初始上下文工厂构建器。如果为null,则不设置构建器。
      抛出:
      SecurityException - 由于安全原因无法安装构建器。
      NamingException - 由于非安全相关原因无法安装构建器。
      IllegalStateException - 如果先前安装了构建器。
      参见:
    • hasInitialContextFactoryBuilder

      public static boolean hasInitialContextFactoryBuilder()
      确定是否已设置初始上下文工厂构建器。
      返回:
      如果已设置初始上下文工厂构建器,则返回true;否则返回false。
      参见:
    • getContinuationContext

      public static Context getContinuationContext(CannotProceedException cpe) throws NamingException
      创建一个上下文,以继续上下文操作。 CannotProceedException来指示其已经进行到哪里。然后通过调用 getContinuationContext从JNDI获取一个继续上下文。然后,上下文实现应通过在继续上下文上调用相同操作来恢复上下文操作,使用尚未解析的名称的其余部分。

      cpe参数之前,此方法通过将与该对象关联的环境的属性值设置为 cpe来更新该对象的环境。此属性将被继承到继续上下文,并且可能被该上下文的服务提供程序用于检查此异常的字段。

      参数:
      cpe - 触发此继续操作的非空异常。
      返回:
      用于继续操作的非空上下文对象。
      抛出:
      NamingException - 如果发生命名异常。
    • getStateToBind

      public static Object getStateToBind(Object obj, Name name, Context nameCtx, Hashtable<?,?> environment) throws NamingException
      检索要绑定对象的状态。 DirContext接口的服务提供程序应使用 DirectoryManager.getStateToBind(),而不是此方法。仅实现 Context接口的服务提供程序应使用此方法。

      Context.STATE_FACTORIES中指定的状态工厂,并按顺序使用与 nameCtx关联的提供程序资源文件。此属性的值是一个以冒号分隔的工厂类名列表,按顺序尝试,成功返回对象状态的第一个将被使用。如果无法以此方式检索对象的状态,则返回对象本身。如果在检索状态时遇到异常,则将异常传递给调用者。

      java.naming模块的包中。

      namenameCtx参数可选择用于指定要创建的对象的名称。有关详细信息,请参阅“名称和上下文参数”中的描述ObjectFactory.getObjectInstance()

      Referenceable对象。获取此对象的服务提供程序可以选择直接存储它,或提取其引用(使用 Referenceable.getReference())并存储该引用。

      参数:
      obj - 要获取状态以绑定的非空对象。
      name - 相对于nameCtx的此对象的名称,如果未指定名称,则为null。
      nameCtx - 指定name参数的上下文,如果name相对于默认初始上下文,则为null。
      environment - 用于创建状态工厂和对象状态的可能为null的环境。
      返回:
      表示obj的状态以进行绑定的非空对象。它可以是对象(obj)本身。
      抛出:
      NamingException - 如果访问的工厂之一抛出异常,或者在加载和实例化工厂和对象类时遇到错误。如果工厂不希望使用其他工厂尝试创建对象,则应抛出异常。请参见StateFactory.getStateToBind()
      自:
      1.3
      参见: