Search in sources :

Example 11 with XStreamException

use of com.thoughtworks.xstream.XStreamException in project spacesettlers by amymcgovern.

the class PacifistHeuristicAsteroidCollectorTeamClient method shutDown.

/**
 * Demonstrates saving out to the xstream file
 * You can save out other ways too.  This is a human-readable way to examine
 * the knowledge you have learned.
 */
@Override
public void shutDown(Toroidal2DPhysics space) {
    XStream xstream = new XStream();
    xstream.alias("ExampleKnowledge", ExampleKnowledge.class);
    try {
        // if you want to compress the file, change FileOuputStream to a GZIPOutputStream
        xstream.toXML(myKnowledge, new FileOutputStream(new File(knowledgeFile)));
    } catch (XStreamException e) {
        // if you get an error, handle it somehow as it means your knowledge didn't save
        // the error will happen the first time you run
        myKnowledge = new ExampleKnowledge();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        myKnowledge = new ExampleKnowledge();
    }
}
Also used : XStreamException(com.thoughtworks.xstream.XStreamException) XStream(com.thoughtworks.xstream.XStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 12 with XStreamException

use of com.thoughtworks.xstream.XStreamException in project spacesettlers by amymcgovern.

the class ExampleGAClient method shutDown.

@Override
public void shutDown(Toroidal2DPhysics space) {
    XStream xstream = new XStream();
    xstream.alias("ExampleGAPopulation", ExampleGAPopulation.class);
    try {
        // if you want to compress the file, change FileOuputStream to a GZIPOutputStream
        xstream.toXML(population, new FileOutputStream(new File(getKnowledgeFile())));
    } catch (XStreamException e) {
        // if you get an error, handle it somehow as it means your knowledge didn't save
        System.out.println("Can't save knowledge file in shutdown ");
        System.out.println(e.getMessage());
    } catch (FileNotFoundException e) {
        // file is missing so start from scratch (but tell the user)
        System.out.println("Can't save knowledge file in shutdown ");
        System.out.println(e.getMessage());
    }
}
Also used : XStreamException(com.thoughtworks.xstream.XStreamException) XStream(com.thoughtworks.xstream.XStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 13 with XStreamException

use of com.thoughtworks.xstream.XStreamException in project camel by apache.

the class XmlRestProcessor method processResponse.

@Override
protected void processResponse(Exchange exchange, InputStream responseEntity, SalesforceException exception, AsyncCallback callback) {
    final XStream localXStream = xStream.get();
    try {
        // do we need to un-marshal a response
        if (responseEntity != null) {
            final Class<?> responseClass = exchange.getProperty(RESPONSE_CLASS, Class.class);
            Object response;
            if (responseClass != null) {
                // its ok to call this multiple times, as xstream ignores duplicate calls
                localXStream.processAnnotations(responseClass);
                final String responseAlias = exchange.getProperty(RESPONSE_ALIAS, String.class);
                if (responseAlias != null) {
                    // extremely dirty, need to flush entire cache if its holding on to an old alias!!!
                    final CachingMapper mapper = (CachingMapper) localXStream.getMapper();
                    try {
                        if (mapper.realClass(responseAlias) != responseClass) {
                            mapper.flushCache();
                        }
                    } catch (CannotResolveClassException ignore) {
                        // recent XStream versions add a ClassNotFoundException to cache
                        mapper.flushCache();
                    }
                    localXStream.alias(responseAlias, responseClass);
                }
                response = responseClass.newInstance();
                localXStream.fromXML(responseEntity, response);
            } else {
                // return the response as a stream, for getBlobField
                response = responseEntity;
            }
            exchange.getOut().setBody(response);
        } else {
            exchange.setException(exception);
        }
        // copy headers and attachments
        exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
        exchange.getOut().getAttachmentObjects().putAll(exchange.getIn().getAttachmentObjects());
    } catch (XStreamException e) {
        String msg = "Error parsing XML response: " + e.getMessage();
        exchange.setException(new SalesforceException(msg, e));
    } catch (Exception e) {
        String msg = "Error creating XML response: " + e.getMessage();
        exchange.setException(new SalesforceException(msg, e));
    } finally {
        // cleanup temporary exchange headers
        exchange.removeProperty(RESPONSE_CLASS);
        exchange.removeProperty(RESPONSE_ALIAS);
        // consume response entity
        if (responseEntity != null) {
            try {
                responseEntity.close();
            } catch (IOException ignored) {
            }
        }
        // notify callback that exchange is done
        callback.done(false);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) XStreamException(com.thoughtworks.xstream.XStreamException) XStream(com.thoughtworks.xstream.XStream) CachingMapper(com.thoughtworks.xstream.mapper.CachingMapper) IOException(java.io.IOException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException) XStreamException(com.thoughtworks.xstream.XStreamException) IOException(java.io.IOException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Example 14 with XStreamException

use of com.thoughtworks.xstream.XStreamException in project GDSC-SMLM by aherbert.

the class SettingsManager method unsafeLoadFitEngineConfiguration.

/**
	 * Load the configuration within the specified file
	 * 
	 * @param filename
	 * @return The configuration (or null)
	 */
public static FitEngineConfiguration unsafeLoadFitEngineConfiguration(String filename) {
    XStream xs = createXStream();
    FitEngineConfiguration config = null;
    FileInputStream fs = null;
    try {
        fs = new FileInputStream(filename);
        config = (FitEngineConfiguration) xs.fromXML(fs);
    } catch (ClassCastException ex) {
    //ex.printStackTrace();
    } catch (FileNotFoundException ex) {
    //ex.printStackTrace();
    } catch (XStreamException ex) {
        ex.printStackTrace();
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return config;
}
Also used : XStreamException(com.thoughtworks.xstream.XStreamException) XStream(com.thoughtworks.xstream.XStream) FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 15 with XStreamException

use of com.thoughtworks.xstream.XStreamException in project GDSC-SMLM by aherbert.

the class BatchPeakFit method saveRunSettings.

private String saveRunSettings(String prefix, String imageFilename, FitEngineConfiguration fitConfig) {
    BatchRun batchRun = new BatchRun(imageFilename, fitConfig);
    FileOutputStream fs = null;
    try {
        String settingsFilename = prefix + ".xml";
        fs = new FileOutputStream(settingsFilename);
        xs.toXML(batchRun, fs);
        return settingsFilename;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (XStreamException e) {
        e.printStackTrace();
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : XStreamException(com.thoughtworks.xstream.XStreamException) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BatchRun(gdsc.smlm.ij.settings.BatchRun)

Aggregations

XStreamException (com.thoughtworks.xstream.XStreamException)24 IOException (java.io.IOException)13 XStream (com.thoughtworks.xstream.XStream)12 FileNotFoundException (java.io.FileNotFoundException)9 FileOutputStream (java.io.FileOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 FitEngineConfiguration (gdsc.smlm.engine.FitEngineConfiguration)3 InputStream (java.io.InputStream)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 Response (javax.ws.rs.core.Response)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)2 StringReader (java.io.StringReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)2 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)1