001/* 002 * NativeAppLauncher.java 003 * $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/tools/NativeAppLauncher.java,v $ 004 * $Revision: 563 $ $Date: 2012-11-03 19:28:37 -0700 (Sat, 03 Nov 2012) $ $Locker: $ 005 * Copyright 2002 (C) WebARTS Design. All rights reserved. 006 * 007 * This program is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU General Public License 009 * as published by the Free Software Foundation; either version 2 010 * of the License, or any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 020 */ 021 022package ca.bc.webarts.tools; 023 024import java.io.File; 025import java.net.URL; 026import java.net.MalformedURLException; 027import java.util.Properties; 028 029/** 030 * This Wraps the System Exec Call for a native application. 031 * It can be used from within an existing java app via instantiating this class 032 * or run from the commandline (mainly for testing). 033 * 034 * 035 * <p>Author: <a href="mailto:tgutwin@webarts.bc.ca">Tom Gutwin P.Eng.</a> 036 * <br />Copyright (C) 2002 <a href="http://www.webarts.bc.ca">Web<i>ARTS</i> 037 * Design</a>, North Vancouver Canada. All Rights Reserved.</p> 038 * @author <a href="mailto:tgutwin@webarts.bc.ca">Tom Gutwin P.Eng.</a> 039 **/ 040public class NativeAppLauncher extends AutoUpdateApp 041{ 042 public int retVal = 0; 043 /** 044 * Basic constructor adds to super to set the name and properties for the 045 * application. This entry point simply takes the passed properties and 046 * calls super.runApp - 047 * <A HREF="#runApp(java.util.Properties)">runApp(Properties)</A>method. 048 * 049 * @see #main 050 * @see ca.bc.webarts.tools.AutoUpdateApp#runApp 051 **/ 052 public NativeAppLauncher(String appFilename, String[] appArgs) 053 { 054 if (System.getProperty("os.name").equals("OS/2")) 055 launchOS2App(appFilename, appArgs); 056 else 057 { 058 //launchApp(appFilename, appArgs); 059 retVal = executeNativeApp(appFilename, appArgs); 060 } 061 } 062 063 064 /** 065 * A helper method when lauching OS/2 apps to wrap the setting up of the 066 * Properties to send to the <a href="#runApp(Properties)">runApp </a> method. 067 * The OS/2 System Exec call has troubles exececuting *.cmd files because it 068 * tries to spawn a DOS command window and pukes. This forces an OS/2 cmd 069 * session. 070 **/ 071 private void launchOS2App(String appFilename, String[] appArgs) 072 { 073 String driveLetter = "C:"; 074 String os2Cmd = driveLetter + "\\os2\\cmd.exe"; 075 String [] newAppArgs = null; 076 if (appArgs != null) 077 { 078 newAppArgs = new String[appArgs.length+2]; 079 newAppArgs[0] = "/c"; 080 newAppArgs[1] = appFilename; 081 for (int i=0; i < appArgs.length; i++) 082 newAppArgs[i+2] = appArgs[i]; 083 } 084 launchApp(os2Cmd, newAppArgs); 085 086 } 087 088 089 /** 090 * A helper method to wrap the setting up of the Properties to send to the 091 * <a href="#runApp(Properties)">runApp </a> method. 092 **/ 093 private void launchApp(String appFilename, String[] appArgs) 094 { 095 if (appFilename!=null && !appFilename.equals("")) 096 { 097 File appFile = new File(appFilename); 098 if (appFile.canRead()) 099 { 100 Properties nativeAppProps = new Properties(); 101 nativeAppProps.setProperty("appType", NATIVE_APP); 102 String appFileUrlStr = ""; 103 try 104 { 105 URL appFileUrl = appFile.toURL(); 106 appFileUrlStr = appFileUrl.toString(); 107 } 108 catch (MalformedURLException urlEx) 109 { 110 //try it this way 111 appFileUrlStr = "file:////"+appFilename; 112 } 113 nativeAppProps.setProperty("remoteAppURL", appFileUrlStr); 114 appName_ = appFile.getName(); 115 if (appArgs!=null) 116 for (int i=0; i < appArgs.length; i++) 117 nativeAppProps.setProperty("arg_" + i ,appArgs[i]); 118 runApp(nativeAppProps); 119 } 120 } 121 } 122 123 124 /** 125 * The main entry for this app. It takes as many args as required on the 126 * commandline to run the requested app. It wraps converts them to the 127 * required Properties object then runs the app. 128 * 129 * @param args are the commandline parameters.This app expects the 1st 130 * parameter as the absolute executable file path. The rest are 131 * passed as arguments to the executable that is being launched. 132 * 133 * @see #runApp 134 **/ 135 public static void main(String [] args) 136 { 137 if (args.length > 0) 138 { 139 String [] passedParms = new String [args.length-1]; 140 for (int i=1; i< args.length; i++) 141 passedParms[i-1] = args[i]; 142 NativeAppLauncher me = new NativeAppLauncher(args[0], passedParms); 143 } 144 else 145 System.out.println("NativeAppLauncher ERROR - incorrect number of" + 146 "commandline args:\nUSAGE: java NativeAppLauncher " + 147 "NativeAppPropertyFileName " ); 148 System.exit(0); 149 } 150}