Skip to content

Commit

Permalink
fix #2579 add AbstractConfigHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
marevol committed Jun 23, 2021
1 parent 6375002 commit 92a6184
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 54 deletions.
40 changes: 40 additions & 0 deletions src/main/java/org/codelibs/fess/helper/AbstractConfigHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2012-2021 CodeLibs Project and the Others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.codelibs.fess.helper;

import org.codelibs.core.concurrent.CommonPoolUtil;
import org.codelibs.core.lang.ThreadUtil;

public abstract class AbstractConfigHelper {

protected long reloadInterval = 1000L;

public void update() {
CommonPoolUtil.execute(() -> load());
}

protected void waitForNext() {
if (reloadInterval > 0) {
ThreadUtil.sleep(reloadInterval);
}
}

public abstract int load();

public void setReloadInterval(final long reloadInterval) {
this.reloadInterval = reloadInterval;
}
}
28 changes: 6 additions & 22 deletions src/main/java/org/codelibs/fess/helper/KeyMatchHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codelibs.core.concurrent.CommonPoolUtil;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.core.lang.ThreadUtil;
import org.codelibs.core.misc.Tuple3;
import org.codelibs.fesen.index.query.BoolQueryBuilder;
import org.codelibs.fesen.index.query.QueryBuilder;
Expand All @@ -46,24 +44,18 @@
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.DocumentUtil;

public class KeyMatchHelper {
public class KeyMatchHelper extends AbstractConfigHelper {
private static final Logger logger = LogManager.getLogger(KeyMatchHelper.class);

protected volatile Map<String, Map<String, List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>>>> keyMatchQueryMap =
Collections.emptyMap();

protected long reloadInterval = 1000L;

@PostConstruct
public void init() {
if (logger.isDebugEnabled()) {
logger.debug("Initialize {}", this.getClass().getSimpleName());
}
reload(0);
}

public void update() {
CommonPoolUtil.execute(() -> reload(reloadInterval));
load();
}

public List<KeyMatch> getAvailableKeyMatchList() {
Expand All @@ -73,7 +65,8 @@ public List<KeyMatch> getAvailableKeyMatchList() {
});
}

protected void reload(final long interval) {
@Override
public int load() {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final Map<String, Map<String, List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>>>> keyMatchQueryMap = new HashMap<>();
getAvailableKeyMatchList().stream().forEach(keyMatch -> {
Expand Down Expand Up @@ -116,14 +109,13 @@ protected void reload(final long interval) {
logger.debug("No KeyMatch boost docs");
}

if (interval > 0) {
ThreadUtil.sleep(interval);
}
waitForNext();
} catch (final Exception e) {
logger.warn("Cannot load {}", keyMatch, e);
}
});
this.keyMatchQueryMap = keyMatchQueryMap;
return keyMatchQueryMap.size();
}

protected List<Map<String, Object>> getDocumentList(final KeyMatch keyMatch) {
Expand All @@ -136,14 +128,6 @@ protected List<Map<String, Object>> getDocumentList(final KeyMatch keyMatch) {
.responseFields(new String[] { fessConfig.getIndexFieldDocId() }).build());
}

public long getReloadInterval() {
return reloadInterval;
}

public void setReloadInterval(final long reloadInterval) {
this.reloadInterval = reloadInterval;
}

protected Map<String, List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>>> getQueryMap(final String key) {
final Map<String, List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>>> map = keyMatchQueryMap.get(key);
if (map != null) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/codelibs/fess/helper/LabelTypeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.codelibs.fess.es.config.exentity.LabelType;
import org.codelibs.fess.util.ComponentUtil;

public class LabelTypeHelper {
public class LabelTypeHelper extends AbstractConfigHelper {
private static final Logger logger = LogManager.getLogger(LabelTypeHelper.class);

protected volatile List<LabelTypeItem> labelTypeItemList;
Expand All @@ -51,10 +51,11 @@ public void init() {
if (logger.isDebugEnabled()) {
logger.debug("Initialize {}", this.getClass().getSimpleName());
}
update();
load();
}

public int update() {
@Override
public int load() {
final List<LabelType> labelTypeList = ComponentUtil.getComponent(LabelTypeService.class).getLabelTypeList();
buildLabelTypeItems(labelTypeList);
buildLabelTypePatternList(labelTypeList);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/codelibs/fess/helper/PathMappingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.lastaflute.di.core.factory.SingletonLaContainerFactory;
import org.lastaflute.web.util.LaRequestUtil;

public class PathMappingHelper {
public class PathMappingHelper extends AbstractConfigHelper {

private static final Logger logger = LogManager.getLogger(PathMappingHelper.class);

Expand All @@ -53,10 +53,11 @@ public void init() {
if (logger.isDebugEnabled()) {
logger.debug("Initialize {}", this.getClass().getSimpleName());
}
update();
load();
}

public int update() {
@Override
public int load() {
final List<String> ptList = getProcessTypeList();

try {
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/org/codelibs/fess/helper/RelatedContentHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
import org.codelibs.fess.es.config.exentity.RelatedContent;
import org.codelibs.fess.util.ComponentUtil;

public class RelatedContentHelper {
public class RelatedContentHelper extends AbstractConfigHelper {

private static final Logger logger = LogManager.getLogger(RelatedContentHelper.class);

protected volatile Map<String, Pair<Map<String, String>, List<Pair<Pattern, String>>>> relatedContentMap = Collections.emptyMap();
protected Map<String, Pair<Map<String, String>, List<Pair<Pattern, String>>>> relatedContentMap = Collections.emptyMap();

protected String regexPrefix = "regex:";

Expand All @@ -48,15 +48,10 @@ public void init() {
if (logger.isDebugEnabled()) {
logger.debug("Initialize {}", this.getClass().getSimpleName());
}
reload();
}

public int update() {
return reload();
load();
}

public List<RelatedContent> getAvailableRelatedContentList() {

return ComponentUtil.getComponent(RelatedContentBhv.class).selectList(cb -> {
cb.query().matchAll();
cb.query().addOrderBy_SortOrder_Asc();
Expand All @@ -65,7 +60,8 @@ public List<RelatedContent> getAvailableRelatedContentList() {
});
}

protected int reload() {
@Override
public int load() {
final Map<String, Pair<Map<String, String>, List<Pair<Pattern, String>>>> relatedContentMap = new HashMap<>();
getAvailableRelatedContentList().stream().forEach(entity -> {
final String key = getHostKey(entity);
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/codelibs/fess/helper/RelatedQueryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.codelibs.fess.es.config.exentity.RelatedQuery;
import org.codelibs.fess.util.ComponentUtil;

public class RelatedQueryHelper {
public class RelatedQueryHelper extends AbstractConfigHelper {
private static final Logger logger = LogManager.getLogger(RelatedQueryHelper.class);

protected volatile Map<String, Map<String, String[]>> relatedQueryMap = Collections.emptyMap();
Expand All @@ -40,11 +40,7 @@ public void init() {
if (logger.isDebugEnabled()) {
logger.debug("Initialize {}", this.getClass().getSimpleName());
}
reload();
}

public int update() {
return reload();
load();
}

public List<RelatedQuery> getAvailableRelatedQueryList() {
Expand All @@ -56,7 +52,8 @@ public List<RelatedQuery> getAvailableRelatedQueryList() {
});
}

protected int reload() {
@Override
public int load() {
final Map<String, Map<String, String[]>> relatedQueryMap = new HashMap<>();
getAvailableRelatedQueryList().stream().forEach(entity -> {
final String key = getHostKey(entity);
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/codelibs/fess/helper/SystemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,18 @@ protected String createSearchRole(final String type, final String name) {

public void reloadConfiguration() {
ComponentUtil.getSearchEngineClient().refresh();
ComponentUtil.getLabelTypeHelper().init();
ComponentUtil.getPathMappingHelper().init();

ComponentUtil.getSuggestHelper().init();
ComponentUtil.getPopularWordHelper().init();
ComponentUtil.getJobManager().reboot();
ComponentUtil.getLdapManager().updateConfig();

ComponentUtil.getLabelTypeHelper().update();
ComponentUtil.getPathMappingHelper().update();
ComponentUtil.getRelatedContentHelper().update();
ComponentUtil.getRelatedQueryHelper().update();
ComponentUtil.getKeyMatchHelper().update();

ComponentUtil.getLdapManager().updateConfig();
ComponentUtil.getJobManager().reboot();
updateSystemProperties();
}

Expand Down Expand Up @@ -506,10 +509,11 @@ public void updateSystemProperties() {

public String updateConfiguration() {
final StringBuilder buf = new StringBuilder();
buf.append("Label: ").append(ComponentUtil.getLabelTypeHelper().update()).append("\n");
buf.append("PathMapping: ").append(ComponentUtil.getPathMappingHelper().update()).append("\n");
buf.append("RelatedContent: ").append(ComponentUtil.getRelatedContentHelper().update()).append("\n");
buf.append("RelatedQuery: ").append(ComponentUtil.getRelatedQueryHelper().update()).append("\n");
buf.append("Label: ").append(ComponentUtil.getLabelTypeHelper().load()).append("\n");
buf.append("PathMapping: ").append(ComponentUtil.getPathMappingHelper().load()).append("\n");
buf.append("RelatedContent: ").append(ComponentUtil.getRelatedContentHelper().load()).append("\n");
buf.append("RelatedQuery: ").append(ComponentUtil.getRelatedQueryHelper().load()).append("\n");
buf.append("KeyMatch: ").append(ComponentUtil.getKeyMatchHelper().load()).append("\n");
return buf.toString();
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/codelibs/fess/job/JobExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.codelibs.fess.job;

import org.codelibs.fess.Constants;

public abstract class JobExecutor {
protected ShutdownListener shutdownListener;

Expand Down

0 comments on commit 92a6184

Please sign in to comment.