Search in sources :

Example 1 with PortObjectSpecSerializer

use of org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer in project knime-core by knime.

the class PortUtil method writeObjectToStream.

/**
 * Write the given port object into the given output stream. The output stream does not need to be buffered and is
 * not closed after calling this method.
 *
 * @param po any port object
 * @param output any output stream, does not need to be buffered
 * @param exec execution context for reporting progress and checking for cancelation
 * @throws IOException if an I/O error occurs while serializing the port object
 * @throws CanceledExecutionException if the user canceled the operation
 */
public static void writeObjectToStream(final PortObject po, final OutputStream output, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    final boolean originalOutputIsBuffered = ((output instanceof BufferedOutputStream) || (output instanceof ByteArrayOutputStream));
    OutputStream os = originalOutputIsBuffered ? output : new BufferedOutputStream(output);
    final ZipOutputStream zipOut = new ZipOutputStream(os);
    PortObjectSpec spec = po.getSpec();
    zipOut.putNextEntry(new ZipEntry("content.xml"));
    ModelContent toc = new ModelContent("content");
    toc.addInt("version", 1);
    toc.addString("port_spec_class", spec.getClass().getName());
    toc.addString("port_object_class", po.getClass().getName());
    NotInWorkflowWriteFileStoreHandler fileStoreHandler = null;
    if (po instanceof FileStorePortObject) {
        fileStoreHandler = NotInWorkflowWriteFileStoreHandler.create();
        ModelContentWO fileStoreModelContent = toc.addModelContent("filestores");
        fileStoreModelContent.addString("handlerUUID", fileStoreHandler.getStoreUUID().toString());
        final FileStorePortObject fileStorePO = (FileStorePortObject) po;
        FileStoreUtil.invokeFlush(fileStorePO);
        List<FileStore> fileStores = FileStoreUtil.getFileStores(fileStorePO);
        ModelContentWO fileStoreKeysModel = fileStoreModelContent.addModelContent("port_file_store_keys");
        for (int i = 0; i < fileStores.size(); i++) {
            FileStoreKey key = fileStoreHandler.translateToLocal(fileStores.get(i), fileStorePO);
            key.save(fileStoreKeysModel.addModelContent("filestore_key_" + i));
        }
    }
    toc.saveToXML(new NonClosableOutputStream.Zip(zipOut));
    zipOut.putNextEntry(new ZipEntry("objectSpec.file"));
    try (PortObjectSpecZipOutputStream specOut = getPortObjectSpecZipOutputStream(new NonClosableOutputStream.Zip(zipOut))) {
        PortObjectSpecSerializer specSer = PortTypeRegistry.getInstance().getSpecSerializer(spec.getClass()).get();
        specSer.savePortObjectSpec(spec, specOut);
    }
    // 'close' will propagate as closeEntry
    zipOut.putNextEntry(new ZipEntry("object.file"));
    try (PortObjectZipOutputStream objOut = getPortObjectZipOutputStream(new NonClosableOutputStream.Zip(zipOut))) {
        PortObjectSerializer objSer = PortTypeRegistry.getInstance().getObjectSerializer(po.getClass()).get();
        objSer.savePortObject(po, objOut, exec);
    }
    if (fileStoreHandler != null && fileStoreHandler.hasCopiedFileStores()) {
        zipOut.putNextEntry(new ZipEntry("filestores/"));
        zipOut.closeEntry();
        File baseDir = fileStoreHandler.getBaseDir();
        FileUtil.zipDir(zipOut, Arrays.asList(baseDir.listFiles()), "filestores/", FileUtil.ZIP_INCLUDEALL_FILTER, exec.createSubProgress(0.5));
    }
    zipOut.finish();
    if (!originalOutputIsBuffered) {
        os.flush();
    }
}
Also used : NotInWorkflowWriteFileStoreHandler(org.knime.core.data.filestore.internal.NotInWorkflowWriteFileStoreHandler) ModelContent(org.knime.core.node.ModelContent) ModelContentWO(org.knime.core.node.ModelContentWO) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NonClosableOutputStream(org.knime.core.data.util.NonClosableOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FileStore(org.knime.core.data.filestore.FileStore) FileStoreKey(org.knime.core.data.filestore.FileStoreKey) ZipOutputStream(java.util.zip.ZipOutputStream) PortObjectSpecSerializer(org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) NonClosableOutputStream(org.knime.core.data.util.NonClosableOutputStream) PortObjectSerializer(org.knime.core.node.port.PortObject.PortObjectSerializer)

Example 2 with PortObjectSpecSerializer

use of org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer in project knime-core by knime.

the class PortTypeRegistry method scanExtensionPointForSpecSerializer.

private <T extends PortObjectSpec> Optional<PortObjectSpecSerializer<T>> scanExtensionPointForSpecSerializer(final String specClassName) {
    // not found => scan extension point
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint point = registry.getExtensionPoint(EXT_POINT_ID);
    Optional<IConfigurationElement> o = Stream.of(point.getExtensions()).flatMap(ext -> Stream.of(ext.getConfigurationElements())).filter(cfe -> cfe.getAttribute("specClass").equals(specClassName)).findFirst();
    if (o.isPresent()) {
        IConfigurationElement configElement = o.get();
        return createSpecSerializer(configElement);
    } else {
        return Optional.empty();
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SerializerMethodLoader(org.knime.core.internal.SerializerMethodLoader) HashMap(java.util.HashMap) CoreException(org.eclipse.core.runtime.CoreException) PortObjectSpecSerializer(org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer) BufferedDataTable(org.knime.core.node.BufferedDataTable) Stream(java.util.stream.Stream) PortObjectSerializer(org.knime.core.node.port.PortObject.PortObjectSerializer) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry) NodeLogger(org.knime.core.node.NodeLogger) Map(java.util.Map) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) Optional(java.util.Optional) Platform(org.eclipse.core.runtime.Platform) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry)

Example 3 with PortObjectSpecSerializer

use of org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer in project knime-core by knime.

the class FileNodePersistor method savePortObject.

private static void savePortObject(final PortObjectSpec spec, final PortObject object, final File portDir, final NodeSettingsWO settings, final ExecutionMonitor exec) throws IOException, FileNotFoundException, CanceledExecutionException {
    settings.addString("port_spec_class", spec.getClass().getName());
    settings.addString("port_object_class", object.getClass().getName());
    String specDirName = "spec";
    String specFileName = "spec.zip";
    String specPath = specDirName + "/" + specFileName;
    File specDir = createDirectory(new File(portDir, specDirName));
    File specFile = new File(specDir, specFileName);
    settings.addString("port_spec_location", specPath);
    try (PortObjectSpecZipOutputStream out = PortUtil.getPortObjectSpecZipOutputStream(new BufferedOutputStream(new FileOutputStream(specFile)))) {
        PortObjectSpecSerializer serializer = PortTypeRegistry.getInstance().getSpecSerializer(spec.getClass()).get();
        serializer.savePortObjectSpec(spec, out);
    }
    String objectDirName = null;
    objectDirName = "object";
    File objectDir = createDirectory(new File(portDir, objectDirName));
    String objectPath;
    String objectFileName = "portobject.zip";
    objectPath = objectDirName + "/" + objectFileName;
    settings.addString("port_object_location", objectPath);
    File file = new File(objectDir, objectFileName);
    try (PortObjectZipOutputStream out = PortUtil.getPortObjectZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
        PortObjectSerializer serializer = PortTypeRegistry.getInstance().getObjectSerializer(object.getClass()).get();
        serializer.savePortObject(object, out, exec);
        if (object instanceof FileStorePortObject) {
            List<FileStoreKey> fileStoreKeys = FileStoreUtil.getFileStores((FileStorePortObject) object).stream().map(FileStoreUtil::getFileStoreKey).collect(Collectors.toList());
            File fileStoreXML = new File(objectDir, "filestore.xml");
            final ModelContent fileStoreModelContent = new ModelContent("filestore");
            ModelContentWO keysContent = fileStoreModelContent.addModelContent("filestore_keys");
            for (int i = 0; i < fileStoreKeys.size(); i++) {
                FileStoreKey key = fileStoreKeys.get(i);
                ModelContentWO keyContent = keysContent.addModelContent("filestore_key_" + i);
                key.save(keyContent);
            }
            fileStoreModelContent.saveToXML(new FileOutputStream(fileStoreXML));
        }
    }
}
Also used : PortObjectSpecZipOutputStream(org.knime.core.node.port.PortObjectSpecZipOutputStream) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) PortObjectZipOutputStream(org.knime.core.node.port.PortObjectZipOutputStream) FileStoreKey(org.knime.core.data.filestore.FileStoreKey) FileOutputStream(java.io.FileOutputStream) PortObjectSpecSerializer(org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) PortObjectSerializer(org.knime.core.node.port.PortObject.PortObjectSerializer)

Aggregations

PortObjectSerializer (org.knime.core.node.port.PortObject.PortObjectSerializer)3 PortObjectSpecSerializer (org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer)3 BufferedOutputStream (java.io.BufferedOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 FileStoreKey (org.knime.core.data.filestore.FileStoreKey)2 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Stream (java.util.stream.Stream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 CoreException (org.eclipse.core.runtime.CoreException)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)1