You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. /* *******************************************************************
  2. * Copyright (c) 2000-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package $installer$.org.aspectj;
  14. import java.awt.BorderLayout;
  15. import java.awt.Component;
  16. import java.awt.Dimension;
  17. import java.awt.FlowLayout;
  18. import java.awt.Font;
  19. import java.awt.GridBagConstraints;
  20. import java.awt.GridBagLayout;
  21. import java.awt.Toolkit;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.WindowAdapter;
  25. import java.awt.event.WindowEvent;
  26. import java.awt.event.WindowListener;
  27. import java.io.BufferedOutputStream;
  28. import java.io.BufferedReader;
  29. import java.io.BufferedWriter;
  30. import java.io.File;
  31. import java.io.FileInputStream;
  32. import java.io.FileOutputStream;
  33. import java.io.FileWriter;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.io.OutputStream;
  38. import java.io.PrintStream;
  39. import java.lang.reflect.InvocationTargetException;
  40. import java.net.JarURLConnection;
  41. import java.net.URL;
  42. import java.util.Enumeration;
  43. import java.util.Map;
  44. import java.util.Properties;
  45. import java.util.jar.JarEntry;
  46. import java.util.jar.JarFile;
  47. import java.util.zip.ZipEntry;
  48. import java.util.zip.ZipInputStream;
  49. import javax.swing.BorderFactory;
  50. import javax.swing.BoxLayout;
  51. import javax.swing.Icon;
  52. import javax.swing.JButton;
  53. import javax.swing.JEditorPane;
  54. import javax.swing.JFileChooser;
  55. import javax.swing.JFrame;
  56. import javax.swing.JLabel;
  57. import javax.swing.JOptionPane;
  58. import javax.swing.JPanel;
  59. import javax.swing.JProgressBar;
  60. import javax.swing.JTextArea;
  61. import javax.swing.JTextField;
  62. import javax.swing.SwingUtilities;
  63. import javax.swing.border.Border;
  64. import javax.swing.border.CompoundBorder;
  65. import javax.swing.border.EmptyBorder;
  66. /**
  67. * Invoke the Installer gui. There are two ways to run without GUI by passing parameters to main:
  68. * <ol>
  69. * <li>pass <code>-text {pathToPropertiesFile}</code>:
  70. * <ul>
  71. * <li>"-text".equals(arg[0])</li>
  72. * <li>arg[1] is the path to a properties file which defines name="output.dir" value="{path to output dir}" name="context.javaPath"
  73. * value="{path to JDKDIR}", i.e,.
  74. *
  75. * <pre>
  76. * output.dir=c:/latest
  77. * "context.javaPath=c:/apps/jdk1.3.1
  78. * </pre>
  79. *
  80. * </li>
  81. * <li>outputDir must be created and empty (i.e., no overwriting</li>
  82. * <li>the VM being invoked should be the target vm</li>
  83. * </ul>
  84. * </li>
  85. * <li>pass <code>-to {pathToTargetDir}</code>:
  86. * <ul>
  87. * <li>"-to".equals(arg[0])</li>
  88. * <li>arg[1] is the path to a writable install directory.</li>
  89. * </ul>
  90. * </li>
  91. */
  92. public class Main {
  93. public static void main(String[] args) {
  94. Options.loadArgs(args);
  95. boolean hasGui = true;
  96. Properties properties = new Properties();
  97. InputStream istream = null;
  98. try {
  99. istream = Main.class.getResourceAsStream(Installer.RESOURCE_DIR + "/properties.txt");
  100. if (istream == null) {
  101. System.err.println("unable to load properties.txt using Main.class - exiting");
  102. Main.exit(-1);
  103. }
  104. properties.load(istream);
  105. // when running outside GUI, load values into properties
  106. // so that property-value resolution works
  107. // (otherwise, could just set values below).
  108. // XXX not sure if this indirection is actually needed.
  109. if (null != Options.textProperties) {
  110. istream.close();
  111. istream = new FileInputStream(Options.textProperties);
  112. properties.load(istream);
  113. hasGui = false;
  114. } else if (null != Options.targetDir) {
  115. String path = null;
  116. try {
  117. path = Options.targetDir.getCanonicalPath();
  118. } catch (IOException e) {
  119. path = Options.targetDir.getAbsolutePath();
  120. }
  121. String javaPath = ConfigureLauncherPane.getDefaultJavaHomeLocation();
  122. if (null == javaPath) {
  123. System.err.println("using GUI - unable to find java");
  124. } else {
  125. properties.setProperty("output.dir", path);
  126. properties.setProperty("context.javaPath", javaPath);
  127. hasGui = false;
  128. }
  129. }
  130. } catch (IOException ioe) {
  131. handleException(ioe);
  132. } finally {
  133. if (null != istream) {
  134. try {
  135. istream.close();
  136. } catch (IOException e) {
  137. } // ignore
  138. }
  139. }
  140. try {
  141. String className = (String) properties.get("installer.main.class");
  142. //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  143. Installer installer = (Installer) Class.forName(className).newInstance();
  144. InstallContext installerContext = new InstallContext(properties);
  145. installerContext.setHasGui(hasGui);
  146. installer.setContext(installerContext);
  147. if (installerContext.hasGui()) { // let context force whether or not to run gui
  148. installer.runGUI();
  149. } else {
  150. // set output dir and java path in context after minimal validation
  151. String propName = "output.dir";
  152. String propValue = properties.getProperty(propName);
  153. if (null == propValue) {
  154. throw new Exception("expecting property " + propName);
  155. }
  156. String outputDirName = propValue;
  157. propName = "context.javaPath";
  158. propValue = properties.getProperty(propName);
  159. if (null == propValue) {
  160. throw new Exception("expecting property " + propName);
  161. }
  162. String javaPath = propValue;
  163. File outputDir = new File(outputDirName);
  164. if (!outputDir.isDirectory()) {
  165. throw new Exception("not a dir outputDirName: " + outputDirName + " dir: " + outputDir);
  166. }
  167. if (!outputDir.canWrite()) {
  168. throw new Exception("cannot write outputDirName: " + outputDirName + " dir: " + outputDir);
  169. }
  170. InstallContext context = installer.getContext(); // todo: why not use installerContext?
  171. context.setOutputDir(outputDir);
  172. context.javaPath = new File(javaPath);
  173. // todo: check javaPath for ... bin/java? lib/rt.jar?
  174. if (!outputDir.isDirectory() || !outputDir.canRead()) {
  175. throw new Exception("invalid javaPath: " + javaPath);
  176. }
  177. // directly set context and run
  178. WizardPane.setContext(installerContext);
  179. installer.run();
  180. }
  181. } catch (Exception e) {
  182. handleException(e);
  183. }
  184. }
  185. public static void handleException(Throwable e) {
  186. System.out.println("internal error: " + e.toString());
  187. e.printStackTrace();
  188. Main.exit(-1);
  189. }
  190. /** indirection for System.exit - todo apply cleanup here as necessary */
  191. public static void exit(int value) {
  192. System.exit(value);
  193. }
  194. } // class Main
  195. class Options {
  196. public static boolean verbose = false;
  197. public static String textProperties = null;
  198. public static File targetDir = null;
  199. public static boolean forceError1 = false;
  200. public static boolean forceError2 = false;
  201. public static boolean forceHandConfigure = false;
  202. public static void loadArgs(String[] args) {
  203. if (args == null) {
  204. return;
  205. }
  206. for (int i = 0; i < args.length; i++) {
  207. String arg = args[i];
  208. if (arg == null) {
  209. continue;
  210. }
  211. if (arg.equals("-verbose")) {
  212. verbose = true;
  213. } else if (arg.equals("-forceError1")) {
  214. forceError1 = true;
  215. } else if (arg.equals("-forceError2")) {
  216. forceError2 = true;
  217. } else if (arg.equals("-forceHandConfigure")) {
  218. forceHandConfigure = true;
  219. } else if (arg.equals("-text")) {
  220. if (i + 1 < args.length) {
  221. textProperties = args[++i];
  222. }
  223. } else if (arg.equals("-to")) {
  224. String next = "no argument";
  225. if (i + 1 < args.length) {
  226. next = args[++i];
  227. File targDir = new File(next);
  228. if (targDir.isDirectory() && targDir.canWrite()) {
  229. targetDir = targDir;
  230. }
  231. }
  232. if (null == targetDir) {
  233. System.err.println("invalid -to dir: " + next);
  234. }
  235. }
  236. }
  237. }
  238. }
  239. /** tools installer installs the entire 1.1+ distribution */
  240. class ToolsInstaller extends Installer {
  241. public String getTitle() {
  242. return "Installer for AspectJ(TM)";
  243. }
  244. public String getPrefix() {
  245. return "tools";
  246. }
  247. public String getReadmeFilename() {
  248. return "README-AspectJ.html";
  249. }
  250. public ToolsInstaller() {
  251. InstallPane installPane = new InstallPane(true);
  252. setInstallPane(installPane);
  253. panes = new WizardPane[] { new IntroPane(), new ConfigureLauncherPane(), new LocationPane(), installPane, new FinishPane() };
  254. }
  255. }
  256. class DocsInstaller extends Installer {
  257. public String getTitle() {
  258. return "AspectJ(TM) Documentation and Examples Installer";
  259. }
  260. public String getPrefix() {
  261. return "docs";
  262. }
  263. public DocsInstaller() {
  264. InstallPane installPane = new InstallPane(false);
  265. setInstallPane(installPane);
  266. panes = new WizardPane[] { new IntroPane(), new LocationPane(), installPane, new FinishPane() };
  267. }
  268. }
  269. class AJDEForJBuilderInstaller extends Installer {
  270. public String getTitle() {
  271. return "AspectJ(TM) Support for JBuilder";
  272. }
  273. public String getPrefix() {
  274. return "ajdeForJBuilder";
  275. }
  276. public AJDEForJBuilderInstaller() {
  277. InstallPane installPane = new InstallPane(false);
  278. setInstallPane(installPane);
  279. panes = new WizardPane[] { new IntroPane(), new LocationPane() {
  280. public String getDefaultLocation() {
  281. if (context.onWindows()) {
  282. // check some default locations
  283. String[] paths = { "c:\\JBuilder6\\lib\\ext", "c:\\apps\\JBuilder6\\lib\\ext",
  284. "c:\\Program Files\\JBuilder6\\lib\\ext" };
  285. int pathIndex = 0;
  286. for (; pathIndex < paths.length; pathIndex++) {
  287. if (new File(paths[pathIndex]).exists()) {
  288. return paths[pathIndex];
  289. }
  290. }
  291. return "c:\\JBuilder6\\lib\\ext";
  292. } else {
  293. return "/usr/JBuilder6/lib/ext";
  294. }
  295. }
  296. /**
  297. * Make sure that the old jar file gets removed.
  298. */
  299. public void verify() {
  300. File jbuilder = new File(location.getText() + "/../../lib/jbuilder.jar");
  301. if (!jbuilder.exists() && hasGui()) {
  302. int ret = JOptionPane.showConfirmDialog(frame, "The location you specified does not seem to be a "
  303. + "valid JBuilder install directory." + " Continue?", "Confirm Install", JOptionPane.YES_NO_OPTION,
  304. JOptionPane.QUESTION_MESSAGE);
  305. if (ret != JOptionPane.YES_OPTION) {
  306. Main.exit(-1);
  307. } else {
  308. // do nothing
  309. }
  310. }
  311. File oldFile = new File(location.getText() + "/ajbuilder.jar");
  312. if (oldFile.exists() && hasGui()) {
  313. int ret = JOptionPane.showConfirmDialog(frame,
  314. "This old version of AJDE for JBuilder (\"ajbuilder.jar\") exists"
  315. + " and must be removed from the install directory." + " OK to delete?", "Confirm Delete",
  316. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  317. if (ret != JOptionPane.YES_OPTION) {
  318. Main.exit(-1);
  319. } else {
  320. oldFile.delete();
  321. }
  322. }
  323. }
  324. }, installPane, new FinishPane() };
  325. }
  326. }
  327. class AJDEForForteInstaller extends Installer {
  328. public String getTitle() {
  329. return "AspectJ(TM) Support for Forte 4J";
  330. }
  331. public String getPrefix() {
  332. return "ajdeForForte";
  333. }
  334. private String installLoc = "";
  335. public AJDEForForteInstaller() {
  336. InstallPane installPane = new InstallPane(false);
  337. setInstallPane(installPane);
  338. panes = new WizardPane[] { new IntroPane(), new LocationPane() {
  339. public String getDefaultLocation() {
  340. if (context.onWindows()) {
  341. // check some default locations
  342. String[] paths = { "c:\\forte4j\\modules", "c:\\apps\\forte4j\\modules", "c:\\Program Files\\forte4j\\modules" };
  343. int pathIndex = 0;
  344. for (; pathIndex < paths.length; pathIndex++) {
  345. if (new File(paths[pathIndex]).exists()) {
  346. return paths[pathIndex];
  347. }
  348. }
  349. return "c:\\forte4j\\modules";
  350. } else {
  351. return "/usr/forte4j/modules";
  352. }
  353. }
  354. public void verify() {
  355. File forte = new File(location.getText() + "/../lib/openide.jar");
  356. installLoc = location.getText();
  357. if (!forte.exists() && hasGui()) {
  358. int ret = JOptionPane.showConfirmDialog(frame, "The location you specified does not seem to be a "
  359. + "valid Forte install directory." + " Continue?", "Confirm Install", JOptionPane.YES_NO_OPTION,
  360. JOptionPane.QUESTION_MESSAGE);
  361. if (ret != JOptionPane.YES_OPTION) {
  362. Main.exit(-1);
  363. } else {
  364. // do nothing
  365. }
  366. }
  367. }
  368. }, installPane, new FinishPane() {
  369. public void finalActions() { // todo verify dir ../lib/ext exists?
  370. // !!! this should be done with two install locations, not by moving a file
  371. new File(installLoc + "/../lib/ext/aspectjrt.jar").delete();
  372. new File(installLoc + "/aspectjrt.jar").renameTo(new File((installLoc + "/../lib/ext/aspectjrt.jar")));
  373. new File(installLoc + "/aspectjrt.jar").delete();
  374. }
  375. } };
  376. }
  377. }
  378. class SrcInstaller extends Installer {
  379. public String getTitle() {
  380. return "AspectJ(TM) Compiler and Core Tools Sources Installer";
  381. }
  382. public String getPrefix() {
  383. return "sources";
  384. }
  385. public SrcInstaller() {
  386. InstallPane installPane = new InstallPane(false);
  387. setInstallPane(installPane);
  388. panes = new WizardPane[] { new IntroPane(), new LocationPane(), installPane, new FinishPane() };
  389. }
  390. }
  391. abstract class Installer {
  392. static final String EXIT_MESSAGE = "Are you sure you want to cancel the installation?";
  393. static final String EXIT_TITLE = "Exiting installer";
  394. /**
  395. * relative directory in jar from package $installer$.org.aspectj for loading resources - todo must be tracked during build
  396. */
  397. public static final String RESOURCE_DIR = "resources";
  398. JFrame frame;
  399. InstallContext context;
  400. /** special pane that actually does the installation */
  401. InstallPane installPane;
  402. public Installer() {
  403. }
  404. protected void setInstallPane(InstallPane installPane) {
  405. this.installPane = installPane;
  406. }
  407. public InstallPane getInstallPane() {
  408. return installPane;
  409. }
  410. /** directly run the install pane, if any */
  411. public void run() {
  412. if (null != installPane) {
  413. installPane.run();
  414. }
  415. }
  416. public abstract String getPrefix();
  417. public String getReadmeFilename() {
  418. return "README-" + getPrefix().toUpperCase() + ".html";
  419. }
  420. public void setContext(InstallContext context) {
  421. this.context = context;
  422. context.installer = this;
  423. }
  424. public InstallContext getContext() {
  425. return context;
  426. }
  427. public String getTitle() {
  428. return "AspectJ(TM) Installer";
  429. }
  430. public int getWidth() {
  431. return 640;
  432. }
  433. public int getHeight() {
  434. return 460;
  435. }
  436. protected WizardPane[] panes = new WizardPane[0];
  437. public WizardPane[] getPanes() {
  438. return panes;
  439. }
  440. public int findPaneIndex(WizardPane pane) {
  441. for (int i = 0; i < panes.length; i++) {
  442. if (panes[i] == pane) {
  443. return i;
  444. }
  445. }
  446. return -1;
  447. }
  448. Component header, footer, body;
  449. public void runGUI() {
  450. frame = new JFrame(getTitle());
  451. WindowListener wl = new WindowAdapter() {
  452. public void windowClosing(WindowEvent arg0) {
  453. Main.exit(-1); // -1 unless exiting through done button
  454. }
  455. };
  456. frame.addWindowListener(wl);
  457. if (Options.forceError1) {
  458. throw new RuntimeException("forced error1 for testing purposes");
  459. }
  460. Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
  461. int x = (int) (size.getWidth() - getWidth()) / 2;
  462. int y = (int) (size.getHeight() - getHeight()) / 2;
  463. //include a few sanity checks on starting position
  464. if (x < 0) {
  465. x = 0;
  466. }
  467. if (x > 600) {
  468. x = 600;
  469. }
  470. if (y < 0) {
  471. y = 0;
  472. }
  473. if (y > 400) {
  474. y = 400;
  475. }
  476. frame.setLocation(x, y);
  477. frame.setSize(getWidth(), getHeight());
  478. moveToPane(getPanes()[0]);
  479. frame.setVisible(true);
  480. }
  481. public void moveToPane(WizardPane pane) {
  482. WizardPane.setContext(this.context);
  483. Dimension size = frame.getContentPane().getSize();
  484. JPanel contents = new JPanel();
  485. contents.setLayout(new BorderLayout());
  486. header = makeHeader();
  487. contents.add(header, BorderLayout.NORTH);
  488. body = pane.getPanel();
  489. contents.add(body, BorderLayout.CENTER);
  490. footer = pane.getButtons();
  491. contents.add(footer, BorderLayout.SOUTH);
  492. contents.revalidate();
  493. contents.setSize(size);
  494. frame.setContentPane(contents);
  495. //XXX deal with threading here?
  496. pane.run();
  497. }
  498. public Icon loadImage(String name) {
  499. return new javax.swing.ImageIcon(this.getClass().getResource(name));
  500. }
  501. public Component makeHeader() {
  502. return new JLabel(loadImage(Installer.RESOURCE_DIR + "/aspectjBanner.gif"));
  503. }
  504. public ActionListener makeNextAction(final WizardPane pane) {
  505. int nextPaneIndex = findPaneIndex(pane) + 1;
  506. if (nextPaneIndex >= getPanes().length) {
  507. return null;
  508. }
  509. final WizardPane nextPane = getPanes()[nextPaneIndex];
  510. return new ActionListener() {
  511. public void actionPerformed(ActionEvent e) {
  512. pane.finish();
  513. moveToPane(nextPane);
  514. }
  515. };
  516. }
  517. public ActionListener makeBackAction(final WizardPane pane) {
  518. int nextPaneIndex = findPaneIndex(pane) - 1;
  519. if (nextPaneIndex < 0) {
  520. return null;
  521. }
  522. final WizardPane nextPane = getPanes()[nextPaneIndex];
  523. return new ActionListener() {
  524. public void actionPerformed(ActionEvent e) {
  525. moveToPane(nextPane);
  526. }
  527. };
  528. }
  529. public ActionListener makeCancelAction(WizardPane pane) {
  530. return new ActionListener() {
  531. public void actionPerformed(ActionEvent e) {
  532. int ret = JOptionPane.showConfirmDialog(frame, EXIT_MESSAGE, EXIT_TITLE, JOptionPane.YES_NO_OPTION,
  533. JOptionPane.QUESTION_MESSAGE);
  534. if (ret == JOptionPane.YES_OPTION) {
  535. Main.exit(-1);
  536. }
  537. }
  538. };
  539. }
  540. public ActionListener makeFinishAction(WizardPane pane) {
  541. return new ActionListener() {
  542. public void actionPerformed(ActionEvent e) {
  543. Main.exit(0);
  544. }
  545. };
  546. }
  547. }
  548. // willing to go up to 3 levels deep to find either jre or jdk
  549. // jre\[*\]lib\ext
  550. // jdk*\lib\tools.jar
  551. /*****
  552. * final static int MAX_DEPTH = 4; public static void findPaths(String prefix, File currentDir, int currentDepth) { if (currentDepth
  553. * > MAX_DEPTH) return; if (!currentDir.exists() || !currentDir.isDirectory()) return; File [] files = currentDir.listFiles(); if
  554. * (files == null) return; for (int i=0; i<files.length; i++) { if (files[i] == null) continue; if (!files[i].isDirectory())
  555. * continue; if (files[i].getName().startsWith(prefix)) { System.out.println("found: " + files[i]); } else { findPaths(prefix,
  556. * files[i], currentDepth + 1); } } }
  557. *
  558. * public static void findPaths(String prefix) { File [] files = File.listRoots(); for (int i=1; i<files.length; i++) { if
  559. * (!files[i].isDirectory()) continue; if (files[i].getName().toLowerCase().startsWith(prefix)) { System.out.println("found: " +
  560. * files[i]); } else { findPaths(prefix, files[i], 1); } } }
  561. *****/
  562. class InstallContext {
  563. public InstallContext(Map properties) {
  564. this.properties = properties;
  565. properties.put("user.home", System.getProperty("user.home"));
  566. //System.out.println("new install context");
  567. }
  568. private File outputDir;
  569. public void setOutputDir(File outputDir) {
  570. this.outputDir = outputDir;
  571. properties.put("installer.output.dir", outputDir.getAbsolutePath());
  572. properties.put("installer.output.dir.bin", new File(outputDir, "bin").getAbsolutePath());
  573. properties.put("installer.output.dir.doc", new File(outputDir, "doc").getAbsolutePath());
  574. properties.put("installer.output.aspectjrt", new File(new File(outputDir, "lib"), "aspectjrt.jar").getAbsolutePath());
  575. properties.put("installer.output.readme", new File(outputDir, installer.getReadmeFilename()).getAbsolutePath());
  576. }
  577. public File getOutputDir() {
  578. return outputDir;
  579. }
  580. private boolean hasGui;
  581. public File javaPath;
  582. public File toolsJarPath;
  583. public Installer installer;
  584. private Map<String,String> properties;
  585. public boolean hasGui() {
  586. return hasGui;
  587. }
  588. public void setHasGui(boolean hasGui) {
  589. if (this.hasGui != hasGui) {
  590. this.hasGui = hasGui;
  591. }
  592. }
  593. public Font getFont() {
  594. return new Font("Serif", Font.PLAIN, 14);
  595. }
  596. public String getOS() {
  597. return System.getProperty("os.name");
  598. }
  599. public boolean onOS2() {
  600. return getOS().equals("OS2") || getOS().equals("OS/2");
  601. }
  602. public boolean onWindows() {
  603. return getOS().startsWith("Windows") || onOS2();
  604. }
  605. public boolean onWindowsPro() {
  606. return getOS().equals("Windows NT") || getOS().equals("Windows 2000") || getOS().equals("Windows XP")
  607. || getOS().equals("Windows Vista") || getOS().equals("Windows 7") || getOS().startsWith("Windows 8");
  608. }
  609. public boolean onMacintosh() {
  610. return getOS().startsWith("Mac");
  611. }
  612. public boolean onUnix() {
  613. return !onWindows();
  614. }
  615. static final String[] TEXT_EXTENSIONS = { ".txt", ".text", ".htm", ".html", ".java", ".ajava", "README", ".lst" };
  616. public boolean isTextFile(File file) {
  617. String name = file.getName();
  618. for (String textExtension : TEXT_EXTENSIONS) {
  619. if (name.endsWith(textExtension)) {
  620. return true;
  621. }
  622. }
  623. return false;
  624. }
  625. public void handleException(Throwable e) {
  626. System.out.println("internal error: " + e.toString());
  627. e.printStackTrace();
  628. if (hasGui()) {
  629. JOptionPane.showMessageDialog(installer.frame, e.toString(), "Unexpected Exception", JOptionPane.ERROR_MESSAGE);
  630. }
  631. }
  632. final static String OVERWRITE_MESSAGE = "Overwrite file ";
  633. final static String OVERWRITE_TITLE = "Overwrite?";
  634. final static String[] OVERWRITE_OPTIONS = { "Yes", "No", "Yes to all" //, "No to all"
  635. };
  636. final static int OVERWRITE_YES = 0;
  637. final static int OVERWRITE_NO = 1;
  638. final static int OVERWRITE_ALL = 2;
  639. //final static int OVERWRITE_NONE = 3;
  640. int overwriteState = OVERWRITE_NO;
  641. boolean shouldOverwrite(final File file) {
  642. //System.out.println("overwrite: " + file + " state " + overwriteState);
  643. if (overwriteState == OVERWRITE_ALL) {
  644. return true;
  645. //if (overwriteState == OVERWRITE_NONE) return false;
  646. }
  647. try {
  648. SwingUtilities.invokeAndWait(new Runnable() {
  649. public void run() {
  650. int ret = JOptionPane.showOptionDialog(installer.frame, OVERWRITE_MESSAGE + file.getPath(), OVERWRITE_TITLE,
  651. JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, OVERWRITE_OPTIONS,
  652. OVERWRITE_OPTIONS[OVERWRITE_YES]);
  653. overwriteState = ret;
  654. }
  655. });
  656. } catch (InvocationTargetException ite) {
  657. handleException(ite.getTargetException());
  658. } catch (InterruptedException ie) {
  659. }
  660. return overwriteState == OVERWRITE_YES || overwriteState == OVERWRITE_ALL;
  661. }
  662. public Map<String,String> getProperties() {
  663. return properties;
  664. }
  665. }
  666. abstract class WizardPane {
  667. static InstallContext context;
  668. protected JButton backButton = null;
  669. protected JButton nextButton = null;
  670. protected JButton cancelButton = null;
  671. public static void setContext(InstallContext con) {
  672. context = con;
  673. }
  674. public abstract JPanel makePanel();
  675. protected JTextArea makeTextArea(String data) {
  676. JTextArea text = new JTextArea(data);
  677. text.setOpaque(false);
  678. text.setFont(context.getFont());
  679. text.setEditable(false);
  680. return text;
  681. }
  682. /** @return false only if there is an InstallContext saying there is no GUI */
  683. protected boolean hasGui() {
  684. final InstallContext icontext = context;
  685. return ((null == icontext) || icontext.hasGui());
  686. }
  687. public static String stringFromStream(InputStream stream) throws IOException {
  688. BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "US-ASCII"));
  689. StringBuffer ret = new StringBuffer();
  690. int data;
  691. while ((data = reader.read()) != -1) {
  692. ret.append((char) data);
  693. }
  694. return ret.toString();
  695. }
  696. public static String removeHead(String text) {
  697. int startIndex = text.indexOf("<head>");
  698. int stopIndex = text.indexOf("</head>");
  699. if (startIndex == -1 || stopIndex == -1) {
  700. return text;
  701. }
  702. stopIndex += 7;
  703. return text.substring(0, startIndex) + text.substring(stopIndex);
  704. }
  705. static String styleHeader = "<head></head>";/*
  706. * <STYLE TYPE=\"text/css\"><!--\n" + " h2 {\n" + " font-size: x-large;\n" +
  707. * " font-family: Serif;\n" + " font-weight: normal;\n" + " }\n" + " p {\n" +
  708. * " font-family: Serif;\n" + " font-weight: normal;\n" + //" color:black;\n"
  709. * + "}</head>\n";
  710. */
  711. public static String applyProperties(String text, Map<String,String> map) {
  712. // ${name} -> map.get(name).toString()
  713. int lastIndex = 0;
  714. StringBuffer buf = new StringBuffer();
  715. int startIndex;
  716. while ((startIndex = text.indexOf("${", lastIndex)) != -1) {
  717. int endIndex = text.indexOf('}', startIndex);
  718. //XXX internal error here
  719. if (endIndex == -1) {
  720. break;
  721. }
  722. buf.append(text.substring(lastIndex, startIndex));
  723. String key = text.substring(startIndex + 2, endIndex);
  724. lastIndex = endIndex + 1;
  725. Object replaceText = (map == null ? null : map.get(key));
  726. //System.out.println("key: " + key + " -> " + replaceText);
  727. if (replaceText == null) {
  728. replaceText = "NOT_FOUND";
  729. }
  730. buf.append(replaceText.toString());
  731. }
  732. buf.append(text.substring(lastIndex));
  733. return buf.toString();
  734. }
  735. public static String applyProperties(String text) {
  736. return applyProperties(text, (context == null ? null : context.getProperties()));
  737. }
  738. protected String loadText(String filename) {
  739. String fullname = Installer.RESOURCE_DIR + "/" + filename;
  740. //context.installer.getPrefix() + "-" + filename;
  741. try {
  742. String text = stringFromStream(getClass().getResourceAsStream(fullname));
  743. text = styleHeader + removeHead(text);
  744. text = applyProperties(text);
  745. //System.out.println(text);
  746. return text;
  747. } catch (IOException e) {
  748. context.handleException(e);
  749. return "";
  750. }
  751. }
  752. protected JEditorPane makeHTMLArea(String filename) {
  753. JEditorPane editorPane = new JEditorPane("text/html", loadText(filename));
  754. /*
  755. * { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  756. * RenderingHints.VALUE_ANTIALIAS_ON); super.paint(g2); } };
  757. */
  758. editorPane.setEditable(false);
  759. editorPane.setOpaque(false);
  760. return editorPane;
  761. }
  762. protected void setHTMLArea(JEditorPane pane, String filename) {
  763. pane.setText(loadText(filename));
  764. }
  765. protected JPanel makeLocationBox(String label, JTextField textField, JButton browseButton) {
  766. JPanel box = new JPanel();
  767. box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
  768. textField.setFont(context.getFont());
  769. textField.selectAll();
  770. box.add(textField);
  771. box.add(browseButton);
  772. Border border = BorderFactory.createTitledBorder(label);
  773. final int INSET = 8;
  774. border = new CompoundBorder(border, new EmptyBorder(1, INSET, INSET, INSET));
  775. box.setBorder(border);
  776. return box;
  777. }
  778. private JPanel panel = null;
  779. public JPanel getPanel() {
  780. if (panel == null) {
  781. panel = makePanel();
  782. panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
  783. }
  784. return panel;
  785. }
  786. protected void setListener(JButton button, ActionListener listener) {
  787. if (listener == null) {
  788. button.setEnabled(false);
  789. } else {
  790. button.addActionListener(listener);
  791. }
  792. }
  793. protected Component makeButtons(Installer installer) {
  794. JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  795. backButton = new JButton("Back");
  796. setListener(backButton, installer.makeBackAction(this));
  797. panel.add(backButton);
  798. nextButton = new JButton("Next");
  799. setListener(nextButton, installer.makeNextAction(this));
  800. panel.add(nextButton); //.setDefaultCapable(true);
  801. JLabel space = new JLabel();
  802. space.setPreferredSize(new Dimension(20, 0));
  803. panel.add(space);
  804. cancelButton = new JButton("Cancel");
  805. setListener(cancelButton, installer.makeCancelAction(this));
  806. panel.add(cancelButton);
  807. return panel;
  808. }
  809. private Component buttons = null;
  810. public Component getButtons() {
  811. if (buttons == null) {
  812. buttons = makeButtons(context.installer);
  813. //buttons.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
  814. }
  815. context.installer.frame.getRootPane().setDefaultButton(nextButton);
  816. return buttons;
  817. }
  818. public void finish() {
  819. if (Options.forceError2) {
  820. throw new RuntimeException("forced error2 for testing purposes");
  821. }
  822. }
  823. public void run() {
  824. }
  825. }
  826. class IntroPane extends WizardPane {
  827. public JPanel makePanel() {
  828. Component text = makeHTMLArea("intro.html");
  829. JPanel panel = new JPanel(new BorderLayout());
  830. panel.add(text);
  831. return panel;
  832. }
  833. }
  834. class FinishPane extends WizardPane {
  835. public JPanel makePanel() {
  836. Component text = makeHTMLArea("finish.html");
  837. JPanel panel = new JPanel(new BorderLayout());
  838. panel.add(text);
  839. finalActions();
  840. return panel;
  841. }
  842. public Component makeButtons(Installer installer) {
  843. Component ret = super.makeButtons(installer);
  844. nextButton.setText("Finish");
  845. nextButton.setEnabled(true);
  846. nextButton.addActionListener(installer.makeFinishAction(this));
  847. backButton.setEnabled(false);
  848. cancelButton.setEnabled(false);
  849. return ret;
  850. }
  851. public void finalActions() {
  852. }
  853. }
  854. class LocationPane extends WizardPane implements ActionListener {
  855. //XXX need more sophisticated default location finding
  856. //XXX would like to find the place they last chose...
  857. public String getDefaultLocation() {
  858. if (context.onWindows()) {
  859. return "c:\\aspectj1.9";
  860. } else {
  861. return new File(System.getProperty("user.home"), "aspectj1.9").getAbsolutePath();
  862. }
  863. }
  864. protected JTextField location;
  865. public JPanel makePanel() {
  866. Component text = makeHTMLArea("location.html");
  867. location = new JTextField(getDefaultLocation());
  868. JButton browse = new JButton("Browse...");
  869. browse.addActionListener(this);
  870. JPanel locationBox = makeLocationBox("installation directory", location, browse);
  871. GridBagLayout bag = new GridBagLayout();
  872. GridBagConstraints c = new GridBagConstraints();
  873. JPanel panel = new JPanel(bag);
  874. c.fill = GridBagConstraints.BOTH;
  875. c.weightx = 1.0;
  876. c.gridwidth = GridBagConstraints.REMAINDER;
  877. bag.setConstraints(text, c);
  878. panel.add(text);
  879. c.weighty = 1.0;
  880. c.fill = GridBagConstraints.HORIZONTAL;
  881. c.gridwidth = GridBagConstraints.REMAINDER;
  882. bag.setConstraints(locationBox, c);
  883. panel.add(locationBox);
  884. //XXX set next button to read install
  885. //context.nextButton.setText("Install");
  886. return panel;
  887. }
  888. public Component makeButtons(Installer installer) {
  889. Component ret = super.makeButtons(installer);
  890. nextButton.setText("Install");
  891. return ret;
  892. }
  893. public void actionPerformed(ActionEvent e) {
  894. JFileChooser chooser = new JFileChooser(); // {
  895. // public void approveSelection() {
  896. // System.out.println("approved selection");
  897. // }
  898. //}; //field.getText());
  899. chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  900. int returnVal = chooser.showDialog(location, "Select");
  901. if (returnVal == JFileChooser.APPROVE_OPTION) {
  902. File file = chooser.getSelectedFile();
  903. if (!file.isDirectory()) {
  904. file = file.getParentFile();
  905. }
  906. String name = file.getPath();
  907. location.setText(name);
  908. location.selectAll();
  909. }
  910. }
  911. /**
  912. * Override to do any additional checks.
  913. */
  914. protected void verify() {
  915. }
  916. public void finish() {
  917. verify();
  918. context.setOutputDir(new File(location.getText()));
  919. }
  920. }
  921. class ConfigureLauncherPane extends WizardPane {
  922. /*
  923. * //XXX check that the returned file is valid public String getDefaultJavaLocation() { String name = "java"; if
  924. * (context.onWindows()) name += ".exe";
  925. *
  926. * if (Options.verbose) { System.out.println("java.home: " + System.getProperty("java.home")); System.out.println(" java: " +
  927. * new File(new File(System.getProperty("java.home"), "bin"), name)); System.out.println(" java: " + new File(new
  928. * File(System.getProperty("java.home"), "bin"), name).getPath()); }
  929. *
  930. * return new File(new File(System.getProperty("java.home"), "bin"), name).getPath(); }
  931. */
  932. public static String getDefaultJavaHomeLocation() {
  933. if (!Options.forceHandConfigure) {
  934. File javaHome = findJavaHome();
  935. if (javaHome != null) {
  936. return javaHome.getPath();
  937. }
  938. }
  939. return null;
  940. }
  941. public void chooseFile(JTextField field) {
  942. JFileChooser chooser = new JFileChooser(); //field.getText());
  943. chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  944. int returnVal = chooser.showDialog(field, "Select");
  945. if (returnVal == JFileChooser.APPROVE_OPTION) {
  946. String name = chooser.getSelectedFile().getPath();
  947. field.setText(name);
  948. field.selectAll();
  949. }
  950. }
  951. public ActionListener makeJavaLocationBrowseListener() {
  952. return new ActionListener() {
  953. public void actionPerformed(ActionEvent e) {
  954. chooseFile(javaLocation);
  955. }
  956. };
  957. }
  958. // public ActionListener makeToolsJarLocationBrowseListener() {
  959. // return new ActionListener() {
  960. // public void actionPerformed(ActionEvent e) {
  961. // chooseFile(toolsJarLocation);
  962. // }
  963. // };
  964. // }
  965. private JTextField javaLocation;
  966. //private JTextField toolsJarLocation;
  967. public JPanel makePanel() {
  968. String javaPath = getDefaultJavaHomeLocation();
  969. //String toolsJarPath = getDefaultToolsJarLocation();
  970. Component text;
  971. if (javaPath == null) {
  972. javaPath = "<java home not found>";
  973. text = makeHTMLArea("configure-hand.html");
  974. } else {
  975. text = makeHTMLArea("configure-auto.html");
  976. }
  977. javaLocation = new JTextField(javaPath);
  978. JButton javaLocationBrowse = new JButton("Browse...");
  979. javaLocationBrowse.addActionListener(makeJavaLocationBrowseListener());
  980. JPanel javaLocationBox = makeLocationBox("java home directory", javaLocation, javaLocationBrowse);
  981. // toolsJarLocation = new JTextField(toolsJarPath);
  982. // JButton toolsJarLocationBrowse = new JButton("Browse...");
  983. // toolsJarLocationBrowse.addActionListener(makeToolsJarLocationBrowseListener());
  984. // JPanel toolsJarLocationBox = makeLocationBox("full path to tools.jar", toolsJarLocation, toolsJarLocationBrowse);
  985. GridBagLayout bag = new GridBagLayout();
  986. GridBagConstraints c = new GridBagConstraints();
  987. JPanel panel = new JPanel(bag);
  988. c.fill = GridBagConstraints.BOTH;
  989. c.weightx = 1.0;
  990. c.weighty = 1.0;
  991. //c.ipady = 10;
  992. c.gridwidth = GridBagConstraints.REMAINDER;
  993. bag.setConstraints(text, c);
  994. panel.add(text);
  995. c.weighty = 0.0;
  996. //c.fill = GridBagConstraints.VERTICAL;
  997. bag.setConstraints(javaLocationBox, c);
  998. panel.add(javaLocationBox);
  999. // c.weighty = 0.25;
  1000. // JLabel space = new JLabel();
  1001. // bag.setConstraints(space, c);
  1002. // panel.add(space);
  1003. // c.weighty = 0.0;
  1004. // bag.setConstraints(toolsJarLocationBox, c);
  1005. // panel.add(toolsJarLocationBox);
  1006. c.weighty = 0.5;
  1007. JLabel space = new JLabel();
  1008. bag.setConstraints(space, c);
  1009. panel.add(space);
  1010. return panel;
  1011. }
  1012. public void finish() {
  1013. context.javaPath = new File(javaLocation.getText());
  1014. // context.toolsJarPath = new File(toolsJarLocation.getText());
  1015. //XXX need much more work on helping the user get these paths right
  1016. // if (context.javaPath.isDirectory()) {
  1017. // context.javaPath = new File(context.javaPath, "java");
  1018. // }
  1019. // if (context.toolsJarPath.isDirectory()) {
  1020. // context.toolsJarPath = new File(context.toolsJarPath, "tools.jar");
  1021. // }
  1022. }
  1023. //XXX add user.home to prefixes in a rational way
  1024. public static final String[] windowsPaths = { "c:\\jdk", "c:\\apps\\jdk", "${user.home}\\jdk" };
  1025. public static final String[] unixPaths = { "/usr/local/bin/jdk", "/usr/bin/jdk", "/usr/bin/jdk", "${user.home}/jdk" };
  1026. public static final String[] suffixes = { "1.3.1", "1.3", "1.2", "13", "12", "2", "", "1.4" };
  1027. public static boolean windows = true;
  1028. public static boolean isLegalJavaHome(File home) {
  1029. File bin = new File(home, "bin");
  1030. return new File(bin, "java").isFile() || new File(bin, "java.exe").isFile();
  1031. }
  1032. public static boolean isLegalJDKHome(File home) {
  1033. File lib = new File(home, "lib");
  1034. return new File(lib, "tools.jar").isFile();
  1035. }
  1036. public static File findJavaHome() {
  1037. String s = System.getProperty("java.home");
  1038. File javaHome = null;
  1039. if (s != null) {
  1040. javaHome = new File(s);
  1041. if (isLegalJDKHome(javaHome)) {
  1042. return javaHome;
  1043. }
  1044. if (isLegalJavaHome(javaHome)) {
  1045. File parent = javaHome.getParentFile();
  1046. if (parent != null && isLegalJDKHome(parent)) {
  1047. return parent;
  1048. }
  1049. }
  1050. }
  1051. String[] paths;
  1052. if (windows) {
  1053. paths = windowsPaths;
  1054. } else {
  1055. paths = unixPaths;
  1056. }
  1057. for (String suffix : suffixes) {
  1058. for (String path : paths) {
  1059. String prefix = path;
  1060. prefix = applyProperties(prefix);
  1061. File test = new File(prefix + suffix);
  1062. if (isLegalJavaHome(test)) {
  1063. if (isLegalJDKHome(test)) {
  1064. return test;
  1065. } else if (javaHome == null) {
  1066. javaHome = test;
  1067. }
  1068. }
  1069. }
  1070. }
  1071. return javaHome;
  1072. }
  1073. }
  1074. class InstallPane extends WizardPane {
  1075. private JProgressBar progressBar;
  1076. private JTextField progressItem;
  1077. private JEditorPane message;
  1078. private boolean makeLaunchScripts = false;
  1079. public InstallPane(boolean makeLaunchScripts) {
  1080. this.makeLaunchScripts = makeLaunchScripts;
  1081. }
  1082. public JPanel makePanel() {
  1083. message = makeHTMLArea("install-start.html");
  1084. progressBar = new JProgressBar();
  1085. progressItem = new JTextField();
  1086. progressItem.setOpaque(false);
  1087. progressItem.setFont(context.getFont());
  1088. progressItem.setEditable(false);
  1089. GridBagLayout bag = new GridBagLayout();
  1090. GridBagConstraints c = new GridBagConstraints();
  1091. JPanel panel = new JPanel(bag);
  1092. c.fill = GridBagConstraints.BOTH;
  1093. c.weightx = 1.0;
  1094. c.weighty = 1.0;
  1095. //c.ipady = 10;
  1096. c.gridwidth = GridBagConstraints.REMAINDER;
  1097. bag.setConstraints(message, c);
  1098. panel.add(message);
  1099. c.weighty = 0.0;
  1100. //c.fill = GridBagConstraints.VERTICAL;
  1101. bag.setConstraints(progressBar, c);
  1102. panel.add(progressBar);
  1103. c.weighty = 0.1;
  1104. JLabel space = new JLabel();
  1105. bag.setConstraints(space, c);
  1106. panel.add(space);
  1107. c.weighty = 0.0;
  1108. bag.setConstraints(progressItem, c);
  1109. panel.add(progressItem);
  1110. c.weighty = 0.5;
  1111. space = new JLabel();
  1112. bag.setConstraints(space, c);
  1113. panel.add(space);
  1114. return panel;
  1115. }
  1116. class InstallRunner implements Runnable {
  1117. public InstallRunner() {
  1118. }
  1119. public void run() {
  1120. try {
  1121. new CurrentJarUnpacker(context, InstallPane.this).unpack(Installer.RESOURCE_DIR + "/contents.txt",
  1122. context.getOutputDir());
  1123. if (makeLaunchScripts) {
  1124. LaunchScriptMaker lsm = new LaunchScriptMaker(context);
  1125. lsm.writeScript("ajc");
  1126. lsm.writeScript("ajdoc");
  1127. //lsm.writeScript("ajdb");
  1128. lsm.writeScript("ajbrowser");
  1129. // Moved to the bin dir in 1.2.1
  1130. // we should now come back and make the generation of this
  1131. // script uniform with those above.
  1132. lsm.writeAJLaunchScript("aj", false);
  1133. lsm.writeAJLaunchScript("aj5", true);
  1134. }
  1135. if (hasGui()) {
  1136. progressBar.setValue(100);
  1137. setHTMLArea(message, "install-finish.html");
  1138. }
  1139. } catch (IOException ioe) {
  1140. context.handleException(ioe);
  1141. }
  1142. if (hasGui()) {
  1143. cancelButton.setEnabled(false);
  1144. nextButton.setEnabled(true);
  1145. }
  1146. }
  1147. }
  1148. public Component makeButtons(Installer installer) {
  1149. Component ret = super.makeButtons(installer);
  1150. //nextButton.setText("Finish");
  1151. nextButton.setEnabled(false);
  1152. //nextButton.addActionListener(installer.makeFinishAction(this));
  1153. backButton.setEnabled(false);
  1154. return ret;
  1155. }
  1156. public void run() {
  1157. Thread thread = new Thread(new InstallRunner());
  1158. thread.start();
  1159. }
  1160. public void progressMessage(final String message) {
  1161. if (!hasGui()) {
  1162. return;
  1163. }
  1164. try {
  1165. //XXX performance tradeoff between invokeAndWait and invokeLater...
  1166. SwingUtilities.invokeAndWait(new Runnable() {
  1167. public void run() {
  1168. progressItem.setText(message);
  1169. }
  1170. });
  1171. } catch (InvocationTargetException ite) {
  1172. context.handleException(ite.getTargetException());
  1173. } catch (InterruptedException ie) {
  1174. }
  1175. }
  1176. int nBytes = 0;
  1177. int bytesWritten = 0;
  1178. public void progressBytesWritten(int bytes) {
  1179. if (!hasGui()) {
  1180. return;
  1181. }
  1182. bytesWritten += bytes;
  1183. final int PCT = (int) (100.0 * bytesWritten / nBytes);
  1184. //System.out.println("bytesWritten: " + bytesWritten);
  1185. try {
  1186. //XXX performance tradeoff between invokeAndWait and invokeLater...
  1187. SwingUtilities.invokeAndWait(new Runnable() {
  1188. public void run() {
  1189. progressBar.setValue(PCT);
  1190. }
  1191. });
  1192. } catch (InvocationTargetException ite) {
  1193. context.handleException(ite.getTargetException());
  1194. } catch (InterruptedException ie) {
  1195. }
  1196. }
  1197. }
  1198. class CurrentJarUnpacker {
  1199. InstallContext context;
  1200. InstallPane installPane;
  1201. public CurrentJarUnpacker(InstallContext context, InstallPane installPane) {
  1202. this.context = context;
  1203. this.installPane = installPane;
  1204. }
  1205. public File makeOutputFile(String name, File outputFile) {
  1206. int index;
  1207. int lastIndex = 0;
  1208. while ((index = name.indexOf('/', lastIndex)) != -1) {
  1209. outputFile = new File(outputFile, name.substring(lastIndex, index));
  1210. lastIndex = index + 1;
  1211. }
  1212. return new File(outputFile, name.substring(lastIndex));
  1213. }
  1214. final static int BUF_SIZE = 4096;
  1215. public void writeStream(InputStream zis, File outputFile) throws IOException {
  1216. if (outputFile.exists()) {
  1217. if (!context.shouldOverwrite(outputFile)) {
  1218. return;
  1219. }
  1220. }
  1221. installPane.progressMessage("writing " + outputFile.getAbsolutePath());
  1222. outputFile.getParentFile().mkdirs();
  1223. if (context.isTextFile(outputFile)) {
  1224. writeTextStream(zis, outputFile);
  1225. } else {
  1226. writeBinaryStream(zis, outputFile);
  1227. }
  1228. }
  1229. public void writeBinaryStream(InputStream zis, File outputFile) throws IOException {
  1230. byte[] buffer = new byte[BUF_SIZE];
  1231. int nRead = 0;
  1232. OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
  1233. while ((nRead = zis.read(buffer)) != -1) {
  1234. os.write(buffer, 0, nRead);
  1235. installPane.progressBytesWritten(nRead);
  1236. }
  1237. os.close();
  1238. }
  1239. public void writeTextStream(InputStream zis, File outputFile) throws IOException {
  1240. BufferedWriter os = new BufferedWriter(new FileWriter(outputFile));
  1241. BufferedReader r = new BufferedReader(new InputStreamReader(zis, "US-ASCII"));
  1242. String l;
  1243. while ((l = r.readLine()) != null) {
  1244. os.write(l);
  1245. os.newLine();
  1246. installPane.progressBytesWritten(l.length() + 1);
  1247. }
  1248. os.close();
  1249. }
  1250. public void writeResource(String name, File outputDir) throws IOException {
  1251. File outputFile = makeOutputFile(name, outputDir);
  1252. //System.out.println("finding name: " + name);
  1253. writeStream(getClass().getResourceAsStream("/" + name), outputFile);
  1254. }
  1255. public void writeResource(JarFile jarFile, JarEntry entry, File outputDir) throws IOException {
  1256. String name = entry.getName().substring(6);
  1257. File outputFile = makeOutputFile(name, outputDir);
  1258. //System.out.println("finding name: " + name);
  1259. // writeStream(getClass().getResourceAsStream("/" + name), outputFile);
  1260. writeStream(jarFile.getInputStream(entry), outputFile);
  1261. }
  1262. public void unpack(String contentsName, File outputDir) throws IOException {
  1263. URL url = getClass().getResource(contentsName);
  1264. // Process everything under 'files/**' copying to the target
  1265. // install directory with 'files/' removed
  1266. JarURLConnection juc = (JarURLConnection) url.openConnection();
  1267. JarFile jf = juc.getJarFile();
  1268. Enumeration<JarEntry> entries = jf.entries();
  1269. while (entries.hasMoreElements()) {
  1270. JarEntry je = entries.nextElement();
  1271. if (je.getName().startsWith("files/") && !je.getName().endsWith("/")) {
  1272. writeResource(jf, je, outputDir);
  1273. }
  1274. }
  1275. // InputStream stream = url.openStream();
  1276. // BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "US-ASCII"));
  1277. // String line = reader.readLine();
  1278. // installPane.nBytes = Integer.parseInt(line);
  1279. //
  1280. // while ((line = reader.readLine()) != null) {
  1281. // writeResource(line, outputDir);
  1282. // }
  1283. installPane.progressMessage("done writing");
  1284. }
  1285. }
  1286. class LaunchScriptMaker {
  1287. static final String toolsPackage = "org.aspectj.tools";
  1288. InstallContext context;
  1289. public LaunchScriptMaker(InstallContext context) {
  1290. this.context = context;
  1291. }
  1292. /**
  1293. *
  1294. */
  1295. public void writeAJLaunchScript(String name, boolean isJava5) throws IOException {
  1296. if (!context.onUnix()) {
  1297. if (context.onOS2()) {
  1298. name += ".cmd";
  1299. } else if (context.onWindows()) {
  1300. name += ".bat";
  1301. }
  1302. }
  1303. File destDir = new File(context.getOutputDir(), "bin");
  1304. destDir.mkdirs();
  1305. File file = new File(destDir, name);
  1306. PrintStream ps = getPrintStream(file);
  1307. writeAJLaunchScriptContent(ps, isJava5);
  1308. ps.close();
  1309. if (context.onUnix()) {
  1310. makeExecutable(file);
  1311. }
  1312. }
  1313. /**
  1314. * @param ps
  1315. */
  1316. private void writeAJLaunchScriptContent(PrintStream ps, boolean isJava5) {
  1317. if (context.onUnix()) {
  1318. writeUnixHeader(ps);
  1319. if (isJava5) {
  1320. writeAJ5UnixLaunchLine(ps);
  1321. } else {
  1322. writeAJUnixLaunchLine(ps);
  1323. }
  1324. } else {
  1325. writeWindowsHeader(ps);
  1326. if (isJava5) {
  1327. writeAJ5WindowsLaunchLine(ps);
  1328. } else {
  1329. writeAJWindowsLaunchLine(ps);
  1330. }
  1331. }
  1332. }
  1333. /**
  1334. * @param ps
  1335. */
  1336. private void writeAJWindowsLaunchLine(PrintStream ps) {
  1337. ps.println("\"%JAVA_HOME%\\bin\\java\" -classpath " + "\"%ASPECTJ_HOME%\\lib\\aspectjweaver.jar\""
  1338. + " \"-Djava.system.class.loader=org.aspectj.weaver.loadtime.WeavingURLClassLoader\""
  1339. + " \"-Daj.class.path=%ASPECTPATH%;%CLASSPATH%\"" + " \"-Daj.aspect.path=%ASPECTPATH%\"" + " "
  1340. + makeScriptArgs(false));
  1341. }
  1342. /**
  1343. * @param ps
  1344. */
  1345. private void writeAJ5WindowsLaunchLine(PrintStream ps) {
  1346. ps.println("\"%JAVA_HOME%\\bin\\java\" -classpath " + "\"%ASPECTJ_HOME%\\lib\\aspectjweaver.jar;%CLASSPATH%\""
  1347. + " \"-javaagent:%ASPECTJ_HOME%\\lib\\aspectjweaver.jar\"" + " " + makeScriptArgs(false));
  1348. }
  1349. /**
  1350. * @param ps
  1351. */
  1352. private void writeAJUnixLaunchLine(PrintStream ps) {
  1353. ps.println("\"$JAVA_HOME/bin/java\" -classpath" + " \"$ASPECTJ_HOME/lib/aspectjweaver.jar\""
  1354. + " \"-Djava.system.class.loader=org.aspectj.weaver.loadtime.WeavingURLClassLoader\""
  1355. + " \"-Daj.class.path=$ASPECTPATH:$CLASSPATH\"" + " \"-Daj.aspect.path=$ASPECTPATH\"" + " " + makeScriptArgs(true));
  1356. }
  1357. /**
  1358. * @param ps
  1359. */
  1360. private void writeAJ5UnixLaunchLine(PrintStream ps) {
  1361. ps.println("\"$JAVA_HOME/bin/java\" -classpath" + " \"$ASPECTJ_HOME/lib/aspectjweaver.jar:$CLASSPATH\""
  1362. + " \"-javaagent:$ASPECTJ_HOME/lib/aspectjweaver.jar\"" + " " + makeScriptArgs(true));
  1363. }
  1364. private void writeWindowsHeader(PrintStream ps) {
  1365. ps.println("@echo off");
  1366. ps.println("REM This file generated by AspectJ installer");
  1367. ps.println("REM Created on " + new java.util.Date() + " by " + System.getProperty("user.name"));
  1368. ps.println("");
  1369. ps.println("if \"%JAVA_HOME%\" == \"\" set JAVA_HOME=" + context.javaPath.getAbsolutePath());
  1370. ps.println("if \"%ASPECTJ_HOME%\" == \"\" set ASPECTJ_HOME=" + context.getOutputDir().getAbsolutePath());
  1371. ps.println("");
  1372. ps.println("if exist \"%JAVA_HOME%\\bin\\java.exe\" goto haveJava");
  1373. ps.println("if exist \"%JAVA_HOME%\\bin\\java.bat\" goto haveJava");
  1374. ps.println("if exist \"%JAVA_HOME%\\bin\\java\" goto haveJava");
  1375. ps.println("echo java does not exist as %JAVA_HOME%\\bin\\java");
  1376. ps.println("echo please fix the JAVA_HOME environment variable");
  1377. ps.println(":haveJava");
  1378. }
  1379. private void writeWindowsLaunchLine(String className, PrintStream ps) {
  1380. ps.println("\"%JAVA_HOME%\\bin\\java\" -classpath " +
  1381. // "\"%ASPECTJ_HOME%\\lib\\aspectjtools.jar;%CLASSPATH%\""+
  1382. "\"%ASPECTJ_HOME%\\lib\\aspectjtools.jar;%JAVA_HOME%\\lib\\tools.jar;%CLASSPATH%\"" + " -Xmx64M " + className + //" -defaultClasspath " + "\"%CLASSPATH%\"" +
  1383. " " + makeScriptArgs(false));
  1384. }
  1385. private void writeUnixHeader(PrintStream ps) {
  1386. File binsh = new File(File.separator + "bin", "sh");
  1387. if (binsh.canRead()) {
  1388. ps.println("#!" + binsh.getPath());
  1389. }
  1390. ps.println("# This file generated by AspectJ installer");
  1391. ps.println("# Created on " + new java.util.Date() + " by " + System.getProperty("user.name"));
  1392. ps.println("");
  1393. ps.println("if [ \"$JAVA_HOME\" = \"\" ] ; then JAVA_HOME=" + quote(true, false, context.javaPath.getAbsolutePath()));
  1394. ps.println("fi");
  1395. ps.println("if [ \"$ASPECTJ_HOME\" = \"\" ] ; then ASPECTJ_HOME=" + quote(true, false, context.getOutputDir()));
  1396. ps.println("fi");
  1397. ps.println("");
  1398. }
  1399. private void writeUnixLaunchLine(String className, PrintStream ps) {
  1400. String sep = File.pathSeparator;
  1401. ps.println("\"$JAVA_HOME/bin/java\" -classpath " + "\"$ASPECTJ_HOME/lib/aspectjtools.jar" + sep
  1402. + "$JAVA_HOME/lib/tools.jar" + sep + "$CLASSPATH\"" + " -Xmx64M " + className + " " + makeScriptArgs(true));
  1403. }
  1404. private void makeExecutable(File file) {
  1405. try {
  1406. Runtime curRuntime = Runtime.getRuntime();
  1407. curRuntime.exec("chmod 777 " + quote(true, false, file));
  1408. } catch (Throwable t) {
  1409. // ignore any errors that occur while trying to chmod
  1410. }
  1411. }
  1412. private String makeScriptArgs(boolean unixStyle) {
  1413. if (unixStyle) {
  1414. return "\"$@\"";
  1415. } else if (context.onWindowsPro()) {
  1416. return "%*";
  1417. } else {
  1418. return "%1 %2 %3 %4 %5 %6 %7 %8 %9";
  1419. }
  1420. }
  1421. private String quote(boolean unixStyle, boolean forceQuotes, File file) {
  1422. return quote(unixStyle, forceQuotes, file.getAbsolutePath());
  1423. }
  1424. private String quote(boolean unixStyle, boolean forceQuotes, String s) {
  1425. if (context.onWindows() && unixStyle) {
  1426. s = s.replace('\\', '/');
  1427. }
  1428. if (!forceQuotes && s.indexOf(' ') == -1) {
  1429. return s;
  1430. }
  1431. return "\"" + s + "\"";
  1432. }
  1433. private File makeScriptFile(String name, boolean unixStyle) throws IOException {
  1434. if (!unixStyle) {
  1435. if (context.onOS2()) {
  1436. name += ".cmd";
  1437. } else if (context.onWindows()) {
  1438. name += ".bat";
  1439. }
  1440. }
  1441. //XXX probably want a context.getOutputBinDir()
  1442. File bindir = new File(context.getOutputDir(), "bin");
  1443. bindir.mkdirs();
  1444. File file = new File(bindir, name);
  1445. return file;
  1446. }
  1447. private PrintStream getPrintStream(File file) throws IOException {
  1448. return new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
  1449. }
  1450. String makeClassPathVar(boolean unixStyle) {
  1451. if (unixStyle) {
  1452. return "$CLASSPATH";
  1453. } else {
  1454. return "%CLASSPATH%";
  1455. }
  1456. }
  1457. public String makeClassPath(boolean unixStyle) throws IOException {
  1458. return context.toolsJarPath.getAbsolutePath() + File.pathSeparator
  1459. +
  1460. //XXX want context.getOutputLibDir()
  1461. new File(new File(context.getOutputDir(), "lib"), "aspectjtools.jar").getAbsolutePath() + File.pathSeparator
  1462. + makeClassPathVar(unixStyle);
  1463. }
  1464. public void writeScript(String className, PrintStream ps, boolean unixStyle) throws IOException {
  1465. if (unixStyle) {
  1466. writeUnixHeader(ps);
  1467. writeUnixLaunchLine(className, ps);
  1468. } else {
  1469. writeWindowsHeader(ps);
  1470. writeWindowsLaunchLine(className, ps);
  1471. }
  1472. /*
  1473. * ps.print(quote(unixStyle, false, context.javaPath.getAbsolutePath())); ps.print(" "); ps.print("-classpath ");
  1474. * ps.print(quote(unixStyle, true, makeClassPath(unixStyle))); ps.print(" "); ps.print("-Xmx64M "); ps.print(className);
  1475. * ps.print(" "); ps.print(makeScriptArgs(unixStyle));
  1476. */
  1477. }
  1478. public void writeScript(String className, boolean unixStyle) throws IOException {
  1479. File file = makeScriptFile(className, unixStyle);
  1480. if (!checkExistingFile(file)) {
  1481. return;
  1482. }
  1483. PrintStream ps = getPrintStream(file);
  1484. writeScript(toolsPackage + '.' + className + ".Main", ps, unixStyle);
  1485. ps.close();
  1486. //??? unixStyle vs. onUnix()
  1487. if (context.onUnix()) {
  1488. makeExecutable(file);
  1489. }
  1490. }
  1491. public boolean checkExistingFile(File file) {
  1492. if (!file.exists()) {
  1493. return true;
  1494. }
  1495. return context.shouldOverwrite(file);
  1496. }
  1497. /*
  1498. * final static String OVERWRITE_MESSAGE = "Overwrite launch script "; final static String OVERWRITE_TITLE = "Overwrite?";
  1499. *
  1500. * final static String[] OVERWRITE_OPTIONS = { "Yes", "No", "Yes to all", "No to all" };
  1501. *
  1502. * final static int OVERWRITE_YES = 0; final static int OVERWRITE_NO = 1; final static int OVERWRITE_ALL = 2; final static int
  1503. * OVERWRITE_NONE = 3;
  1504. *
  1505. * int overwriteState = OVERWRITE_NO; boolean shouldOverwrite(final File file) { if (overwriteState == OVERWRITE_ALL) return
  1506. * true; if (overwriteState == OVERWRITE_NONE) return false;
  1507. *
  1508. * try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { int ret =
  1509. * JOptionPane.showOptionDialog(context.installer.frame, OVERWRITE_MESSAGE+file.getPath(), OVERWRITE_TITLE,
  1510. * JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, OVERWRITE_OPTIONS, OVERWRITE_OPTIONS[OVERWRITE_YES]);
  1511. *
  1512. * overwriteState = ret; } }); } catch (InvocationTargetException ite) { context.handleException(ite.getTargetException()); }
  1513. * catch (InterruptedException ie) { }
  1514. *
  1515. * return overwriteState == OVERWRITE_YES || overwriteState == OVERWRITE_ALL; }
  1516. */
  1517. public void writeScript(String className) throws IOException {
  1518. writeScript(className, true);
  1519. if (context.onWindows()) {
  1520. writeScript(className, false);
  1521. }
  1522. }
  1523. }
  1524. class JarUnpacker {
  1525. InstallContext context;
  1526. InstallPane installPane;
  1527. public JarUnpacker(InstallContext context, InstallPane installPane) {
  1528. this.context = context;
  1529. this.installPane = installPane;
  1530. }
  1531. public File makeOutputFile(String name, File outputFile) {
  1532. int index;
  1533. int lastIndex = 0;
  1534. while ((index = name.indexOf('/', lastIndex)) != -1) {
  1535. outputFile = new File(outputFile, name.substring(lastIndex, index));
  1536. lastIndex = index + 1;
  1537. }
  1538. return new File(outputFile, name.substring(lastIndex));
  1539. }
  1540. final static int BUF_SIZE = 4096;
  1541. public void writeStream(ZipInputStream zis, File outputFile) throws IOException {
  1542. if (outputFile.exists()) {
  1543. if (!context.shouldOverwrite(outputFile)) {
  1544. return;
  1545. }
  1546. }
  1547. installPane.progressMessage("writing " + outputFile.getAbsolutePath());
  1548. outputFile.getParentFile().mkdirs();
  1549. if (context.isTextFile(outputFile)) {
  1550. writeTextStream(zis, outputFile);
  1551. } else {
  1552. writeBinaryStream(zis, outputFile);
  1553. }
  1554. }
  1555. public void writeBinaryStream(ZipInputStream zis, File outputFile) throws IOException {
  1556. byte[] buffer = new byte[BUF_SIZE];
  1557. int nRead = 0;
  1558. OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
  1559. while ((nRead = zis.read(buffer)) != -1) {
  1560. os.write(buffer, 0, nRead);
  1561. installPane.progressBytesWritten(nRead);
  1562. }
  1563. os.close();
  1564. }
  1565. public void writeTextStream(ZipInputStream zis, File outputFile) throws IOException {
  1566. BufferedWriter os = new BufferedWriter(new FileWriter(outputFile));
  1567. BufferedReader r = new BufferedReader(new InputStreamReader(zis, "US-ASCII"));
  1568. String l;
  1569. while ((l = r.readLine()) != null) {
  1570. os.write(l);
  1571. os.newLine();
  1572. installPane.progressBytesWritten(l.length() + 1);
  1573. }
  1574. os.close();
  1575. }
  1576. public void writeEntry(ZipInputStream zis, ZipEntry entry, File outputDir) throws IOException {
  1577. if (entry.isDirectory()) {
  1578. return;
  1579. }
  1580. String name = entry.getName();
  1581. File outputFile = makeOutputFile(name, outputDir);
  1582. writeStream(zis, outputFile);
  1583. }
  1584. public void unpack(String jarName, File outputDir) throws IOException {
  1585. URL url = getClass().getResource(jarName);
  1586. InputStream stream = url.openStream();
  1587. ZipInputStream zis = new ZipInputStream(stream);
  1588. // int i = 0;
  1589. ZipEntry entry;
  1590. while ((entry = zis.getNextEntry()) != null) {
  1591. // final String name = entry.getName();
  1592. writeEntry(zis, entry, outputDir);
  1593. //
  1594. }
  1595. installPane.progressMessage("done writing");
  1596. }
  1597. }