// Ok, if the result is an applet initialize it.
AppletStub stub = null;
if (result instanceof Applet) {
Applet applet = (Applet) result;
boolean needDummies = initializer == null;
if (needDummies) {
// Figure our the codebase and docbase URLs. We do this
// by locating the URL for a known resource, and then
// massaging the URL.
// First find the "resource name" corresponding to the bean
// itself. So a serialzied bean "a.b.c" would imply a
// resource name of "a/b/c.ser" and a classname of "x.y"
// would imply a resource name of "x/y.class".
final String resourceName;
if (serialized) {
// Serialized bean
resourceName = beanName.replace('.','/').concat(".ser");
} else {
// Regular class
resourceName = beanName.replace('.','/').concat(".class");
}
URL objectUrl = null;
URL codeBase = null;
URL docBase = null;
// Now get the URL correponding to the resource name.
final ClassLoader cloader = cls;
objectUrl = (URL)
AccessController.doPrivileged
(new PrivilegedAction() {
public Object run() {
if (cloader == null)
return ClassLoader.getSystemResource
(resourceName);
else
return cloader.getResource(resourceName);
}
});
// If we found a URL, we try to locate the docbase by taking
// of the final path name component, and the code base by taking
// of the complete resourceName.
// So if we had a resourceName of "a/b/c.class" and we got an
// objectURL of "file://bert/classes/a/b/c.class" then we would
// want to set the codebase to "file://bert/classes/" and the
// docbase to "file://bert/classes/a/b/"
if (objectUrl != null) {
String s = objectUrl.toExternalForm();
if (s.endsWith(resourceName)) {
int ix = s.length() - resourceName.length();
codeBase = new URL(s.substring(0,ix));
docBase = codeBase;
ix = s.lastIndexOf('/');
if (ix >= 0) {
docBase = new URL(s.substring(0,ix+1));
}
}
}
// Setup a default context and stub.
BeansAppletContext context = new BeansAppletContext(applet);
stub = (AppletStub)new BeansAppletStub(applet, context, codeBase, docBase);
applet.setStub(stub);
} else {
initializer.initialize(applet, beanContext);
}
// now, if there is a BeanContext, add the bean, if applicable.
if (beanContext != null) {
beanContext.add(result);
}
// If it was deserialized then it was already init-ed.
// Otherwise we need to initialize it.
if (!serialized) {
// We need to set a reasonable initial size, as many
// applets are unhappy if they are started without
// having been explicitly sized.
applet.setSize(100,100);
applet.init();
}
if (needDummies) {
((BeansAppletStub)stub).active = true;
} else initializer.activate(applet);