下面列出了怎么用javax.naming.spi.InitialContextFactory的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
@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;
}
}
@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);
}
@Override
protected void setUp() throws Exception {
super.setUp();
configureEnvironment();
InitialContextFactory factory = new ActiveMQInitialContextFactory();
context = factory.getInitialContext(environment);
assertTrue("No context created", context != null);
}
public CarbonInitialJNDIContextFactory(InitialContextFactory factory) {
this.factory = factory;
}
@Override
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException
{
return this;
}
public InitialContextFactory createInitialContextFactory( final Hashtable<?, ?> environment ) throws NamingException {
return new DebugJndiContextFactory();
}
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException {
return new MockContextFactory();
}