3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.FileInputStream;
6 import java.io.BufferedReader;
7 import java.io.BufferedInputStream;
8 import java.io.InputStreamReader;
9 import java.io.FileReader;
10 import java.util.Properties;
12 import java.util.TreeMap;
13 import java.util.ArrayList;
14 import java.util.Iterator;
16 import java.awt.GraphicsEnvironment;
22 throws FileNotFoundException, Exception
28 pb =
new ProcessBuilder();
29 prop = pb.environment();
33 prop.put(
"PATH", System.getenv(
"PATH"));
45 if ( args.length == 1 ){
46 String em =
"Error: The flag '-command' must be followed by at least one argument."
47 +
"The command line is '" + args[0] +
"'.";
48 throw new UnsupportedOperationException(em);
51 for(
int i = 1;
i < args.length;
i++)
61 boolean isJVMFlag(
final String[] args,
int i){
62 if (args[i].equals(
"-command"))
return false;
63 if (args[i].equals(
"-file"))
return false;
64 if (args[i].equals(
"-run"))
return false;
65 if (args[i].equals(
"-update"))
return false;
66 if (args[i].equals(
"-console"))
return false;
67 if (args[i].equals(
"-diagnostics"))
return false;
68 if (args[i].equals(
"-command"))
return false;
69 if (i == args.length-1)
return false;
70 if (
new File(args[i]).exists())
return false;
82 ArrayList<String> JVMFlags){
83 boolean getCommandFlags =
false;
84 boolean checkForJVMFlag =
true;
86 for (
int i = 0; i < args.length; i++){
87 if (checkForJVMFlag && isJVMFlag(args, i)){
88 JVMFlags.add(args[
i]);
91 checkForJVMFlag =
false;
92 if(args[i].equals(
"-file")){
94 if ( i == args.length -1 ){
95 String em =
"Error: The flag '-file' must be followed by at least one argument."
96 +
"The command line is'";
97 for(
int j = 0; j < args.length; j++)
100 throw new UnsupportedOperationException(em);
103 getCommandFlags =
false;
105 }
else if(args[i].equals(
"-console")){
108 getCommandFlags =
false;
109 }
else if(args[i].equals(
"-run")){
112 getCommandFlags =
false;
113 }
else if(args[i].equals(
"-update")){
116 getCommandFlags =
true;
117 }
else if(args[i].equals(
"-diagnostics")){
121 else if(getCommandFlags){
126 getCommandFlags =
false;
130 if (!
new File(
ptFile).exists())
131 throw new UnsupportedOperationException(
"Error: Argument '" +
ptFile +
"' is not a file.");
133 getCommandFlags =
true;
145 return userProp.getProperty(key);
155 String userPropertyFile;
159 userPropertyFile = root +
"/bin/systemVariables-linux.properties";
162 userPropertyFile = root +
"/bin/systemVariables-mac.properties";
165 userPropertyFile = root +
"\\bin\\systemVariables-windows.properties";
168 final String em =
"Unsupported operating system when trying to get system property file." +
170 throw new Exception(em);
172 return userPropertyFile;
183 void updateClasspath(){
185 final String osNam = System.getProperty(
"os.name").toLowerCase();
186 if (osNam.indexOf(
"windows") > -1){
187 Iterator it = prop.entrySet().iterator();
188 while (it.hasNext()) {
189 final Map.Entry pairs = (Map.Entry)it.next();
190 final String key = (String)pairs.getKey();
191 final String val = (String)pairs.getValue();
193 if (key.equalsIgnoreCase(
"CLASSPATH") && (!key.equals(
"CLASSPATH"))){
194 if (
prop.containsKey(
"CLASSPATH"))
195 prop.put(
"CLASSPATH",
prop.get(
"CLASSPATH") +
";" + val);
197 prop.put(
"CLASSPATH", val);
215 void setCommand(String[] args)
216 throws UnsupportedOperationException{
218 ArrayList<String> comLis =
new ArrayList<String>();
219 ArrayList<String> ptArg =
new ArrayList<String>();
220 ArrayList<String> JVMFlags =
new ArrayList<String>();
223 if (args.length > 0 && args[0].equals(
"-command")){
230 if ( comLis.size() == 0 ){
234 for ( Iterator<String> iter = JVMFlags.iterator(); iter.hasNext(); )
235 comLis.add( iter.next() );
242 comLis.add(
"-Dptolemy.ptII.isHeadless=true");
243 comLis.add(
"ptolemy.moml.MoMLCommandLineApplication");
246 comLis.add(
"ptolemy.actor.gui.PtExecuteApplication");
247 comLis.add(
"-bcvtb");
250 comLis.add(
"-classpath");
251 comLis.add(prop.get(
"BCVTB_HOME") +
PS +
"bin" +
PS +
"BCVTB.jar");
252 comLis.add(
"BCVTB.VersionUpdater");
255 comLis.add(
"ptolemy.vergil.VergilApplication");
256 comLis.add(
"-bcvtb");
259 for ( Iterator<String> iter = ptArg.iterator(); iter.hasNext(); )
260 comLis.add( iter.next() );
263 final String em =
"Error: If flag -console is used, a model file is required as an argument."
264 +
LS +
" Call program as java -jar bin/BCVTB.jar -console modelFile.xml";
265 throw new UnsupportedOperationException(em);
268 final String em =
"Error: If flag -update is used, a model file is required as an argument."
269 +
LS +
" Call program as java -jar bin/BCVTB.jar -update toVersion modelFile.xml"
270 +
LS +
" where 'toVersion' is the new version number, such as 1.2";
271 throw new UnsupportedOperationException(em);
305 final String osNam = System.getProperty(
"os.name").toLowerCase();
306 if (osNam.indexOf(
"windows") > -1){
308 prop.put(
"BCVTB_OS",
"windows");
310 else if (osNam.indexOf(
"mac") > -1){
312 prop.put(
"BCVTB_OS",
"mac");
314 else if (osNam.indexOf(
"linux") > -1){
316 prop.put(
"BCVTB_OS",
"linux");
319 throw new Exception(
"Unsupported operating system " +
320 System.getProperty(
"os.name") +
".");
328 throws FileNotFoundException {
330 final String localPS =
"/";
331 String dir = Launcher.class.getResource(
"Launcher.class").toString();
332 final String
start = (
os == osName.WINDOWS ) ?
"file:/" :
"file:";
333 dir = dir.substring(dir.indexOf(
start) + start.length(),
334 dir.indexOf(
"BCVTB.jar"));
336 if (dir.endsWith(localPS))
337 dir =
new String(dir.substring(0, dir.length()-1));
342 final String libPT = localPS +
"lib" + localPS +
"ptII";
343 while ( dir.indexOf(localPS) > -1 ){
344 final String fname = dir + libPT;
345 if (
new File(fname).exists()){
349 dir = dir.substring(0, dir.lastIndexOf(localPS, dir.length()-1));
355 if ( dir.equals(
"bin"))
357 if ( (!
new File(dir+libPT).exists()) ){
358 final String em =
"Error: Could not determine BCVTB root directory." +
LS
359 +
"Expected to find a directory that contains " + libPT +
LS
360 +
"Did you move the file BCVTB.jar?";
361 throw new FileNotFoundException(em);
363 if (
os == osName.WINDOWS)
364 dir = dir.replace(
"/",
"\\");
369 System.err.println(
"********** Environment variables:");
370 TreeMap<String, String> tm =
new TreeMap<String, String>();
373 Iterator it = tm.entrySet().iterator();
374 while (it.hasNext()) {
375 Map.Entry pairs = (Map.Entry)it.next();
376 System.out.println(pairs.getKey() +
" = " + pairs.getValue());
378 System.err.println(
"*********************************");
386 boolean _isSet(String var){
387 boolean ret = (var != null && var.trim().length() > 0);
402 prop.put(name, value);
405 private static String
_replaceAll(
final String s,
final String pattern,
final String value){
408 if (value != null && value.contains(pattern))
410 String ret =
new String(s);
411 int i = ret.indexOf(pattern);
415 ret =
new String(ret.substring(0, i) + ret.substring(i+pattern.length()));
417 ret =
new String(ret.substring(0, i) + value + ret.substring(i+pattern.length()));
418 i = ret.indexOf(pattern);
424 final String pattern,
427 final String osNam = System.getProperty(
"os.name").toLowerCase();
428 if (osNam.indexOf(
"windows") > -1)
429 ret =
_replaceAll(ret,
"%" + pattern +
"%" , value);
431 if (! pattern.equals(value)){
433 ret =
_replaceAll(ret,
"${" + pattern +
"}", value);
443 Iterator itOut = prop.entrySet().iterator();
444 while (itOut.hasNext()) {
445 Map.Entry pairsOut = (Map.Entry)itOut.next();
447 String keyOut = (String)pairsOut.getKey();
448 String valOut = (String)pairsOut.getValue();
450 Iterator itIn = prop.entrySet().iterator();
451 while (itIn.hasNext()) {
452 Map.Entry pairsIn = (Map.Entry)itIn.next();
453 final String keyIn = (String)pairsIn.getKey();
454 final String valIn = (String)pairsIn.getValue();
457 prop.put(keyOut, valOut);
464 Iterator it = prop.entrySet().iterator();
465 while (it.hasNext()) {
466 Map.Entry pairs = (Map.Entry)it.next();
467 final String key = (String)pairs.getKey();
468 final String val = (String)pairs.getValue();
470 Iterator itUsePro = userProp.entrySet().iterator();
471 while (itUsePro.hasNext()){
472 Map.Entry pairUsePro = (Map.Entry)itUsePro.next();
473 final String keyUsePro = (String)pairUsePro.getKey();
474 final String valUsePro = (String)pairUsePro.getValue();
475 userProp.setProperty(keyUsePro,
485 Iterator it = userProp.entrySet().iterator();
486 while (it.hasNext()) {
487 Map.Entry pairs = (Map.Entry)it.next();
488 final String key = (String)pairs.getKey();
489 final String val = (String)pairs.getValue();
491 Iterator itUsePro = userProp.entrySet().iterator();
492 while (itUsePro.hasNext()){
493 Map.Entry pairUsePro = (Map.Entry)itUsePro.next();
494 final String keyUsePro = (String)pairUsePro.getKey();
495 final String valUsePro = (String)pairUsePro.getValue();
496 userProp.setProperty(keyUsePro,
509 throws FileNotFoundException, Exception{
513 prop.put(
"BCVTB_HOME", root);
516 String ptIIDir = prop.get(
"PTII");
517 if ( ! _isSet(ptIIDir) ){
518 ptIIDir = root +
PS +
"lib" +
PS +
"ptII";
519 prop.put(
"PTII", ptIIDir);
526 userProp.loadFromXML(
new FileInputStream(userPropertyFile));
528 catch(IOException e){
529 throw new IOException(
"IOException when reading user property file." +
LS +
535 Iterator itUsePro = userProp.entrySet().iterator();
536 while (itUsePro.hasNext()){
537 Map.Entry pairUsePro = (Map.Entry)itUsePro.next();
538 final String key = (String)pairUsePro.getKey();
539 String val = (String)pairUsePro.getValue();
540 if ( val.indexOf(
LS) != -1 ){
542 userProp.setProperty(key, val);
552 Iterator itu = userProp.entrySet().iterator();
553 while (itu.hasNext()) {
554 Map.Entry pairs = (Map.Entry)itu.next();
555 final String key = (String)pairs.getKey();
556 final String val = ((String)pairs.getValue()).trim();
572 Process p = pb.redirectErrorStream(
true).
start();
573 writeProcessOutput(p);
574 retVal = p.waitFor();
576 catch (IOException e) {
577 System.err.println(e.getMessage());
579 catch (InterruptedException e) {
580 System.err.println(e.getMessage());
582 catch (Exception e) {
583 System.err.println(e.getMessage());
589 static void writeProcessOutput(Process process)
throws Exception{
590 InputStreamReader tempReader =
new InputStreamReader(
591 new BufferedInputStream(process.getInputStream()));
592 BufferedReader reader =
new BufferedReader(tempReader);
594 String line = reader.readLine();
597 System.out.println(line);
605 static void showThrowable(Throwable t){
606 System.err.println(t.getMessage());
610 final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
612 if (!
console && !ge.isHeadless())
613 javax.swing.JOptionPane.showMessageDialog(null,
616 javax.swing.JOptionPane.ERROR_MESSAGE);
621 public static void main(String[] args){
623 if (args.length ==2 && args[0].equals(
"--getEnvironmentVariable")){
628 final String key = args[1];
629 final String value = l.getProperty(key);
630 if ( value == null ){
631 final String filNam = l.getUserPropertyFileName();
632 final String em =
"Did not find the entry '" + key +
"' in the system variables " +
LS +
633 "file '" + filNam +
"'." +
LS +
634 "Verify that '" + filNam +
"'"+
LS +
635 "has an entry of the form" +
LS +
636 " <entry key=\"" + key +
"\">value</entry>";
637 Exception e =
new Exception(em);
650 System.out.println(value);
675 l.updateSystemFile();
676 retVal = l.startProcess();
678 catch(NoClassDefFoundError e){
694 throws FileNotFoundException, IOException, Exception{
697 boolean foundText =
false;
699 FileReader fr =
new FileReader(
ptFile);
700 BufferedReader reader =
new BufferedReader(fr);
702 while ((lin = reader.readLine()) != null) {
703 if ((lin.indexOf(
"property name=\"startTime\"") > -1 )||
704 (lin.indexOf(
"property name=\"finalTime\"") > -1 )){
712 String[] args = {
"1.1",
ptFile};
713 VersionUpdater.main(args);
729 protected Map<String,String>
prop;
735 protected ProcessBuilder
pb;
738 protected static final String
PS = System.getProperty(
"file.separator");
741 protected static final String
LS = System.getProperty(
"line.separator");
744 enum osName {WINDOWS, LINUX, MAC}
void _replaceEnvironmentVariables()
Resolves all references to existing environment variables in the user properties. ...
boolean update
Flag, true if user set -update flag.
Properties userProp
User properties.
static final String PS
File separator.
osName os
The operating system.
ProcessBuilder pb
The process builder to start Ptolemy.
void _resolveReferences()
Resolves all references within the user properties.
void resolveEnvironmentVariable()
Resolves all references to environment variables in the properties.
void setupCommandList(String[] args, ArrayList< String > comLis)
Sets up the argument list if the first flag is -command
static String _replaceAll(final String s, final String pattern, final String value)
void setSystemVariable(final String name, final String value)
Sets the variable to the system properties.
static void XMLCALL start(void *data, const char *el, const char **attr)
Call back functions that will be used by the expat xml parser.
static void main(String[] args)
Main method.
String originalPath
The path variable.
static int startBCVTB(String[] args)
Starts the BCVTB.
static String _replaceAllSystemVariables(final String s, final String pattern, final String value)
String ptFile
Ptolemy file, or null if not specified.
static final String LS
Line separator.
String getProperty(final String key)
Gets a system property.
void setupPtolemyArguments(String[] args, ArrayList< String > ptArgs, ArrayList< String > JVMFlags)
Sets up the argument list if the first flag is not -command
void initializeEnvironmentVariables()
Initializes the settings of environment variables.
String getBCVTBRoot()
Gets the root directory of the BCVTB.
int startProcess()
Starts the process.
void updateSystemFile()
Update the ptFile if it is from an old version.
boolean run
Flag, true if user set -run flag.
void detectSystemVariables()
Detect system variables.
static boolean console
Flag, true if user set -console flag.
Map< String, String > prop
The properties of the process builder.
String getUserPropertyFileName()
Get the user property file name.
static void _dumpProperties(Map< String, String > p)