下面列出了org.apache.http.impl.auth.DigestScheme#org.lastaflute.di.core.SingletonLaContainer 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected boolean isContentUpdated(final CrawlerClient client, final UrlQueue<?> urlQueue) {
final RiverConfigManager riverConfigManager = SingletonLaContainer.getComponent(RiverConfigManager.class);
final RiverConfig riverConfig = riverConfigManager.get(crawlerContext.getSessionId());
if (riverConfig.isIncremental()) {
final EsClient esClient = SingletonLaContainer.getComponent(EsClient.class);
try {
final SearchResponse response = esClient.prepareSearch(riverConfig.getIndex()).setTypes(riverConfig.getType())
.setQuery(QueryBuilders.termQuery("url", urlQueue.getUrl())).addField("lastModified")
.addSort("lastModified", SortOrder.DESC).execute().actionGet();
final SearchHits hits = response.getHits();
if (hits.getTotalHits() > 0) {
final SearchHitField lastModifiedField = hits.getAt(0).getFields().get("lastModified");
if (lastModifiedField != null) {
final Date lastModified = ConversionUtil.convert(lastModifiedField.getValue(), Date.class);
if (lastModified != null) {
urlQueue.setLastModified(lastModified.getTime());
}
}
}
} catch (final Exception e) {
logger.debug("Failed to retrieve lastModified.", e);
}
}
return super.isContentUpdated(client, urlQueue);
}
public static <T> T getComponent(final Class<T> clazz) {
try {
return SingletonLaContainer.getComponent(clazz);
} catch (final NullPointerException e) {
if (logger.isDebugEnabled()) {
throw new ContainerNotAvailableException(clazz.getCanonicalName(), e);
} else {
throw new ContainerNotAvailableException(clazz.getCanonicalName());
}
}
}
public static boolean available() {
try {
return SingletonLaContainer.getComponent(SYSTEM_HELPER) != null;
} catch (final Exception e) {
// ignore
}
return false;
}
public static Object execute(final Map<String, Object> scriptSettings, final String target, final Consumer<Map<String, Object>> vars) {
final String script = SettingsUtils.get(scriptSettings, target);
final String lang = SettingsUtils.get(scriptSettings, "lang", WebRiverConstants.DEFAULT_SCRIPT_LANG);
final String scriptTypeValue = SettingsUtils.get(scriptSettings, "script_type", "inline");
ScriptType scriptType;
if (ScriptType.FILE.toString().equalsIgnoreCase(scriptTypeValue)) {
scriptType = ScriptType.FILE;
} else if (ScriptType.INDEXED.toString().equalsIgnoreCase(scriptTypeValue)) {
scriptType = ScriptType.INDEXED;
} else {
scriptType = ScriptType.INLINE;
}
if (StringUtil.isNotBlank(script)) {
final Map<String, Object> localVars = new HashMap<String, Object>();
vars.accept(localVars);
try {
final ScriptService scriptService = SingletonLaContainer.getComponent(ScriptService.class);
final Object result = scriptService.execute(lang, script, scriptType, localVars);
if (logger.isDebugEnabled()) {
logger.debug("[{}] \"{}\" => {}", target, script, result);
}
return result;
} catch (final Exception e) {
logger.warn("Failed to execute script: " + script, e);
}
}
return null;
}
private Object executeScript(final String lang, final String script, final String scriptTypeValue, final Map<String, Object> vars) {
ScriptType scriptType;
if (ScriptType.FILE.toString().equalsIgnoreCase(scriptTypeValue)) {
scriptType = ScriptType.FILE;
} else if (ScriptType.INDEXED.toString().equalsIgnoreCase(scriptTypeValue)) {
scriptType = ScriptType.INDEXED;
} else {
scriptType = ScriptType.INLINE;
}
vars.put("logger", logger);
final ScriptService scriptService = SingletonLaContainer.getComponent(ScriptService.class);
return scriptService.execute(lang, script, scriptType, vars);
}
@PostConstruct
public void init() {
esClient = SingletonLaContainer.getComponent(EsClient.class);
riverConfigManager = SingletonLaContainer.getComponent(RiverConfigManager.class);
}
/**
* @param componentType The component type to find. (NotNull)
* @return The found component. (NotNull)
* @throws ComponentNotFoundException When the component is not found by the type.
* @throws TooManyRegistrationComponentException When the component key is related to plural components.
* @throws CyclicReferenceComponentException When the components refers each other.
*/
public static <COMPONENT> COMPONENT getComponent(Class<COMPONENT> componentType) { // most frequently used
return (COMPONENT) SingletonLaContainer.getComponent(componentType);
}
/**
* @param componentName The component name to find. (NotNull)
* @return The found component. (NotNull)
* @throws ComponentNotFoundException When the component is not found by the type.
* @throws TooManyRegistrationComponentException When the component key is related to plural components.
* @throws CyclicReferenceComponentException When the components refers each other.
*/
public static <COMPONENT> COMPONENT pickupComponentByName(String componentName) {
final COMPONENT component = SingletonLaContainer.getComponent(componentName); // variable for generic
return component;
}