2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.projectopener;
45
import java.io.IOException;
46
import java.net.MalformedURLException;
48
import java.net.UnknownHostException;
49
import java.util.ArrayList;
50
import java.util.Arrays;
51
import java.util.Comparator;
52
import java.util.List;
53
import java.util.ResourceBundle;
54
import java.util.logging.Logger;
60
public class WSProjectOpener {
62
public static String APP_VERSION = "1.1";
64
public static String MIN_NB_VERSION = "5.5.1";
66
public static Logger LOGGER = Logger.getLogger("org.netbeans.projectopener.WSProjectOpener");
68
private static String DEFAULT_USERDIR = "5.5.1";
70
private static Comparator COMPARATOR = NBInstallation.LAST_USED_COMPARATOR;
72
private WSProjectOpener() {}
75
* @param args the command line arguments
77
public static void main(String[] args) {
78
new WSProjectOpener().openProject(args);
81
private void openProject(String args[]) {
83
ArgsHandler handler = new ArgsHandler(args);
84
ResourceBundle bundle = ResourceBundle.getBundle("org/netbeans/projectopener/Bundle"); // NOI18N
86
// Register own logger that prints messages to ${TEMP}/projectopener.log
87
LOGGER.addHandler(new FileLogHandler());
89
// ######################
90
// ### Test Arguments ###
91
// ######################
93
String allArgs = handler.getAllArgs();
94
if (allArgs.equals("")) {
95
LOGGER.severe("No arguments passed, exiting ..."); // NOI18N
96
Utils.showErrMessage(bundle.getString("ERR_No_Args"), bundle.getString("ERR_Title"));
99
LOGGER.info("Passed arguments: " + handler.getAllArgs()); // NOI18N
105
String prjURLStr = handler.getArgValue("projecturl"); // NOI18N
106
if (prjURLStr == null) {
107
LOGGER.severe("Project URL argument not specified, exiting ...");
108
Utils.showErrMessage(bundle.getString("ERR_No_URL"), bundle.getString("ERR_Title"));
112
prjURL = new URL(prjURLStr);
113
} catch (MalformedURLException ex) {
114
LOGGER.severe("Exception: " + Utils.exc2String(ex)); // NOI18N
115
Utils.showErrMessage(bundle.getString("ERR_Bad_URL"), bundle.getString("ERR_Title"));
119
LOGGER.info("Project URL: " + prjURL.toExternalForm()); // NOI18N
121
// ################################################
122
// ### Get user home dir from system properties ###
123
// ################################################
125
String userHome = System.getProperty("user.home"); // NOI18N
126
if (userHome != null) {
127
LOGGER.info("Userhome: " + userHome); // NOI18N
129
// XXX Ask for NB installation?
130
LOGGER.severe("Cannot determine user home directory, exiting ..."); // NOI18N
131
Utils.showErrMessage(bundle.getString("ERR_No_User_Home"), bundle.getString("ERR_Title"));
135
// ######################################
136
// ### Handle required min NB version ###
137
// ######################################
139
String nbVersion = handler.getArgValue("minversion"); // NOI18N
140
if (nbVersion == null) {
141
nbVersion = DEFAULT_USERDIR;
142
LOGGER.info("No NB version specified, using default: " + nbVersion); // NOI18N
144
LOGGER.info("Requested NB userdir: " + nbVersion);
145
String verParts[] = Utils.getVersionParts(nbVersion);
146
if (verParts != null && !verParts[0].equals("")) {
147
if (Utils.compareVersions(verParts[0], MIN_NB_VERSION) < 0) {
148
LOGGER.severe("Requested version is lower than allowed, exiting ..."); // NOI18N
149
Utils.showErrMessage(bundle.getString("ERR_Low_Version"), bundle.getString("ERR_Title"));
153
// nbVersion is not recognized as valid or version is not specified
154
// if the version is only 'dev' it's OK
155
if ((verParts == null) || (verParts != null && verParts[0].equals("") && !verParts[1].equals("dev"))) {
156
LOGGER.severe("Requested version is not valid, exiting ..."); // NOI18N
157
Utils.showErrMessage(bundle.getString("ERR_Not_Valid_Version"), bundle.getString("ERR_Title"));
163
// ######################################################
164
// ### Create temp files, download and unzip projects ###
165
// ######################################################
167
File tempFile = null;
171
tempFile = Utils.createTempFile(null, "nbproject", ".zip", true);
172
tempDir = Utils.createTempDir(null, "nbproject");
173
} catch (IOException ioe) {
174
// XXX Ask user for different dir to create temp files
177
if (tempFile == null || tempDir == null) {
178
LOGGER.severe("Temporary file or folder creation failed, project cannot be downloaded, exiting ...");
179
Utils.showErrMessage(bundle.getString("ERR_Temp_Creation_Failed"), bundle.getString("ERR_Title"));
183
LOGGER.info("Temp file: " + tempFile.getAbsolutePath());
184
LOGGER.info("Temp project dir: " + tempDir.getAbsolutePath());
187
boolean downloadFinished = false;
188
while (!downloadFinished) {
190
Utils.download(prjURLStr, tempFile);
191
LOGGER.info("Download finished.");
192
downloadFinished = true;
193
} catch (UnknownHostException uhe) {
194
LOGGER.severe("Exception during download operation: " + Utils.exc2String(uhe));
195
// Might be problem with Proxy settings
196
// look for another proxy and try again
197
boolean cont = Utils.maybeAnotherProxy();
199
// user selected exit in the dialog
204
} catch (IOException ioe) {
205
LOGGER.severe("Exception during download operation: " + Utils.exc2String(ioe));
206
Utils.showErrMessage(bundle.getString("ERR_Download_Failed"), bundle.getString("ERR_Title"));
211
Utils.unzip(tempFile, tempDir);
212
LOGGER.info("Unzip finished.");
213
} catch (IOException ioe) {
214
LOGGER.severe("Exception during unzip operation: " + Utils.exc2String(ioe));
215
Utils.showErrMessage(bundle.getString("ERR_Unzip_Failed"), bundle.getString("ERR_Title"));
219
// ######################################
220
// ### Process downloaded NB Projects ###
221
// ######################################
223
String projPaths[] = null;
224
SavedProjects sp = Utils.getSavedProjects(tempDir);
225
String mainPrjPath = handler.getArgValue("mainproject");
226
if (mainPrjPath != null) {
227
projPaths = sp.getSortedProjectsPaths(mainPrjPath);
229
projPaths = sp.getProjectPaths();
232
if (projPaths.length == 0) {
233
LOGGER.severe("No NetBeans projects were downloaded, exiting ...");
234
Utils.showErrMessage(bundle.getString("ERR_No_Prj_Downloaded"), bundle.getString("ERR_Title"));
238
LOGGER.info("Project paths: " + Arrays.asList(projPaths));
240
// ##################################
241
// ### Find right NB Installation ###
242
// ##################################
245
NBInstallation nbis[] = UserdirScanner.suitableNBInstallations(new File(userHome), nbVersion, COMPARATOR);
246
LOGGER.info("Suitable NB installations: " + Arrays.asList(nbis).toString());
247
if (nbis.length > 0) {
248
for (int i = 0; i < nbis.length; i++) {
249
// try to find running IDE that can handle downloaded projects
250
if (nbis[i].isLocked() && nbis[i].canHandle(sp.getTypes())) {
251
LOGGER.info("IDE: " + nbis[i].getInstallDir() + " is already running and can handle downloaded projects.");
252
execDir = nbis[i].getExecDir();
256
if (execDir == null) {
257
for (int i = 0; i < nbis.length; i++) {
258
// try to find any IDE that can handle downloaded projects
259
if (nbis[i].canHandle(sp.getTypes())) {
260
LOGGER.info("IDE: " + nbis[i].getInstallDir() + " can handle downloaded projects.");
261
execDir = nbis[i].getExecDir();
267
// no nb installation found
268
if (execDir == null) {
269
// XXX look for saved install dir from previous failed search
270
// then ask user for another NB install dir and save to properties
271
boolean found = false;
273
Integer cont = Utils.getAnotherNBInstallDir(nbVersion);
274
if (cont.equals(Utils.DialogDescriptor.EXIT)) {
275
LOGGER.info("User selected Exit when asked for another NB install dir, exiting ...");
278
if (cont.equals(Utils.DialogDescriptor.DOWNLOAD)) {
279
LOGGER.info("User selected Download, opening the page in browser, exiting ...");
280
Utils.showDocument(bundle.getString("URL_Download_NB"));
283
File nbDir = Utils.anotherNBDir;
284
LOGGER.info("User selected alternative NB install dir: " + nbDir.getAbsolutePath());
285
if (NBInstallation.isNBInstallation(nbDir)) {
286
execDir = new File(nbDir, "bin");
287
// save the installdir to muffin
288
Utils.setProperty("jws.netbeans.installdir", nbDir.getAbsolutePath());
291
LOGGER.info("Selected dir is probably not NB install dir, try again ...");
296
// probably not necessary
297
if (execDir == null) {
298
LOGGER.severe("Cannot locate NetBeans userdir or install dir, exiting ...");
299
Utils.showErrMessage(bundle.getString("ERR_No_NB_Userdir_Or_Installdir"), bundle.getString("ERR_Title"));
300
// XXX Dialog here ???
301
Utils.showDocument(bundle.getString("URL_Download_NB"));
304
LOGGER.info("Exec dir: " + execDir);
306
// ##############################
307
// ### Find platform launcher ###
308
// ##############################
310
String launcher = Utils.getPlatformLauncher();
311
if (launcher == null || "".equals(launcher)) {
312
LOGGER.severe("Cannot determine NetBeans launcher name, exiting ...");
313
Utils.showErrMessage(bundle.getString("ERR_No_Launcher"), bundle.getString("ERR_Title"));
314
// XXX Do you want to save the project?
317
LOGGER.info("Launcher name: " + launcher);
319
// ########################################
320
// ### Build command line to launch IDE ###
321
// ########################################
323
List cmdList = new ArrayList();
324
cmdList.add(execDir.getAbsolutePath() + File.separator + launcher);
325
cmdList.add("--open");
326
for (int i = 0; i < projPaths.length; i++) {
327
cmdList.add(projPaths[i]);
329
String cmdArray[] = (String[]) cmdList.toArray(new java.lang.String[cmdList.size()]);
330
LOGGER.info("Command line: " + Arrays.asList(cmdArray));
332
// ###################
333
// ### Run the IDE ###
334
// ###################
337
Process proc = Runtime.getRuntime().exec(cmdArray, null, execDir);
338
// int exitVal = proc.exitValue();
339
// LOGGER.info("Process exit value: " + exitVal);
340
// XXX Try to log output from the process
341
} catch (IOException ioe) {
342
LOGGER.severe("Exception during launching NetBeans IDE: " + Utils.exc2String(ioe));
343
// XXX Do you want to save the project?