Skip to content

Commit

Permalink
removing commons chain dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sriram-ponangi committed Jan 4, 2022
1 parent d275aaf commit 1c8162e
Show file tree
Hide file tree
Showing 33 changed files with 162 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private InputStream getValidMonitoringConfigs() throws Exception {
if (yamlFile.isFile()) {
return new FileInputStream(yamlFile);
}

return this.getClass()
.getClassLoader()
.getResourceAsStream(monitoringConfigYMLPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.file.monitoring.application.events.config.beans.EventInfo;
import com.file.monitoring.application.events.constants.MonitoringEventType;
import com.file.monitoring.catalog.constants.ChainNames;
import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ContextBase;
import com.file.monitoring.common.configs.Catalog;
import com.file.monitoring.common.configs.Chain;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand Down Expand Up @@ -39,15 +37,14 @@ private final void executeChain(EventInfo eventInfo){
return;
}

Context chainContext = new ContextBase();
HashMap<String, Object> chainContext = new HashMap<>();
eventInfo.getChainInfo().getContextParams().forEach(param -> {
chainContext.put(param.getKey(), param.getValue());
});


Command commandsChain = chainCatalog.getCommand(eventInfo.getChainInfo().getName());
try {
commandsChain.execute(chainContext);
Chain chain = chainCatalog.getChain(eventInfo.getChainInfo().getName());
chain.execute(chainContext);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
2 changes: 1 addition & 1 deletion application/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ application:
chain:
config:
yml:
path: chain/chain-config.yml
path: chain/chain-config.yaml

monitoring-events:
config:
Expand Down
4 changes: 0 additions & 4 deletions chain-catalog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
</dependency>
<dependency>
<groupId>com.file.monitoring</groupId>
<artifactId>generic-commands</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.file.monitoring.catalog.beans.CatalogBean;
import com.file.monitoring.catalog.beans.ChainConfig;
import org.apache.commons.chain.Catalog;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.impl.CatalogBase;
import org.apache.commons.chain.impl.ChainBase;
import com.file.monitoring.common.configs.Catalog;
import com.file.monitoring.common.configs.Chain;
import com.file.monitoring.common.configs.Command;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,6 +15,8 @@
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Component
Expand All @@ -28,30 +29,38 @@ public class ChainYAMLCatalogConfiguration {
String chainConfigYMLPath;

@Bean("ChainCatalog")
private Catalog parseConfigFile() {
private Catalog parseConfigFile() throws Exception {
Yaml yaml = new Yaml(new Constructor(CatalogBean.class));
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream(chainConfigYMLPath);
CatalogBean catalogBean = yaml.load(inputStream);

CatalogBean catalogBean = yaml.load(getValidChainConfigs());

CatalogBase catalogBase = new CatalogBase();
Catalog catalog = new Catalog();

for (ChainConfig cc : catalogBean.getCatalog()) {
ChainBase chainBase = new ChainBase();
if (StringUtils.isBlank(cc.getChain())) {
break;
}
Chain chain = new Chain(cc.getChain());
catalog.addChain(chain);

for (String commandName : cc.getCommands()) {
if(StringUtils.isNotBlank(commandName)) {
chainBase.addCommand(beanFactory.getBean(commandName, Command.class));
if (StringUtils.isNotBlank(commandName)) {
chain.addCommand(beanFactory.getBean(commandName, Command.class));
}
}
if(StringUtils.isNotBlank(cc.getChain())) {
catalogBase.addCommand(cc.getChain(), chainBase);
}

}

return catalogBase;
return catalog;
}

private InputStream getValidChainConfigs() throws Exception {
File yamlFile = new File(chainConfigYMLPath);
if (yamlFile.isFile() && yamlFile.canRead()) {
return new FileInputStream(yamlFile);
}
return this.getClass()
.getClassLoader()
.getResourceAsStream(chainConfigYMLPath);
}
}

Expand Down
5 changes: 1 addition & 4 deletions common-configs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
</dependency>

</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.file.monitoring.common.configs;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class Catalog {
private static final Map<String, Chain> catalog = new ConcurrentHashMap<>();

public void addChain(Chain chain) throws Exception {
if (chain == null) {
throw new Exception("Invalid Argument Exception");
}
catalog.put(chain.getName(), chain);
}

public Chain getChain(String chainName) throws Exception {
if (chainName == null) {
throw new Exception("Invalid Argument Exception");
}
return catalog.get(chainName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.file.monitoring.common.configs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.LinkedList;

public final class Chain {
private static final Logger LOGGER = LoggerFactory.getLogger(Chain.class);

private LinkedList<Command> commands = new LinkedList<>();
private final String name;

public Chain(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void addCommand(Command command) {
this.commands.add(command);
}

public void execute(HashMap<String, Object> context) {
for (Command command : this.commands) {
boolean chainMustEnd;
try {
chainMustEnd = command.execute(context);
} catch (Exception e) {
chainMustEnd = Command.COMPLETE_PROCESSING;
LOGGER.error("Stopping Chain Execution. ", e);
}

if (chainMustEnd){
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.file.monitoring.common.configs;

import java.util.HashMap;

public interface Command {
boolean CONTINUE_PROCESSING = false;
boolean COMPLETE_PROCESSING = true;

boolean execute(HashMap<String, Object> context) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.file.monitoring.common.configs.commands;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

import com.file.monitoring.common.configs.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public abstract class CommonFileMonitoringCommand implements Command {
private static final Logger LOGGER = LoggerFactory.getLogger(CommonFileMonitoringCommand.class);

@Override
public boolean execute(Context context) throws Exception {
public boolean execute(HashMap<String, Object> context) throws Exception {
long startTime = System.currentTimeMillis();
LOGGER.debug("-----------------------------------------------------------");
LOGGER.debug("[{}] :: Started Execution", this.getClass().getSimpleName());
Expand All @@ -23,5 +25,5 @@ public boolean execute(Context context) throws Exception {
return Command.CONTINUE_PROCESSING;
}

protected abstract void executeCommand(Context context) throws Exception;
protected abstract void executeCommand(HashMap<String, Object> context) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.file.monitoring.common.configs.commands.processors;

import org.apache.commons.chain.Context;
import java.util.HashMap;

public interface CommonFileMonitoringProcessor {

void process(Context context) throws Exception;
void process(HashMap<String, Object> context) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.file.monitoring.common.configs.commands.validators;

import org.apache.commons.chain.Context;

import java.util.HashMap;
import java.util.List;

public interface CommonFileMonitoringValidators {

List<String> validate(Context context) throws Exception;
List<String> validate(HashMap<String, Object> context) throws Exception;

}
5 changes: 0 additions & 5 deletions generic-commands/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@


<dependencies>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
</dependency>

<dependency>
<groupId>com.file.monitoring</groupId>
<artifactId>common-configs</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.file.monitoring.generic.commands;

import com.file.monitoring.generic.commands.processors.factory.GenericProcessorsFactory;
import org.apache.commons.chain.Context;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component("GenericCommand1")
public class GenericCommand1 extends GenericCommand {

Expand All @@ -12,7 +13,7 @@ public GenericCommand1(GenericProcessorsFactory genericProcessorsFactory) {
}

@Override
public void executeCommand(Context context) throws Exception {
public void executeCommand(HashMap<String, Object> context) throws Exception {
this.genericProcessorsFactory.createGenericProcessor1_1().process(context);
this.genericProcessorsFactory.createGenericProcessor1_2().process(context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.file.monitoring.generic.commands;

import com.file.monitoring.generic.commands.processors.factory.GenericProcessorsFactory;
import org.apache.commons.chain.Context;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component("GenericCommand2")
public class GenericCommand2 extends GenericCommand {

Expand All @@ -12,7 +13,7 @@ public GenericCommand2(GenericProcessorsFactory genericProcessorsFactory) {
}

@Override
public void executeCommand(Context context) throws Exception {
public void executeCommand(HashMap<String, Object> context) throws Exception {
this.genericProcessorsFactory.createGenericProcessor2_1().process(context);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.file.monitoring.generic.commands;

import com.file.monitoring.generic.commands.processors.factory.GenericProcessorsFactory;
import org.apache.commons.chain.Context;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component("GenericCommand3")
public class GenericCommand3 extends GenericCommand {

Expand All @@ -12,7 +13,7 @@ public GenericCommand3(GenericProcessorsFactory genericProcessorsFactory) {
}

@Override
public void executeCommand(Context context) throws Exception {
public void executeCommand(HashMap<String, Object> context) throws Exception {
this.genericProcessorsFactory.createGenericProcessor3_1().process(context);
this.genericProcessorsFactory.createGenericProcessor3_2().process(context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.file.monitoring.generic.commands;

import com.file.monitoring.generic.commands.processors.factory.GenericProcessorsFactory;
import org.apache.commons.chain.Context;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component("GenericCommand4")
public class GenericCommand4 extends GenericCommand {

Expand All @@ -12,7 +13,7 @@ public GenericCommand4(GenericProcessorsFactory genericProcessorsFactory) {
}

@Override
public void executeCommand(Context context) throws Exception {
public void executeCommand(HashMap<String, Object> context) throws Exception {
this.genericProcessorsFactory.createGenericProcessor4_1().process(context);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.file.monitoring.generic.commands;

import com.file.monitoring.generic.commands.processors.factory.GenericProcessorsFactory;
import org.apache.commons.chain.Context;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component("GenericCommand5")
public class GenericCommand5 extends GenericCommand {

Expand All @@ -12,7 +13,7 @@ public GenericCommand5(GenericProcessorsFactory genericProcessorsFactory) {
}

@Override
public void executeCommand(Context context) throws Exception {
public void executeCommand(HashMap<String, Object> context) throws Exception {
this.genericProcessorsFactory.createGenericProcessor5_1().process(context);
this.genericProcessorsFactory.createGenericProcessor5_2().process(context);
}
Expand Down
Loading

0 comments on commit 1c8162e

Please sign in to comment.