Example usage for org.apache.commons.lang SerializationUtils serialize

List of usage examples for org.apache.commons.lang SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang SerializationUtils serialize.

Prototype

public static void serialize(Serializable obj, OutputStream outputStream) 

Source Link

Document

Serializes an Object to the specified stream.

The stream will be closed once the object is written.

Usage

From source file:org.datacleaner.result.save.AnalysisResultSaveHandler.java

private static void saveOrThrow(final AnalysisResult analysisResult, final Resource resource) {
    final SimpleAnalysisResult simpleAnalysisResult;
    if (analysisResult instanceof SimpleAnalysisResult) {
        simpleAnalysisResult = (SimpleAnalysisResult) analysisResult;
    } else {//  w w  w.  jav  a  2s  .c  o m
        simpleAnalysisResult = new SimpleAnalysisResult(analysisResult.getResultMap(),
                analysisResult.getCreationDate());
    }

    final OutputStream out = resource.write();
    try {
        SerializationUtils.serialize(simpleAnalysisResult, out);
    } catch (final SerializationException e) {
        logger.error("Error serializing analysis result: " + analysisResult, e);
        throw e;
    } finally {
        FileHelper.safeClose(out);
    }
}

From source file:org.datacleaner.result.save.AnalysisResultSaveHandler.java

/**
 * Gets a map of unsafe result elements, ie. elements that cannot be saved
 * because serialization fails.//  www  .  j av a  2 s.com
 *
 * @return
 */
public Map<ComponentJob, AnalyzerResult> getUnsafeResultElements() {
    if (_unsafeResultElements == null) {
        _unsafeResultElements = new LinkedHashMap<>();
        final Map<ComponentJob, AnalyzerResult> resultMap = _analysisResult.getResultMap();
        for (final Entry<ComponentJob, AnalyzerResult> entry : resultMap.entrySet()) {
            final AnalyzerResult analyzerResult = entry.getValue();
            try {
                SerializationUtils.serialize(analyzerResult, new NullOutputStream());
            } catch (final SerializationException e) {
                _unsafeResultElements.put(entry.getKey(), analyzerResult);
            }
        }
    }
    return _unsafeResultElements;
}

From source file:org.eclipse.dataset.metadata.Metadata.java

@Override
public IMetadata clone() {
    Metadata c = null;//from  w  w w  . j a  va2 s.  co m
    try {
        c = (Metadata) super.clone();
        if (metadata != null) {
            HashMap<String, Serializable> md = new HashMap<String, Serializable>();
            c.metadata = md;
            ByteArrayOutputStream os = new ByteArrayOutputStream(512);
            for (String k : metadata.keySet()) {
                Serializable v = metadata.get(k);
                if (v != null) {
                    // TODO: Note that there is a dependency on org.apache.commons.lang that is only used for
                    // accessing SerializationUtils. In reality, the serialize call really is just:
                    // try (ObjectOutputStream out = new ObjectOutputStream(outputStream)) {
                    //    out.writeObject(v);
                    // } finally {
                    //   out.close();
                    // }
                    // and deserialize is similarly simple.
                    SerializationUtils.serialize(v, os);
                    Serializable nv = (Serializable) SerializationUtils.deserialize(os.toByteArray());
                    os.reset();
                    md.put(k, nv);
                } else {
                    md.put(k, null);
                }
            }
        }
        c.shapes = new HashMap<String, int[]>(1);
        for (Entry<String, int[]> e : shapes.entrySet()) {
            int[] s = e.getValue();
            c.shapes.put(e.getKey(), s == null ? null : s.clone());
        }
    } catch (CloneNotSupportedException e) {
        // Allowed for some objects not to be cloned.
    } catch (Throwable e) {
        if (e instanceof ClassNotFoundException) {
            // Fix to http://jira.diamond.ac.uk/browse/SCI-1644
            // Happens when cloning meta data with GridPreferences
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
    }
    return c;
}

From source file:org.eclipse.dawnsci.analysis.api.metadata.Metadata.java

@Override
public IMetadata clone() {
    Metadata c = null;/*  ww w  .  j a  va2 s  .  c  o  m*/
    try {
        c = (Metadata) super.clone();
        if (metadata != null) {
            HashMap<String, Serializable> md = new HashMap<String, Serializable>();
            c.metadata = md;
            ByteArrayOutputStream os = new ByteArrayOutputStream(512);
            for (String k : metadata.keySet()) {
                Serializable v = metadata.get(k);
                if (v != null) {
                    SerializationUtils.serialize(v, os);
                    Serializable nv = (Serializable) SerializationUtils.deserialize(os.toByteArray());
                    os.reset();
                    md.put(k, nv);
                } else {
                    md.put(k, null);
                }
            }
        }
        c.shapes = new HashMap<String, int[]>(1);
        for (Entry<String, int[]> e : shapes.entrySet()) {
            int[] s = e.getValue();
            c.shapes.put(e.getKey(), s == null ? null : s.clone());
        }
    } catch (CloneNotSupportedException e) {
        // Allowed for some objects not to be cloned.
    } catch (Throwable e) {
        if (e instanceof ClassNotFoundException) {
            // Fix to http://jira.diamond.ac.uk/browse/SCI-1644
            // Happens when cloning meta data with GridPreferences
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
    }
    return c;
}

From source file:org.eobjects.analyzer.cli.SerializedAnalysisResultWriter.java

@Override
public void write(AnalysisResult result, AnalyzerBeansConfiguration configuration, Ref<Writer> writerRef,
        Ref<OutputStream> outputStreamRef) {
    final SimpleAnalysisResult simpleAnalysisResult;

    if (result instanceof SimpleAnalysisResult) {
        simpleAnalysisResult = (SimpleAnalysisResult) result;
    } else {/*w w  w. ja  va 2  s . c  om*/
        simpleAnalysisResult = new SimpleAnalysisResult(result.getResultMap());
    }

    SerializationUtils.serialize(simpleAnalysisResult, outputStreamRef.get());
}

From source file:org.eobjects.datacleaner.actions.SaveAnalysisResultActionListener.java

@Override
public void actionPerformed(ActionEvent event) {
    if (_result.get() == null) {
        WidgetUtils.showErrorMessage("Result not ready",
                "Please wait for the job to finish before saving the result", null);
        return;//from   w w  w  .j  a  v  a2 s.c om
    }

    final DCFileChooser fileChooser = new DCFileChooser(_userPreferences.getAnalysisJobDirectory());
    fileChooser.setFileFilter(FileFilters.ANALYSIS_RESULT_SER);

    final Component parent;
    if (event.getSource() instanceof Component) {
        parent = (Component) event.getSource();
    } else {
        parent = null;
    }

    final int result = fileChooser.showSaveDialog(parent);
    if (result == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();

        if (!file.getName().endsWith(FileFilters.ANALYSIS_RESULT_SER.getExtension())) {
            file = new File(file.getParentFile(),
                    file.getName() + FileFilters.ANALYSIS_RESULT_SER.getExtension());
        }

        if (file.exists()) {
            int overwrite = JOptionPane.showConfirmDialog(parent,
                    "Are you sure you want to overwrite the file '" + file.getName() + "'?",
                    "Overwrite existing file?", JOptionPane.YES_NO_OPTION);
            if (overwrite != JOptionPane.YES_OPTION) {
                return;
            }
        }

        _userPreferences.setAnalysisJobDirectory(file.getParentFile());

        final SimpleAnalysisResult analysisResult;
        if (_result instanceof SimpleAnalysisResult) {
            analysisResult = (SimpleAnalysisResult) _result;
        } else {
            analysisResult = new SimpleAnalysisResult(_result.get().getResultMap());
        }

        final OutputStream out = FileHelper.getOutputStream(file);
        try {
            SerializationUtils.serialize(analysisResult, out);
        } catch (Exception e) {
            logger.error("Error serializing analysis result: " + analysisResult, e);
            WidgetUtils.showErrorMessage("Error writing result to file", e);
        } finally {
            FileHelper.safeClose(out);
        }
    }
}

From source file:org.gatherdata.archiver.dao.vfs.internal.VfsArchiverDao.java

public GatherArchive save(GatherArchive envelopeToSave) {
    try {/*ww w. j  av  a 2s.com*/
        FileObject envelopeFile = fsManager.resolveFile(fsBase, envelopeToSave.getUid().toASCIIString());
        if (envelopeFile.exists()) {
            envelopeFile.delete();
        }
        envelopeFile.createFile();
        SerializationUtils.serialize(envelopeToSave, envelopeFile.getContent().getOutputStream());
    } catch (FileSystemException e) {
        e.printStackTrace();
    }

    return envelopeToSave;
}

From source file:org.xdi.service.ObjectSerializationService.java

public boolean saveObject(String path, Serializable obj, boolean append) {
    File file = new File(path);
    FileOutputStream fos;// ww w . j ava2s . c o m
    try {
        fos = new FileOutputStream(file, append);
    } catch (FileNotFoundException ex) {
        log.error("Faield to serialize to file: '{0}'. Error: ", path, ex);

        return false;
    }

    BufferedOutputStream bos = new BufferedOutputStream(fos);
    try {
        GZIPOutputStream gos = new GZIPOutputStream(bos);
        SerializationUtils.serialize(obj, gos);
        gos.flush();
        IOUtils.closeQuietly(gos);
    } catch (IOException ex) {
        log.error("Faield to serialize to file: '{0}'. Error: ", path, ex);
        IOUtils.closeQuietly(bos);

        return false;
    }

    return true;
}