001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.projectopener;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.net.UnknownHostException;
049: import java.util.ArrayList;
050: import java.util.Arrays;
051: import java.util.Comparator;
052: import java.util.List;
053: import java.util.ResourceBundle;
054: import java.util.logging.Logger;
055:
056: /**
057: *
058: * @author Milan Kubec
059: */
060: public class WSProjectOpener {
061:
062: public static String APP_VERSION = "1.1";
063:
064: public static String MIN_NB_VERSION = "5.5.1";
065:
066: public static Logger LOGGER = Logger
067: .getLogger("org.netbeans.projectopener.WSProjectOpener");
068:
069: private static String DEFAULT_USERDIR = "5.5.1";
070:
071: private static Comparator COMPARATOR = NBInstallation.LAST_USED_COMPARATOR;
072:
073: private WSProjectOpener() {
074: }
075:
076: /**
077: * @param args the command line arguments
078: */
079: public static void main(String[] args) {
080: new WSProjectOpener().openProject(args);
081: }
082:
083: private void openProject(String args[]) {
084:
085: ArgsHandler handler = new ArgsHandler(args);
086: ResourceBundle bundle = ResourceBundle
087: .getBundle("org/netbeans/projectopener/Bundle"); // NOI18N
088:
089: // Register own logger that prints messages to ${TEMP}/projectopener.log
090: LOGGER.addHandler(new FileLogHandler());
091:
092: // ######################
093: // ### Test Arguments ###
094: // ######################
095:
096: String allArgs = handler.getAllArgs();
097: if (allArgs.equals("")) {
098: LOGGER.severe("No arguments passed, exiting ..."); // NOI18N
099: Utils.showErrMessage(bundle.getString("ERR_No_Args"),
100: bundle.getString("ERR_Title"));
101: System.exit(0);
102: } else {
103: LOGGER.info("Passed arguments: " + handler.getAllArgs()); // NOI18N
104: }
105:
106: // ---
107:
108: URL prjURL = null;
109: String prjURLStr = handler.getArgValue("projecturl"); // NOI18N
110: if (prjURLStr == null) {
111: LOGGER
112: .severe("Project URL argument not specified, exiting ...");
113: Utils.showErrMessage(bundle.getString("ERR_No_URL"), bundle
114: .getString("ERR_Title"));
115: System.exit(0);
116: } else {
117: try {
118: prjURL = new URL(prjURLStr);
119: } catch (MalformedURLException ex) {
120: LOGGER.severe("Exception: " + Utils.exc2String(ex)); // NOI18N
121: Utils.showErrMessage(bundle.getString("ERR_Bad_URL"),
122: bundle.getString("ERR_Title"));
123: System.exit(0);
124: }
125: }
126: LOGGER.info("Project URL: " + prjURL.toExternalForm()); // NOI18N
127:
128: // ################################################
129: // ### Get user home dir from system properties ###
130: // ################################################
131:
132: String userHome = System.getProperty("user.home"); // NOI18N
133: if (userHome != null) {
134: LOGGER.info("Userhome: " + userHome); // NOI18N
135: } else {
136: // XXX Ask for NB installation?
137: LOGGER
138: .severe("Cannot determine user home directory, exiting ..."); // NOI18N
139: Utils.showErrMessage(bundle.getString("ERR_No_User_Home"),
140: bundle.getString("ERR_Title"));
141: System.exit(0);
142: }
143:
144: // ######################################
145: // ### Handle required min NB version ###
146: // ######################################
147:
148: String nbVersion = handler.getArgValue("minversion"); // NOI18N
149: if (nbVersion == null) {
150: nbVersion = DEFAULT_USERDIR;
151: LOGGER.info("No NB version specified, using default: "
152: + nbVersion); // NOI18N
153: } else {
154: LOGGER.info("Requested NB userdir: " + nbVersion);
155: String verParts[] = Utils.getVersionParts(nbVersion);
156: if (verParts != null && !verParts[0].equals("")) {
157: if (Utils.compareVersions(verParts[0], MIN_NB_VERSION) < 0) {
158: LOGGER
159: .severe("Requested version is lower than allowed, exiting ..."); // NOI18N
160: Utils.showErrMessage(bundle
161: .getString("ERR_Low_Version"), bundle
162: .getString("ERR_Title"));
163: System.exit(0);
164: }
165: } else {
166: // nbVersion is not recognized as valid or version is not specified
167: // if the version is only 'dev' it's OK
168: if ((verParts == null)
169: || (verParts != null && verParts[0].equals("") && !verParts[1]
170: .equals("dev"))) {
171: LOGGER
172: .severe("Requested version is not valid, exiting ..."); // NOI18N
173: Utils.showErrMessage(bundle
174: .getString("ERR_Not_Valid_Version"), bundle
175: .getString("ERR_Title"));
176: System.exit(0);
177: }
178: }
179: }
180:
181: // ######################################################
182: // ### Create temp files, download and unzip projects ###
183: // ######################################################
184:
185: File tempFile = null;
186: File tempDir = null;
187:
188: try {
189: tempFile = Utils.createTempFile(null, "nbproject", ".zip",
190: true);
191: tempDir = Utils.createTempDir(null, "nbproject");
192: } catch (IOException ioe) {
193: // XXX Ask user for different dir to create temp files
194: }
195:
196: if (tempFile == null || tempDir == null) {
197: LOGGER
198: .severe("Temporary file or folder creation failed, project cannot be downloaded, exiting ...");
199: Utils.showErrMessage(bundle
200: .getString("ERR_Temp_Creation_Failed"), bundle
201: .getString("ERR_Title"));
202: System.exit(0);
203: }
204:
205: LOGGER.info("Temp file: " + tempFile.getAbsolutePath());
206: LOGGER.info("Temp project dir: " + tempDir.getAbsolutePath());
207:
208: try {
209: boolean downloadFinished = false;
210: while (!downloadFinished) {
211: try {
212: Utils.download(prjURLStr, tempFile);
213: LOGGER.info("Download finished.");
214: downloadFinished = true;
215: } catch (UnknownHostException uhe) {
216: LOGGER
217: .severe("Exception during download operation: "
218: + Utils.exc2String(uhe));
219: // Might be problem with Proxy settings
220: // look for another proxy and try again
221: boolean cont = Utils.maybeAnotherProxy();
222: if (!cont) {
223: // user selected exit in the dialog
224: System.exit(0);
225: }
226: }
227: }
228: } catch (IOException ioe) {
229: LOGGER.severe("Exception during download operation: "
230: + Utils.exc2String(ioe));
231: Utils.showErrMessage(bundle
232: .getString("ERR_Download_Failed"), bundle
233: .getString("ERR_Title"));
234: System.exit(0);
235: }
236:
237: try {
238: Utils.unzip(tempFile, tempDir);
239: LOGGER.info("Unzip finished.");
240: } catch (IOException ioe) {
241: LOGGER.severe("Exception during unzip operation: "
242: + Utils.exc2String(ioe));
243: Utils.showErrMessage(bundle.getString("ERR_Unzip_Failed"),
244: bundle.getString("ERR_Title"));
245: System.exit(0);
246: }
247:
248: // ######################################
249: // ### Process downloaded NB Projects ###
250: // ######################################
251:
252: String projPaths[] = null;
253: SavedProjects sp = Utils.getSavedProjects(tempDir);
254: String mainPrjPath = handler.getArgValue("mainproject");
255: if (mainPrjPath != null) {
256: projPaths = sp.getSortedProjectsPaths(mainPrjPath);
257: } else {
258: projPaths = sp.getProjectPaths();
259: }
260:
261: if (projPaths.length == 0) {
262: LOGGER
263: .severe("No NetBeans projects were downloaded, exiting ...");
264: Utils.showErrMessage(bundle
265: .getString("ERR_No_Prj_Downloaded"), bundle
266: .getString("ERR_Title"));
267: System.exit(0);
268: }
269:
270: LOGGER.info("Project paths: " + Arrays.asList(projPaths));
271:
272: // ##################################
273: // ### Find right NB Installation ###
274: // ##################################
275:
276: File execDir = null;
277: NBInstallation nbis[] = UserdirScanner.suitableNBInstallations(
278: new File(userHome), nbVersion, COMPARATOR);
279: LOGGER.info("Suitable NB installations: "
280: + Arrays.asList(nbis).toString());
281: if (nbis.length > 0) {
282: for (int i = 0; i < nbis.length; i++) {
283: // try to find running IDE that can handle downloaded projects
284: if (nbis[i].isLocked()
285: && nbis[i].canHandle(sp.getTypes())) {
286: LOGGER
287: .info("IDE: "
288: + nbis[i].getInstallDir()
289: + " is already running and can handle downloaded projects.");
290: execDir = nbis[i].getExecDir();
291: break;
292: }
293: }
294: if (execDir == null) {
295: for (int i = 0; i < nbis.length; i++) {
296: // try to find any IDE that can handle downloaded projects
297: if (nbis[i].canHandle(sp.getTypes())) {
298: LOGGER.info("IDE: " + nbis[i].getInstallDir()
299: + " can handle downloaded projects.");
300: execDir = nbis[i].getExecDir();
301: break;
302: }
303: }
304: }
305: }
306: // no nb installation found
307: if (execDir == null) {
308: // XXX look for saved install dir from previous failed search
309: // then ask user for another NB install dir and save to properties
310: boolean found = false;
311: while (!found) {
312: Integer cont = Utils.getAnotherNBInstallDir(nbVersion);
313: if (cont.equals(Utils.DialogDescriptor.EXIT)) {
314: LOGGER
315: .info("User selected Exit when asked for another NB install dir, exiting ...");
316: System.exit(0);
317: }
318: if (cont.equals(Utils.DialogDescriptor.DOWNLOAD)) {
319: LOGGER
320: .info("User selected Download, opening the page in browser, exiting ...");
321: Utils.showDocument(bundle
322: .getString("URL_Download_NB"));
323: System.exit(0);
324: }
325: File nbDir = Utils.anotherNBDir;
326: LOGGER
327: .info("User selected alternative NB install dir: "
328: + nbDir.getAbsolutePath());
329: if (NBInstallation.isNBInstallation(nbDir)) {
330: execDir = new File(nbDir, "bin");
331: // save the installdir to muffin
332: Utils.setProperty("jws.netbeans.installdir", nbDir
333: .getAbsolutePath());
334: found = true;
335: } else {
336: LOGGER
337: .info("Selected dir is probably not NB install dir, try again ...");
338: }
339: }
340: }
341:
342: // probably not necessary
343: if (execDir == null) {
344: LOGGER
345: .severe("Cannot locate NetBeans userdir or install dir, exiting ...");
346: Utils.showErrMessage(bundle
347: .getString("ERR_No_NB_Userdir_Or_Installdir"),
348: bundle.getString("ERR_Title"));
349: // XXX Dialog here ???
350: Utils.showDocument(bundle.getString("URL_Download_NB"));
351: System.exit(0);
352: }
353: LOGGER.info("Exec dir: " + execDir);
354:
355: // ##############################
356: // ### Find platform launcher ###
357: // ##############################
358:
359: String launcher = Utils.getPlatformLauncher();
360: if (launcher == null || "".equals(launcher)) {
361: LOGGER
362: .severe("Cannot determine NetBeans launcher name, exiting ...");
363: Utils.showErrMessage(bundle.getString("ERR_No_Launcher"),
364: bundle.getString("ERR_Title"));
365: // XXX Do you want to save the project?
366: System.exit(0);
367: }
368: LOGGER.info("Launcher name: " + launcher);
369:
370: // ########################################
371: // ### Build command line to launch IDE ###
372: // ########################################
373:
374: List cmdList = new ArrayList();
375: cmdList.add(execDir.getAbsolutePath() + File.separator
376: + launcher);
377: cmdList.add("--open");
378: for (int i = 0; i < projPaths.length; i++) {
379: cmdList.add(projPaths[i]);
380: }
381: String cmdArray[] = (String[]) cmdList
382: .toArray(new java.lang.String[cmdList.size()]);
383: LOGGER.info("Command line: " + Arrays.asList(cmdArray));
384:
385: // ###################
386: // ### Run the IDE ###
387: // ###################
388:
389: try {
390: Process proc = Runtime.getRuntime().exec(cmdArray, null,
391: execDir);
392: // int exitVal = proc.exitValue();
393: // LOGGER.info("Process exit value: " + exitVal);
394: // XXX Try to log output from the process
395: } catch (IOException ioe) {
396: LOGGER.severe("Exception during launching NetBeans IDE: "
397: + Utils.exc2String(ioe));
398: // XXX Do you want to save the project?
399: }
400:
401: }
402:
403: }
|