throws ActivationException, RemoteException
{
RegistryImpl.checkAccess("ActivationInstantiator.newInstance");
if (!groupID.equals(desc.getGroupID()))
throw new ActivationException("newInstance in wrong group");
try {
acquireLock(id);
synchronized (this) {
if (groupInactive == true)
throw new InactiveGroupException("group is inactive");
}
ActiveEntry entry = active.get(id);
if (entry != null)
return entry.mobj;
String className = desc.getClassName();
final Class<? extends Remote> cl =
RMIClassLoader.loadClass(desc.getLocation(), className)
.asSubclass(Remote.class);
Remote impl = null;
final Thread t = Thread.currentThread();
final ClassLoader savedCcl = t.getContextClassLoader();
ClassLoader objcl = cl.getClassLoader();
final ClassLoader ccl = covers(objcl, savedCcl) ? objcl : savedCcl;
/*
* Fix for 4164971: allow non-public activatable class
* and/or constructor, create the activatable object in a
* privileged block
*/
try {
/*
* The code below is in a doPrivileged block to
* protect against user code which code might have set
* a global socket factory (in which case application
* code would be on the stack).
*/
impl = AccessController.doPrivileged(
new PrivilegedExceptionAction<Remote>() {
public Remote run() throws InstantiationException,
NoSuchMethodException, IllegalAccessException,
InvocationTargetException
{
Constructor<? extends Remote> constructor =
cl.getDeclaredConstructor(
ActivationID.class, MarshalledObject.class);
constructor.setAccessible(true);
try {
/*
* Fix for 4289544: make sure to set the
* context class loader to be the class
* loader of the impl class before
* constructing that class.
*/
t.setContextClassLoader(ccl);
return constructor.newInstance(id,
desc.getData());
} finally {
t.setContextClassLoader(savedCcl);
}
}
});
} catch (PrivilegedActionException pae) {
Throwable e = pae.getException();
// narrow the exception's type and rethrow it
if (e instanceof InstantiationException) {
throw (InstantiationException) e;
} else if (e instanceof NoSuchMethodException) {
throw (NoSuchMethodException) e;
} else if (e instanceof IllegalAccessException) {
throw (IllegalAccessException) e;
} else if (e instanceof InvocationTargetException) {
throw (InvocationTargetException) e;
} else if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else if (e instanceof Error) {
throw (Error) e;
}
}
entry = new ActiveEntry(impl);
active.put(id, entry);
return entry.mobj;
} catch (NoSuchMethodException e) {
/* user forgot to provide activatable constructor? */
throw new ActivationException
("Activatable object must provide an activation"+
" constructor", e);
} catch (NoSuchMethodError e) {
/* code recompiled and user forgot to provide
* activatable constructor?
*/
throw new ActivationException
("Activatable object must provide an activation"+
" constructor", e );
} catch (InvocationTargetException e) {
throw new ActivationException("exception in object constructor",
e.getTargetException());
} catch (Exception e) {
throw new ActivationException("unable to activate object", e);
} finally {
releaseLock(id);
checkInactiveGroup();
}
}