下面列出了怎么用javax.naming.Referenceable的API类实例代码及写法,或者点击链接到github查看源代码。
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<?>[0]);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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 JRMPproDelegateOld 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 proDelegateOld = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject();
proDelegateOld.exportObject(irw);
Remote oldObjBB = (Remote) addToExported(name, irw);
if (oldObjBB != null) {
if (replace) {
proDelegateOld.unexportObject(oldObjBB);
} else {
proDelegateOld.unexportObject(irw);
addToExported(name, oldObjBB);
throw new NamingException("Object '" + o + "' with name '" + name + "' is already bind");
}
System.out.println("MyInsertedStmt");
}
return irw;
} else {
return o;
}
} catch (Exception e1111) {
throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : "
+ e1111.getMessage(), e1111);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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 JRMPproDelegateOld 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 proDelegateOld = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject();
proDelegateOld.exportObject(irw);
Remote oldObjBB = (Remote) addToExported(name, irw);
if (oldObjBB != null) {
if (replace) {
proDelegateOld.unexportObject(oldObjBB);
} else {
proDelegateOld.unexportObject(irw);
addToExported(name, oldObjBB);
throw new NamingException("Object '" + o + "' with name '" + name + "' is already bind");
}
}
return irw;
} else {
return o;
}
} catch (Exception e1111) {
throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : "
+ e1111.getMessage(), e1111);
}
}
/**
* 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;
}
/**
* 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);
}
/**
* 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 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)
* @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);
}
@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);
}
/**
* 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);
}