[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Thu Apr 12 01:02:29 UTC 2007


gordonp
Wed Apr 11 21:02:29 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services
In directory dev.open-bio.org:/tmp/cvs-serv30722/src/main/ca/ucalgary/services

Modified Files:
	ACDService.java 
Log Message:
Made servlet param fetching more robust, allow advanced parameters in ACD files to be optionally published, service name now carried from MobyServlet.getServiceName()
moby-live/Java/src/main/ca/ucalgary/services ACDService.java,1.1,1.2
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/ACDService.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/ACDService.java	2007/03/12 14:32:02	1.1
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/ACDService.java	2007/04/12 01:02:29	1.2
@@ -27,6 +27,7 @@
     // Used for web.xml configuration
     public final static String EMBOSS_ROOT_PARAM = "embossRoot";
     public final static String EMBOSS_PARAMS_PARAM = "embossParams";
+    public final static String EMBOSS_ADV_PARAMS_PARAM = "embossUseAdvancedParams";
     public final static String EMBOSS_OUTPUT_PARAM = "embossOutput";
     public final static String ACD_FILE_PARAM = "acdFile";    
     public final static String ACD_RULES_LOCATION_PARAM = "regexRulesLoc";
@@ -43,6 +44,7 @@
     private MobyClient mobyClient;
     // For converting MOBY data to plain-text representation
     private TextClient textClient;
+    private MobyDataType binaryDataType;
 
     // Keep track of what MOBY input parameters are to be converted to what ACD types
     private Map<String,String> acdTypes;
@@ -111,7 +113,7 @@
 					       MobyServiceException.INTERNAL_PROCESSING_ERROR, 
 					       request.getID(), 
 					       "The requested output parameter in the servlet configuration (" +
-					       outputName + "could not be created from the program's results data, please " +
+					       outputName + ") could not be created from the program's results data, please " +
 					       " ask the service provider to correct the output transformation rules");
 	    }
 	    result.put(outputName, mdi);
@@ -134,30 +136,48 @@
 	Map<String, File> tempFiles = new HashMap<String, File>(); 
 
 	// Primary input
-	String stdin = "";
+	byte[] stdin = null;
 	for(MobyPrimaryData mobyInputTemplate: inputs){
 	    // Retrieve the input with the same name as the service template specifies
 	    String paramName = mobyInputTemplate.getName();
 	    MobyDataInstance inputData = request.get(paramName);
-	    String inputTextData = textClient.getText(inputData, acdTypes.get(mobyInputTemplate.getName()));
+	    byte[] inputDataBytes = null;
+
+	    // Transform the moby data to text, unless it's binary data, which will be passed as decoded bytes
+	    // Now, for binary data, we have to ignore any fields other than the Base64 encoded one.  Sorry!
+	    String tempFileSuffix = ".txt";
+	    if(mobyInputTemplate.getDataType().inheritsFrom(binaryDataType)){
+		inputDataBytes = org.apache.axis.encoding.Base64.decode(MobyDataBytes.ENCODED_MEMBER_NAME);
+		tempFileSuffix = ".bin";
+	    }
+	    else{
+		String inputTextData = textClient.getText(inputData, acdTypes.get(mobyInputTemplate.getName()));
+		if(inputTextData == null){
+		    throw new NullPointerException("The TextClient returned null after transforming the " +
+						   "input parameter "  + mobyInputTemplate.getName() + 
+						   " to text type " + acdTypes.get(mobyInputTemplate.getName()));
+		}
+		inputDataBytes = inputTextData.getBytes();
+	    }
 
 	    // Create the required command-line flag for the parameter
 	    if(inputs.length == 1){
 		// If only one input, we can pass it via stdin 
 		command.add("-"+paramName);
 		command.add("stdin");
-		stdin = inputTextData;
+		stdin = inputDataBytes;
 	    }
 	    else{
 		// Otherwise we need to create temporary files to store the data
-		File tempFile = File.createTempFile("ACDService."+programBinaryFile.getName()+".input."+paramName, ".txt");
+		File tempFile = File.createTempFile("ACDService."+programBinaryFile.getName()+".input."+paramName, 
+						    tempFileSuffix);
 		command.add("-"+paramName);
 		command.add(tempFile.toString());
 		tempFiles.put(paramName, tempFile);
 		// Write the data to the file
-		FileWriter fileWriter = new FileWriter(tempFile);
-		fileWriter.write(inputTextData);
-		fileWriter.close();
+		FileOutputStream fileOS = new FileOutputStream(tempFile);
+		fileOS.write(inputDataBytes);
+		fileOS.close();
 	    }
 	}
 
@@ -179,7 +199,7 @@
 
 	// Primary output
 	for(MobyPrimaryData mobyOutputTemplate: outputs){
-	    // Retrieve the input with the same name as the service template specifies
+	    // Retrieve the output with the same name as the service template specifies
 	    String paramName = mobyOutputTemplate.getName();
 
 	    // Create the required command-line flag for the parameter
@@ -190,7 +210,12 @@
 	    }
 	    else{
 		// Otherwise we need to create temporary files to store the data
-		File tempFile = File.createTempFile("ACDService."+programBinaryFile.getName()+".output."+paramName, ".txt");
+		String tempFileSuffix = ".txt";
+		if(mobyOutputTemplate.getDataType().inheritsFrom(binaryDataType)){
+		    tempFileSuffix = ".bin";
+		}
+		File tempFile = File.createTempFile("ACDService."+programBinaryFile.getName()+".output."+paramName,
+						    tempFileSuffix);
 		command.add("-"+paramName);
 		command.add(tempFile.toString());
 		tempFiles.put(paramName, tempFile);
@@ -271,7 +296,7 @@
     /**
      * @return the standard output of the command
      */
-    private String runProgram(MobyDataJob request, String[] command, File workingDir, final String input) throws Exception{
+    private String runProgram(MobyDataJob request, String[] command, File workingDir, final byte[] input) throws Exception{
 	// TODO: ensure $embossRootDirName/lib is in the LD_LIBRARY_PATH, 
 	// what is the equivalent in Windows?
 	Process process = runtime.exec(command, 
@@ -302,10 +327,18 @@
 	new Thread(){
 		public void run(){
 		    try{
-			for(String line: input.split("\n")){
-			// Feed in the data one line at a time 
-			    stdin.write((line+"\n").getBytes());
-			    stdin.flush();
+			final int SENDING_BLOCK_SIZE = 1024;
+			for(int i = 0; i < input.length; i += SENDING_BLOCK_SIZE){
+			    // Last chunk
+			    if(i + SENDING_BLOCK_SIZE > input.length){
+				stdin.write(input, i, input.length-i);
+				break;
+			    }
+			    // Feed the data to the command-line process in chunks
+			    else{
+				stdin.write(input, i, SENDING_BLOCK_SIZE);
+				stdin.flush();
+			    }
 			}
 			stdin.flush();
 			stdin.close();
@@ -366,17 +399,13 @@
 	throws java.lang.Exception{
 	MobyService service = super.createServiceFromConfig(request);
 
-	javax.servlet.ServletConfig config = getServletConfig();
-
-	if(config == null){
-	    throw new Exception("There is no servlet configuration available");
+	if(getCoCInitParameter(EMBOSS_ROOT_PARAM) != null){
+	    embossRootDirName = getCoCInitParameter(EMBOSS_ROOT_PARAM);
 	}
-
-	if(config.getInitParameter(EMBOSS_ROOT_PARAM) == null){
+	else{
 	    throw new Exception("No parameter called " + EMBOSS_ROOT_PARAM + 
 				" was found in the servlet configuration");
 	}
-	embossRootDirName = config.getInitParameter(EMBOSS_ROOT_PARAM);
 	if(embossRootDirName.length() == 0){
 	    throw new Exception("Parameter " + EMBOSS_ROOT_PARAM +
 				" was blank in the servlet configuration");
@@ -393,7 +422,8 @@
 				") exists, but is not a directory, as expected");
 	}
 	// See if it's really the emboss root (has bin dir)
-	String embossBinDirName = embossRootDirName+File.separator+"bin";
+	String slash = File.separator;
+	String embossBinDirName = embossRootDirName+slash+"bin";
 	File embossBinDir = new File(embossBinDirName);
 	if(!embossBinDir.exists()){
 	    throw new Exception("The EMBOSS binaries directory inferred from " +
@@ -405,8 +435,23 @@
 				"the servlet configuration (" + embossBinDirName + 
 				") exists, but is not a directory, as expected");
 	}
-	String programName = config.getServletName();
-	
+
+	String programName = null;
+	if(getServletConfig() != null){
+	    programName = getServletConfig().getServletName();
+	}
+	if(programName == null && getServletContext() != null){
+	    programName = getServletContext().getServletContextName();
+	}
+	if(programName == null || programName.length() == 0){
+	    throw new Exception("Could not determine the program name, no servlet " +
+				"name or servlet context name is available");
+	}
+	if(programName.length() == 0){
+	    throw new Exception("The program name is blank, based on the available " +
+				"servlet and servlet context names");
+	}
+
 	// See that the emboss root and program work together
 	programBinaryFile = new File(embossBinDir, programName);
 	if(!programBinaryFile.exists()){
@@ -420,13 +465,12 @@
 				") exists, but is not a file, as expected");
 	}
 
-	String slash = File.separator;
 	// The default location EMBOSS uses is the default we'll use...
 	String acdFileName = embossRootDir.getPath()+slash+"share"+slash+"EMBOSS"+slash+
 	                     "acd"+slash+programName+".acd";
 	// Unless overriden in the config...
-	if(config.getInitParameter(ACD_FILE_PARAM) != null){
-	    acdFileName = config.getInitParameter(ACD_FILE_PARAM);
+	if(getCoCInitParameter(ACD_FILE_PARAM) != null){
+	    acdFileName = getCoCInitParameter(ACD_FILE_PARAM);
 	    if(acdFileName.length() == 0){
 		throw new Exception("Parameter " + ACD_FILE_PARAM +
 				    " was blank in the servlet configuration");
@@ -444,8 +488,8 @@
 	acdRootDirName = acdFile.getParent();
 
 	URL regexRulesURL = null;
-	if(config.getInitParameter(ACD_RULES_LOCATION_PARAM) != null){
-	    String rulesLocationName = config.getInitParameter(ACD_RULES_LOCATION_PARAM);
+	if(getCoCInitParameter(ACD_RULES_LOCATION_PARAM) != null){
+	    String rulesLocationName = getCoCInitParameter(ACD_RULES_LOCATION_PARAM);
 	    if(rulesLocationName.length() == 0){
 		throw new Exception("Parameter " + ACD_RULES_LOCATION_PARAM + 
 				    " was blank in the servlet configuration (please " +
@@ -489,8 +533,8 @@
 	}
 
 	URL xsltRulesURL = null;
-	if(config.getInitParameter(MOBY_RULES_LOCATION_PARAM) != null){
-	    String rulesLocationName = config.getInitParameter(MOBY_RULES_LOCATION_PARAM);
+	if(getCoCInitParameter(MOBY_RULES_LOCATION_PARAM) != null){
+	    String rulesLocationName = getCoCInitParameter(MOBY_RULES_LOCATION_PARAM);
 	    if(rulesLocationName.length() == 0){
 		throw new Exception("Parameter " + MOBY_RULES_LOCATION_PARAM + 
 				    " was blank in the servlet configuration (please " +
@@ -543,19 +587,21 @@
 	textClient = new TextClient();
 	textClient.addMappingsFromURL(xsltRulesURL);
 
+	boolean useAdvancedParams = Boolean.parseBoolean(getCoCInitParameter(EMBOSS_ADV_PARAMS_PARAM));
+
 	// All the parameters have been specified correctly, now check the ACD file
 	// and create the MOBYService signature from it.
 	try{
-	    configureServiceFromACDFile(service, acdFile);
+	    configureServiceFromACDFile(service, acdFile, useAdvancedParams);
 	}
 	catch(Exception e){
 	    log("While parsing the ACD file (" + acdFileName + ")", e);
 	    throw new Exception("While parsing the ACD file (" + acdFileName + "): " + e);
 	}
 
-	// Override params?
-	if(config.getInitParameter(EMBOSS_PARAMS_PARAM) != null){
-	    for(String param: config.getInitParameter(EMBOSS_PARAMS_PARAM).split(",")){
+	// Override params? (will also add non-existing params if you really want)
+	if(getCoCInitParameter(EMBOSS_PARAMS_PARAM) != null){
+	    for(String param: getCoCInitParameter(EMBOSS_PARAMS_PARAM).split(",")){
 		String[] specs = param.split(":");
 		if(specs.length != 2){		    
 		    log("While parsing the " + EMBOSS_PARAMS_PARAM + " specs, item \""+
@@ -573,8 +619,8 @@
 	// A parameter telling us what hard-coded output file name patterns
 	// are used in the ACD program (i.e. ones we can't specify ourselves on the command line,
 	// so we need to know them in order to hand them back to the user).
-	if(config.getInitParameter(EMBOSS_OUTPUT_PARAM) != null){
-	    String[] specs = config.getInitParameter(EMBOSS_OUTPUT_PARAM).split(":");
+	if(getCoCInitParameter(EMBOSS_OUTPUT_PARAM) != null){
+	    String[] specs = getCoCInitParameter(EMBOSS_OUTPUT_PARAM).split(":");
 	    if(specs.length != 2){
 		throw new Exception("While parsing the " + EMBOSS_OUTPUT_PARAM + "  specs, "+
 				    "the value did not have the expected \"name:regex\" format"); 
@@ -599,19 +645,24 @@
 	    }	    
 	}
 
+	binaryDataType = MobyDataType.getDataType(MobyDataBytes.BASE64_DATATYPE);
+	
 	return service;
     }
 
     /**
      * Parses the ACD file and sets the MOBY signature parameters appropriately.
      */
-    public void configureServiceFromACDFile(MobyService service, File acdFile) throws Exception{	
+    public void configureServiceFromACDFile(MobyService service, File acdFile, boolean useAdvancedParams) 
+	throws Exception{	
 	ACDFile parsedACDData = new ACDFile(acdFile);
 
 	configureServiceFromACDApplication(service, parsedACDData.getApplicationSection());
 	configureServiceFromACDInput(service, parsedACDData.getInputSection());
 	configureServiceFromACDParams(service, parsedACDData.getAdditionalParamsSection());
-	configureServiceFromACDParams(service, parsedACDData.getAdvancedParamsSection());
+	if(useAdvancedParams){
+	    configureServiceFromACDParams(service, parsedACDData.getAdvancedParamsSection());
+	}
 	configureServiceFromACDOutput(service, parsedACDData.getOutputSection());
     }
     
@@ -632,8 +683,8 @@
 				"but got \"" + entries.get(ACDFile.BLOCK_TYPE_KEY) + "\"");
 	}
 
-	// Get the service name and description
-	service.setName(entries.get(ACDFile.BLOCK_NAME_KEY));
+	// service.setName(entries.get(ACDFile.BLOCK_NAME_KEY));
+	// Get the service description
 	if(entries.containsKey("documentation")){
 	    service.setDescription(entries.get("documentation"));
 	}
@@ -660,9 +711,13 @@
 
 		// Make sure it maps to a MOBY Input
 		if(!paramUsed.containsKey(acdInputName)){
+		    String declInputNames = "";
+		    for(String inputName: paramUsed.keySet()){
+			declInputNames = inputName+" ";
+		    }
 		    throw new Exception("A required ACD input parameter ("+acdInputName+
 					") does not have a matching declaration in the " +
-					"servlet configuration");
+					"servlet configuration ( "+declInputNames+")");
 		}
 
 		MobyPrimaryData mobyPrimaryInput = paramUsed.get(acdInputName);




More information about the MOBY-guts mailing list