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