private Context getBackingURLContext(String name) throws NamingException { String scheme = name.substring(0, name.indexOf(":")).trim(); Context urlContext = backingURLContextMap.get(scheme); if (urlContext != null) { return urlContext; } urlContext = JNDIUtils.getServiceReferences(bundleContext, ObjectFactory.class, "(" + OSGI_JNDI_URL_SCHEME + "=" + scheme + ")") .stream() .map(serviceRef -> getService(bundleContext, serviceRef)) .flatMap(objectFactoryOptional -> objectFactoryOptional.map(Stream::of).orElseGet(Stream::empty)) .map(rethrowFunction(objectFactory -> objectFactory.getObjectInstance(null, null, null, env))) .filter(object -> object instanceof Context) .map(object -> (Context) object) .findFirst() .orElseGet(rethrowSupplier(this::getDefaultBackingContext)); backingURLContextMap.put(scheme, urlContext); return urlContext; }
/** * Tests that we can get a connection from the DataSource bound in JNDI * during test setup * * @throws Exception * if an error occurs */ public void testDataSource() throws Exception { NameParser nameParser = this.ctx.getNameParser(""); Name datasourceName = nameParser.parse("_test"); Object obj = this.ctx.lookup(datasourceName); DataSource boundDs = null; if (obj instanceof DataSource) { boundDs = (DataSource) obj; } else if (obj instanceof Reference) { // // For some reason, this comes back as a Reference instance under CruiseControl !? // Reference objAsRef = (Reference) obj; ObjectFactory factory = (ObjectFactory) Class.forName(objAsRef.getFactoryClassName()).newInstance(); boundDs = (DataSource) factory.getObjectInstance(objAsRef, datasourceName, this.ctx, new Hashtable<Object, Object>()); } assertTrue("Datasource not bound", boundDs != null); Connection con = boundDs.getConnection(); con.close(); assertTrue("Connection can not be obtained from data source", con != null); }
private DataSource lookupDatasourceInJNDI(String jndiName) throws Exception { NameParser nameParser = this.ctx.getNameParser(""); Name datasourceName = nameParser.parse(this.tempDir.getAbsolutePath() + jndiName); Object obj = this.ctx.lookup(datasourceName); DataSource boundDs = null; if (obj instanceof DataSource) { boundDs = (DataSource) obj; } else if (obj instanceof Reference) { // // For some reason, this comes back as a Reference instance under CruiseControl !? // Reference objAsRef = (Reference) obj; ObjectFactory factory = (ObjectFactory) Class.forName(objAsRef.getFactoryClassName()).newInstance(); boundDs = (DataSource) factory.getObjectInstance(objAsRef, datasourceName, this.ctx, new Hashtable<Object, Object>()); } return boundDs; }
private DataSource lookupDatasourceInJNDI(String jndiName) throws Exception { NameParser nameParser = this.ctx.getNameParser(""); Name datasourceName = nameParser.parse(this.tempDir.getAbsolutePath() + jndiName); Object obj = this.ctx.lookup(datasourceName); DataSource boundDs = null; if (obj instanceof DataSource) { boundDs = (DataSource) obj; } else if (obj instanceof Reference) { // // For some reason, this comes back as a Reference // instance under CruiseControl !? // Reference objAsRef = (Reference) obj; ObjectFactory factory = (ObjectFactory) Class.forName( objAsRef.getFactoryClassName()).newInstance(); boundDs = (DataSource) factory.getObjectInstance(objAsRef, datasourceName, this.ctx, new Hashtable<Object, Object>()); } return boundDs; }
/** * Resolve a Remote Object: If this object is a reference return the * reference * @param o the object to resolve * @param n the name of this object * @return a <code>Referenceable</code> if o is a Reference and the * inititial object o if else */ private Object resolveObject(Object o, Name name) { try { if (o instanceof Reference) { // build of the Referenceable object with is Reference Reference objRef = (Reference) o; ObjectFactory objFact = (ObjectFactory) (Thread.currentThread().getContextClassLoader() .loadClass(objRef.getFactoryClassName())).newInstance(); return objFact.getObjectInstance(objRef, name, this, this.getEnvironment()); } else { return o; } } catch (Exception e) { TraceCarol.error("LmiInitialContext.resolveObject()", e); return o; } }
@Override public void execute(ExecutionContext context) throws Exception { // Attempt to retrieve the object from JNDI. If this succeeds, we can // put it in the context and return immediately. String jndiName = context.substitute(getNode().getAttribute("name")); Object theObject = retrieveJndiObject(jndiName); if (theObject != null) { log.debug("JNDI object named '" + jndiName + "' resolved to instance of " + theObject.getClass().getName()); context.put(getNode().getAttribute("id"), theObject); return; } // Create and invoke an instance of the ObjectFactory. ObjectFactory objectFactory = (ObjectFactory) Class.forName( context.substitute(getNode().getAttribute("factoryClassName"))) .newInstance(); theObject = invokeObjectFactory(context, objectFactory); log.debug("ObjectFactory created instance of " + theObject.getClass().getName()); context.put(getNode().getAttribute("id"), theObject); }
public void testJNDI2Pools() throws Exception { Reference refObj = new Reference(SharedPoolDataSource.class.getName()); refObj.add(new StringRefAddr("dataSourceName","java:comp/env/jdbc/bookstoreCPDS")); Context context = new InitialContext(); Hashtable env = new Hashtable(); ObjectFactory factory = new SharedPoolDataSourceFactory(); Name name = new CompositeName("myDB"); Object obj = factory.getObjectInstance(refObj, name, context, env); assertNotNull(obj); Name name2 = new CompositeName("myDB2"); Object obj2 = factory.getObjectInstance(refObj, name2, context, env); assertNotNull(obj2); }
protected Object federate(final String compositName) throws NamingException { final ObjectFactory[] factories = getFederatedFactories(); for (final ObjectFactory factory : factories) { try { final CompositeName name = new CompositeName(compositName); final Object obj = factory.getObjectInstance(null, name, null, null); if (obj instanceof Context) { return ((Context) obj).lookup(compositName); } else if (obj != null) { return obj; } } catch (final Exception doNothing) { // no-op } } throw new NameNotFoundException("Name \"" + compositName + "\" not found."); }
@Test public void testJNDI2Pools() throws Exception { final Reference refObj = new Reference(SharedPoolDataSource.class.getName()); refObj.add(new StringRefAddr("dataSourceName","java:comp/env/jdbc/bookstoreCPDS")); final Context context = new InitialContext(); final Hashtable<?, ?> env = new Hashtable<>(); final ObjectFactory factory = new SharedPoolDataSourceFactory(); final Name name = new CompositeName("myDB"); final Object obj = factory.getObjectInstance(refObj, name, context, env); assertNotNull(obj); final Name name2 = new CompositeName("myDB2"); final Object obj2 = factory.getObjectInstance(refObj, name2, context, env); assertNotNull(obj2); }
@Test(timeout = 60000) public void testBindable() throws Exception { JmsPoolXAConnectionFactory pcf = new JmsPoolXAConnectionFactory(); assertTrue(pcf instanceof ObjectFactory); assertTrue(((ObjectFactory)pcf).getObjectInstance(null, null, null, null) instanceof JmsPoolXAConnectionFactory); assertTrue(pcf.isTmFromJndi()); pcf.stop(); }
@Test(timeout = 60000) public void testBindableEnvOverrides() throws Exception { JmsPoolXAConnectionFactory pcf = new JmsPoolXAConnectionFactory(); assertTrue(pcf instanceof ObjectFactory); Hashtable<String, String> environment = new Hashtable<String, String>(); environment.put("tmFromJndi", String.valueOf(Boolean.FALSE)); assertTrue(((ObjectFactory) pcf).getObjectInstance(null, null, null, environment) instanceof JmsPoolXAConnectionFactory); assertFalse(pcf.isTmFromJndi()); pcf.stop(); }
public Object getObjectInstance(Object ref, Name name, Context nameCtx, Hashtable<?,?> env) throws Exception { if (!isLdapRef(ref)) { return null; } ObjectFactory factory = new ldapURLContextFactory(); String[] urls = getURLs((Reference)ref); return factory.getObjectInstance(urls, name, nameCtx, env); }
private void testDataSourceFactory() throws Exception { ObjectFactory factory = new JdbcDataSourceFactory(); assertTrue(null == factory.getObjectInstance("test", null, null, null)); Reference ref = new Reference("java.lang.String"); assertTrue(null == factory.getObjectInstance(ref, null, null, null)); ref = new Reference(JdbcDataSource.class.getName()); ref.add(new StringRefAddr("url", "jdbc:h2:mem:")); ref.add(new StringRefAddr("user", "u")); ref.add(new StringRefAddr("password", "p")); ref.add(new StringRefAddr("loginTimeout", "1")); ref.add(new StringRefAddr("description", "test")); JdbcDataSource ds = (JdbcDataSource) factory.getObjectInstance( ref, null, null, null); assertEquals(1, ds.getLoginTimeout()); assertEquals("test", ds.getDescription()); assertEquals("jdbc:h2:mem:", ds.getURL()); assertEquals("u", ds.getUser()); assertEquals("p", ds.getPassword()); Reference ref2 = ds.getReference(); assertEquals(ref.size(), ref2.size()); assertEquals(ref.get("url").getContent().toString(), ref2.get("url").getContent().toString()); assertEquals(ref.get("user").getContent().toString(), ref2.get("user").getContent().toString()); assertEquals(ref.get("password").getContent().toString(), ref2.get("password").getContent().toString()); assertEquals(ref.get("loginTimeout").getContent().toString(), ref2.get("loginTimeout").getContent().toString()); assertEquals(ref.get("description").getContent().toString(), ref2.get("description").getContent().toString()); ds.setPasswordChars("abc".toCharArray()); assertEquals("abc", ds.getPassword()); }
/** * Make sure it is possible to create a new data source using * <code>Referencable</code>, that the new instance has the correct * default values set for the bean properties and finally that the * data source can be serialized/deserialized. * * @param dsDesc data source descriptor * @param className data source class name * @throws Exception on a wide variety of error conditions... */ private void assertDataSourceReferenceEmpty(DataSourceDescriptor dsDesc, String className) throws Exception { println("Testing recreated empty data source."); // Create an empty data source. Object ds = Class.forName(className).newInstance(); Referenceable refDs = (Referenceable)ds; Reference dsAsReference = refDs.getReference(); String factoryClassName = dsAsReference.getFactoryClassName(); ObjectFactory factory = (ObjectFactory)Class.forName(factoryClassName).newInstance(); Object recreatedDs = factory.getObjectInstance(dsAsReference, null, null, null); // Empty, recreated data source should not be the same as the one we // created earlier on. assertNotNull("Recreated datasource is <null>", recreatedDs); assertNotSame(recreatedDs, ds); compareDataSources(dsDesc, ds, recreatedDs, true); // Serialize and recreate data source with default values. ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(ds); oos.flush(); oos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); recreatedDs = ois.readObject(); compareDataSources(dsDesc, ds, recreatedDs, true); }
@Override public void start(BundleContext bundleContext) throws Exception { try { NamingManager.setInitialContextFactoryBuilder(new DefaultContextFactoryBuilder()); NamingManager.setObjectFactoryBuilder(new DefaultObjectFactoryBuilder()); Dictionary<String, String> propertyMap = new Hashtable<>(); propertyMap.put(JNDIConstants.JNDI_URLSCHEME, "java"); bundleContext.registerService(ObjectFactory.class, new JavaURLContextFactory(), propertyMap); //register osgi url scheme Dictionary<String, String> osgiPropertyMap = new Hashtable<>(); osgiPropertyMap.put(JNDIConstants.JNDI_URLSCHEME, "osgi"); bundleContext.registerService(ObjectFactory.class.getName(), new OSGiURLContextServiceFactory(), osgiPropertyMap); // InitialContextFactory Provider should be registered with its implementation class as well as the // InitialContextFactory class. bundleContext.registerService(InitialContextFactory.class, new InMemoryInitialContextFactory(), null); logger.debug("Registering JNDIContextManager OSGi service."); bundleContext.registerService(JNDIContextManager.class, new JNDIContextManagerServiceFactory(), null); } catch (Throwable e) { logger.error(e.getMessage(), e); } }
/** * Tests that we can get a connection from the DataSource bound in JNDI * during test setup * * @throws Exception * if an error occurs */ public void testDataSource() throws Exception { NameParser nameParser = this.ctx.getNameParser(""); Name datasourceName = nameParser.parse("_test"); Object obj = this.ctx.lookup(datasourceName); DataSource boundDs = null; if (obj instanceof DataSource) { boundDs = (DataSource) obj; } else if (obj instanceof Reference) { // // For some reason, this comes back as a Reference // instance under CruiseControl !? // Reference objAsRef = (Reference) obj; ObjectFactory factory = (ObjectFactory) Class.forName( objAsRef.getFactoryClassName()).newInstance(); boundDs = (DataSource) factory.getObjectInstance(objAsRef, datasourceName, this.ctx, new Hashtable<Object, Object>()); } assertTrue("Datasource not bound", boundDs != null); Connection con = boundDs.getConnection(); con.close(); assertTrue("Connection can not be obtained from data source", con != null); }