Skip to content

Commit 92dac83

Browse files
committed
Refresh Context
Error Handling
1 parent 3a140f6 commit 92dac83

File tree

7 files changed

+92
-15
lines changed

7 files changed

+92
-15
lines changed

src/main/java/com/gaurav/restify/bootstrap/ConfigurationListener.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.gaurav.restify.constants.ConfigurationConstants;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.ApplicationContext;
810
import org.springframework.context.event.ContextRefreshedEvent;
911
import org.springframework.context.event.EventListener;
1012
import org.springframework.stereotype.Component;
@@ -18,6 +20,10 @@ public class ConfigurationListener {
1820

1921
Logger logger = LoggerFactory.getLogger(ConfigurationUtil.class);
2022

23+
@Autowired
24+
private static ApplicationContext appContext;
25+
26+
2127

2228
@EventListener(ContextRefreshedEvent.class)
2329
private void instantiateConfiguration() {
@@ -34,7 +40,7 @@ private void createDirectories() {
3440
Files.createDirectories(ConfigurationConstants.REST_CONFIG_PATH);
3541
} catch (IOException e) {
3642
logger.error("Could not create directories", e);
37-
System.exit(0);
43+
System.exit(2);
3844

3945
}
4046
}

src/main/java/com/gaurav/restify/bootstrap/ShutDownListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ShutDownListener {
1414

1515
@EventListener(ContextClosedEvent.class)
1616
private void applicationStopped() {
17-
logger.warn("Application forced to close!");
17+
logger.debug("Application forced to close!");
1818
}
1919

2020
}

src/main/java/com/gaurav/restify/configuration/RestConfigurationManager.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashMap;
1313

1414
import static com.gaurav.restify.constants.ConfigurationConstants.REST_CONFIG_FILE;
15+
import static com.gaurav.restify.constants.ErrorCodes.ERROR_TERMINATE;
1516

1617
@Component
1718
public class RestConfigurationManager {
@@ -22,6 +23,7 @@ public class RestConfigurationManager {
2223
private static HashMap<String, RestJob> restJobHashMap = null;
2324
private static final Logger logger = LoggerFactory.getLogger(RestConfigurationManager.class);
2425

26+
2527
private RestConfigurationManager() {
2628

2729
}
@@ -38,13 +40,13 @@ public static RestConfigurationManager getInstance() {
3840

3941
} catch (JAXBException e) {
4042

41-
logger.error( "Could not parse xml file - " + REST_CONFIG_FILE, e);
42-
System.exit(0);
43+
logger.error("Could not parse xml file - " + REST_CONFIG_FILE, e);
44+
System.exit(ERROR_TERMINATE);
45+
4346

4447
} catch (FileNotFoundException e) {
4548
logger.error("xml file not found- " + REST_CONFIG_FILE, e);
46-
System.exit(0);
47-
49+
System.exit(ERROR_TERMINATE);
4850

4951
}
5052

@@ -56,7 +58,7 @@ public static RestConfigurationManager getInstance() {
5658

5759

5860
private static void instantiateRestJobMap() {
59-
logger.info( "Going to instantiate rest job map");
61+
logger.info("Going to instantiate rest job map");
6062
if (null != restConfiguration) {
6163
restJobHashMap = new HashMap<>();
6264
restConfiguration.getJobs().forEach(job -> restJobHashMap.put(job.getCommand(), job));
@@ -71,4 +73,15 @@ public RestJob getRestJob(String commandName) {
7173
}
7274

7375

76+
public void refreshConfiguration(){
77+
logger.info("Going to refresh rest job map");
78+
79+
restConfigurationManager = null;
80+
81+
RestConfigurationManager.getInstance();
82+
83+
84+
}
85+
86+
7487
}

src/main/java/com/gaurav/restify/configuration/configurationBeans/RestJob.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import javax.xml.bind.annotation.XmlRootElement;
44
import javax.xml.bind.annotation.XmlType;
5+
import java.util.Arrays;
56

67
@XmlRootElement(name = "RestJob")
78

@@ -53,4 +54,15 @@ public long getWaitTime() {
5354
public void setWaitTime(long waitTime) {
5455
this.waitTime = waitTime;
5556
}
57+
58+
@Override
59+
public String toString() {
60+
return "RestJob{" +
61+
"path='" + path + '\'' +
62+
", commandType='" + commandType + '\'' +
63+
", command='" + command + '\'' +
64+
", args=" + Arrays.toString(args) +
65+
", waitTime=" + waitTime +
66+
'}';
67+
}
5668
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.gaurav.restify.constants;
2+
3+
public interface ErrorCodes {
4+
5+
int ERROR_TERMINATE = -1;
6+
}

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
import com.gaurav.restify.beans.Response;
66
import com.gaurav.restify.configuration.RestConfigurationManager;
77
import com.gaurav.restify.configuration.configurationBeans.RestJob;
8+
import com.gaurav.restify.constants.ErrorCodes;
89
import com.gaurav.restify.constants.ExecutorConstants;
910
import com.gaurav.restify.services.ExecutorService;
11+
import org.slf4j.LoggerFactory;
1012
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.web.bind.annotation.PathVariable;
1214
import org.springframework.web.bind.annotation.RequestMapping;
1315
import org.springframework.web.bind.annotation.RestController;
1416

15-
import java.io.BufferedReader;
1617
import java.io.IOException;
17-
import java.io.InputStreamReader;
18-
import java.util.logging.Logger;
1918

2019
@RestController
2120
public class ProcessController {
@@ -26,14 +25,21 @@ public class ProcessController {
2625
@Autowired
2726
private RestConfigurationManager restConfigurationManager;
2827

28+
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ProcessController.class);
29+
2930

3031
@RequestMapping(value = "/execute/{scriptName}")
3132
public Response executeScripts(@PathVariable String scriptName) {
3233

34+
logger.info("Current Rest Call for Script ::: " + scriptName);
35+
3336
RestJob restJob = restConfigurationManager.getRestJob(scriptName);
3437

38+
logger.info("Rest Job Found " + restJob);
3539

36-
ExecutorTaskOutput taskOutput = null;
40+
41+
ExecutorTaskOutput taskOutput;
42+
Response response;
3743
try {
3844

3945

@@ -44,16 +50,24 @@ public Response executeScripts(@PathVariable String scriptName) {
4450

4551

4652
taskOutput = executorService.executeTask(executorTask);
53+
response = new Response(String.valueOf(taskOutput.getExitCode()));
54+
response.setOutput(taskOutput.getOutput());
4755

4856

4957
} catch (IOException ioEx) {
5058

51-
ioEx.printStackTrace();
52-
}
59+
logger.error("File Not Found", ioEx);
60+
response = new Response(String.valueOf(ErrorCodes.ERROR_TERMINATE));
61+
response.setOutput(ioEx.toString());
5362

63+
} catch (Exception e) {
64+
65+
logger.error("Script execution failed! ", e);
66+
response = new Response(String.valueOf(ErrorCodes.ERROR_TERMINATE));
67+
response.setOutput(e.toString());
68+
69+
}
5470

55-
Response response = new Response(String.valueOf(taskOutput.getExitCode()));
56-
response.setOutput(taskOutput.getOutput());
5771

5872
return response;
5973
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.gaurav.restify.controllers;
2+
3+
import com.gaurav.restify.beans.Response;
4+
import com.gaurav.restify.configuration.RestConfigurationManager;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class RefreshConfiguration {
11+
12+
@Autowired
13+
RestConfigurationManager restConfigurationManager;
14+
15+
@RequestMapping(value = "/refresh")
16+
public Response refreshConfiguration(){
17+
18+
restConfigurationManager.refreshConfiguration();
19+
20+
Response response = new Response(String.valueOf(0));
21+
response.setOutput("Configuration Refreshed");
22+
23+
return response;
24+
25+
}
26+
}

0 commit comments

Comments
 (0)