本文整理汇总了Java中javax.naming.NoInitialContextException类的典型用法代码示例。如果您正苦于以下问题:Java NoInitialContextException类的具体用法?Java NoInitialContextException怎么用?Java NoInitialContextException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NoInitialContextException类属于javax.naming包,在下文中一共展示了NoInitialContextException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getInitialContext
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Creates an initial context from the JNDIContextManager OSGi service retrieved from the caller bundle context.
*
* @param environment The possibly null environment
* specifying information to be used in the creation
* of the initial context.
* @return A non-null initial context object that implements the Context interface.
* @throws NamingException If cannot create an initial context.
*/
@Override
public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
//1) Find the BundleContext of the caller of this method. If a BundleContext cannot be found
// then throw NoInitialContext Exception.
Optional<BundleContext> bundleContextOptional = getCallersBundleContext(environment);
bundleContextOptional.orElseThrow(NoInitialContextException::new);
//2) Retrieve the JNDIContextManager service from the BundleContext and invoke getInitialContext method.
// If no BundleContext is found then, throw NoInitialContext Exception.
BundleContext callersBC = bundleContextOptional.get();
Optional<ServiceReference<JNDIContextManager>> contextManagerSR = Optional.ofNullable(
callersBC.getServiceReference(JNDIContextManager.class));
return contextManagerSR
.map(callersBC::getService)
.map(rethrowFunction(jndiContextManager -> jndiContextManager.newInitialContext(environment)))
.orElseThrow(NoInitialContextException::new);
}
示例2: simulateBuilderLessNamingManager
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Builds {@link InitialContextFactory} from passed requested (
* {@link String}) class name
*
* @param requestedFactory
* @return {@link InitialContextFactory}
* @throws NoInitialContextException
*/
private InitialContextFactory simulateBuilderLessNamingManager(String requestedFactory)
throws NoInitialContextException {
InitialContextFactory factory;
Class<?> requestedClass;
try {
requestedClass = ClassUtils.initClassForName(requestedFactory);
Object instance = ClassUtils.instantiate(requestedClass);
factory = ObjectUtils.cast(instance, InitialContextFactory.class);
} catch (IOException ex) {
NoInitialContextException nex = new NoInitialContextException(COULD_NOT_FIND_ERROR);
nex.setRootCause(ex);
throw nex;
}
return factory;
}
示例3: testWith
import javax.naming.NoInitialContextException; //导入依赖的package包/类
private static void testWith(String appletProperty) throws NamingException {
Hashtable<Object, Object> env = new Hashtable<>();
// Deliberately put java.lang.Object rather than java.applet.Applet
// if an applet was used we would see a ClassCastException down there
env.put(appletProperty, new Object());
// It's ok to instantiate InitialContext with no parameters
// and be unaware of it right until you try to use it
Context ctx = new InitialContext(env);
boolean threw = true;
try {
ctx.lookup("whatever");
threw = false;
} catch (NoInitialContextException e) {
String m = e.getMessage();
if (m == null || m.contains("applet"))
throw new RuntimeException("The exception message is incorrect", e);
} catch (Throwable t) {
throw new RuntimeException(
"The test was supposed to catch NoInitialContextException" +
" here, but caught: " + t.getClass().getName(), t);
} finally {
ctx.close();
}
if (!threw)
throw new RuntimeException("The test was supposed to catch NoInitialContextException here");
}
示例4: getURLOrDefaultInitDirCtx
import javax.naming.NoInitialContextException; //导入依赖的package包/类
private DirContext getURLOrDefaultInitDirCtx (Name name)
throws NamingException
{
Context c = getURLOrDefaultInitCtx (name);
if (c == null)
throw new NoInitialContextException ();
else if (! (c instanceof DirContext))
throw new NotContextException ();
return (DirContext) c;
}
示例5: getDefaultInitLdapCtx
import javax.naming.NoInitialContextException; //导入依赖的package包/类
private LdapContext getDefaultInitLdapCtx ()
throws NamingException
{
Context c = getDefaultInitCtx ();
if (c == null)
throw new NoInitialContextException ();
else if (! (c instanceof LdapContext))
throw new NotContextException ();
return (LdapContext) c;
}
示例6: getJndiAndSpringDataSources
import javax.naming.NoInitialContextException; //导入依赖的package包/类
static Map<String, DataSource> getJndiAndSpringDataSources() throws NamingException {
Map<String, DataSource> dataSources;
try {
dataSources = new LinkedHashMap<String, DataSource>(getJndiDataSources());
} catch (final NoInitialContextException e) {
dataSources = new LinkedHashMap<String, DataSource>();
}
dataSources.putAll(SPRING_DATASOURCES);
return dataSources;
}
示例7: castToDirContext
import javax.naming.NoInitialContextException; //导入依赖的package包/类
private DirContext castToDirContext(Context ctx)
throws NoInitialContextException, NotContextException {
if (ctx instanceof DirContext) {
return (DirContext) ctx;
} else if (null == ctx) {
// jndi.1A=Cannot create initial context.
throw new NoInitialContextException(Messages.getString("jndi.1A")); //$NON-NLS-1$
} else {
// jndi.1B=DirContext object is required.
throw new NotContextException(Messages.getString("jndi.1B")); //$NON-NLS-1$
}
}
示例8: testGetInitialContext_NoBuilder_NullFactory
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Test the behavior when the class name is null before factory builder is
* set.
*/
public void testGetInitialContext_NoBuilder_NullFactory()
throws NamingException {
log.setMethod("testGetInitialContext_NoBuilder_NullFactory()");
Hashtable<String, String> envWithNoFac = new Hashtable<String, String>();
try {
NamingManager.getInitialContext(envWithNoFac);
fail("Should throw NoInitialContextException.");
} catch (NoInitialContextException e) {
}
}
示例9: testGetInitialContext_NoBuilder_EmptyFactory
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Test the behavior when the class name is empty before factory builder is
* set.
*/
public void testGetInitialContext_NoBuilder_EmptyFactory()
throws NamingException {
log.setMethod("testGetInitialContext_NoBuilder_EmptyFactory()");
Hashtable<String, String> envWithEmptyFac = new Hashtable<String, String>();
envWithEmptyFac.put(Context.INITIAL_CONTEXT_FACTORY, "");
try {
NamingManager.getInitialContext(envWithEmptyFac);
fail("Should throw NoInitialContextException.");
} catch (NoInitialContextException e) {
}
}
示例10: testGetInitialContext_NoBuilder_InvalidFactory
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Test the behavior when the class name is invalid before factory builder
* is set.
*/
public void testGetInitialContext_NoBuilder_InvalidFactory()
throws NamingException {
log.setMethod("testGetInitialContext_NoBuilder_InvalidFactory()");
Hashtable<String, String> envWithInvalidFac = new Hashtable<String, String>();
envWithInvalidFac.put(Context.INITIAL_CONTEXT_FACTORY, "junk.Factory");
try {
NamingManager.getInitialContext(envWithInvalidFac);
fail("Should throw NoInitialContextException.");
} catch (NoInitialContextException e) {
}
}
示例11: testSerializable_Simple
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Test serialize NoInitialContextException: write a
* NoInitialContextException object into a byte array, and read from it. The
* two objects should be equal.
*/
public void testSerializable_Simple() throws ClassNotFoundException,
IOException, InvalidNameException {
NoInitialContextException exception = new NoInitialContextException(
"Test exception Serializable: NoInitialContextException");
exception.setRemainingName(new CompositeName(
"www.apache.org/foundation"));
exception.setResolvedName(new CompositeName(
"http://www.apache.org/index.html"));
exception.setResolvedObj("This is a string object.");
exception.setRootCause(new NullPointerException("null pointer"));
// write to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(exception);
byte[] buffer = baos.toByteArray();
oos.close();
baos.close();
// read from byte array
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
ObjectInputStream ois = new ObjectInputStream(bais);
NoInitialContextException exception2 = (NoInitialContextException) ois
.readObject();
ois.close();
bais.close();
assertEquals(exception.getExplanation(), exception2.getExplanation());
assertEquals(exception.getResolvedObj(), exception2.getResolvedObj());
assertEquals(exception.getRemainingName(), exception2
.getRemainingName());
assertEquals(exception.getResolvedName(), exception2.getResolvedName());
assertEquals(exception.getRootCause().getMessage(), exception2
.getRootCause().getMessage());
assertEquals(exception.getRootCause().getClass(), exception2
.getRootCause().getClass());
}
示例12: testSerializable_compatibility
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Test InvalidNameException serialization compatibility
*/
public void testSerializable_compatibility() throws InvalidNameException,
ClassNotFoundException, IOException {
ObjectInputStream ois = new ObjectInputStream(
getClass()
.getClassLoader()
.getResourceAsStream(
"/serialization/javax/naming/NoInitialContextException.ser"));
NoInitialContextException exception2 = (NoInitialContextException) ois
.readObject();
ois.close();
NoInitialContextException exception = new NoInitialContextException(
"Test exception Serializable: NoInitialContextException");
exception.setRemainingName(new CompositeName("www.apache.org/foundation"));
exception.setResolvedName(new CompositeName(
"http://www.apache.org/index.html"));
exception.setResolvedObj("This is a string object.");
exception.setRootCause(new NullPointerException("null pointer"));
assertEquals(exception.getExplanation(), exception2.getExplanation());
assertEquals(exception.getResolvedObj(), exception2.getResolvedObj());
assertEquals(exception.getRemainingName(), exception2
.getRemainingName());
assertEquals(exception.getResolvedName(), exception2.getResolvedName());
assertEquals(exception.getRootCause().getMessage(), exception2
.getRootCause().getMessage());
assertEquals(exception.getRootCause().getClass(), exception2
.getRootCause().getClass());
}
示例13: castToDirContext
import javax.naming.NoInitialContextException; //导入依赖的package包/类
private DirContext castToDirContext(Context ctx)
throws NoInitialContextException, NotContextException {
if (ctx instanceof DirContext) {
return (DirContext) ctx;
} else if (null == ctx) {
// jndi.1A=Cannot create initial context.
throw new NoInitialContextException(Messages.getString("jndi.1A")); //$NON-NLS-1$
} else {
// jndi.1B=DirContext object is required.
throw new NotContextException(Messages.getString("jndi.1B")); //$NON-NLS-1$
}
}
示例14: instantiateFromName
import javax.naming.NoInitialContextException; //导入依赖的package包/类
/**
* Initializes {@link InitialContextFactory} from passed class name
*
* @param requestedFactory
* @return {@link InitialContextFactory} instance from factory class name
* @throws NoInitialContextException
*/
private InitialContextFactory instantiateFromName(String requestedFactory) throws NoInitialContextException {
InitialContextFactory initialContextFactory;
if (requestedFactory == null) {
initialContextFactory = new LightmareContextFactory();
} else {
initialContextFactory = simulateBuilderLessNamingManager(requestedFactory);
}
return initialContextFactory;
}
示例15: getDefaultBackingContext
import javax.naming.NoInitialContextException; //导入依赖的package包/类
private Context getDefaultBackingContext() throws NamingException {
backingContext.orElseThrow(NoInitialContextException::new);
return backingContext.get();
}