private static Class<?>[] getObjectInterfaces(Class<?> objectClass, List<Class<?>> interfaces) { // Rq: object.getClass().getInterfaces() ne suffit pas pour Connection dans Tomcat // car la connection est une instance de PoolGuardConnectionWrapper // et connection.getClass().getInterfaces() est vide dans ce cas final List<Class<?>> myInterfaces; if (interfaces == null) { myInterfaces = new ArrayList<Class<?>>(Arrays.asList(objectClass.getInterfaces())); Class<?> classe = objectClass.getSuperclass(); while (classe != null) { final Class<?>[] classInterfaces = classe.getInterfaces(); if (classInterfaces.length > 0) { final List<Class<?>> superInterfaces = Arrays.asList(classInterfaces); // removeAll d'abord car il ne faut pas de doublon dans la liste myInterfaces.removeAll(superInterfaces); myInterfaces.addAll(superInterfaces); } classe = classe.getSuperclass(); } // on ignore l'interface javax.naming.Referenceable car sinon le rebind sous jetty appelle // referenceable.getReference() et devient inutile myInterfaces.remove(Referenceable.class); } else { myInterfaces = interfaces; } return myInterfaces.toArray(new Class<?>[myInterfaces.size()]); }
/** * Prepares object for binding. It calls * {@link NamingManager#getStateToBind(Object, Name, Context, Hashtable)} * and makes the resulting object {@link Remote} by wrapping it into * {@link RemoteReferenceWrapper}. * * @param name * Object name. * * @param obj * Object to prepare for binding. * * @return Object ready for binding. * * @throws NamingException * If some naming error occurs. * * @throws RemoteException * If remote exception occurs. */ protected Remote getStateToBind(String name, Object obj) throws NamingException, RemoteException { obj = NamingManager.getStateToBind(obj, new CompositeName().add(name), this, environment); if (obj instanceof Remote) { return (Remote) obj; } if (obj instanceof Reference) { return new RemoteReferenceWrapper((Reference) obj); } if (obj instanceof Referenceable) { return new RemoteReferenceWrapper(((Referenceable) obj) .getReference()); } // jndi.82=Cannot bind to RMI Registry object that is neither Remote nor // Reference nor Referenceable throw new IllegalArgumentException(Messages.getString("jndi.82")); //$NON-NLS-1$ }
/** * @param r * @param object * @param object2 * @param env * @param a * @return */ private void assertGetObjectResult(Object o, Name n, Context c, Hashtable<String, String> h, Attributes a) throws NamingException { Object obj = null; try { obj = DirectoryManager.getObjectInstance(o, n, c, h, a); } catch (Exception e) { fail(); } Hashtable<?, ?> t = (Hashtable<?, ?>) obj; if (o instanceof Referenceable) { assertSame(t.get("o"), ((Referenceable) o).getReference()); } else { assertSame(t.get("o"), o); } assertSame(t.get("n"), n); assertSame(t.get("c"), c); assertSame(t.get("h"), h); assertSame(t.get("a"), a); }
/** * Prepares object for binding. It calls * {@link NamingManager#getStateToBind(Object, Name, Context, Hashtable)} * and makes the resulting object {@link Remote} by wrapping it into * {@link RemoteReferenceWrapper}. * * @param name * Object name. * * @param obj * Object to prepare for binding. * * @return Object ready for binding. * * @throws NamingException * If some naming error occurs. * * @throws RemoteException * If remote exception occurs. */ protected Remote getStateToBind(String name, Object obj) throws NamingException, RemoteException { obj = NamingManager.getStateToBind( obj, new CompositeName().add(name), this, environment); if (obj instanceof Remote) { return (Remote) obj; } if (obj instanceof Reference) { return new RemoteReferenceWrapper((Reference) obj); } if (obj instanceof Referenceable) { return new RemoteReferenceWrapper( ((Referenceable) obj).getReference()); } // jndi.82=Cannot bind to RMI Registry object that is neither Remote nor Reference nor Referenceable throw new IllegalArgumentException(Messages.getString("jndi.82")); //$NON-NLS-1$ }
@Override public Object getObjectInstance(Object obj, Name jndiNameObject, Context nameCtx, Hashtable<?,?> environment) throws Exception { Reference ref = (Reference) obj; if (log.isDebugEnabled()) { log.debug("referencing resource with reference of type " + ref.getClass()); } RefAddr refAddr = ref.get("uniqueName"); if (refAddr == null) throw new NamingException("no 'uniqueName' RefAddr found"); Object content = refAddr.getContent(); if (!(content instanceof String)) throw new NamingException("'uniqueName' RefAddr content is not of type java.lang.String"); String uniqueName = (String) content; if (log.isDebugEnabled()) { log.debug("getting registered resource with uniqueName '" + uniqueName + "'"); } Referenceable resource = ResourceRegistrar.get(uniqueName); if (resource == null) throw new NamingException("no resource registered with uniqueName '" + uniqueName + "', available resources: " + ResourceRegistrar.getResourcesUniqueNames()); return resource; }
/** * Serialize and write data sources to file. * * @param versionString Derby version string (i.e. 10.3.2.1) * @param buildNumber Derby build number (svn) * @param dataSourceClasses list of data source class names * @return The number of data sources serialized and written to file. * * @throws ClassNotFoundException required class is not on the classpath * @throws InstantiationException if instantiating data source class fails * @throws IllegalAccessException if instantiating data source class fails * @throws IOException if writing to file fails * @throws NamingException if creating a naming reference for the data * source fails */ private static int serializeDataSources(String versionString, String buildNumber, String[] dataSourceClasses) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, NamingException { String modifiedVersionString = versionString.replaceAll("\\.", "_"); int dsCount = 0; for (String dsClassName : dataSourceClasses) { Class dsClass; // Try to load the class. try { dsClass = Class.forName(dsClassName); } catch (ClassNotFoundException cnfe) { // Print error message, but keep going. System.out.println("\tcouldn't load " + dsClassName); continue; } // Create new instance. DataSource ds = (DataSource)dsClass.newInstance(); // Generate file name. File serialized = new File(dsClass.getSimpleName() + "-" + modifiedVersionString + ".ser"); System.out.println("\twriting " + serialized.getName()); OutputStream os = new FileOutputStream(serialized); ObjectOutputStream oos = new ObjectOutputStream(os); // Wrote version string, build number, the data source object and finally // a {@link javax.naming.Reference} for the data source. oos.writeUTF(versionString); oos.writeUTF(buildNumber); oos.writeObject(ds); Reference dsRef = ((Referenceable)ds).getReference(); oos.writeObject(dsRef); oos.flush(); oos.close(); dsCount++; } return dsCount; }
/** * 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); }
@SuppressWarnings("cast") @Test public void testReferenceable() throws Exception { ProxyAssertSupport.assertTrue(cf instanceof Referenceable); ProxyAssertSupport.assertTrue(queue1 instanceof Referenceable); ProxyAssertSupport.assertTrue(ActiveMQServerTestCase.topic1 instanceof Referenceable); }
/** * Encode an Object : If the object is a referenceable bind this reference * @param o the object to encode * @return a <code>Remote Object</code> if o is a ressource o if else */ private Object encodeObject(Object o) throws NamingException { try { if ((!(o instanceof Remote)) && (o instanceof Referenceable)) { return ((Referenceable) o).getReference(); } else if ((!(o instanceof Remote)) && (o instanceof Reference)) { return (Reference) o; } else { return o; } } catch (Exception e) { throw new NamingException("" + e); } }
/** * Wrap an Object : If the object is a reference wrap it into a Reference * Wrapper Object here the good way is to contact the carol configuration to * get the portable remote object * @param o the object to encode * @param name of the object * @param replace if the object need to be replaced * @return a <code>Remote JNDIRemoteReference Object</code> if o is a * resource o if else * @throws NamingException if object cannot be wrapped */ protected Object wrapObject(Object o, Name name, boolean replace) throws NamingException { try { // Add wrapper for the two first cases. Then it will use PortableRemoteObject instead of UnicastRemoteObject // and when fixing JRMP exported objects port, it use JRMPProdelegate which is OK. if ((!(o instanceof Remote)) && (o instanceof Referenceable)) { return new UnicastJNDIReferenceWrapper(((Referenceable) o).getReference(), getObjectPort()); } else if ((!(o instanceof Remote)) && (o instanceof Reference)) { return new UnicastJNDIReferenceWrapper((Reference) o, getObjectPort()); } else if ((!(o instanceof Remote)) && (!(o instanceof Referenceable)) && (!(o instanceof Reference)) && (o instanceof Serializable)) { // Only Serializable (not implementing Remote or Referenceable or // Reference) JNDIResourceWrapper irw = new JNDIResourceWrapper((Serializable) o); PortableRemoteObjectDelegate proDelegate = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject(); proDelegate.exportObject(irw); Remote oldObj = (Remote) addToExported(name, irw); if (oldObj != null) { if (replace) { proDelegate.unexportObject(oldObj); } else { proDelegate.unexportObject(irw); addToExported(name, oldObj); throw new NamingException("Object '" + o + "' with name '" + name + "' is already bind"); } } return irw; } else { return o; } } catch (Exception e) { throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : " + e.getMessage(), e); } }
/** * Serialize and write data sources to file. * * @param versionString Derby version string (i.e. 10.3.2.1) * @param buildNumber Derby build number (svn) * @param dataSourceClasses list of data source class names * @return The number of data sources serialized and written to file. * * @throws ClassNotFoundException required class is not on the classpath * @throws InstantiationException if instantiating data source class fails * @throws IllegalAccessException if instantiating data source class fails * @throws IOException if writing to file fails * @throws NamingException if creating a naming reference for the data * source fails */ private static int serializeDataSources(String versionString, String buildNumber, String[] dataSourceClasses) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, NamingException { String modifiedVersionString = versionString.replaceAll("\\.", "F"); int dsCount = 0; for (String dsClassName : dataSourceClasses) { Class dsClass; // Try to load the class. try { dsClass = Class.forName(dsClassName); } catch (ClassNotFoundException cnfe) { // Print error message, but keep going. System.out.println("\tcouldn't load " + dsClassName); continue; } // Create new instance. DataSource ds = (DataSource)dsClass.newInstance(); // Generate file name. File serialized = new File(dsClass.getSimpleName() + "-" + modifiedVersionString + ".ser"); System.out.println("\twriting " + serialized.getName()); OutputStream os = new FileOutputStream(serialized); ObjectOutputStream oos = new ObjectOutputStream(os); // Wrote version string, build number, the data source object and finally // a {@link javax.naming.Reference} for the data source. oos.writeUTF(versionString); oos.writeUTF(buildNumber); oos.writeObject(ds); Reference dsRef = ((Referenceable)ds).getReference(); oos.writeObject(dsRef); oos.flush(); oos.close(); dsCount++; } return dsCount; }
/** * Binds a name to an object. All intermediate contexts and the target * context (that named by all but terminal atomic component of the name) * must already exist. * * @param name the name to bind; may not be empty * @param obj the object to bind; possibly null * @param rebind if true, then perform a rebind (ie, overwrite) * @exception NameAlreadyBoundException if name is already bound * @exception javax.naming.directory.InvalidAttributesException if object * did not supply all mandatory attributes * @exception NamingException if a naming exception is encountered */ protected void bind(Name name, Object obj, boolean rebind) throws NamingException { if (!checkWritable()) { return; } while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) throw new NamingException (sm.getString("namingContext.invalidName")); NamingEntry entry = bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException(sm.getString( "namingContext.nameNotBound", name, name.get(0))); } if (entry.type == NamingEntry.CONTEXT) { if (rebind) { ((Context) entry.value).rebind(name.getSuffix(1), obj); } else { ((Context) entry.value).bind(name.getSuffix(1), obj); } } else { throw new NamingException (sm.getString("namingContext.contextExpected")); } } else { if ((!rebind) && (entry != null)) { throw new NameAlreadyBoundException (sm.getString("namingContext.alreadyBound", name.get(0))); } else { // Getting the type of the object and wrapping it within a new // NamingEntry Object toBind = NamingManager.getStateToBind(obj, name, this, env); if (toBind instanceof Context) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT); } else if (toBind instanceof LinkRef) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF); } else if (toBind instanceof Reference) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else if (toBind instanceof Referenceable) { toBind = ((Referenceable) toBind).getReference(); entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else { entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY); } bindings.put(name.get(0), entry); } } }
/** * Binds a name to an object. All intermediate contexts and the target * context (that named by all but terminal atomic component of the name) * must already exist. * * @param name the name to bind; may not be empty * @param obj the object to bind; possibly null * @param rebind if true, then perform a rebind (ie, overwrite) * @exception NameAlreadyBoundException if name is already bound * @exception InvalidAttributesException if object did not supply all * mandatory attributes * @exception NamingException if a naming exception is encountered */ protected void bind(Name name, Object obj, boolean rebind) throws NamingException { checkWritable(); while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) throw new NamingException (sm.getString("namingContext.invalidName")); NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type == NamingEntry.CONTEXT) { if (rebind) { ((Context) entry.value).rebind(name.getSuffix(1), obj); } else { ((Context) entry.value).bind(name.getSuffix(1), obj); } } else { throw new NamingException (sm.getString("namingContext.contextExpected")); } } else { if ((!rebind) && (entry != null)) { throw new NameAlreadyBoundException (sm.getString("namingContext.alreadyBound", name.get(0))); } else { // Getting the type of the object and wrapping it within a new // NamingEntry Object toBind = NamingManager.getStateToBind(obj, name, this, env); if (toBind instanceof Context) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT); } else if (toBind instanceof LinkRef) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF); } else if (toBind instanceof Reference) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else if (toBind instanceof Referenceable) { toBind = ((Referenceable) toBind).getReference(); entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else { entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY); } bindings.put(name.get(0), entry); } } }
/** * Binds a name to an object. All intermediate contexts and the target * context (that named by all but terminal atomic component of the name) * must already exist. * * @param name the name to bind; may not be empty * @param object the object to bind; possibly null * @param rebind if true, then perform a rebind (ie, overwrite) * @exception NameAlreadyBoundException if name is already bound * @exception InvalidAttributesException if object did not supply all * mandatory attributes * @exception NamingException if a naming exception is encountered */ protected void bind(Name name, Object obj, boolean rebind) throws NamingException { checkWritable(); while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) throw new NamingException (sm.getString("namingContext.invalidName")); NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type == NamingEntry.CONTEXT) { if (rebind) { ((Context) entry.value).rebind(name.getSuffix(1), obj); } else { ((Context) entry.value).bind(name.getSuffix(1), obj); } } else { throw new NamingException (sm.getString("namingContext.contextExpected")); } } else { if ((!rebind) && (entry != null)) { throw new NamingException (sm.getString("namingContext.alreadyBound", name.get(0))); } else { // Getting the type of the object and wrapping it within a new // NamingEntry Object toBind = NamingManager.getStateToBind(obj, name, this, env); if (toBind instanceof Context) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT); } else if (toBind instanceof LinkRef) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF); } else if (toBind instanceof Reference) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else if (toBind instanceof Referenceable) { toBind = ((Referenceable) toBind).getReference(); entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else { entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY); } bindings.put(name.get(0), entry); } } }
/** * Binds a name to an object. All intermediate contexts and the target * context (that named by all but terminal atomic component of the name) * must already exist. * * @param name * the name to bind; may not be empty * @param obj * the object to bind; possibly null * @param rebind * if true, then perform a rebind (ie, overwrite) * @exception NameAlreadyBoundException * if name is already bound * @exception javax.naming.directory.InvalidAttributesException * if object did not supply all mandatory attributes * @exception NamingException * if a naming exception is encountered */ protected void bind(Name name, Object obj, boolean rebind) throws NamingException { if (!checkWritable()) { return; } while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) throw new NamingException(sm.getString("namingContext.invalidName")); NamingEntry entry = bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type == NamingEntry.CONTEXT) { if (rebind) { ((Context) entry.value).rebind(name.getSuffix(1), obj); } else { ((Context) entry.value).bind(name.getSuffix(1), obj); } } else { throw new NamingException(sm.getString("namingContext.contextExpected")); } } else { if ((!rebind) && (entry != null)) { throw new NameAlreadyBoundException(sm.getString("namingContext.alreadyBound", name.get(0))); } else { // Getting the type of the object and wrapping it within a new // NamingEntry Object toBind = NamingManager.getStateToBind(obj, name, this, env); if (toBind instanceof Context) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT); } else if (toBind instanceof LinkRef) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF); } else if (toBind instanceof Reference) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else if (toBind instanceof Referenceable) { toBind = ((Referenceable) toBind).getReference(); entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else { entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY); } bindings.put(name.get(0), entry); } } }
/** * Make sure it is possible to recreate and serialize/deserialize a * populated data source. * <p> * Populated means the various bean properties have non-default * values set. * * @param dsDesc data source descriptor * @param className data source class name * @throws Exception on a wide variety of error conditions... */ private void assertDataSourceReferencePopulated( DataSourceDescriptor dsDesc, String className) throws Exception { println("Testing recreated populated data source."); Object ds = Class.forName(className).newInstance(); // Populate the data source. Iterator propIter = dsDesc.getPropertyIterator(); while (propIter.hasNext()) { String property = (String)propIter.next(); String value = dsDesc.getPropertyValue(property); Method getMethod = getGet(property, ds); Method setMethod = getSet(getMethod, ds); Class paramType = getMethod.getReturnType(); if (paramType.equals(Integer.TYPE)) { setMethod.invoke(ds, new Object[] {Integer.valueOf(value)}); } else if (paramType.equals(String.class)) { setMethod.invoke(ds, new Object[] {value}); } else if (paramType.equals(Boolean.TYPE)) { setMethod.invoke(ds, new Object[] {Boolean.valueOf(value)}); } else if (paramType.equals(Short.TYPE)) { setMethod.invoke(ds, new Object[] {Short.valueOf(value)}); } else if (paramType.equals(Long.TYPE)) { setMethod.invoke(ds, new Object[] {Long.valueOf(value)}); } else { fail("'" + property + "' not settable - update test!!"); } } 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); // Recreated should not be same instance as original. assertNotSame(recreatedDs, ds); compareDataSources(dsDesc, ds, recreatedDs, false); // 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(); compareDataSources(dsDesc, ds, recreatedDs, false); }
/** * Binds a name to an object. All intermediate contexts and the target * context (that named by all but terminal atomic component of the name) * must already exist. * * @param name the name to bind; may not be empty * @param obj the object to bind; possibly null * @param rebind if true, then perform a rebind (ie, overwrite) * @throws NameAlreadyBoundException if name is already bound * @throws javax.naming.directory.InvalidAttributesException if object * did not supply all mandatory attributes * @throws NamingException if a jndi exception is encountered */ protected void bind(Name name, Object obj, boolean rebind) throws NamingException { while ((!name.isEmpty()) && (name.get(0).length() == 0)) { name = name.getSuffix(1); } if (name.isEmpty()) { throw new NamingException(SM.getString("namingContext.invalidName")); } NamingEntry entry = bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException(SM.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type == NamingEntry.CONTEXT) { if (rebind) { ((Context) entry.value).rebind(name.getSuffix(1), obj); } else { ((Context) entry.value).bind(name.getSuffix(1), obj); } } else { throw new NamingException(SM.getString("namingContext.contextExpected")); } } else { if ((!rebind) && (entry != null)) { throw new NameAlreadyBoundException(SM.getString("namingContext.alreadyBound", name.get(0))); } else { // Getting the type of the object and wrapping it within a new // NamingEntry Object toBind = NamingManager.getStateToBind(obj, name, this, env); if (toBind instanceof Context) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT); } else if (toBind instanceof LinkRef) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF); } else if (toBind instanceof Reference) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else if (toBind instanceof Referenceable) { toBind = ((Referenceable) toBind).getReference(); entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else { entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY); } bindings.put(name.get(0), entry); } } }
@Test public void testReferenceCF() throws Exception { Reference cfRef = ((Referenceable) cf).getReference(); String factoryName = cfRef.getFactoryClassName(); Class<?> factoryClass = Class.forName(factoryName); ObjectFactory factory = (ObjectFactory) factoryClass.newInstance(); Object instance = factory.getObjectInstance(cfRef, null, null, null); ProxyAssertSupport.assertTrue(instance instanceof ActiveMQConnectionFactory); ActiveMQJMSConnectionFactory cf2 = (ActiveMQJMSConnectionFactory) instance; simpleSendReceive(cf2, queue1); }
@Test public void testReferenceQueue() throws Exception { Reference queueRef = ((Referenceable) queue1).getReference(); String factoryName = queueRef.getFactoryClassName(); Class<?> factoryClass = Class.forName(factoryName); ObjectFactory factory = (ObjectFactory) factoryClass.newInstance(); Object instance = factory.getObjectInstance(queueRef, null, null, null); ProxyAssertSupport.assertTrue(instance instanceof ActiveMQDestination); ActiveMQQueue queue2 = (ActiveMQQueue) instance; ProxyAssertSupport.assertEquals(queue1.getQueueName(), queue2.getQueueName()); simpleSendReceive(cf, queue2); }
@Test public void testReferenceTopic() throws Exception { Reference topicRef = ((Referenceable) ActiveMQServerTestCase.topic1).getReference(); String factoryName = topicRef.getFactoryClassName(); Class factoryClass = Class.forName(factoryName); ObjectFactory factory = (ObjectFactory) factoryClass.newInstance(); Object instance = factory.getObjectInstance(topicRef, null, null, null); ProxyAssertSupport.assertTrue(instance instanceof ActiveMQDestination); ActiveMQTopic topic2 = (ActiveMQTopic) instance; ProxyAssertSupport.assertEquals(ActiveMQServerTestCase.topic1.getTopicName(), topic2.getTopicName()); simpleSendReceive(cf, topic2); }
public void bind(Name name, Object obj, Attributes attributes) throws NamingException { checkName(name); if (hasMultiNamingSpace(name)) { /* * multi ns, find next ns context, delegate operation to the next * contex */ DirContext nns = (DirContext) findNnsContext(name); Name remainingName = name.getSuffix(1); nns.bind(remainingName, attributes); return; } /* * there is only one ldap ns */ if (obj == null && attributes == null) { // ldap.2E=cannot bind null object without attributes throw new IllegalArgumentException(Messages.getString("ldap.2E")); //$NON-NLS-1$ } if (obj == null) { createSubcontext(name, attributes); return; } Result result = DirectoryManager.getStateToBind(obj, name, this, env, attributes); Object o = result.getObject(); Attributes attrs = null; if (o instanceof Reference) { attrs = convertRefToAttribute((Reference) o); } else if (o instanceof Referenceable) { attrs = convertRefToAttribute(((Referenceable) o).getReference()); } else if (o instanceof Serializable) { attrs = convertSerialToAttribute((Serializable) o); } else if (o instanceof DirContext) { DirContext cxt = (DirContext) o; attrs = cxt.getAttributes(""); } else { throw new IllegalArgumentException(Messages.getString("ldap.24")); //$NON-NLS-1$ } NamingEnumeration<? extends Attribute> enu = attrs.getAll(); if (result.getAttributes() != null) { Attributes resultAttributes = result.getAttributes(); while (enu.hasMore()) { Attribute element = enu.next(); if (element.getID().equalsIgnoreCase("objectClass")) { element = mergeAttribute(resultAttributes .get("objectClass"), element); Attribute oc = resultAttributes.get("objectClass"); if (oc != null) { if (!oc.contains("javaContainer") && oc.size() > 0) { element.remove("javaContainer"); } } resultAttributes.put(element); } else if (resultAttributes.get(element.getID()) == null) { resultAttributes.put(element); } } createSubcontext(name, resultAttributes); } else { createSubcontext(name, attrs); } }
/** * Binds a name to an object. All intermediate contexts and the target * context (that named by all but terminal atomic component of the name) * must already exist. * * @param name the name to bind; may not be empty * @param obj the object to bind; possibly null * @param rebind if true, then perform a rebind (ie, overwrite) * @exception NameAlreadyBoundException if name is already bound * @exception javax.naming.directory.InvalidAttributesException if object * did not supply all mandatory attributes * @exception NamingException if a naming exception is encountered */ protected void bind(Name name, Object obj, boolean rebind) throws NamingException { checkWritable(); while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) throw new NamingException (sm.getString("namingContext.invalidName")); NamingEntry entry = bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type == NamingEntry.CONTEXT) { if (rebind) { ((Context) entry.value).rebind(name.getSuffix(1), obj); } else { ((Context) entry.value).bind(name.getSuffix(1), obj); } } else { throw new NamingException (sm.getString("namingContext.contextExpected")); } } else { if ((!rebind) && (entry != null)) { throw new NameAlreadyBoundException (sm.getString("namingContext.alreadyBound", name.get(0))); } else { // Getting the type of the object and wrapping it within a new // NamingEntry Object toBind = NamingManager.getStateToBind(obj, name, this, env); if (toBind instanceof Context) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT); } else if (toBind instanceof LinkRef) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF); } else if (toBind instanceof Reference) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else if (toBind instanceof Referenceable) { toBind = ((Referenceable) toBind).getReference(); entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else { entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY); } bindings.put(name.get(0), entry); } } }