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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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 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 (int i = 0; i < TEXT_EXTENSIONS.length; i++) {
  619. if (name.endsWith(TEXT_EXTENSIONS[i])) {
  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 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 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 (int suffixIndex = 0; suffixIndex < suffixes.length; suffixIndex++) {
  1058. String suffix = suffixes[suffixIndex];
  1059. for (int prefixIndex = 0; prefixIndex < paths.length; prefixIndex++) {
  1060. String prefix = paths[prefixIndex];
  1061. prefix = applyProperties(prefix);
  1062. File test = new File(prefix + suffix);
  1063. if (isLegalJavaHome(test)) {
  1064. if (isLegalJDKHome(test)) {
  1065. return test;
  1066. } else if (javaHome == null) {
  1067. javaHome = test;
  1068. }
  1069. }
  1070. }
  1071. }
  1072. return javaHome;
  1073. }
  1074. }
  1075. class InstallPane extends WizardPane {
  1076. private JProgressBar progressBar;
  1077. private JTextField progressItem;
  1078. private JEditorPane message;
  1079. private boolean makeLaunchScripts = false;
  1080. public InstallPane(boolean makeLaunchScripts) {
  1081. this.makeLaunchScripts = makeLaunchScripts;
  1082. }
  1083. public JPanel makePanel() {
  1084. message = makeHTMLArea("install-start.html");
  1085. progressBar = new JProgressBar();
  1086. progressItem = new JTextField();
  1087. progressItem.setOpaque(false);
  1088. progressItem.setFont(context.getFont());
  1089. progressItem.setEditable(false);
  1090. GridBagLayout bag = new GridBagLayout();
  1091. GridBagConstraints c = new GridBagConstraints();
  1092. JPanel panel = new JPanel(bag);
  1093. c.fill = GridBagConstraints.BOTH;
  1094. c.weightx = 1.0;
  1095. c.weighty = 1.0;
  1096. //c.ipady = 10;
  1097. c.gridwidth = GridBagConstraints.REMAINDER;
  1098. bag.setConstraints(message, c);
  1099. panel.add(message);
  1100. c.weighty = 0.0;
  1101. //c.fill = GridBagConstraints.VERTICAL;
  1102. bag.setConstraints(progressBar, c);
  1103. panel.add(progressBar);
  1104. c.weighty = 0.1;
  1105. JLabel space = new JLabel();
  1106. bag.setConstraints(space, c);
  1107. panel.add(space);
  1108. c.weighty = 0.0;
  1109. bag.setConstraints(progressItem, c);
  1110. panel.add(progressItem);
  1111. c.weighty = 0.5;
  1112. space = new JLabel();
  1113. bag.setConstraints(space, c);
  1114. panel.add(space);
  1115. return panel;
  1116. }
  1117. class InstallRunner implements Runnable {
  1118. public InstallRunner() {
  1119. }
  1120. public void run() {
  1121. try {
  1122. new CurrentJarUnpacker(context, InstallPane.this).unpack(Installer.RESOURCE_DIR + "/contents.txt",
  1123. context.getOutputDir());
  1124. if (makeLaunchScripts) {
  1125. LaunchScriptMaker lsm = new LaunchScriptMaker(context);
  1126. lsm.writeScript("ajc");
  1127. lsm.writeScript("ajdoc");
  1128. //lsm.writeScript("ajdb");
  1129. lsm.writeScript("ajbrowser");
  1130. // Moved to the bin dir in 1.2.1
  1131. // we should now come back and make the generation of this
  1132. // script uniform with those above.
  1133. lsm.writeAJLaunchScript("aj", false);
  1134. lsm.writeAJLaunchScript("aj5", true);
  1135. }
  1136. if (hasGui()) {
  1137. progressBar.setValue(100);
  1138. setHTMLArea(message, "install-finish.html");
  1139. }
  1140. } catch (IOException ioe) {
  1141. context.handleException(ioe);
  1142. }
  1143. if (hasGui()) {
  1144. cancelButton.setEnabled(false);
  1145. nextButton.setEnabled(true);
  1146. }
  1147. }
  1148. }
  1149. public Component makeButtons(Installer installer) {
  1150. Component ret = super.makeButtons(installer);
  1151. //nextButton.setText("Finish");
  1152. nextButton.setEnabled(false);
  1153. //nextButton.addActionListener(installer.makeFinishAction(this));
  1154. backButton.setEnabled(false);
  1155. return ret;
  1156. }
  1157. public void run() {
  1158. Thread thread = new Thread(new InstallRunner());
  1159. thread.start();
  1160. }
  1161. public void progressMessage(final String message) {
  1162. if (!hasGui()) {
  1163. return;
  1164. }
  1165. try {
  1166. //XXX performance tradeoff between invokeAndWait and invokeLater...
  1167. SwingUtilities.invokeAndWait(new Runnable() {
  1168. public void run() {
  1169. progressItem.setText(message);
  1170. }
  1171. });
  1172. } catch (InvocationTargetException ite) {
  1173. context.handleException(ite.getTargetException());
  1174. } catch (InterruptedException ie) {
  1175. }
  1176. }
  1177. int nBytes = 0;
  1178. int bytesWritten = 0;
  1179. public void progressBytesWritten(int bytes) {
  1180. if (!hasGui()) {
  1181. return;
  1182. }
  1183. bytesWritten += bytes;
  1184. final int PCT = (int) (100.0 * bytesWritten / nBytes);
  1185. //System.out.println("bytesWritten: " + bytesWritten);
  1186. try {
  1187. //XXX performance tradeoff between invokeAndWait and invokeLater...
  1188. SwingUtilities.invokeAndWait(new Runnable() {
  1189. public void run() {
  1190. progressBar.setValue(PCT);
  1191. }
  1192. });
  1193. } catch (InvocationTargetException ite) {
  1194. context.handleException(ite.getTargetException());
  1195. } catch (InterruptedException ie) {
  1196. }
  1197. }
  1198. }
  1199. class CurrentJarUnpacker {
  1200. InstallContext context;
  1201. InstallPane installPane;
  1202. public CurrentJarUnpacker(InstallContext context, InstallPane installPane) {
  1203. this.context = context;
  1204. this.installPane = installPane;
  1205. }
  1206. public File makeOutputFile(String name, File outputFile) {
  1207. int index;
  1208. int lastIndex = 0;
  1209. while ((index = name.indexOf('/', lastIndex)) != -1) {
  1210. outputFile = new File(outputFile, name.substring(lastIndex, index));
  1211. lastIndex = index + 1;
  1212. }
  1213. return new File(outputFile, name.substring(lastIndex));
  1214. }
  1215. final static int BUF_SIZE = 4096;
  1216. public void writeStream(InputStream zis, File outputFile) throws IOException {
  1217. if (outputFile.exists()) {
  1218. if (!context.shouldOverwrite(outputFile)) {
  1219. return;
  1220. }
  1221. }
  1222. installPane.progressMessage("writing " + outputFile.getAbsolutePath());
  1223. outputFile.getParentFile().mkdirs();
  1224. if (context.isTextFile(outputFile)) {
  1225. writeTextStream(zis, outputFile);
  1226. } else {
  1227. writeBinaryStream(zis, outputFile);
  1228. }
  1229. }
  1230. public void writeBinaryStream(InputStream zis, File outputFile) throws IOException {
  1231. byte[] buffer = new byte[BUF_SIZE];
  1232. int nRead = 0;
  1233. OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
  1234. while ((nRead = zis.read(buffer)) != -1) {
  1235. os.write(buffer, 0, nRead);
  1236. installPane.progressBytesWritten(nRead);
  1237. }
  1238. os.close();
  1239. }
  1240. public void writeTextStream(InputStream zis, File outputFile) throws IOException {
  1241. BufferedWriter os = new BufferedWriter(new FileWriter(outputFile));
  1242. BufferedReader r = new BufferedReader(new InputStreamReader(zis, "US-ASCII"));
  1243. String l;
  1244. while ((l = r.readLine()) != null) {
  1245. os.write(l);
  1246. os.newLine();
  1247. installPane.progressBytesWritten(l.length() + 1);
  1248. }
  1249. os.close();
  1250. }
  1251. public void writeResource(String name, File outputDir) throws IOException {
  1252. File outputFile = makeOutputFile(name, outputDir);
  1253. //System.out.println("finding name: " + name);
  1254. writeStream(getClass().getResourceAsStream("/" + name), outputFile);
  1255. }
  1256. public void writeResource(JarFile jarFile, JarEntry entry, File outputDir) throws IOException {
  1257. String name = entry.getName().substring(6);
  1258. File outputFile = makeOutputFile(name, outputDir);
  1259. //System.out.println("finding name: " + name);
  1260. // writeStream(getClass().getResourceAsStream("/" + name), outputFile);
  1261. writeStream(jarFile.getInputStream(entry), outputFile);
  1262. }
  1263. public void unpack(String contentsName, File outputDir) throws IOException {
  1264. URL url = getClass().getResource(contentsName);
  1265. // Process everything under 'files/**' copying to the target
  1266. // install directory with 'files/' removed
  1267. JarURLConnection juc = (JarURLConnection) url.openConnection();
  1268. JarFile jf = juc.getJarFile();
  1269. Enumeration<JarEntry> entries = jf.entries();
  1270. while (entries.hasMoreElements()) {
  1271. JarEntry je = entries.nextElement();
  1272. if (je.getName().startsWith("files/") && !je.getName().endsWith("/")) {
  1273. writeResource(jf, je, outputDir);
  1274. }
  1275. }
  1276. // InputStream stream = url.openStream();
  1277. // BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "US-ASCII"));
  1278. // String line = reader.readLine();
  1279. // installPane.nBytes = Integer.parseInt(line);
  1280. //
  1281. // while ((line = reader.readLine()) != null) {
  1282. // writeResource(line, outputDir);
  1283. // }
  1284. installPane.progressMessage("done writing");
  1285. }
  1286. }
  1287. class LaunchScriptMaker {
  1288. static final String toolsPackage = "org.aspectj.tools";
  1289. InstallContext context;
  1290. public LaunchScriptMaker(InstallContext context) {
  1291. this.context = context;
  1292. }
  1293. /**
  1294. *
  1295. */
  1296. public void writeAJLaunchScript(String name, boolean isJava5) throws IOException {
  1297. if (!context.onUnix()) {
  1298. if (context.onOS2()) {
  1299. name += ".cmd";
  1300. } else if (context.onWindows()) {
  1301. name += ".bat";
  1302. }
  1303. }
  1304. File destDir = new File(context.getOutputDir(), "bin");
  1305. destDir.mkdirs();
  1306. File file = new File(destDir, name);
  1307. PrintStream ps = getPrintStream(file);
  1308. writeAJLaunchScriptContent(ps, isJava5);
  1309. ps.close();
  1310. if (context.onUnix()) {
  1311. makeExecutable(file);
  1312. }
  1313. }
  1314. /**
  1315. * @param ps
  1316. */
  1317. private void writeAJLaunchScriptContent(PrintStream ps, boolean isJava5) {
  1318. if (context.onUnix()) {
  1319. writeUnixHeader(ps);
  1320. if (isJava5) {
  1321. writeAJ5UnixLaunchLine(ps);
  1322. } else {
  1323. writeAJUnixLaunchLine(ps);
  1324. }
  1325. } else {
  1326. writeWindowsHeader(ps);
  1327. if (isJava5) {
  1328. writeAJ5WindowsLaunchLine(ps);
  1329. } else {
  1330. writeAJWindowsLaunchLine(ps);
  1331. }
  1332. }
  1333. }
  1334. /**
  1335. * @param ps
  1336. */
  1337. private void writeAJWindowsLaunchLine(PrintStream ps) {
  1338. ps.println("\"%JAVA_HOME%\\bin\\java\" -classpath " + "\"%ASPECTJ_HOME%\\lib\\aspectjweaver.jar\""
  1339. + " \"-Djava.system.class.loader=org.aspectj.weaver.loadtime.WeavingURLClassLoader\""
  1340. + " \"-Daj.class.path=%ASPECTPATH%;%CLASSPATH%\"" + " \"-Daj.aspect.path=%ASPECTPATH%\"" + " "
  1341. + makeScriptArgs(false));
  1342. }
  1343. /**
  1344. * @param ps
  1345. */
  1346. private void writeAJ5WindowsLaunchLine(PrintStream ps) {
  1347. ps.println("\"%JAVA_HOME%\\bin\\java\" -classpath " + "\"%ASPECTJ_HOME%\\lib\\aspectjweaver.jar;%CLASSPATH%\""
  1348. + " \"-javaagent:%ASPECTJ_HOME%\\lib\\aspectjweaver.jar\"" + " " + makeScriptArgs(false));
  1349. }
  1350. /**
  1351. * @param ps
  1352. */
  1353. private void writeAJUnixLaunchLine(PrintStream ps) {
  1354. ps.println("\"$JAVA_HOME/bin/java\" -classpath" + " \"$ASPECTJ_HOME/lib/aspectjweaver.jar\""
  1355. + " \"-Djava.system.class.loader=org.aspectj.weaver.loadtime.WeavingURLClassLoader\""
  1356. + " \"-Daj.class.path=$ASPECTPATH:$CLASSPATH\"" + " \"-Daj.aspect.path=$ASPECTPATH\"" + " " + makeScriptArgs(true));
  1357. }
  1358. /**
  1359. * @param ps
  1360. */
  1361. private void writeAJ5UnixLaunchLine(PrintStream ps) {
  1362. ps.println("\"$JAVA_HOME/bin/java\" -classpath" + " \"$ASPECTJ_HOME/lib/aspectjweaver.jar:$CLASSPATH\""
  1363. + " \"-javaagent:$ASPECTJ_HOME/lib/aspectjweaver.jar\"" + " " + makeScriptArgs(true));
  1364. }
  1365. private void writeWindowsHeader(PrintStream ps) {
  1366. ps.println("@echo off");
  1367. ps.println("REM This file generated by AspectJ installer");
  1368. ps.println("REM Created on " + new java.util.Date() + " by " + System.getProperty("user.name"));
  1369. ps.println("");
  1370. ps.println("if \"%JAVA_HOME%\" == \"\" set JAVA_HOME=" + context.javaPath.getAbsolutePath());
  1371. ps.println("if \"%ASPECTJ_HOME%\" == \"\" set ASPECTJ_HOME=" + context.getOutputDir().getAbsolutePath());
  1372. ps.println("");
  1373. ps.println("if exist \"%JAVA_HOME%\\bin\\java.exe\" goto haveJava");
  1374. ps.println("if exist \"%JAVA_HOME%\\bin\\java.bat\" goto haveJava");
  1375. ps.println("if exist \"%JAVA_HOME%\\bin\\java\" goto haveJava");
  1376. ps.println("echo java does not exist as %JAVA_HOME%\\bin\\java");
  1377. ps.println("echo please fix the JAVA_HOME environment variable");
  1378. ps.println(":haveJava");
  1379. }
  1380. private void writeWindowsLaunchLine(String className, PrintStream ps) {
  1381. ps.println("\"%JAVA_HOME%\\bin\\java\" -classpath " +
  1382. // "\"%ASPECTJ_HOME%\\lib\\aspectjtools.jar;%CLASSPATH%\""+
  1383. "\"%ASPECTJ_HOME%\\lib\\aspectjtools.jar;%JAVA_HOME%\\lib\\tools.jar;%CLASSPATH%\"" + " -Xmx64M " + className + //" -defaultClasspath " + "\"%CLASSPATH%\"" +
  1384. " " + makeScriptArgs(false));
  1385. }
  1386. private void writeUnixHeader(PrintStream ps) {
  1387. File binsh = new File(File.separator + "bin", "sh");
  1388. if (binsh.canRead()) {
  1389. ps.println("#!" + binsh.getPath());
  1390. }
  1391. ps.println("# This file generated by AspectJ installer");
  1392. ps.println("# Created on " + new java.util.Date() + " by " + System.getProperty("user.name"));
  1393. ps.println("");
  1394. ps.println("if [ \"$JAVA_HOME\" = \"\" ] ; then JAVA_HOME=" + quote(true, false, context.javaPath.getAbsolutePath()));
  1395. ps.println("fi");
  1396. ps.println("if [ \"$ASPECTJ_HOME\" = \"\" ] ; then ASPECTJ_HOME=" + quote(true, false, context.getOutputDir()));
  1397. ps.println("fi");
  1398. ps.println("");
  1399. }
  1400. private void writeUnixLaunchLine(String className, PrintStream ps) {
  1401. String sep = File.pathSeparator;
  1402. ps.println("\"$JAVA_HOME/bin/java\" -classpath " + "\"$ASPECTJ_HOME/lib/aspectjtools.jar" + sep
  1403. + "$JAVA_HOME/lib/tools.jar" + sep + "$CLASSPATH\"" + " -Xmx64M " + className + " " + makeScriptArgs(true));
  1404. }
  1405. private void makeExecutable(File file) {
  1406. try {
  1407. Runtime curRuntime = Runtime.getRuntime();
  1408. curRuntime.exec("chmod 777 " + quote(true, false, file));
  1409. } catch (Throwable t) {
  1410. // ignore any errors that occur while trying to chmod
  1411. }
  1412. }
  1413. private String makeScriptArgs(boolean unixStyle) {
  1414. if (unixStyle) {
  1415. return "\"$@\"";
  1416. } else if (context.onWindowsPro()) {
  1417. return "%*";
  1418. } else {
  1419. return "%1 %2 %3 %4 %5 %6 %7 %8 %9";
  1420. }
  1421. }
  1422. private String quote(boolean unixStyle, boolean forceQuotes, File file) {
  1423. return quote(unixStyle, forceQuotes, file.getAbsolutePath());
  1424. }
  1425. private String quote(boolean unixStyle, boolean forceQuotes, String s) {
  1426. if (context.onWindows() && unixStyle) {
  1427. s = s.replace('\\', '/');
  1428. }
  1429. if (!forceQuotes && s.indexOf(' ') == -1) {
  1430. return s;
  1431. }
  1432. return "\"" + s + "\"";
  1433. }
  1434. private File makeScriptFile(String name, boolean unixStyle) throws IOException {
  1435. if (!unixStyle) {
  1436. if (context.onOS2()) {
  1437. name += ".cmd";
  1438. } else if (context.onWindows()) {
  1439. name += ".bat";
  1440. }
  1441. }
  1442. //XXX probably want a context.getOutputBinDir()
  1443. File bindir = new File(context.getOutputDir(), "bin");
  1444. bindir.mkdirs();
  1445. File file = new File(bindir, name);
  1446. return file;
  1447. }
  1448. private PrintStream getPrintStream(File file) throws IOException {
  1449. return new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
  1450. }
  1451. String makeClassPathVar(boolean unixStyle) {
  1452. if (unixStyle) {
  1453. return "$CLASSPATH";
  1454. } else {
  1455. return "%CLASSPATH%";
  1456. }
  1457. }
  1458. public String makeClassPath(boolean unixStyle) throws IOException {
  1459. return context.toolsJarPath.getAbsolutePath() + File.pathSeparator
  1460. +
  1461. //XXX want context.getOutputLibDir()
  1462. new File(new File(context.getOutputDir(), "lib"), "aspectjtools.jar").getAbsolutePath() + File.pathSeparator
  1463. + makeClassPathVar(unixStyle);
  1464. }
  1465. public void writeScript(String className, PrintStream ps, boolean unixStyle) throws IOException {
  1466. if (unixStyle) {
  1467. writeUnixHeader(ps);
  1468. writeUnixLaunchLine(className, ps);
  1469. } else {
  1470. writeWindowsHeader(ps);
  1471. writeWindowsLaunchLine(className, ps);
  1472. }
  1473. /*
  1474. * ps.print(quote(unixStyle, false, context.javaPath.getAbsolutePath())); ps.print(" "); ps.print("-classpath ");
  1475. * ps.print(quote(unixStyle, true, makeClassPath(unixStyle))); ps.print(" "); ps.print("-Xmx64M "); ps.print(className);
  1476. * ps.print(" "); ps.print(makeScriptArgs(unixStyle));
  1477. */
  1478. }
  1479. public void writeScript(String className, boolean unixStyle) throws IOException {
  1480. File file = makeScriptFile(className, unixStyle);
  1481. if (!checkExistingFile(file)) {
  1482. return;
  1483. }
  1484. PrintStream ps = getPrintStream(file);
  1485. writeScript(toolsPackage + '.' + className + ".Main", ps, unixStyle);
  1486. ps.close();
  1487. //??? unixStyle vs. onUnix()
  1488. if (context.onUnix()) {
  1489. makeExecutable(file);
  1490. }
  1491. }
  1492. public boolean checkExistingFile(File file) {
  1493. if (!file.exists()) {
  1494. return true;
  1495. }
  1496. return context.shouldOverwrite(file);
  1497. }
  1498. /*
  1499. * final static String OVERWRITE_MESSAGE = "Overwrite launch script "; final static String OVERWRITE_TITLE = "Overwrite?";
  1500. *
  1501. * final static String[] OVERWRITE_OPTIONS = { "Yes", "No", "Yes to all", "No to all" };
  1502. *
  1503. * final static int OVERWRITE_YES = 0; final static int OVERWRITE_NO = 1; final static int OVERWRITE_ALL = 2; final static int
  1504. * OVERWRITE_NONE = 3;
  1505. *
  1506. * int overwriteState = OVERWRITE_NO; boolean shouldOverwrite(final File file) { if (overwriteState == OVERWRITE_ALL) return
  1507. * true; if (overwriteState == OVERWRITE_NONE) return false;
  1508. *
  1509. * try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { int ret =
  1510. * JOptionPane.showOptionDialog(context.installer.frame, OVERWRITE_MESSAGE+file.getPath(), OVERWRITE_TITLE,
  1511. * JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, OVERWRITE_OPTIONS, OVERWRITE_OPTIONS[OVERWRITE_YES]);
  1512. *
  1513. * overwriteState = ret; } }); } catch (InvocationTargetException ite) { context.handleException(ite.getTargetException()); }
  1514. * catch (InterruptedException ie) { }
  1515. *
  1516. * return overwriteState == OVERWRITE_YES || overwriteState == OVERWRITE_ALL; }
  1517. */
  1518. public void writeScript(String className) throws IOException {
  1519. writeScript(className, true);
  1520. if (context.onWindows()) {
  1521. writeScript(className, false);
  1522. }
  1523. }
  1524. }
  1525. class JarUnpacker {
  1526. InstallContext context;
  1527. InstallPane installPane;
  1528. public JarUnpacker(InstallContext context, InstallPane installPane) {
  1529. this.context = context;
  1530. this.installPane = installPane;
  1531. }
  1532. public File makeOutputFile(String name, File outputFile) {
  1533. int index;
  1534. int lastIndex = 0;
  1535. while ((index = name.indexOf('/', lastIndex)) != -1) {
  1536. outputFile = new File(outputFile, name.substring(lastIndex, index));
  1537. lastIndex = index + 1;
  1538. }
  1539. return new File(outputFile, name.substring(lastIndex));
  1540. }
  1541. final static int BUF_SIZE = 4096;
  1542. public void writeStream(ZipInputStream zis, File outputFile) throws IOException {
  1543. if (outputFile.exists()) {
  1544. if (!context.shouldOverwrite(outputFile)) {
  1545. return;
  1546. }
  1547. }
  1548. installPane.progressMessage("writing " + outputFile.getAbsolutePath());
  1549. outputFile.getParentFile().mkdirs();
  1550. if (context.isTextFile(outputFile)) {
  1551. writeTextStream(zis, outputFile);
  1552. } else {
  1553. writeBinaryStream(zis, outputFile);
  1554. }
  1555. }
  1556. public void writeBinaryStream(ZipInputStream zis, File outputFile) throws IOException {
  1557. byte[] buffer = new byte[BUF_SIZE];
  1558. int nRead = 0;
  1559. OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
  1560. while ((nRead = zis.read(buffer)) != -1) {
  1561. os.write(buffer, 0, nRead);
  1562. installPane.progressBytesWritten(nRead);
  1563. }
  1564. os.close();
  1565. }
  1566. public void writeTextStream(ZipInputStream zis, File outputFile) throws IOException {
  1567. BufferedWriter os = new BufferedWriter(new FileWriter(outputFile));
  1568. BufferedReader r = new BufferedReader(new InputStreamReader(zis, "US-ASCII"));
  1569. String l;
  1570. while ((l = r.readLine()) != null) {
  1571. os.write(l);
  1572. os.newLine();
  1573. installPane.progressBytesWritten(l.length() + 1);
  1574. }
  1575. os.close();
  1576. }
  1577. public void writeEntry(ZipInputStream zis, ZipEntry entry, File outputDir) throws IOException {
  1578. if (entry.isDirectory()) {
  1579. return;
  1580. }
  1581. String name = entry.getName();
  1582. File outputFile = makeOutputFile(name, outputDir);
  1583. writeStream(zis, outputFile);
  1584. }
  1585. public void unpack(String jarName, File outputDir) throws IOException {
  1586. URL url = getClass().getResource(jarName);
  1587. InputStream stream = url.openStream();
  1588. ZipInputStream zis = new ZipInputStream(stream);
  1589. // int i = 0;
  1590. ZipEntry entry;
  1591. while ((entry = zis.getNextEntry()) != null) {
  1592. // final String name = entry.getName();
  1593. writeEntry(zis, entry, outputDir);
  1594. //
  1595. }
  1596. installPane.progressMessage("done writing");
  1597. }
  1598. }