Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
refactoring of GenericRProcess: split up to several functional files,
Browse files Browse the repository at this point in the history
start reducing calls to R_Config.getInstance()
  • Loading branch information
Daniel Nüst committed Apr 17, 2014
1 parent d719a6a commit c90bf4d
Show file tree
Hide file tree
Showing 17 changed files with 1,552 additions and 1,312 deletions.
1,234 changes: 55 additions & 1,179 deletions 52n-wps-r/src/main/java/org/n52/wps/server/r/GenericRProcess.java

Large diffs are not rendered by default.

Expand Up @@ -60,15 +60,18 @@ public class LocalRAlgorithmRepository implements ITransactionalAlgorithmReposit

private static Logger LOGGER = LoggerFactory.getLogger(LocalRAlgorithmRepository.class);

// registered Processes
// registered processes
private Map<String, String> algorithmMap;

// local cache for algorithm descriptions
private Map<String, GenericRProcess> algorithmDescriptionMap = new HashMap<String, GenericRProcess>();

private R_Config rConfig;

public LocalRAlgorithmRepository() {
LOGGER.info("Initializing LocalRAlgorithmRepository");
this.algorithmMap = new HashMap<String, String>();
this.rConfig = R_Config.getInstance();

// Check WPS Config properties:
RPropertyChangeManager changeManager = RPropertyChangeManager.getInstance();
Expand Down Expand Up @@ -99,7 +102,6 @@ private boolean checkStartUpConditions() {
// Try to build up a connection to Rserve
// If it is refused, a new instance of Rserve will be opened
LOGGER.debug("[Rserve] Trying to connect to Rserve.");
R_Config rConfig = R_Config.getInstance();
try {
RConnection testcon = rConfig.openRConnection();
LOGGER.info("[Rserve] WPS successfully connected to Rserve.");
Expand All @@ -125,7 +127,7 @@ private void addAllAlgorithms() {
String algorithm_wkn = property.getStringValue();

if (property.getName().equalsIgnoreCase(RWPSConfigVariables.ALGORITHM.toString())) {
processInfo = new RProcessInfo(algorithm_wkn);
processInfo = new RProcessInfo(algorithm_wkn, this.rConfig);
processInfoList.add(processInfo);
}
else
Expand All @@ -135,15 +137,15 @@ private void addAllAlgorithms() {
if ( !processInfo.isAvailable()) {
// property.setActive(false);
// propertyChanged=true;
LOGGER.error("[WPS4R] Missing R script for process " + algorithm_wkn
LOGGER.error("Missing R script for process " + algorithm_wkn
+ ". Process ignored. Check WPS configuration.");
continue;
}

if ( !processInfo.isValid()) {
// property.setActive(false);
// propertyChanged=true;
LOGGER.error("[WPS4R] Invalid R script for process "
LOGGER.error("Invalid R script for process "
+ algorithm_wkn
// + ". Process ignored. Check previous logs.");
+ ". You may enable/disable it manually from the Web Admin console. Check previous logs for details.");
Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.n52.wps.server.ExceptionReport;
import org.n52.wps.server.r.data.CustomDataTypeManager;
import org.n52.wps.server.r.syntax.RAnnotationException;
import org.n52.wps.server.r.util.RFileExtensionFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -262,7 +263,7 @@ private boolean registerRScripts(Repository repositoryDocument,
R_Config.getInstance().resetWknFileMapping();
File algorithmDir = new File(scriptDir);
if (algorithmDir.isDirectory()) {
File[] scripts = algorithmDir.listFiles(new R_Config.RFileExtensionFilter());
File[] scripts = algorithmDir.listFiles(new RFileExtensionFilter());
LOGGER.debug("Loading script files from " + algorithmDir + ": " + Arrays.toString(scripts));
for (File scriptf : scripts) {
try {
Expand Down
42 changes: 16 additions & 26 deletions 52n-wps-r/src/main/java/org/n52/wps/server/r/R_Config.java
Expand Up @@ -26,18 +26,17 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package org.n52.wps.server.r;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.n52.wps.ServerDocument.Server;
import org.n52.wps.commons.WPSConfig;
Expand All @@ -58,8 +57,6 @@

public class R_Config {

private final int TEMPDIR_NAME_LENGTH = 8;

private static Logger LOGGER = LoggerFactory.getLogger(R_Config.class);

public static final String SCRIPT_FILE_EXTENSION = "R";
Expand Down Expand Up @@ -91,7 +88,7 @@ public class R_Config {

private RConnector connector = new RConnector();

private RAnnotationParser annotationParser = new RAnnotationParser();
private RAnnotationParser annotationParser;

/** Maps current R-script files to identifiers **/
private HashMap<File, String> fileToWknMap = new HashMap<File, String>();
Expand All @@ -117,6 +114,8 @@ private R_Config() {
catch (Exception e) {
LOGGER.error("Error getting full path of baseDir and configDir.", e);
}

this.annotationParser = new RAnnotationParser(this);
}

public static R_Config getInstance() {
Expand Down Expand Up @@ -185,7 +184,7 @@ public URL getScriptURL(String wkn) throws MalformedURLException, ExceptionRepor
return url;
}

public String getUrlPathUpToWebapp() {
private String getUrlPathUpToWebapp() {
Server server = WPSConfig.getInstance().getWPSConfig().getServer();
String host = server.getHostname();
String port = server.getHostport();
Expand All @@ -208,19 +207,6 @@ public URL getOutputFileURL(String currentWorkdir, String filename) throws IOExc
return new URL(urlString);
}

public static class RFileExtensionFilter implements FileFilter {

@Override
public boolean accept(File f) {
if (f.isFile() && f.canRead()) {
String name = f.getName();
if (name.endsWith(R_Config.SCRIPT_FILE_SUFFIX))
return true;
}
return false;
}
}

void registerScript(File file) throws FileNotFoundException, RAnnotationException, IOException, ExceptionReport {
if ( !fileToWknMap.containsKey(file.getAbsoluteFile())) {
FileInputStream fis = new FileInputStream(file);
Expand Down Expand Up @@ -279,13 +265,6 @@ public void resetWknFileMapping() {
this.wknConflicts.clear();
}

public String createTemporaryWPSWorkDir() {
File tempdir = new File(System.getProperty("java.io.tmpdir"), "wps4r-wps-workdir-tmp-"
+ UUID.randomUUID().toString().substring(0, TEMPDIR_NAME_LENGTH));
tempdir.mkdir();
return tempdir.getAbsolutePath();
}

public String getScriptDirFullPath() {
return new File(WebProcessingService.BASE_DIR, getConfigVariable(RWPSConfigVariables.SCRIPT_DIR)).getAbsolutePath();
}
Expand Down Expand Up @@ -407,4 +386,15 @@ public String getCurrentSessionInfo() throws RserveException, REXPMismatchExcept
rCon.close();
return info;
}

public URL getProcessDescriptionURL(String processWKN) {
String s = getUrlPathUpToWebapp() + "/WebProcessingService?Request=DescribeProcess&identifier=" + processWKN;
try {
return new URL(s);
}
catch (MalformedURLException e) {
LOGGER.error("Could not create URL for process {}", processWKN, e);
return null;
}
}
}
Expand Up @@ -54,13 +54,11 @@ public String getResourceValue() {
return this.resourceValue;
}

public URL getFullResourceURL() {
R_Config config = R_Config.getInstance();
String dirUrl = config.getResourceDirURL();
public URL getFullResourceURL(String resourceDirUrl) {

String fullResourceURL = null;
if (dirUrl != null)
fullResourceURL = dirUrl + "/" + this.resourceValue;
if (resourceDirUrl != null)
fullResourceURL = resourceDirUrl + "/" + this.resourceValue;
else
fullResourceURL = "http://not_available/" + this.resourceValue;

Expand All @@ -83,10 +81,10 @@ public URL getFullResourceURL() {
return resourceURL;
}

public File getFullResourcePath() {
public File getFullResourcePath(R_Config config) {
String fullResourcePath = null;
try {
fullResourcePath = R_Config.getInstance().getConfigVariableFullPath(RWPSConfigVariables.RESOURCE_DIR)
fullResourcePath = config.getConfigVariableFullPath(RWPSConfigVariables.RESOURCE_DIR)
+ File.separatorChar + this.resourceValue;
}
catch (ExceptionReport e) {
Expand Down
53 changes: 27 additions & 26 deletions 52n-wps-r/src/main/java/org/n52/wps/server/r/info/RProcessInfo.java
Expand Up @@ -26,6 +26,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package org.n52.wps.server.r.info;

import java.io.File;
Expand All @@ -51,79 +52,80 @@ public class RProcessInfo {

private boolean isValid;

private R_Config config;

static List<RProcessInfo> rProcessInfoList;

public RProcessInfo(String wkn) {
public RProcessInfo(String wkn, R_Config config) {
this.wkn = wkn;
this.config = config;

File scriptfile;
FileInputStream fis = null;
try {
scriptfile = R_Config.getInstance().getScriptFileForWKN(wkn);
RAnnotationParser parser = new RAnnotationParser();
scriptfile = config.getScriptFileForWKN(wkn);
RAnnotationParser parser = new RAnnotationParser(this.config);
fis = new FileInputStream(scriptfile);
this.isValid = parser.validateScript(fis, wkn);
} catch (Exception e) {
}
catch (Exception e) {
LOGGER.error("Script validation failed. Last exception stored for the process information.", e);
this.lastException = e;
this.isValid = false;
} finally {
}
finally {
if (fis != null)
try {
fis.close();
} catch (IOException e) {
}
catch (IOException e) {
LOGGER.error("Could not close file input stream of script file.", e);
}
}
}

public String getWkn()
{
public String getWkn() {
return this.wkn;
}

public String getScriptURL()
{
public String getScriptURL() {
try {
return R_Config.getInstance().getScriptURL(this.wkn).getPath();
} catch (ExceptionReport e) {
return config.getScriptURL(this.wkn).getPath();
}
catch (ExceptionReport e) {
e.printStackTrace();
} catch (MalformedURLException e) {
}
catch (MalformedURLException e) {
e.printStackTrace();
}

return null;
}

public boolean isAvailable()
{
return R_Config.getInstance().isScriptAvailable(this.wkn);
public boolean isAvailable() {
return config.isScriptAvailable(this.wkn);
}

public boolean isValid()
{
public boolean isValid() {

return this.isValid;
}

public Exception getLastException()
{
public Exception getLastException() {
return this.lastException;
}

/**
* @return The last Error message or null
*/
public String getLastErrormessage()
{
public String getLastErrormessage() {
if (getLastException() == null)
return null;
else
return getLastException().getMessage();
}

public static List<RProcessInfo> getRProcessInfoList()
{
public static List<RProcessInfo> getRProcessInfoList() {
if (rProcessInfoList == null) {
rProcessInfoList = new ArrayList<RProcessInfo>();
}
Expand All @@ -135,8 +137,7 @@ public static List<RProcessInfo> getRProcessInfoList()
*
* @param rProcessInfoList
*/
public static void setRProcessInfoList(List<RProcessInfo> rProcessInfoList)
{
public static void setRProcessInfoList(List<RProcessInfo> rProcessInfoList) {
RProcessInfo.rProcessInfoList = rProcessInfoList;
}

Expand Down
Expand Up @@ -43,6 +43,7 @@
import net.opengis.wps.x100.ProcessDescriptionType;

import org.n52.wps.server.ExceptionReport;
import org.n52.wps.server.r.R_Config;
import org.n52.wps.server.r.data.R_Resource;
import org.n52.wps.server.r.syntax.RAnnotation;
import org.n52.wps.server.r.syntax.RAnnotationException;
Expand All @@ -61,7 +62,10 @@ public class RAnnotationParser {

private static Logger LOGGER = LoggerFactory.getLogger(RAnnotationParser.class);

public RAnnotationParser() {
private R_Config config;

public RAnnotationParser(R_Config config) {
this.config = config;
LOGGER.info("New " + this);
}

Expand All @@ -81,7 +85,7 @@ public boolean validateScript(InputStream script,
// try to parse annotations:
List<RAnnotation> annotations = parseAnnotationsfromScript(script);
// try to create process description:
RProcessDescriptionCreator descriptionCreator = new RProcessDescriptionCreator();
RProcessDescriptionCreator descriptionCreator = new RProcessDescriptionCreator(this.config);

// TODO: WPS.des and WPS.res should only occur once or not at all
try {
Expand Down Expand Up @@ -266,7 +270,9 @@ private RAnnotation createResourceAnnotation(String attributeString) throws IOEx

// add empty hasmap for now
HashMap<RAttribute, Object> attributeHash = new HashMap<RAttribute, Object>();
ResourceAnnotation resourceAnnotation = new ResourceAnnotation(attributeHash, resources);
ResourceAnnotation resourceAnnotation = new ResourceAnnotation(attributeHash,
resources,
config.getResourceDirURL());

return resourceAnnotation;
}
Expand Down

0 comments on commit c90bf4d

Please sign in to comment.