类javax.naming.spi.InitialContextFactory源码实例Demo

下面列出了怎么用javax.naming.spi.InitialContextFactory的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: sqlg   文件: TestJNDIInitialization.java
@BeforeClass
public static void beforeClass() throws Exception {
    URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
    configuration = new PropertiesConfiguration(sqlProperties);
    if (!configuration.containsKey("jdbc.url")) {
        throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
    }

    ds = C3P0DataSource.create(configuration).getDatasource();

    //change the connection url to be a JNDI one
    configuration.setProperty("jdbc.url", "jndi:testConnection");

    //set up the initial context
    NamingManager.setInitialContextFactoryBuilder(environment -> {
        InitialContextFactory mockFactory = mock(InitialContextFactory.class);
        Context mockContext = mock(Context.class);
        when(mockFactory.getInitialContext(any())).thenReturn(mockContext);

        when(mockContext.lookup("testConnection")).thenReturn(ds);

        return mockFactory;
    });
}
 
public Context getInitialContext( final Hashtable environment ) throws NamingException {
  final Hashtable hashtable = new Hashtable();
  if ( environment != null ) {
    hashtable.putAll( environment );
  }

  final String o = (String) hashtable.get( "java.naming.factory.initial" );
  if ( StringUtils.isEmpty( o ) == false && DebugJndiContextFactory.class.getName().equals( o ) == false ) {
    final InitialContextFactory contextFactory =
        ObjectUtilities.loadAndInstantiate( o, DebugJndiContextFactory.class, InitialContextFactory.class );
    return contextFactory.getInitialContext( environment );
  }

  hashtable.put( JndiLoader.SIMPLE_DELIMITER, "/" );
  try {
    final File directory = GoldenSampleGenerator.findMarker();
    final File jndi = new File( directory, "jndi" );
    if ( jndi != null ) {
      hashtable.put( SimpleContext.SIMPLE_ROOT, jndi.getAbsolutePath() );
    }
  } catch ( SecurityException se ) {
    // ignore ..
  }
  return new SimpleContext( hashtable );
}
 
/**
 * Simple InitialContextFactoryBuilder implementation,
 * creating a new SimpleNamingContext instance.
 * @see SimpleNamingContext
 */
@Override
@SuppressWarnings("unchecked")
public InitialContextFactory createInitialContextFactory(@Nullable Hashtable<?,?> environment) {
	if (activated == null && environment != null) {
		Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
		if (icf != null) {
			Class<?> icfClass;
			if (icf instanceof Class) {
				icfClass = (Class<?>) icf;
			}
			else if (icf instanceof String) {
				icfClass = ClassUtils.resolveClassName((String) icf, getClass().getClassLoader());
			}
			else {
				throw new IllegalArgumentException("Invalid value type for environment key [" +
						Context.INITIAL_CONTEXT_FACTORY + "]: " + icf.getClass().getName());
			}
			if (!InitialContextFactory.class.isAssignableFrom(icfClass)) {
				throw new IllegalArgumentException(
						"Specified class does not implement [" + InitialContextFactory.class.getName() + "]: " + icf);
			}
			try {
				return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf, ex);
			}
		}
	}

	// Default case...
	return env -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) env);
}
 
/**
 * Simple InitialContextFactoryBuilder implementation,
 * creating a new SimpleNamingContext instance.
 * @see SimpleNamingContext
 */
@Override
@SuppressWarnings("unchecked")
public InitialContextFactory createInitialContextFactory(@Nullable Hashtable<?,?> environment) {
	if (activated == null && environment != null) {
		Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
		if (icf != null) {
			Class<?> icfClass;
			if (icf instanceof Class) {
				icfClass = (Class<?>) icf;
			}
			else if (icf instanceof String) {
				icfClass = ClassUtils.resolveClassName((String) icf, getClass().getClassLoader());
			}
			else {
				throw new IllegalArgumentException("Invalid value type for environment key [" +
						Context.INITIAL_CONTEXT_FACTORY + "]: " + icf.getClass().getName());
			}
			if (!InitialContextFactory.class.isAssignableFrom(icfClass)) {
				throw new IllegalArgumentException(
						"Specified class does not implement [" + InitialContextFactory.class.getName() + "]: " + icf);
			}
			try {
				return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf, ex);
			}
		}
	}

	// Default case...
	return env -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) env);
}
 
/**
 * Simple InitialContextFactoryBuilder implementation,
 * creating a new SimpleNamingContext instance.
 * @see SimpleNamingContext
 */
@Override
@SuppressWarnings("unchecked")
public InitialContextFactory createInitialContextFactory(@Nullable Hashtable<?,?> environment) {
	if (activated == null && environment != null) {
		Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
		if (icf != null) {
			Class<?> icfClass;
			if (icf instanceof Class) {
				icfClass = (Class<?>) icf;
			}
			else if (icf instanceof String) {
				icfClass = ClassUtils.resolveClassName((String) icf, getClass().getClassLoader());
			}
			else {
				throw new IllegalArgumentException("Invalid value type for environment key [" +
						Context.INITIAL_CONTEXT_FACTORY + "]: " + icf.getClass().getName());
			}
			if (!InitialContextFactory.class.isAssignableFrom(icfClass)) {
				throw new IllegalArgumentException(
						"Specified class does not implement [" + InitialContextFactory.class.getName() + "]: " + icf);
			}
			try {
				return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf, ex);
			}
		}
	}

	// Default case...
	return env -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) env);
}
 
/**
 * Simple InitialContextFactoryBuilder implementation,
 * creating a new SimpleNamingContext instance.
 * @see SimpleNamingContext
 */
@Override
@SuppressWarnings("unchecked")
public InitialContextFactory createInitialContextFactory(@Nullable Hashtable<?,?> environment) {
	if (activated == null && environment != null) {
		Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
		if (icf != null) {
			Class<?> icfClass;
			if (icf instanceof Class) {
				icfClass = (Class<?>) icf;
			}
			else if (icf instanceof String) {
				icfClass = ClassUtils.resolveClassName((String) icf, getClass().getClassLoader());
			}
			else {
				throw new IllegalArgumentException("Invalid value type for environment key [" +
						Context.INITIAL_CONTEXT_FACTORY + "]: " + icf.getClass().getName());
			}
			if (!InitialContextFactory.class.isAssignableFrom(icfClass)) {
				throw new IllegalArgumentException(
						"Specified class does not implement [" + InitialContextFactory.class.getName() + "]: " + icf);
			}
			try {
				return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf, ex);
			}
		}
	}

	// Default case...
	return env -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) env);
}
 
源代码7 项目: quarkus   文件: QuarkusDirContextFactory.java
@Override
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException {
    final String className = (String) environment.get(Context.INITIAL_CONTEXT_FACTORY);
    try {
        final ClassLoader cl = Thread.currentThread().getContextClassLoader();
        return (InitialContextFactory) Class.forName(className, true, cl).newInstance();
    } catch (Exception e) {
        NoInitialContextException ne = new NoInitialContextException(
                "Cannot instantiate class: " + className);
        ne.setRootCause(e);
        throw ne;
    }
}
 
源代码8 项目: unitime   文件: LocalContext.java
@Override
public InitialContextFactory createInitialContextFactory(Hashtable env) throws NamingException {
	String className = (String)(env == null ? null : env.get(Context.INITIAL_CONTEXT_FACTORY));
	if (className != null) {
		try {
			return (InitialContextFactory)Class.forName(className).newInstance();
		} catch (Exception e) {
			throw new NamingException(e.getMessage());
		}
	}
	return new LocalContext(env);
}
 
源代码9 项目: activemq-artemis   文件: JNDITestSupport.java
@Override
protected void setUp() throws Exception {
   super.setUp();

   configureEnvironment();

   InitialContextFactory factory = new ActiveMQInitialContextFactory();
   context = factory.getInitialContext(environment);
   assertTrue("No context created", context != null);
}
 
源代码10 项目: micro-integrator   文件: CarbonContextDataHolder.java
public CarbonInitialJNDIContextFactory(InitialContextFactory factory) {
    this.factory = factory;
}
 
源代码11 项目: jqm   文件: JndiContext.java
@Override
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException
{
    return this;
}
 
public InitialContextFactory createInitialContextFactory( final Hashtable<?, ?> environment ) throws NamingException {
  return new DebugJndiContextFactory();
}
 
源代码13 项目: tomee   文件: MainTest.java
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException {
    return new MockContextFactory();
}
 
 类所在包
 同包方法