From: Peter S. <sch...@us...> - 2007-01-05 16:58:17
|
Update of /cvsroot/plb4jedit/plb4jedit/Plb/src/java/net/sf/plb4jedit/plb In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29703/Plb/src/java/net/sf/plb4jedit/plb Added Files: ProcessRunner.java PLBCompiler.java Log Message: added compilation support --- NEW FILE: PLBCompiler.java --- /* $Id: PLBCompiler.java,v 1.1 2007/01/05 16:58:14 schaefep Exp $ */ package net.sf.plb4jedit.plb; /* * Copyright 2001 Riege Software International, All Rights Reserved. This * software is the proprietary information of Riege Software International Use * is subject to license terms. */ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; import java.io.PrintStream; import org.apache.commons.cli.*; import org.apache.commons.lang.StringUtils; /** */ public class PLBCompiler { private Properties plbEnv; private File tmp; private String plbwinINIFile; public PLBCompiler() throws IOException { tmp = File.createTempFile("PLBCompiler",".log"); plbEnv = new Properties(); if (File.separator.equals("/")) { _unixDefaults(); } } private void _unixDefaults() { plbEnv.setProperty("PLB_TERM", "linux"); plbEnv.setProperty("PLB_SYSTEM", "/opt/rsi/plb"); plbEnv.setProperty("PLB_FNC", "lower"); plbEnv.setProperty("PLB_EOR", "LF, NOAUTO"); plbEnv.setProperty("PLBCMP_EXT", "dbs"); plbEnv.setProperty("PLB_PATH", "/opt/rsi/plb:/home/schaefer/cvsroot/procars/incs/rcd:/home/schaefer/cvsroot/procars/incs/io:/home/schaefer/cvsroot/procars/incs/misc"); plbEnv.setProperty("RSI_PLBCMD","plb"); plbEnv.setProperty("RSI_PLCDIR","/opt/rsi/plc"); } public void usePlbWinINIFile(String wini) throws Exception { plbwinINIFile = wini; } public void usePlbCompilerProperties(String cmpprops) throws Exception { Properties cmpenv = new Properties(); cmpenv.load(new FileInputStream(cmpprops)); Enumeration keys = cmpenv.propertyNames(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); if (StringUtils.isNotBlank(cmpenv.getProperty(key)) && key.toUpperCase().startsWith("RSI")) { plbEnv.setProperty(key.toUpperCase(), cmpenv.getProperty(key)); } } } public void compile(File plbSource) throws Exception { if (plbSource == null || !plbSource.exists()) { throw new IllegalArgumentException("PLB Source does not exist "); } final File workingDir = plbSource.getParentFile(); final String name = plbSource.getName(); String[] cmd; int i = -1; if (StringUtils.isNotBlank(plbwinINIFile)) { cmd = new String[14]; cmd[++i] = plbEnv.getProperty("RSI_PLBCMD","plb"); cmd[++i] = "-h"; cmd[++i] = "-i"; cmd[++i] = plbwinINIFile; } else { cmd = new String[11]; cmd[++i] = plbEnv.getProperty("RSI_PLBCMD","plb"); } cmd[++i] = "plbcmp"; //cmd[++i] = name; cmd[++i] = plbSource.getAbsolutePath(); cmd[++i] = plbEnv.getProperty("RSI_PLCDIR") + File.separator + StringUtils.replace(name, ".dbs", ".plc"); //cmd[++i] = "-ZH ZT ZG ZV PE 99999 \"x header\" s"; cmd[++i] = "-EP"; cmd[++i] = "\"x header\""; cmd[++i] = "S"; cmd[++i] = "ZH"; cmd[++i] = "ZT"; cmd[++i] = "ZG"; cmd[++i] = "ZV"; PrintStream devnull = new PrintStream(tmp); ProcessRunner.exec(cmd, plbEnv, workingDir, devnull, devnull); } public void analyzeListing(File plbSource) throws Exception { String listing = plbEnv.getProperty("RSI_PLCDIR") + File.separator + StringUtils.replace(plbSource.getName(), ".dbs", ".lst"); //ListingAnalyzer analyzer = new ListingAnalyzer(tmp.getAbsolutePath(), plbSource.getAbsolutePath()); ListingAnalyzer analyzer = new ListingAnalyzer(listing, plbSource.getAbsolutePath()); analyzer.printGeneric(); } public static void main(String[] args) throws Exception { CommandLineParser parser = new GnuParser(); Options options = new Options(); Option plbwinIni = OptionBuilder.withArgName("properties-file") .hasArg() .withDescription( "plbwin ini File") .create( "plbwini"); Option plbcmp = OptionBuilder.withArgName( "properties-file" ) .hasArg() .withDescription( "Configuration Properties for the PLBCompiler") .create( "plbcmp"); options.addOption(plbwinIni); options.addOption(plbcmp); CommandLine line = parser.parse( options, args ); PLBCompiler compiler = new PLBCompiler(); if (line.hasOption("plbwini")) { compiler.usePlbWinINIFile(line.getOptionValue("plbwini")); } if (line.hasOption("plbcmp")) { compiler.usePlbCompilerProperties(line.getOptionValue("plbcmp")); } args = line.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("Compiling " + args[i]); File plbSource = new File(args[i]); compiler.compile(plbSource); compiler.analyzeListing(plbSource); } } } --- NEW FILE: ProcessRunner.java --- /* $Id: ProcessRunner.java,v 1.1 2007/01/05 16:58:13 schaefep Exp $ */ package net.sf.plb4jedit.plb; import java.io.*; import java.util.Iterator; import java.util.Properties; /** * Runs Processes via Runtime..exec(..) avoiding some <a * href="www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html"> traps</a>. * * @author <a href="mailto:sch...@ri...">Peter Schaefer </a> * @version $Revision: 1.1 $ */ public class ProcessRunner { private ProcessRunner() { // avoid instances of this class } /** * execute a command via {@link Runtime.exec(String command, String envp[], File dir)} avoiding * some <a href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html">traps</a> * * @param cmd (mandatory) command and its arguments. Command is element at position 0. Arguments * are optional * @return exit status of the process * * @throws IllegalArgumentException if cmd.length < 1 * @throws IOException if command cmd[0] is not found * @throws InterruptedException if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown. */ public static int exec(String[] cmd) throws IOException, InterruptedException { return exec(cmd, null, null, null, null); } /** * execute a command via {@link Runtime.exec(String command, String envp[], File dir)} avoiding * some <a href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html">traps</a> * * @param cmd (mandatory) command and its arguments. Command is element at position 0. Arguments * are optional * @param envp (optional) environment for the process executing the command * @param workingDir the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process. * @param outputStream ( * @param errorStream * @return exit status of the process * * @throws IllegalArgumentException if cmd.length < 1 * @throws IOException if command cmd[0] is not found * @throws InterruptedException if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown. */ public static int exec(String[] cmd, Properties envp, File workingDir, PrintStream outputStream, PrintStream errorStream) throws IOException, InterruptedException { if (cmd == null || cmd.length < 1) { throw new IllegalArgumentException("invalid command specified"); } String[] environment; if (envp != null) { environment = new String[envp.size()]; int i = 0; for (Iterator it = envp.keySet().iterator(); it.hasNext();) { String key = (String) it.next(); String value = envp.getProperty(key); environment[i++] = key + "=" + value; } } else { environment = new String[0]; } if (false) { System.out.println("++++++++++++++ ProcessRunner debug information ++++++++++++++:"); if (workingDir != null) { System.out.println("Working directory="+workingDir.getAbsolutePath()+"\nEnvironment:"); } for (int i = 0; i < environment.length; i++) { System.out.println(environment[i]); } System.out.println("command line:"); for (int i = 0; i < cmd.length; i++) { System.out.print(cmd[i] + " "); } System.out.println("\n++++++++++++++ ProcessRunner debug information ++++++++++++++"); } Process process = Runtime.getRuntime().exec(cmd, environment, workingDir); if (errorStream == null) { errorStream = System.err; } if (outputStream == null) { outputStream = System.out; } StreamGobbler errorGobbler = new StreamGobbler( process.getErrorStream(), errorStream); StreamGobbler outputGobbler = new StreamGobbler(process .getInputStream(), outputStream); errorGobbler.start(); outputGobbler.start(); return process.waitFor(); } public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("USAGE: java ProcessRunner \"cmd arg1 arg2 arg3 ...\""); System.exit(1); } int exitVal = ProcessRunner.exec(args); System.out.println("ExitValue: " + exitVal); } } /** * Solution from an <a * href="www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html">article </a> * about pitfalls running external commands from java. These "gobblers" avoid * deadlocks * * @author <a href="mailto:sch...@ri...">Peter Schaefer </a> * @version $Revision: 1.1 $ */ class StreamGobbler extends Thread { InputStream is; PrintStream out; public StreamGobbler(InputStream is, PrintStream out) { this.is = is; this.out = out; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) out.println(line); } catch (IOException ioe) { ioe.printStackTrace(); } } } |