Skip to content

Commit 3a140f6

Browse files
committed
Added XML Configuration.
1 parent e5dddd5 commit 3a140f6

File tree

11 files changed

+294
-6
lines changed

11 files changed

+294
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ repositories {
2626
dependencies {
2727
compile("org.springframework.boot:spring-boot-starter-web")
2828
compile("org.springframework.boot:spring-boot-devtools")
29-
// compile("org.apache.commons:commons-exec:jar:1.3")
3029
compile group: 'org.apache.commons', name: 'commons-exec', version: '1.3'
30+
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.11'
3131

3232

3333

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.gaurav.restify.bootstrap;
2+
3+
import com.gaurav.restify.configuration.ConfigurationUtil;
4+
import com.gaurav.restify.configuration.RestConfigurationManager;
5+
import com.gaurav.restify.constants.ConfigurationConstants;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.context.event.ContextRefreshedEvent;
9+
import org.springframework.context.event.EventListener;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.io.IOException;
13+
import java.nio.file.Files;
14+
15+
16+
@Component
17+
public class ConfigurationListener {
18+
19+
Logger logger = LoggerFactory.getLogger(ConfigurationUtil.class);
20+
21+
22+
@EventListener(ContextRefreshedEvent.class)
23+
private void instantiateConfiguration() {
24+
logger.info("Startup Listener :: Configuration");
25+
RestConfigurationManager.getInstance();
26+
}
27+
28+
@EventListener(ContextRefreshedEvent.class)
29+
private void createDirectories() {
30+
logger.info("Startup Listener :: Directories");
31+
32+
try {
33+
Files.createDirectories(ConfigurationConstants.REST_LOG_PATH);
34+
Files.createDirectories(ConfigurationConstants.REST_CONFIG_PATH);
35+
} catch (IOException e) {
36+
logger.error("Could not create directories", e);
37+
System.exit(0);
38+
39+
}
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.gaurav.restify.bootstrap;
2+
3+
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.context.event.ContextClosedEvent;
7+
import org.springframework.context.event.EventListener;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class ShutDownListener {
12+
13+
Logger logger = LoggerFactory.getLogger(ShutDownListener.class);
14+
15+
@EventListener(ContextClosedEvent.class)
16+
private void applicationStopped() {
17+
logger.warn("Application forced to close!");
18+
}
19+
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.gaurav.restify.configuration;
2+
3+
import javax.xml.bind.*;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.nio.file.Path;
7+
8+
9+
public class ConfigurationUtil {
10+
11+
public static void objectToXML(Class clazz, Object clazzObj, Path path) throws JAXBException {
12+
13+
JAXBContext context = JAXBContext.newInstance(clazz);
14+
Marshaller marshaller = context.createMarshaller();
15+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
16+
marshaller.marshal(clazz.cast(clazzObj), path.toFile());
17+
18+
19+
}
20+
21+
22+
public static Object XMLtoObject(Class clazz, Path path) throws JAXBException, FileNotFoundException {
23+
JAXBContext context = JAXBContext.newInstance(clazz);
24+
Unmarshaller um = context.createUnmarshaller();
25+
return um.unmarshal(new FileReader(path.toFile().getAbsolutePath()));
26+
27+
}
28+
29+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.gaurav.restify.configuration;
2+
3+
4+
import com.gaurav.restify.configuration.configurationBeans.RestConfiguration;
5+
import com.gaurav.restify.configuration.configurationBeans.RestJob;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.stereotype.Component;
9+
10+
import javax.xml.bind.JAXBException;
11+
import java.io.FileNotFoundException;
12+
import java.util.HashMap;
13+
14+
import static com.gaurav.restify.constants.ConfigurationConstants.REST_CONFIG_FILE;
15+
16+
@Component
17+
public class RestConfigurationManager {
18+
19+
20+
private static RestConfigurationManager restConfigurationManager = null;
21+
private static RestConfiguration restConfiguration = null;
22+
private static HashMap<String, RestJob> restJobHashMap = null;
23+
private static final Logger logger = LoggerFactory.getLogger(RestConfigurationManager.class);
24+
25+
private RestConfigurationManager() {
26+
27+
}
28+
29+
30+
public static RestConfigurationManager getInstance() {
31+
logger.info("RestConfigurationManager :: Instantiate");
32+
33+
if (null == restConfigurationManager) {
34+
try {
35+
restConfiguration = (RestConfiguration) ConfigurationUtil.XMLtoObject(RestConfiguration.class, REST_CONFIG_FILE);
36+
instantiateRestJobMap();
37+
return new RestConfigurationManager();
38+
39+
} catch (JAXBException e) {
40+
41+
logger.error( "Could not parse xml file - " + REST_CONFIG_FILE, e);
42+
System.exit(0);
43+
44+
} catch (FileNotFoundException e) {
45+
logger.error("xml file not found- " + REST_CONFIG_FILE, e);
46+
System.exit(0);
47+
48+
49+
}
50+
51+
52+
}
53+
54+
return restConfigurationManager;
55+
}
56+
57+
58+
private static void instantiateRestJobMap() {
59+
logger.info( "Going to instantiate rest job map");
60+
if (null != restConfiguration) {
61+
restJobHashMap = new HashMap<>();
62+
restConfiguration.getJobs().forEach(job -> restJobHashMap.put(job.getCommand(), job));
63+
} else {
64+
logger.debug("restConfiguration is null");
65+
66+
}
67+
}
68+
69+
public RestJob getRestJob(String commandName) {
70+
return restJobHashMap.get(commandName);
71+
}
72+
73+
74+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.gaurav.restify.configuration.configurationBeans;
2+
3+
import javax.xml.bind.annotation.XmlElement;
4+
import javax.xml.bind.annotation.XmlElementWrapper;
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
import java.util.ArrayList;
7+
8+
@XmlRootElement(namespace = "com.gaurav.restify.configuration")
9+
public class RestConfiguration {
10+
11+
12+
@XmlElementWrapper(name = "restJobs")
13+
14+
@XmlElement(name = "restJob")
15+
private ArrayList<RestJob> restJobs;
16+
17+
18+
public ArrayList<RestJob> getJobs() {
19+
return restJobs;
20+
}
21+
22+
public void setRestJobs(ArrayList<RestJob> restJobs) {
23+
this.restJobs = restJobs;
24+
}
25+
26+
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.gaurav.restify.configuration.configurationBeans;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
import javax.xml.bind.annotation.XmlType;
5+
6+
@XmlRootElement(name = "RestJob")
7+
8+
@XmlType
9+
public class RestJob {
10+
11+
private String path;
12+
private String commandType;
13+
private String command;
14+
private String[] args;
15+
private long waitTime;
16+
17+
public String getPath() {
18+
return path;
19+
}
20+
21+
public void setPath(String path) {
22+
this.path = path;
23+
}
24+
25+
public String getCommandType() {
26+
return commandType;
27+
}
28+
29+
public void setCommandType(String commandType) {
30+
this.commandType = commandType;
31+
}
32+
33+
public String getCommand() {
34+
return command;
35+
}
36+
37+
public void setCommand(String command) {
38+
this.command = command;
39+
}
40+
41+
public String[] getArgs() {
42+
return args;
43+
}
44+
45+
public void setArgs(String[] args) {
46+
this.args = args;
47+
}
48+
49+
public long getWaitTime() {
50+
return waitTime;
51+
}
52+
53+
public void setWaitTime(long waitTime) {
54+
this.waitTime = waitTime;
55+
}
56+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.gaurav.restify.constants;
2+
3+
import java.io.File;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
7+
public interface ConfigurationConstants {
8+
9+
10+
String DEFAULT_CONFIG_DIR_PATH_LINUX = "/usr/local/var/Restify/Configuration";
11+
String DEFAULT_LOG_DIR_PATH_LINUX = "/usr/local/var/Restify/Logs";
12+
String DEFAULT_CONFIG_FILE_NAME = "Restify_Rest_jobs.xml";
13+
14+
Path REST_CONFIG_FILE = Paths.get(DEFAULT_CONFIG_DIR_PATH_LINUX + File.separator + DEFAULT_CONFIG_FILE_NAME);
15+
Path REST_LOG_PATH = Paths.get(DEFAULT_LOG_DIR_PATH_LINUX);
16+
Path REST_CONFIG_PATH = Paths.get(DEFAULT_CONFIG_DIR_PATH_LINUX);
17+
18+
}

src/main/java/com/gaurav/restify/controllers/ProcessController.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.gaurav.restify.beans.ExecutorTask;
44
import com.gaurav.restify.beans.ExecutorTaskOutput;
55
import com.gaurav.restify.beans.Response;
6+
import com.gaurav.restify.configuration.RestConfigurationManager;
7+
import com.gaurav.restify.configuration.configurationBeans.RestJob;
68
import com.gaurav.restify.constants.ExecutorConstants;
79
import com.gaurav.restify.services.ExecutorService;
810
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,17 +23,23 @@ public class ProcessController {
2123
@Autowired
2224
private ExecutorService executorService;
2325

26+
@Autowired
27+
private RestConfigurationManager restConfigurationManager;
28+
2429

2530
@RequestMapping(value = "/execute/{scriptName}")
2631
public Response executeScripts(@PathVariable String scriptName) {
2732

33+
RestJob restJob = restConfigurationManager.getRestJob(scriptName);
34+
35+
2836
ExecutorTaskOutput taskOutput = null;
2937
try {
3038

3139

32-
ExecutorTask executorTask = new ExecutorTask.ExecutorTaskBuilder("/Users/gaurav/Downloads", scriptName, ExecutorConstants.BASH)
33-
.setArgs(new String[]{})
34-
.setWaitTime(10)
40+
ExecutorTask executorTask = new ExecutorTask.ExecutorTaskBuilder(restJob.getPath(), restJob.getCommand(), ExecutorConstants.valueOf(restJob.getCommandType()))
41+
.setArgs(restJob.getArgs())
42+
.setWaitTime(restJob.getWaitTime())
3543
.build();
3644

3745

src/main/java/com/gaurav/restify/services/ExecutorService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ public ExecutorTaskOutput executeTask(ExecutorTask executorTask) throws IOExcept
3737
public CommandLine getCommandLine(ExecutorTask executorTask) {
3838
CommandLine commandLine = new CommandLine(executorTask.getCommandType().toString());
3939
commandLine.addArgument(executorTask.getCommand());
40-
for (String arg : executorTask.getArgs()) {
40+
if (null != executorTask.getArgs()) {
41+
for (String arg : executorTask.getArgs()) {
4142

42-
commandLine.addArgument(arg);
43+
commandLine.addArgument(arg);
44+
45+
}
4346

4447
}
4548

0 commit comments

Comments
 (0)