/** * 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); }
/** * 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; }
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"); }
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; }
private DirContext getURLOrDefaultInitDirCtx (String name) throws NamingException { Context c = getURLOrDefaultInitCtx (name); if (c == null) throw new NoInitialContextException (); else if (! (c instanceof DirContext)) throw new NotContextException (); return (DirContext) c; }
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; }
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; }
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$ } }
/** * 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) { } }
/** * 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) { } }
/** * 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) { } }
/** * 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()); }
/** * 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()); }
/** * 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; }
private Context getDefaultBackingContext() throws NamingException { backingContext.orElseThrow(NoInitialContextException::new); return backingContext.get(); }
/** * Creates the initial context. If the initial object factory builder has * been set with {@link #setObjectFactoryBuilder(ObjectFactoryBuilder)}, * the work is delegated to this builder. Otherwise, the method searches * for the property Context.INITIAL_CONTEXT_FACTORY first in the passed * table and then in the system properties. The value of this property is * uses as a class name to install the context factory. The corresponding * class must exist, be public and have the public parameterless constructor. * * @param environment the properties, used to create the context. * * @return the created context * * @throws NoInitialContextException if the initial builder is not set, * the property Context.INITIAL_CONTEXT_FACTORY is missing of the * class, named by this property, cannot be instantiated. * @throws NamingException if throws by the context factory */ public static Context getInitialContext (Hashtable<?, ?> environment) throws NamingException { InitialContextFactory icf = null; if (icfb != null) icf = icfb.createInitialContextFactory(environment); else { String java_naming_factory_initial = null; if (environment != null) java_naming_factory_initial = (String) environment.get (Context.INITIAL_CONTEXT_FACTORY); if (java_naming_factory_initial == null) java_naming_factory_initial = System.getProperty (Context.INITIAL_CONTEXT_FACTORY); if (java_naming_factory_initial == null) throw new NoInitialContextException ("Can't find property: " + Context.INITIAL_CONTEXT_FACTORY); try { icf = (InitialContextFactory)Class.forName (java_naming_factory_initial, true, Thread.currentThread().getContextClassLoader()) .newInstance (); } catch (Exception exception) { NoInitialContextException e = new NoInitialContextException ("Can't load InitialContextFactory class: " + java_naming_factory_initial); e.setRootCause(exception); throw e; } } return icf.getInitialContext (environment); }