use of org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException in project stanbol by apache.
the class EntityhubDereferenceEngine method activate.
@Activate
@SuppressWarnings("unchecked")
protected void activate(ComponentContext ctx) throws ConfigurationException {
Dictionary<String, Object> properties = ctx.getProperties();
bundleContext = ctx.getBundleContext();
log.info("> activate {}", getClass().getSimpleName());
// get the metadata later set to the enhancement engine
DereferenceEngineConfig engineConfig = new DereferenceEngineConfig(properties, prefixService);
log.debug(" - engineName: {}", engineConfig.getEngineName());
// parse the Entityhub Site used for dereferencing
Object value = properties.get(SITE_ID);
// init the EntitySource
if (value == null) {
// all referenced sites
siteName = "*";
} else {
siteName = value.toString();
}
if (siteName.isEmpty()) {
siteName = "*";
}
log.debug(" - siteName: {}", siteName);
final boolean sharedPoolState;
value = properties.get(SHARED_THREAD_POOL_STATE);
if (value instanceof Boolean) {
sharedPoolState = ((Boolean) value).booleanValue();
} else if (value != null && !StringUtils.isBlank(value.toString())) {
sharedPoolState = Boolean.parseBoolean(value.toString());
} else {
sharedPoolState = DEFAULT_SHARED_THREAD_POOL_STATE;
}
final ExecutorServiceProvider esProvider;
log.debug(" - shared thread pool state: {}", sharedPoolState);
if (sharedPoolState) {
esProvider = new SharedExecutorServiceProvider(ctx.getBundleContext());
} else {
// we need to create our own ExecutorService
value = properties.get(THREAD_POOL_SIZE);
if (value instanceof Number) {
this.threadPoolSize = ((Number) value).intValue();
} else if (value != null) {
try {
this.threadPoolSize = Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
throw new ConfigurationException(THREAD_POOL_SIZE, "Value '" + value + "'(type: " + value.getClass().getName() + ") can not be parsed " + "as Integer");
}
} else {
this.threadPoolSize = DEFAULT_THREAD_POOL_SIZE;
}
if (threadPoolSize > 0) {
String namePattern = getClass().getSimpleName() + "-" + engineConfig.getEngineName() + "-thread-%s";
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(namePattern).setDaemon(true).build();
log.debug(" - create Threadpool(namePattern='{}' | size='{}')", namePattern, threadPoolSize);
executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
} else {
log.debug(" - no thread pool configured (poolSize: {})", threadPoolSize);
executorService = null;
}
esProvider = new StaticExecutorServiceProvider(executorService);
}
// init the tracking entity searcher
trackedServiceCount = 0;
if (Entityhub.ENTITYHUB_IDS.contains(siteName.toLowerCase())) {
log.info(" ... init Entityhub dereferencer");
entityDereferencer = new EntityhubDereferencer(bundleContext, this, esProvider);
} else if (siteName.equals("*")) {
log.info(" ... init dereferencer for all referenced sites");
entityDereferencer = new SitesDereferencer(bundleContext, this, esProvider);
} else {
log.info(" ... init dereferencer for referenced site {}", siteName);
entityDereferencer = new SiteDereferencer(bundleContext, siteName, this, esProvider);
}
// set the namespace prefix service to the dereferencer
entityDereferencer.setNsPrefixService(prefixService);
// now parse dereference field config
entityDereferencer.setDereferencedFields(engineConfig.getDereferenceFields());
entityDereferencer.setLdPath(engineConfig.getLdPathProgram());
entityDereferenceEngine = new EntityDereferenceEngine(entityDereferencer, engineConfig, new // we want to use our own DereferenceContext impl
DereferenceContextFactory() {
@Override
public DereferenceContext createContext(EntityDereferenceEngine engine, Map<String, Object> enhancementProperties) throws DereferenceConfigurationException {
return new EntityhubDereferenceContext(engine, enhancementProperties);
}
});
// NOTE: registration of this instance as OSGI service is done as soon as the
// entityhub service backing the entityDereferencer is available.
// finally start tracking
entityDereferencer.open();
}
use of org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException in project stanbol by apache.
the class EntityhubDereferenceContext method initLdPath.
protected void initLdPath(String program) throws DereferenceConfigurationException {
TrackingDereferencerBase<?> dereferencer = getEntityhubDereferencer();
ValueFactory valueFactory = dereferencer.getValueFactory();
Program<Object> ldpathProgram;
if (!StringUtils.isBlank(program)) {
@SuppressWarnings("rawtypes") RDFBackend<Object> parseBackend = new ParseBackend<Object>(valueFactory);
EntityhubLDPath parseLdPath = new EntityhubLDPath(parseBackend, valueFactory);
try {
ldpathProgram = parseLdPath.parseProgram(new StringReader(program));
} catch (LDPathParseException e) {
log.error("Unable to parse Context LDPath pogram: \n {}", program);
throw new DereferenceConfigurationException("Unable to parse context LDPath program !", e, dereferencer.getClass(), DEREFERENCE_ENTITIES_LDPATH);
}
// finally validate if all mappings of the program do use a URI as key
// also store used fieldNames as we need them later
Set<String> contextFields = new HashSet<String>();
for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : ldpathProgram.getFields()) {
try {
new URI(mapping.getFieldName());
contextFields.add(mapping.getFieldName());
} catch (URISyntaxException e) {
throw new DereferenceConfigurationException("Parsed LDPath MUST use valid URIs as field names (invalid field name: '" + mapping.getFieldName() + "' | selector: '" + mapping.getSelector().getPathExpression(parseBackend) + "')!", dereferencer.getClass(), DereferenceConstants.DEREFERENCE_ENTITIES_LDPATH);
}
}
// append the mappings configured for the engine
if (dereferencer.getLdPathProgram() != null) {
for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : dereferencer.getLdPathProgram().getFields()) {
if (!contextFields.contains(mapping.getFieldName())) {
ldpathProgram.addMapping(mapping);
}
// else ignore mappings for fields specified in the context
}
}
} else {
// no context specific - use the one of the config
ldpathProgram = dereferencer.getLdPathProgram();
}
if (ldpathProgram != null && !ldpathProgram.getFields().isEmpty()) {
this.ldpathProgram = ldpathProgram;
} else {
this.ldpathProgram = null;
}
}
Aggregations