public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
throws java.io.IOException, ClassNotFoundException { java.io.InputStream ins;
java.io.ObjectInputStream oins = null;
Object result = null;
boolean serialized = false;
java.io.IOException serex = null; // If the given classloader is null, we check if an
// system classloader is available and (if so)
// use that instead.
// Note that calls on the system class loader will
// look in the bootstrap class loader first.
if (cls == null) {
try {
cls = ClassLoader.getSystemClassLoader();
} catch (SecurityException ex) {
// We're not allowed to access the system class loader.
// Drop through.
}
} // Try to find a serialized object with this name
final String serName = beanName.replace('.','/').concat(".ser");
final ClassLoader loader = cls;
ins = (InputStream)java.security.AccessController.doPrivileged
(new java.security.PrivilegedAction() {
public Object run() {
if (loader == null)
return ClassLoader.getSystemResourceAsStream(serName);
else
return loader.getResourceAsStream(serName);
}
});
if (ins != null) {
try {
if (cls == null) {
oins = new ObjectInputStream(ins);
} else {
oins = new ObjectInputStreamWithLoader(ins, cls);
}
result = oins.readObject();
serialized = true;
oins.close();
} catch (java.io.IOException ex) {
ins.close();
// Drop through and try opening the class. But remember
// the exception in case we can't find the class either.
serex = ex;
} catch (ClassNotFoundException ex) {
ins.close();
throw ex;
}
} if (result == null) {
// No serialized object, try just instantiating the class
Class cl; try {
if (cls == null) {
cl = Class.forName(beanName);
} else {
cl = cls.loadClass(beanName);
}
} catch (ClassNotFoundException ex) {
// There is no appropriate class. If we earlier tried to
// deserialize an object and got an IO exception, throw that,
// otherwise rethrow the ClassNotFoundException.
if (serex != null) {
throw serex;
}
throw ex;
} /*
* Try to instantiate the class.
*/ try {
result = cl.newInstance();
} catch (Exception ex) {
// We have to remap the exception to one in our signature.
// But we pass extra information in the detail message.
throw new ClassNotFoundException("" + cl + " : " + ex);
}
} if (result != null) { // 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)
java.security.AccessController.doPrivileged
(new java.security.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); } else if (beanContext != null) beanContext.add(result);
} return result;
}
throws java.io.IOException, ClassNotFoundException { java.io.InputStream ins;
java.io.ObjectInputStream oins = null;
Object result = null;
boolean serialized = false;
java.io.IOException serex = null; // If the given classloader is null, we check if an
// system classloader is available and (if so)
// use that instead.
// Note that calls on the system class loader will
// look in the bootstrap class loader first.
if (cls == null) {
try {
cls = ClassLoader.getSystemClassLoader();
} catch (SecurityException ex) {
// We're not allowed to access the system class loader.
// Drop through.
}
} // Try to find a serialized object with this name
final String serName = beanName.replace('.','/').concat(".ser");
final ClassLoader loader = cls;
ins = (InputStream)java.security.AccessController.doPrivileged
(new java.security.PrivilegedAction() {
public Object run() {
if (loader == null)
return ClassLoader.getSystemResourceAsStream(serName);
else
return loader.getResourceAsStream(serName);
}
});
if (ins != null) {
try {
if (cls == null) {
oins = new ObjectInputStream(ins);
} else {
oins = new ObjectInputStreamWithLoader(ins, cls);
}
result = oins.readObject();
serialized = true;
oins.close();
} catch (java.io.IOException ex) {
ins.close();
// Drop through and try opening the class. But remember
// the exception in case we can't find the class either.
serex = ex;
} catch (ClassNotFoundException ex) {
ins.close();
throw ex;
}
} if (result == null) {
// No serialized object, try just instantiating the class
Class cl; try {
if (cls == null) {
cl = Class.forName(beanName);
} else {
cl = cls.loadClass(beanName);
}
} catch (ClassNotFoundException ex) {
// There is no appropriate class. If we earlier tried to
// deserialize an object and got an IO exception, throw that,
// otherwise rethrow the ClassNotFoundException.
if (serex != null) {
throw serex;
}
throw ex;
} /*
* Try to instantiate the class.
*/ try {
result = cl.newInstance();
} catch (Exception ex) {
// We have to remap the exception to one in our signature.
// But we pass extra information in the detail message.
throw new ClassNotFoundException("" + cl + " : " + ex);
}
} if (result != null) { // 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)
java.security.AccessController.doPrivileged
(new java.security.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); } else if (beanContext != null) beanContext.add(result);
} return result;
}
解决方案 »
- 我写的贪吃蛇-逻辑问题:运行时,会突然多出1个食物,求解
- struts conf 中 forward 配.do与.html有什么区别呢,有代码
- format问题
- 读取最后一行记录! 改正错误!
- 用JAVA自带的jTree1组件,在组件上添加右键弹出菜单,删除选中节点。
- java.net.* 界面聊天程序.请大家帮帮助
- 求导
- 请问怎样才能让JList有滚动的功能
- jbuilder7中的报表
- 启动startWebLogic.cmd后,出现DOS程序执行窗口。提示“Enter password to boot WebLogic server:”,但我却无法输入任何字符。
- 请问如何将用textpad写出来的java程序做成.exe的形式?
- 我对JAVA还是没感觉,能不能举几个JAVA开发的大工程的例子
是创建一个实例,这样就可以调入该类了。至于能否混合使用,我也不知道。
我已尽力了。