Package javax.naming.spi

Examples of javax.naming.spi.ObjectFactory


  public void testNoURLContextCaching() throws Exception {
      final AtomicBoolean second = new AtomicBoolean(false);
      final Context ctx = dummyContext("one");
      final Context ctx2 = dummyContext("two");
     
      ObjectFactory of = new ObjectFactory() {
          public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
              if (second.get()) return ctx2;
              else {
                  second.set(true);
                  return ctx;
View Full Code Here


      assertEquals("two", ic.lookup("test:something"));
  }
 
  @Test
  public void testURLContextErrorPropagation() throws Exception {
      ObjectFactory of = new ObjectFactory() {       
        public Object getObjectInstance(Object obj, Name name, Context nameCtx,
                Hashtable<?, ?> environment) throws Exception {
            throw new Exception("doh");
        }
      };
View Full Code Here

  @Test
  public void testURLReferenceWithMatchingHandler() throws Exception
  {
    String testObject = "Test object";
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);

    Properties props = new Properties();
    props.setProperty("osgi.jndi.urlScheme", "wibble");
View Full Code Here

  @Test
  public void testReferenceWithNoClassName() throws Exception
  {
    String testObject = "Test object";
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);

    bc.registerService(ObjectFactory.class.getName(), factory, null);

    Reference ref = new Reference(null);
View Full Code Here

  @Test
  public void testSpecifiedFactoryWithMatchingFactory() throws Exception
  {
    String testObject = "Test object";
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);

    Reference ref = new Reference("dummy.class.name", factory.getClass().getName(), "");

    bc.registerService(new String[] {ObjectFactory.class.getName(), factory.getClass().getName()},
                       factory, null);

    Object obj = NamingManager.getObjectInstance(ref, null, null, env);
   
    assertEquals("The naming manager should have returned the test object", testObject, obj);
View Full Code Here

  @Test
  public void testSpecifiedFactoryWithRegisteredButNotMatchingFactory() throws Exception
  {
    String testObject = "Test object";
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);

    Reference ref = new Reference("dummy.class.name", "dummy.factory.class.name", "");

    bc.registerService(new String[] {ObjectFactory.class.getName(), factory.getClass().getName()},
                       factory, null);

    Object obj = NamingManager.getObjectInstance(ref, null, null, env);

    assertSame("The naming manager should have returned the reference object", ref, obj);
View Full Code Here

    }

    protected Repository getRepositoryByRMI() {
        try {
            Class<? extends ObjectFactory> factoryClass = Class.forName(factory).asSubclass(ObjectFactory.class);
            ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
            repo = (Repository) factory.getObjectInstance(new Reference(Repository.class.getName(), new StringRefAddr("url", url)), null, null, null);
            logger.info("Repository " + getKey() + " acquired via RMI");
            return repo;
        } catch (Exception e) {
            logger.error("Cannot get by RMI", e);
        }
View Full Code Here

       
        Reference dsAsReference = refDS.getReference();
       
        String factoryClassName = dsAsReference.getFactoryClassName();
       
        ObjectFactory factory =
            (ObjectFactory) Class.forName(factoryClassName).newInstance()
       
        Object recreatedDS =
            factory.getObjectInstance(dsAsReference, null, null, null);
       
        // DERBY-2559 - with jdk16, this recreatedDS will be null.
        // bailing out
        if (JDBC.vmSupportsJDBC4())
            return;
       
        println(" empty DataSource recreated using Reference as " +
            recreatedDS.getClass().getName());
        // empty DataSource recreated using Reference should not be
        // the same as the original
        assertNotSame(recreatedDS, ds);
       
        compareDS(expectedArrayIndex, properties, ds, recreatedDS);
       
        // now serialize and recreate
        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();
        println(" empty DataSource recreated using serialization");
        compareDS(expectedArrayIndex, properties, ds, recreatedDS);
       
        // now populate the data source
        for (int i = 0; i < properties.length; i++)
        {
            String property = properties[i];
            Method getMethod = getGet(property, ds);
           
            Method setMethod = getSet(getMethod, ds);
           
            Class pt = getMethod.getReturnType();
           
            // generate a somewhat unique value for a property
            int val = 0;
            for (int j = 0; j < property.length(); j++)
                val += property.charAt(j);
           
            if (pt.equals(Integer.TYPE))
            {
                setMethod.invoke(ds, new Object[] {new Integer(val)});
                continue;
            }
            if (pt.equals(String.class))
            {
                String value;
                if (property.equals("createDatabase"))
                    value = "create";
                else if (property.equals("shutdownDatabase"))
                    value = "shutdown";
                else if (property.equals("ssl"))
                    value = "basic";
                else
                    value = "XX_" + property + "_" + val;
                   
                setMethod.invoke(ds, new Object[] {value});
                continue;
            }
            if (pt.equals(Boolean.TYPE))
            {
                // set the opposite value
                Object gbv = getMethod.invoke(ds, null);
                Boolean sbv =
                    Boolean.FALSE.equals(gbv) ? Boolean.TRUE : Boolean.FALSE;
                setMethod.invoke(ds, new Object[] {sbv});
                continue;
            }          
            if (pt.equals(Short.TYPE))
            {
                setMethod.invoke(ds, new Object[] {new Short((short)val)});
                continue;
            }
            if (pt.equals(Long.TYPE))
            {
                setMethod.invoke(ds, new Object[] {new Long(val)});
                continue;
            }
            fail ( property + " not settable - update test!!");
        }
       
        dsAsReference = refDS.getReference();
        recreatedDS =
            factory.getObjectInstance(dsAsReference, null, null, null);
        println(" populated DataSource recreated using Reference as "
            + recreatedDS.getClass().getName());
        // again, recreated should not be same instance
        assertNotSame(recreatedDS, ds);
       
View Full Code Here

                                    Hashtable environment)
        throws Exception {
       
        if (obj instanceof ResourceRef) {
            Reference ref = (Reference) obj;
            ObjectFactory factory = null;
            RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
            if (factoryRefAddr != null) {
                // Using the specified factory
                String factoryClassName =
                    factoryRefAddr.getContent().toString();
                // Loading factory
                ClassLoader tcl =
                    Thread.currentThread().getContextClassLoader();
                Class factoryClass = null;
                if (tcl != null) {
                    try {
                        factoryClass = tcl.loadClass(factoryClassName);
                    } catch(ClassNotFoundException e) {
                        throw new NamingException(
                            "Could not create resource factory, ClassNotFoundException:" +
                            e.getMessage());
                    }
                } else {
                    try {
                        factoryClass = Class.forName(factoryClassName);
                    } catch(ClassNotFoundException e) {
                        throw new NamingException(
                            "Could not create resource factory, ClassNotFoundException:" +
                            e.getMessage());
                    }
                }
                if (factoryClass != null) {
                    try {
                        factory = (ObjectFactory) factoryClass.newInstance();
                    } catch(Throwable t) {
                        if( t instanceof NamingException)
                            throw (NamingException)t;
                        throw new NamingException(
                            "Could not create resource factory instance, " +
                            t.getMessage());
                    }
                }
            } else {
                if (ref.getClassName().equals("javax.sql.DataSource")) {
                    String javaxSqlDataSourceFactoryClassName =
                        System.getProperty("javax.sql.DataSource.Factory",
                                           Constants.DBCP_DATASOURCE_FACTORY);
                    try {
                        factory = (ObjectFactory)
                            Class.forName(javaxSqlDataSourceFactoryClassName)
                            .newInstance();
                    } catch(Throwable t) {

                    }
                } else if (ref.getClassName().equals("javax.mail.Session")) {
                    String javaxMailSessionFactoryClassName =
                        System.getProperty("javax.mail.Session.Factory",
                                           "org.apache.naming.factory.MailSessionFactory");
                    try {
                        factory = (ObjectFactory)
                            Class.forName(javaxMailSessionFactoryClassName)
                            .newInstance();
                    } catch(Throwable t) {
                    }
                } else if (ref.getClassName().equals("tyrex.resource.Resource")) {
                    String tyrexResourceFactoryClassName =
                        System.getProperty("tyrex.resource.Resource.Factory",
                                           Constants.TYREX_RESOURCE_FACTORY);
                    try {
                        factory = (ObjectFactory)
                            Class.forName(tyrexResourceFactoryClassName)
                            .newInstance();
                    } catch(Throwable t) {
                    }
                }
            }
            if (factory != null) {
                return factory.getObjectInstance
                    (obj, name, nameCtx, environment);
            } else {
                throw new NamingException
                    ("Cannot create resource instance");
            }
View Full Code Here

                                    Hashtable environment)
        throws Exception {
       
        if (obj instanceof TransactionRef) {
            Reference ref = (Reference) obj;
            ObjectFactory factory = null;
            RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
            if (factoryRefAddr != null) {
                // Using the specified factory
                String factoryClassName =
                    factoryRefAddr.getContent().toString();
                // Loading factory
                ClassLoader tcl =
                    Thread.currentThread().getContextClassLoader();
                Class factoryClass = null;
                if (tcl != null) {
                    try {
                        factoryClass = tcl.loadClass(factoryClassName);
                    } catch(ClassNotFoundException e) {
                    }
                } else {
                    try {
                        factoryClass = Class.forName(factoryClassName);
                    } catch(ClassNotFoundException e) {
                    }
                }
                if (factoryClass != null) {
                    try {
                        factory = (ObjectFactory) factoryClass.newInstance();
                    } catch(Throwable t) {
                    }
                }
            } else {
                // Defaults to Tyrex
                String javaxTransactionUserTransactionFactoryClassName =
                    System.getProperty
                    ("javax.transaction.UserTransaction.Factory",
                     Constants.TYREX_TRANSACTION_FACTORY);
                try {
                    factory = (ObjectFactory) Class.forName
                        (javaxTransactionUserTransactionFactoryClassName)
                        .newInstance();
                } catch(Throwable t) {
                }
            }
            if (factory != null) {
                return factory.getObjectInstance
                    (obj, name, nameCtx, environment);
            } else {
                throw new NamingException
                    ("Cannot create resource instance");
            }
View Full Code Here

TOP

Related Classes of javax.naming.spi.ObjectFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.