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();
}
}
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();
}
}
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));
}
}
}
Aggregations