Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/cdk 95 #1121

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,27 @@ public class ConfigManager {
@Named("kramerius4")
private Provider<Connection> connectionProvider;



public List<Pair<String,String>> getPropertiesByRegularExpression(String regexp) {

LOGGER.info( String.format("Returning properties by regexp %s", regexp));

List<Pair<String,String>> keys = new JDBCQueryTemplate<Pair<String,String>>(this.connectionProvider.get(), true) {
@Override
public boolean handleRow(ResultSet rs, List<Pair<String, String>> returnsList)
throws SQLException {
String key = rs.getString("key");
String value = rs.getString("value");
Pair<String,String> pair = Pair.of(key, value);
LOGGER.info(String.format("Adding pair %s", pair.toString()));
returnsList.add(pair);
return true;
}
}.executeQuery("SELECT * FROM config WHERE key ~ ? ",regexp);
return keys;

}

public List<String> getKeysByRegularExpression(String regexp) {
List<String> keys = new JDBCQueryTemplate<String>(this.connectionProvider.get(), true) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ public Response getAllConnected(/*@QueryParam("health")String health*/) {
this.libraries.allInstances().forEach(library -> {
JSONObject json = libraryJSON(library);
retval.put(library.getName(), json);

/*
boolean healthCheck = Boolean.valueOf(health);
LOGGER.info(String.format("Parameter health '%b'", healthCheck));
if (healthCheck) {
JSONObject channel = new JSONObject();
channelHealth(library.getName(), channel);
json.put("channel", channel);
LOGGER.info(String.format("Channel json is '%s'", channel.toString()));
}*/
});
return Response.ok(retval).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface Instances {
*/
public List<OneInstance> disabledInstances();



/**
* Finds a specific instance by its acronym.
* The acronym is a unique identifier for each instance.
Expand All @@ -38,6 +40,7 @@ public interface Instances {
*/
public OneInstance find(String acronym);


/**
* Checks if any instances are currently disabled.
* @return True if at least one instance is disabled, false otherwise.
Expand All @@ -51,6 +54,8 @@ public interface Instances {
*/
public boolean isEnabledInstance(String acronym);

/** reload all items */
public void refresh();

public void cronRefresh();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sun.jersey.api.client.Client;

import cz.incad.kramerius.SolrAccess;
import cz.incad.kramerius.rest.apiNew.ConfigManager;
import cz.incad.kramerius.rest.apiNew.client.v70.redirection.item.ProxyItemHandler;
import cz.incad.kramerius.rest.apiNew.client.v70.redirection.user.ProxyUserHandler;
import cz.incad.kramerius.security.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.sun.jersey.api.client.Client;

import cz.incad.kramerius.SolrAccess;
import cz.incad.kramerius.rest.apiNew.ConfigManager;
import cz.incad.kramerius.rest.apiNew.admin.v70.reharvest.ReharvestManager;
import cz.incad.kramerius.rest.apiNew.client.v70.libs.Instances;
import cz.incad.kramerius.rest.apiNew.client.v70.libs.OneInstance;
Expand All @@ -19,23 +20,33 @@
import cz.incad.kramerius.rest.apiNew.client.v70.redirection.user.V5ForwardUserHandler;
import cz.incad.kramerius.rest.apiNew.client.v70.redirection.user.V7ForwardUserHandler;
import cz.incad.kramerius.security.User;
import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;

public class DefaultOnePropertiesInstance implements OneInstance {

private Instances instances;
private ReharvestManager reharvestManager;
private String instanceAcronym;
private boolean connected = true;
private TypeOfChangedStatus typeOfChangedStatus = TypeOfChangedStatus.automat;

private ConfigManager configManager;

private Map<String, String> info = new HashMap<>();

public DefaultOnePropertiesInstance(ReharvestManager reharvestManager, Instances instances, String instanceAcronym) {

private boolean connectedState = true;
private TypeOfChangedStatus typeOfChangedStatus;

public DefaultOnePropertiesInstance( ConfigManager configManager, ReharvestManager reharvestManager,
Instances instances,
String instanceAcronym,
boolean connectedState,
TypeOfChangedStatus typeOfChangedStatus) {
super();
this.reharvestManager = reharvestManager;
this.instanceAcronym = instanceAcronym;
this.instances = instances;
this.connectedState = connectedState;
this.typeOfChangedStatus = typeOfChangedStatus;
this.configManager = configManager;
}

@Override
Expand Down Expand Up @@ -119,18 +130,21 @@ public void removeRegistrInfo(String key) {

@Override
public boolean isConnected() {
return this.connected;
return this.connectedState;
}

@Override
public void setConnected(boolean connected, TypeOfChangedStatus type) {
this.connected = connected;
this.typeOfChangedStatus = type;
String keyConnected = String.format("cdk.collections.sources.%s.enabled", this.instanceAcronym);
String keyTypeofStatus = String.format("cdk.collections.sources.%s.status", this.instanceAcronym );
configManager.setProperty(keyConnected, ""+connected);
configManager.setProperty(keyTypeofStatus, type.name());
this.instances.refresh();
}

@Override
public TypeOfChangedStatus getType() {
return this.typeOfChangedStatus;
return typeOfChangedStatus;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import javax.inject.Inject;
import javax.ws.rs.core.MediaType;

import cz.incad.kramerius.rest.apiNew.ConfigManager;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang3.tuple.Pair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -41,19 +43,29 @@ public class DefaultPropertiesInstances implements Instances {

public static final Logger LOGGER = Logger.getLogger(DefaultPropertiesInstances.class.getName());

private List<OneInstance> instances = new ArrayList<>();
private Set<String> names = new HashSet<>();
private ReharvestManager reharvestManager;

private final List<OneInstance> instances = new ArrayList<>();
private final Set<String> names = new HashSet<>();
private final ReharvestManager reharvestManager;
private final ConfigManager configManager;


@Inject
public DefaultPropertiesInstances(ReharvestManager reharvestManager) {
public DefaultPropertiesInstances(ReharvestManager reharvestManager, ConfigManager configManager) {
super();
this.reharvestManager = reharvestManager;
this.configManager = configManager;
LOGGER.info("Refreshing configuration with reharvestManager "+this.reharvestManager);
refreshingConfiguration();
refresh();
}
@Override
public void refresh() {
this.instances.clear(); this.names.clear();

Map<String,String> properties = new HashMap<>();
this.configManager.getPropertiesByRegularExpression("cdk.collections.sources.*").stream().forEach(it-> {
properties.put(it.getKey(),it.getValue());
});

protected void refreshingConfiguration() {
Configuration configuration = KConfiguration.getInstance().getConfiguration();
Iterator<String> keys = configuration.getKeys("cdk.collections.sources");
while (keys.hasNext()) {
Expand All @@ -63,50 +75,67 @@ protected void refreshingConfiguration() {
if (rest.lastIndexOf(".") > 0) {
String acronym = rest.substring(0, rest.lastIndexOf("."));
if (!names.contains(acronym)) {
addOneInstance(acronym);
addOneInstance(acronym, properties);
}
}
}
}
}

private void addOneInstance(String acronym) {

private void addOneInstance(String acronym, Map<String,String> properties) {
LOGGER.info(String.format("Adding library %s with reharvestManager %s", acronym, reharvestManager.toString()));

String keyConnected = String.format("cdk.collections.sources.%s.enabled",acronym);
String keyTypeofStatus = String.format("cdk.collections.sources.%s.status", acronym);
boolean connected = true;
TypeOfChangedStatus typeOfChange = TypeOfChangedStatus.automat;
if (properties.containsKey(keyConnected)) {
connected = Boolean.parseBoolean(properties.get(keyConnected));
}
if (properties.containsKey(keyTypeofStatus)) {
typeOfChange = TypeOfChangedStatus.valueOf(properties.get(keyTypeofStatus));
}

names.add(acronym);
DefaultOnePropertiesInstance di = new DefaultOnePropertiesInstance(this.reharvestManager, this, acronym);

DefaultOnePropertiesInstance di = new DefaultOnePropertiesInstance(this.configManager,this.reharvestManager, this, acronym,
connected, typeOfChange);
instances.add(di);
}

@Override
public List<OneInstance> allInstances() {
this.refreshingConfiguration();
this.refresh();
return this.instances;
}

@Override
public List<OneInstance> enabledInstances() {
this.refreshingConfiguration();
return this.instances.stream().filter(OneInstance::isConnected).collect(Collectors.toList());
this.refresh();
return this.instances.stream().filter(it-> {
return it.isConnected();
}).collect(Collectors.toList());
}

@Override
public List<OneInstance> disabledInstances() {
this.refreshingConfiguration();
this.refresh();
return this.instances.stream().filter(it -> {
return !it.isConnected();
}).collect(Collectors.toList());
}

@Override
public boolean isAnyDisabled() {
this.refreshingConfiguration();
this.refresh();
List<OneInstance> eInsts = this.enabledInstances();
return this.allInstances().size() != eInsts.size();
}

@Override
public OneInstance find(String acronym) {
this.refreshingConfiguration();
this.refresh();
List<OneInstance> collect = this.instances.stream().filter(it -> {
return it.getName().equals(acronym);
}).collect(Collectors.toList());
Expand All @@ -118,12 +147,9 @@ public OneInstance find(String acronym) {
}
}




@Override
public boolean isEnabledInstance(String acronym) {
this.refreshingConfiguration();
this.refresh();
Optional<OneInstance> found = instances.stream().filter(instance -> instance.getName().equals(acronym)).findFirst();
if (found.isPresent()) {
return found.get().isConnected();
Expand All @@ -134,7 +160,7 @@ public boolean isEnabledInstance(String acronym) {

@Override
public void cronRefresh() {
this.refreshingConfiguration();
this.refresh();
try {


Expand Down Expand Up @@ -196,20 +222,12 @@ public void cronRefresh() {
}
}






protected String registerData(Client client, String url) throws IOException {
WebResource r = client.resource(url);
InputStream inputStream = r.accept(MediaType.APPLICATION_XML).get(InputStream.class);
String string = org.apache.commons.io.IOUtils.toString(inputStream, "UTF-8");
return string;
}



}


Loading