com.documents4j.conversion.ExternalConverterDiscovery Maven / Gradle / Ivy
package com.documents4j.conversion;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkState;
enum ExternalConverterDiscovery {
MICROSOFT_WORD("com.documents4j.conversion.msoffice.MicrosoftWordBridge"),
MICROSOFT_EXCEL("com.documents4j.conversion.msoffice.MicrosoftExcelBridge");
private final String className;
private ExternalConverterDiscovery(String className) {
this.className = className;
}
private static IExternalConverter make(Class extends IExternalConverter> externalConverterClass,
File baseFolder,
long processTimeout,
TimeUnit timeUnit) {
try {
return externalConverterClass
.getConstructor(File.class, long.class, TimeUnit.class)
.newInstance(baseFolder, processTimeout, timeUnit);
} catch (Exception e) {
throw new IllegalStateException(String.format("%s could not be created by a (File, long, TimeUnit) constructor", externalConverterClass), e);
}
}
private static Set makeAll(Set> externalConverterClasses,
File baseFolder,
long processTimeout,
TimeUnit timeUnit) {
Set externalConverters = new HashSet();
for (Class extends IExternalConverter> externalConverterClass : externalConverterClasses) {
externalConverters.add(make(externalConverterClass, baseFolder, processTimeout, timeUnit));
}
return externalConverters;
}
private static Set> discover(Map, Boolean> externalConverterRegistration) {
Set> discovered = new HashSet>();
Map autoDetectNameMap = makeAutoDetectNameMap();
for (Map.Entry, Boolean> registration : externalConverterRegistration.entrySet()) {
if (registration.getValue()) {
discovered.add(registration.getKey());
} else {
autoDetectNameMap.remove(registration.getKey().getName());
}
}
for (ExternalConverterDiscovery autoDetect : autoDetectNameMap.values()) {
Class extends IExternalConverter> externalConverterClass = autoDetect.tryFindClass();
if (externalConverterClass != null) {
discovered.add(externalConverterClass);
}
}
return discovered;
}
private static Set> validate(Set> externalConverterClasses) {
if (externalConverterClasses.size() == 0) {
throw new IllegalStateException("The application was started without any registered or class-path discovered converters.");
}
return externalConverterClasses;
}
private static Map makeAutoDetectNameMap() {
Map autoDetectNames = new HashMap();
for (ExternalConverterDiscovery autoDetect : ExternalConverterDiscovery.values()) {
autoDetectNames.put(autoDetect.getClassName(), autoDetect);
}
return autoDetectNames;
}
public static Set loadConfiguration(File baseFolder,
long processTimeout,
TimeUnit timeUnit,
Map, Boolean> externalConverterRegistration) {
return makeAll(validate(discover(externalConverterRegistration)), baseFolder, processTimeout, timeUnit);
}
protected String getClassName() {
return className;
}
@SuppressWarnings("unchecked")
protected Class extends IExternalConverter> tryFindClass() {
try {
Class> foundClass = Class.forName(getClassName(), false, getClass().getClassLoader());
checkState(IExternalConverter.class.isAssignableFrom(foundClass), "Illegal auto discovery class implementation found");
return (Class extends IExternalConverter>) foundClass;
} catch (ClassNotFoundException e) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy