javax.naming.spi.InitialContextFactory Java Examples

The following examples show how to use javax.naming.spi.InitialContextFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuarkusDirContextFactory.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@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;
    }
}
 
Example #2
Source File: TestJNDIInitialization.java    From sqlg with MIT License 6 votes vote down vote up
@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;
    });
}
 
Example #3
Source File: DebugJndiContextFactory.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 );
}
 
Example #4
Source File: SimpleNamingContextBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: SimpleNamingContextBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: SimpleNamingContextBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: SimpleNamingContextBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: LocalContext.java    From unitime with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: JNDITestSupport.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
   super.setUp();

   configureEnvironment();

   InitialContextFactory factory = new ActiveMQInitialContextFactory();
   context = factory.getInitialContext(environment);
   assertTrue("No context created", context != null);
}
 
Example #10
Source File: CarbonContextDataHolder.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
public CarbonInitialJNDIContextFactory(InitialContextFactory factory) {
    this.factory = factory;
}
 
Example #11
Source File: JndiContext.java    From jqm with Apache License 2.0 4 votes vote down vote up
@Override
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException
{
    return this;
}
 
Example #12
Source File: DebugJndiContextFactoryBuilder.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public InitialContextFactory createInitialContextFactory( final Hashtable<?, ?> environment ) throws NamingException {
  return new DebugJndiContextFactory();
}
 
Example #13
Source File: MainTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException {
    return new MockContextFactory();
}