Search in sources :

Example 1 with DataCellFactoryMethod

use of org.knime.core.data.convert.DataCellFactoryMethod in project knime-core by knime.

the class JavaToDataCellConverterRegistry method parseAnnotations.

private void parseAnnotations() {
    final Collection<DataType> availableDataTypes = DataTypeRegistry.getInstance().availableDataTypes();
    for (final DataType dataType : availableDataTypes) {
        final Optional<DataCellFactory> cellFactory = dataType.getCellFactory(null);
        if (cellFactory.isPresent()) {
            final Class<? extends DataCellFactory> cellFactoryClass = cellFactory.get().getClass();
            for (final Pair<Method, DataCellFactoryMethod> pair : ClassUtil.getMethodsWithAnnotation(cellFactoryClass, DataCellFactoryMethod.class)) {
                final Method method = pair.getFirst();
                try {
                    final JavaToDataCellConverterFactory<?> factory = new FactoryMethodToDataCellConverterFactory<>(method, ClassUtil.ensureObjectType(method.getParameterTypes()[0]), dataType, pair.getSecond().name());
                    // Check name of factory
                    if (!validateFactoryName(factory)) {
                        LOGGER.warn("JavaToDataCellFactory name \"" + factory.getName() + "\" of factory with id \"" + factory.getIdentifier() + "\" does not follow naming convention (see DataCellFactoryMethod#name()).");
                        LOGGER.warn("Factory will not be registered.");
                        continue;
                    }
                    register(factory);
                    LOGGER.debug("Registered JavaToDataCellConverterFactory from DataCellFactoryMethod annotation for: " + method.getParameterTypes()[0].getName() + " to " + dataType.getName());
                } catch (IncompleteAnnotationException e) {
                    LOGGER.coding("Incomplete DataCellFactoryMethod annotation for " + cellFactoryClass.getName() + "." + method.getName() + ". Will not register.", e);
                } catch (NoSuchMethodException | SecurityException ex) {
                    LOGGER.error("Could not access default constructor or constructor with parameter 'ExecutionContext' of " + "class '" + cellFactoryClass.getName() + "'. Converter will not be registered.", ex);
                }
            }
        }
    }
}
Also used : DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod) DataCellFactory(org.knime.core.data.DataCellFactory) DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod) Method(java.lang.reflect.Method) DataType(org.knime.core.data.DataType) IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException)

Example 2 with DataCellFactoryMethod

use of org.knime.core.data.convert.DataCellFactoryMethod in project knime-core by knime.

the class BinaryObjectCellFactory method create.

/**
 * Creates cell given by reading from an input stream. The stream will be closed by this method.
 * @param input To read from.
 * @return A cell with a copy of the byte content.
 * @throws IOException If that fails (stream not readable, file store not writable, close problems, ...)
 * @throws NullPointerException If argument is null.
 */
@DataCellFactoryMethod(name = "InputStream")
public DataCell create(final InputStream input) throws IOException {
    String uniqueFileName = "knime-binary-copy-";
    String suffix = ".bin";
    MessageDigest md5MessageDigest = newMD5Digest();
    DeferredFileOutputStream outStream = new DeferredFileOutputStream(MEMORY_LIMIT, uniqueFileName, suffix, TMP_DIR_FOLDER);
    DigestInputStream digestInputStream = new DigestInputStream(input, md5MessageDigest);
    IOUtils.copy(digestInputStream, outStream);
    digestInputStream.close();
    outStream.close();
    byte[] md5sum = md5MessageDigest.digest();
    if (outStream.isInMemory()) {
        return new BinaryObjectDataCell(outStream.getData(), md5sum);
    } else {
        FileStore fs;
        synchronized (this) {
            String name = "binaryObject-" + m_fileNameIndex++;
            fs = m_fileStoreFactory.createFileStore(name);
        }
        File f = outStream.getFile();
        assert f.exists() : "File " + f.getAbsolutePath() + " not created by file output stream";
        FileUtils.moveFile(f, fs.getFile());
        return new BinaryObjectFileStoreDataCell(fs, md5sum);
    }
}
Also used : FileStore(org.knime.core.data.filestore.FileStore) DigestInputStream(java.security.DigestInputStream) MessageDigest(java.security.MessageDigest) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) File(java.io.File) DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod)

Aggregations

DataCellFactoryMethod (org.knime.core.data.convert.DataCellFactoryMethod)2 File (java.io.File)1 IncompleteAnnotationException (java.lang.annotation.IncompleteAnnotationException)1 Method (java.lang.reflect.Method)1 DigestInputStream (java.security.DigestInputStream)1 MessageDigest (java.security.MessageDigest)1 DeferredFileOutputStream (org.apache.commons.io.output.DeferredFileOutputStream)1 DataCellFactory (org.knime.core.data.DataCellFactory)1 DataType (org.knime.core.data.DataType)1 FileStore (org.knime.core.data.filestore.FileStore)1