下面列出了怎么用java.beans.ExceptionListener的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testNodeChange() throws BackingStoreException, IOException {
String filename = File.createTempFile("foo", "bar").getAbsolutePath();
OutputStream objOS = new BufferedOutputStream(new FileOutputStream(filename, false));
XMLEncoder beanEncoder = new XMLEncoder(objOS);
beanEncoder.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception exception) {
System.out.println("XMLStore.save()");
exception.printStackTrace();
}
});
beanEncoder.writeObject(new java.awt.Rectangle(100, 200));
beanEncoder.writeObject(new TesterBean());
beanEncoder.close();
}
public @Override void write(java.io.Writer w, final Object inst) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new ExceptionListener() {
public @Override void exceptionThrown(Exception x) {
Logger.getLogger(XMLBeanConvertor.class.getName()).log(Level.INFO, "Problem writing " + inst, x);
}
});
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
try {
// XXX would inst.getClass().getClassLoader() be more appropriate?
ClassLoader ccl2 = Lookup.getDefault().lookup(ClassLoader.class);
if (ccl2 != null) {
Thread.currentThread().setContextClassLoader(ccl2);
}
e.writeObject(inst);
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
e.close();
String data = new String(out.toByteArray(), "UTF-8");
data = data.replaceFirst("<java", "<!DOCTYPE xmlbeans PUBLIC \"-//NetBeans//DTD XML beans 1.0//EN\" \"http://www.netbeans.org/dtds/xml-beans-1_0.dtd\">\n<java");
w.write(data);
}
private XMLDecoder openBeanDecoder(InputStream objIS) {
// filter stream for XMLDecoder
XMLDecoder beanDecoder = new XMLDecoder(objIS, null, new ExceptionListener() {
public void exceptionThrown(Exception e) {
if (showDecoderExceptions)
System.out.println("***XMLStore.read() got Exception= " + e.getClass().getName() + " " + e.getMessage());
e.printStackTrace();
}
});
return beanDecoder;
}
/**
* Save the current state of the Preferences tree to the given OutputStream.
*/
public void save(OutputStream out) throws java.io.IOException {
outputExceptionMessage = null;
// the OutputMunger strips off the XMLEncoder header
OutputMunger bos = new OutputMunger(out);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(bos, StandardCharsets.UTF_8));
XMLEncoder beanEncoder = new XMLEncoder(bos);
beanEncoder.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception exception) {
System.out.println("XMLStore.save() got Exception: abort saving the preferences!");
exception.printStackTrace();
outputExceptionMessage = exception.getMessage();
}
});
pw.printf("<?xml version='1.0' encoding='UTF-8'?>%n");
pw.printf("<preferences EXTERNAL_XML_VERSION='1.0'>%n");
if (!rootPrefs.isUserNode())
pw.printf(" <root type='system'>%n");
else
pw.printf(" <root type='user'>%n");
Indent indent = new Indent(2);
indent.incr();
writeXmlNode(bos, pw, rootPrefs, beanEncoder, indent);
if (outputExceptionMessage != null)
throw new IOException(outputExceptionMessage);
pw.printf(" </root>%n");
pw.printf("</preferences>%n");
pw.flush();
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
private XMLDecoder openBeanDecoder(InputStream objIS) {
// filter stream for XMLDecoder
XMLDecoder beanDecoder = new XMLDecoder(objIS, null, new ExceptionListener() {
public void exceptionThrown(Exception e) {
if (showDecoderExceptions)
System.out.println("***XMLStore.read() got Exception= " + e.getClass().getName() + " " + e.getMessage());
e.printStackTrace();
}
});
// System.out.println("openBeanDecoder at "+objIS);
return beanDecoder;
}
/**
* Save the current state of the Preferences tree to the given OutputStream.
*/
public void save(OutputStream out) throws java.io.IOException {
outputExceptionMessage = null;
// the OutputMunger strips off the XMLEncoder header
OutputMunger bos = new OutputMunger(out);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(bos, StandardCharsets.UTF_8));
XMLEncoder beanEncoder = new XMLEncoder(bos);
beanEncoder.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception exception) {
System.out.println("XMLStore.save() got Exception: abort saving the preferences!");
exception.printStackTrace();
outputExceptionMessage = exception.getMessage();
}
});
pw.printf("<?xml version='1.0' encoding='UTF-8'?>%n");
pw.printf("<preferences EXTERNAL_XML_VERSION='1.0'>%n");
if (!rootPrefs.isUserNode())
pw.printf(" <root type='system'>%n");
else
pw.printf(" <root type='user'>%n");
Indent indent = new Indent(2);
indent.incr();
writeXmlNode(bos, pw, rootPrefs, beanEncoder, indent);
if (outputExceptionMessage != null)
throw new IOException(outputExceptionMessage);
pw.printf(" </root>%n");
pw.printf("</preferences>%n");
pw.flush();
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = {new URL(sb.toString())};
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
public static JarBasedPlugin toJarBasedPlugin(Path p, ExceptionListener listener) {
try {
return new JarBasedPlugin(p);
} catch (IOException e) {
listener.exceptionThrown(e);
return null;
}
}
public StreamHandler(List<StreamJunction.Receiver> receivers, int batchSize,
String streamName, String siddhiAppName, StreamJunction faultStreamJunction,
StreamJunction.OnErrorAction onErrorAction, ExceptionListener exceptionListener) {
this.receivers = receivers;
this.batchSize = batchSize;
this.streamName = streamName;
this.siddhiAppName = siddhiAppName;
this.faultStreamJunction = faultStreamJunction;
this.onErrorAction = onErrorAction;
this.exceptionListener = exceptionListener;
}
private static void serializeToXML (User settings, String path) throws IOException
{
FileOutputStream fos = new FileOutputStream(path);
XMLEncoder encoder = new XMLEncoder(fos);
encoder.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception e) {
System.out.println("Exception! :"+e.toString());
}
});
encoder.writeObject(settings);
encoder.close();
fos.close();
}
public void handleRuntimeExceptionWith(ExceptionListener exceptionListener) {
siddhiAppContext.setRuntimeExceptionListener(exceptionListener);
}
public ExceptionListener getRuntimeExceptionListener() {
return runtimeExceptionListener;
}
public void setRuntimeExceptionListener(ExceptionListener runtimeExceptionListener) {
this.runtimeExceptionListener = runtimeExceptionListener;
}
/**
* Returns the exception listener for parsing.
* The exception listener is notified
* when handler catches recoverable exceptions.
* If the exception listener has not been explicitly set
* then default exception listener is returned.
*
* @return the exception listener for parsing
*/
public ExceptionListener getExceptionListener() {
return this.listener;
}
/**
* Sets the exception listener for parsing.
* The exception listener is notified
* when handler catches recoverable exceptions.
*
* @param listener the exception listener for parsing
*/
public void setExceptionListener(ExceptionListener listener) {
this.listener = listener;
}
/**
* Returns the exception listener for parsing.
* The exception listener is notified
* when handler catches recoverable exceptions.
* If the exception listener has not been explicitly set
* then default exception listener is returned.
*
* @return the exception listener for parsing
*/
public ExceptionListener getExceptionListener() {
return this.listener;
}
/**
* Sets the exception listener for parsing.
* The exception listener is notified
* when handler catches recoverable exceptions.
*
* @param listener the exception listener for parsing
*/
public void setExceptionListener(ExceptionListener listener) {
this.listener = listener;
}
/**
* Returns the exception listener for parsing.
* The exception listener is notified
* when handler catches recoverable exceptions.
* If the exception listener has not been explicitly set
* then default exception listener is returned.
*
* @return the exception listener for parsing
*/
public ExceptionListener getExceptionListener() {
return this.listener;
}