Search in sources :

Example 31 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileNodePersistor method loadPortObject.

/**
 * @param portDir
 * @param settings
 * @param exec
 * @param fileStoreHandlerRepository
 * @return
 * @throws IOException
 * @throws InvalidSettingsException
 * @throws FileNotFoundException
 * @throws CanceledExecutionException
 */
private Optional<PortObject> loadPortObject(final ReferencedFile portDir, final NodeSettingsRO settings, final ExecutionMonitor exec, final FileStoreHandlerRepository fileStoreHandlerRepository) throws IOException, InvalidSettingsException, FileNotFoundException, CanceledExecutionException {
    exec.setMessage("Loading port object");
    final String specClass = settings.getString("port_spec_class");
    final String objectClass = loadPortObjectClassName(settings);
    PortObject object = null;
    PortObjectSpec spec = null;
    if (specClass != null) {
        Class<? extends PortObjectSpec> cl = PortTypeRegistry.getInstance().getSpecClass(specClass).orElseThrow(() -> new IOException("Invalid spec class \"" + specClass + "\""));
        ReferencedFile specDirRef = new ReferencedFile(portDir, settings.getString("port_spec_location"));
        File specFile = specDirRef.getFile();
        if (!specFile.isFile()) {
            throw new IOException("Can't read spec file " + specFile.getAbsolutePath());
        }
        try (PortObjectSpecZipInputStream in = PortUtil.getPortObjectSpecZipInputStream(new BufferedInputStream(new FileInputStream(specFile)))) {
            PortObjectSpecSerializer<?> serializer = PortTypeRegistry.getInstance().getSpecSerializer(cl).get();
            spec = serializer.loadPortObjectSpec(in);
            if (spec == null) {
                throw new IOException("Serializer \"" + serializer.getClass().getName() + "\" restored null spec ");
            }
        }
    }
    if (spec != null && objectClass != null) {
        Class<? extends PortObject> cl = PortTypeRegistry.getInstance().getObjectClass(objectClass).orElseThrow(() -> new IOException("Invalid object class \"" + objectClass + "\""));
        ReferencedFile objectFileRef = new ReferencedFile(portDir, settings.getString("port_object_location"));
        File objectFile = objectFileRef.getFile();
        if (!objectFile.isFile()) {
            throw new IOException("Can't read file " + objectFile.getAbsolutePath());
        }
        // buffering both disc I/O and the gzip stream pays off
        try (PortObjectZipInputStream in = PortUtil.getPortObjectZipInputStream(new BufferedInputStream(new FileInputStream(objectFile)))) {
            PortObjectSerializer<?> serializer = PortTypeRegistry.getInstance().getObjectSerializer(cl).get();
            object = serializer.loadPortObject(in, spec, exec);
        }
        if (object instanceof FileStorePortObject) {
            File fileStoreXML = new File(objectFile.getParent(), "filestore.xml");
            final ModelContentRO fileStoreModelContent = ModelContent.loadFromXML(new FileInputStream(fileStoreXML));
            List<FileStoreKey> fileStoreKeys = new ArrayList<FileStoreKey>();
            if (getLoadVersion().isOlderThan(LoadVersion.V2100)) {
                // only one filestore in <2.10 (bug 5227)
                FileStoreKey fileStoreKey = FileStoreKey.load(fileStoreModelContent);
                fileStoreKeys.add(fileStoreKey);
            } else {
                ModelContentRO keysContent = fileStoreModelContent.getModelContent("filestore_keys");
                for (String id : keysContent.keySet()) {
                    ModelContentRO keyContent = keysContent.getModelContent(id);
                    fileStoreKeys.add(FileStoreKey.load(keyContent));
                }
            }
            FileStoreUtil.retrieveFileStoreHandlerFrom((FileStorePortObject) object, fileStoreKeys, fileStoreHandlerRepository);
        }
    }
    return Optional.ofNullable(object);
}
Also used : PortObjectZipInputStream(org.knime.core.node.port.PortObjectZipInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) ReferencedFile(org.knime.core.internal.ReferencedFile) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreKey(org.knime.core.data.filestore.FileStoreKey) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FlowVariablePortObjectSpec(org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec) PortObjectSpecZipInputStream(org.knime.core.node.port.PortObjectSpecZipInputStream) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 32 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileNodePersistor method savePorts.

private static void savePorts(final Node node, final ReferencedFile nodeDirRef, final NodeSettingsWO settings, final Set<Integer> savedTableIDs, final ExecutionMonitor exec, final boolean saveData) throws IOException, CanceledExecutionException {
    if (node.getNrOutPorts() == 0) {
        return;
    }
    final int portCount = node.getNrOutPorts();
    NodeSettingsWO portSettings = settings.addNodeSettings("ports");
    exec.setMessage("Saving outport data");
    // starting at port 1 (ignore default flow variable output)
    for (int i = 1; i < portCount; i++) {
        String portName = PORT_FOLDER_PREFIX + i;
        ExecutionMonitor subProgress = exec.createSubProgress(1.0 / portCount);
        NodeSettingsWO singlePortSetting = portSettings.addNodeSettings(portName);
        singlePortSetting.addInt("index", i);
        PortObject object = node.getOutputObject(i);
        String portDirName;
        if (object != null && saveData) {
            portDirName = portName;
            ReferencedFile portDirRef = new ReferencedFile(nodeDirRef, portDirName);
            File portDir = portDirRef.getFile();
            subProgress.setMessage("Cleaning directory " + portDir.getAbsolutePath());
            FileUtil.deleteRecursively(portDir);
            if (!portDir.mkdir() && !portDir.isDirectory()) {
                throw new IOException("Cannot create port directory " + portDir.getAbsolutePath() + " (" + "exists: " + portDir.exists() + ", isDir: " + portDir.isDirectory() + ", " + "parent permissions: " + (portDir.getParentFile().canRead() ? "r" : "-") + (portDir.getParentFile().canWrite() ? "w" : "-") + (portDir.getParentFile().canExecute() ? "x" : "-") + ")");
            }
            if (!portDir.canWrite()) {
                throw new IOException("Cannot write to port directory " + portDir.getAbsolutePath());
            }
            savePort(node, portDir, singlePortSetting, savedTableIDs, subProgress, i, saveData);
        } else {
            portDirName = null;
        }
        singlePortSetting.addString("port_dir_location", portDirName);
        subProgress.setProgress(1.0);
    }
}
Also used : IOException(java.io.IOException) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 33 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class WorkflowEditor method dispose.

/**
 * Deregisters all listeners when the editor is disposed.
 *
 * @see org.eclipse.ui.IWorkbenchPart#dispose()
 */
@Override
public void dispose() {
    NodeLogger.getLogger(WorkflowEditor.class).debug("Disposing editor...");
    if (m_zoomWheelListener != null) {
        m_zoomWheelListener.dispose();
    }
    if (m_fileResource != null && m_manager != null) {
        // disposed is also called when workflow load fails or is canceled
        ProjectWorkflowMap.unregisterClientFrom(m_fileResource, this);
        // removes the workflow from memory
        ProjectWorkflowMap.remove(m_fileResource);
        if (isTempRemoteWorkflowEditor()) {
            // after the workflow is deleted we can delete the temp location
            final AbstractExplorerFileStore flowLoc = getFileStore(m_fileResource);
            if (flowLoc != null) {
                new Thread(() -> {
                    try {
                        m_workflowCanBeDeleted.acquire();
                        File d = flowLoc.toLocalFile();
                        if (d != null && d.exists()) {
                            FileUtils.deleteDirectory(d.getParentFile());
                        }
                    } catch (CoreException | IOException | InterruptedException e) {
                        LOGGER.warn("Error during deletion of temporary workflow location: " + e.getMessage(), e);
                    }
                }, "Delete temporary copy of " + m_origRemoteLocation).start();
            }
        }
    }
    final ReferencedFile autoSaveDirectory;
    if (m_manager != null && m_parentEditor == null && m_fileResource != null) {
        autoSaveDirectory = Wrapper.unwrapWFMOptional(m_manager).map(wfm -> wfm.getAutoSaveDirectory()).orElse(null);
    } else {
        autoSaveDirectory = null;
    }
    // remember that this editor has been closed
    m_closed = true;
    for (IEditorPart child : getSubEditors()) {
        child.getEditorSite().getPage().closeEditor(child, false);
    }
    NodeProvider.INSTANCE.removeListener(this);
    getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(this);
    if (m_parentEditor != null && m_manager != null) {
        // Store the editor settings with the metanode
        if (getWorkflowManagerUI().isDirty()) {
            // doesn't persist settings to disk
            saveEditorSettingsToWorkflowManager();
        }
        // bug fix 2051: Possible memory leak related to sub-flow editor.
        // metanode editors were still referenced by the EditorHistory
        IWorkbench workbench = PlatformUI.getWorkbench();
        if (workbench instanceof Workbench) {
            EditorHistory hist = ((Workbench) workbench).getEditorHistory();
            WorkflowManagerInput wfmInput = new WorkflowManagerInput(m_manager, m_parentEditor);
            hist.remove(wfmInput);
        }
    }
    if (m_autoSaveJob != null) {
        m_autoSaveJob.cancel();
        m_autoSaveJob = null;
    }
    // unregisters wfm listeners
    setWorkflowManager(null);
    if (autoSaveDirectory != null) {
        KNIMEConstants.GLOBAL_THREAD_POOL.enqueue(new Runnable() {

            @Override
            public void run() {
                LOGGER.debugWithFormat("Deleting auto-saved copy (\"%s\")...", autoSaveDirectory);
                try {
                    FileUtils.deleteDirectory(autoSaveDirectory.getFile());
                } catch (IOException e) {
                    LOGGER.error(String.format("Failed to delete auto-saved copy of workflow (folder \"%s\"): %s", autoSaveDirectory, e.getMessage()), e);
                }
            }
        });
    }
    getCommandStack().removeCommandStackListener(this);
    IPreferenceStore prefStore = KNIMEUIPlugin.getDefault().getPreferenceStore();
    prefStore.removePropertyChangeListener(this);
    super.dispose();
}
Also used : Workbench(org.eclipse.ui.internal.Workbench) IWorkbench(org.eclipse.ui.IWorkbench) IEditorPart(org.eclipse.ui.IEditorPart) IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) IWorkbench(org.eclipse.ui.IWorkbench) EditorHistory(org.eclipse.ui.internal.EditorHistory) AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 34 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class AutosaveRunnable method save.

/**
 * {@inheritDoc}
 */
@Override
protected void save(final WorkflowManager wfm, final ExecutionMonitor exec) throws IOException, CanceledExecutionException, LockFailedException {
    wfm.save(m_autosaveDir, m_saveHelper, exec);
    ReferencedFile oldWorkflowPathRef = wfm.getWorkingDir();
    File oldWorkflowPath = oldWorkflowPathRef == null ? null : oldWorkflowPathRef.getFile();
    if (oldWorkflowPath != null) {
        File reportDesignFile = new File(oldWorkflowPath, ReportingConstants.KNIME_REPORT_FILE);
        File reportConfigFile = new File(oldWorkflowPath, ReportingConstants.KNIME_REPORT_CONFIG_FILE);
        if (reportDesignFile.isFile()) {
            FileUtil.copy(reportDesignFile, new File(m_autosaveDir, reportDesignFile.getName()));
        }
        if (reportConfigFile.isFile()) {
            FileUtil.copy(reportConfigFile, new File(m_autosaveDir, reportConfigFile.getName()));
        }
    }
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 35 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class NativeNodeContainer method pushNodeDropDirURLsOntoStack.

/**
 * Support old-style drop dir mechanism - replaced since 2.8 with relative mountpoints,
 * e.g. knime://knime.workflow
 */
private void pushNodeDropDirURLsOntoStack(final FlowObjectStack st) {
    ReferencedFile refDir = getNodeContainerDirectory();
    ReferencedFile dropFolder = refDir == null ? null : new ReferencedFile(refDir, DROP_DIR_NAME);
    if (dropFolder == null) {
        return;
    }
    dropFolder.lock();
    try {
        File directory = dropFolder.getFile();
        if (!directory.exists()) {
            return;
        }
        String[] files = directory.list();
        if (files != null) {
            StringBuilder debug = new StringBuilder("Found " + files.length + " node local file(s) to " + getNameWithID() + ": ");
            debug.append(Arrays.toString(Arrays.copyOf(files, Math.max(3, files.length))));
            for (String f : files) {
                File child = new File(directory, f);
                try {
                    st.push(new FlowVariable(Scope.Local.getPrefix() + "(drop) " + f, // child.getAbsolutePath(), Scope.Local));
                    child.toURI().toURL().toString(), Scope.Local));
                // } catch (Exception mue) {
                } catch (MalformedURLException mue) {
                    LOGGER.warn("Unable to process drop file", mue);
                }
            }
        }
    } finally {
        dropFolder.unlock();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

ReferencedFile (org.knime.core.internal.ReferencedFile)46 File (java.io.File)29 IOException (java.io.IOException)22 BufferedInputStream (java.io.BufferedInputStream)11 FileInputStream (java.io.FileInputStream)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 PortObject (org.knime.core.node.port.PortObject)8 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)8 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)7 InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)7 HashMap (java.util.HashMap)6 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)6 FileOutputStream (java.io.FileOutputStream)5 Map (java.util.Map)5 ContainerTable (org.knime.core.data.container.ContainerTable)5 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)5 FlowVariablePortObjectSpec (org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec)5 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4