-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from maartenlterpstra/master
Added service for querying service ingest status
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/nl/knmi/adaguc/services/servicehealth/ServiceHealthConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package nl.knmi.adaguc.services.servicehealth; | ||
|
||
import nl.knmi.adaguc.tools.ElementNotFoundException; | ||
import nl.knmi.adaguc.config.ConfigurationReader; | ||
import nl.knmi.adaguc.tools.MyXMLParser.XMLElement; | ||
|
||
/** | ||
* | ||
* @author maartenplieger | ||
* | ||
*/ | ||
|
||
|
||
public class ServiceHealthConfigurator implements nl.knmi.adaguc.config.ConfiguratorInterface { | ||
private static boolean enabled=false; | ||
// The directory that contains a file per service with the return code of updatedb | ||
private static String serviceHealthDirectory = null; | ||
static ConfigurationReader configurationReader = new ConfigurationReader (); | ||
public void doConfig(XMLElement configReader){ | ||
if(configReader.getNodeValue("adaguc-services.servicehealth") == null){ | ||
return; | ||
} | ||
String enabledStr=configReader.getNodeValue("adaguc-services.servicehealth.enabled"); | ||
if(enabledStr != null && enabledStr.equals("true")){ | ||
enabled = true; | ||
} | ||
|
||
if(enabled){ | ||
serviceHealthDirectory=configReader.getNodeValue("adaguc-services.servicehealth.servicehealthpath"); | ||
|
||
} | ||
} | ||
|
||
|
||
public static String getServiceHealthDirectory() throws ElementNotFoundException { | ||
configurationReader.readConfig(); | ||
return serviceHealthDirectory; | ||
} | ||
|
||
public static boolean getEnabled() throws ElementNotFoundException { | ||
configurationReader.readConfig(); | ||
return enabled; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/nl/knmi/adaguc/services/servicehealth/ServiceHealthRequestMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package nl.knmi.adaguc.services.servicehealth; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URLEncoder; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
|
||
import nl.knmi.adaguc.tools.ElementNotFoundException; | ||
import nl.knmi.adaguc.config.MainServicesConfigurator; | ||
import nl.knmi.adaguc.services.adagucserver.ADAGUCServer; | ||
import nl.knmi.adaguc.tools.Debug; | ||
import nl.knmi.adaguc.tools.HTTPTools; | ||
import nl.knmi.adaguc.tools.JSONResponse; | ||
import nl.knmi.adaguc.tools.Tools; | ||
|
||
@RestController | ||
public class ServiceHealthRequestMapper { | ||
@Bean | ||
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true); | ||
MappingJackson2HttpMessageConverter converter = | ||
new MappingJackson2HttpMessageConverter(mapper); | ||
return converter; | ||
} | ||
|
||
@ResponseBody | ||
@RequestMapping("servicehealth") | ||
public void serviceHealth(HttpServletResponse response, HttpServletRequest request) throws IOException{ | ||
JSONResponse jsonResponse = new JSONResponse(request); | ||
try { | ||
boolean enabled = ServiceHealthConfigurator.getEnabled(); | ||
if(!enabled){ | ||
jsonResponse.setMessage(new JSONObject().put("error","ADAGUC Service Health is not enabled")); | ||
}else{ | ||
String basePath = new File(ServiceHealthConfigurator.getServiceHealthDirectory()).getCanonicalPath(); | ||
String[] fileList = Tools.ls(basePath); | ||
JSONObject statusList = new JSONObject(); | ||
for (String service : fileList) { | ||
String status = Tools.readFile(basePath + "/" + service).trim(); | ||
String[] contents = status.split("\\r?\\n"); | ||
if (contents[0].startsWith("0")) { | ||
statusList.put(service, new JSONObject().put("statusCode", contents[0]).put("ok", true)); | ||
} else { | ||
JSONObject serviceResponse = new JSONObject(); | ||
serviceResponse.put("statusCode", contents[0]) | ||
.put("ok", false); | ||
if (contents.length > 1) | ||
serviceResponse.put("message", contents[1]); | ||
statusList.put(service, serviceResponse); | ||
|
||
} | ||
} | ||
jsonResponse.setMessage(statusList); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
jsonResponse.setException("error: "+e.getMessage(), e); | ||
} | ||
jsonResponse.print(response); | ||
|
||
} | ||
} |