~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to ide/projectopener/src/org/netbeans/projectopener/WSProjectOpener.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.projectopener;
 
43
 
 
44
import java.io.File;
 
45
import java.io.IOException;
 
46
import java.net.MalformedURLException;
 
47
import java.net.URL;
 
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;
 
55
 
 
56
/**
 
57
 *
 
58
 * @author Milan Kubec
 
59
 */
 
60
public class WSProjectOpener {
 
61
    
 
62
    public static String APP_VERSION = "1.1";
 
63
    
 
64
    public static String MIN_NB_VERSION = "5.5.1";
 
65
    
 
66
    public static Logger LOGGER = Logger.getLogger("org.netbeans.projectopener.WSProjectOpener");
 
67
    
 
68
    private static String DEFAULT_USERDIR = "5.5.1";
 
69
    
 
70
    private static Comparator COMPARATOR = NBInstallation.LAST_USED_COMPARATOR;
 
71
    
 
72
    private WSProjectOpener() {}
 
73
    
 
74
    /**
 
75
     * @param args the command line arguments
 
76
     */
 
77
    public static void main(String[] args) {
 
78
        new WSProjectOpener().openProject(args);
 
79
    }
 
80
    
 
81
    private void openProject(String args[]) {
 
82
        
 
83
        ArgsHandler handler = new ArgsHandler(args);
 
84
        ResourceBundle bundle = ResourceBundle.getBundle("org/netbeans/projectopener/Bundle"); // NOI18N
 
85
        
 
86
        // Register own logger that prints messages to ${TEMP}/projectopener.log
 
87
        LOGGER.addHandler(new FileLogHandler());
 
88
        
 
89
        // ######################
 
90
        // ### Test Arguments ###
 
91
        // ######################
 
92
        
 
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"));
 
97
            System.exit(0);
 
98
        } else {
 
99
            LOGGER.info("Passed arguments: " + handler.getAllArgs()); // NOI18N
 
100
        }
 
101
        
 
102
        // ---
 
103
        
 
104
        URL prjURL = null;
 
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"));
 
109
            System.exit(0);
 
110
        } else {
 
111
            try {
 
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"));
 
116
                System.exit(0);
 
117
            }
 
118
        }
 
119
        LOGGER.info("Project URL: " + prjURL.toExternalForm()); // NOI18N
 
120
        
 
121
        // ################################################
 
122
        // ### Get user home dir from system properties ###
 
123
        // ################################################
 
124
        
 
125
        String userHome = System.getProperty("user.home"); // NOI18N
 
126
        if (userHome != null) {
 
127
            LOGGER.info("Userhome: " + userHome); // NOI18N
 
128
        } else {
 
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"));
 
132
            System.exit(0);
 
133
        }
 
134
        
 
135
        // ######################################
 
136
        // ### Handle required min NB version ###
 
137
        // ######################################
 
138
        
 
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
 
143
        } else {
 
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"));
 
150
                    System.exit(0);
 
151
                }
 
152
            } else {
 
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"));
 
158
                    System.exit(0);
 
159
                }
 
160
            }
 
161
        }
 
162
        
 
163
        // ######################################################
 
164
        // ### Create temp files, download and unzip projects ###
 
165
        // ######################################################
 
166
        
 
167
        File tempFile = null;
 
168
        File tempDir = null;
 
169
        
 
170
        try {
 
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
 
175
        }
 
176
        
 
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"));
 
180
            System.exit(0);
 
181
        }
 
182
        
 
183
        LOGGER.info("Temp file: " + tempFile.getAbsolutePath());
 
184
        LOGGER.info("Temp project dir: " + tempDir.getAbsolutePath());
 
185
        
 
186
        try {
 
187
            boolean downloadFinished = false;
 
188
            while (!downloadFinished) {
 
189
                try {
 
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();
 
198
                    if (!cont) {
 
199
                        // user selected exit in the dialog
 
200
                        System.exit(0);
 
201
                    }
 
202
                }
 
203
            }
 
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"));
 
207
            System.exit(0);
 
208
        }
 
209
        
 
210
        try {
 
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"));
 
216
            System.exit(0);
 
217
        }
 
218
        
 
219
        // ######################################
 
220
        // ### Process downloaded NB Projects ###
 
221
        // ######################################
 
222
        
 
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);
 
228
        } else {
 
229
            projPaths = sp.getProjectPaths();
 
230
        }
 
231
        
 
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"));
 
235
            System.exit(0);
 
236
        } 
 
237
 
 
238
        LOGGER.info("Project paths: " + Arrays.asList(projPaths));
 
239
        
 
240
        // ##################################
 
241
        // ### Find right NB Installation ###
 
242
        // ##################################
 
243
        
 
244
        File execDir = null;
 
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();
 
253
                    break;
 
254
                }
 
255
            }
 
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();
 
262
                        break;
 
263
                    }
 
264
                }
 
265
            }
 
266
        }
 
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;
 
272
            while (!found) {
 
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 ...");
 
276
                    System.exit(0);
 
277
                }
 
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"));
 
281
                    System.exit(0);
 
282
                }
 
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());
 
289
                    found = true;
 
290
                } else {
 
291
                    LOGGER.info("Selected dir is probably not NB install dir, try again ...");
 
292
                }
 
293
            }
 
294
        }
 
295
        
 
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"));
 
302
            System.exit(0);
 
303
        }
 
304
        LOGGER.info("Exec dir: " + execDir);
 
305
        
 
306
        // ##############################
 
307
        // ### Find platform launcher ###
 
308
        // ##############################
 
309
        
 
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?
 
315
            System.exit(0);
 
316
        }
 
317
        LOGGER.info("Launcher name: " + launcher);
 
318
        
 
319
        // ########################################
 
320
        // ### Build command line to launch IDE ###
 
321
        // ########################################
 
322
        
 
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]);
 
328
        }
 
329
        String cmdArray[] = (String[]) cmdList.toArray(new java.lang.String[cmdList.size()]);
 
330
        LOGGER.info("Command line: " + Arrays.asList(cmdArray));
 
331
        
 
332
        // ###################
 
333
        // ### Run the IDE ###
 
334
        // ###################
 
335
        
 
336
        try {
 
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?
 
344
        }
 
345
        
 
346
    }
 
347
    
 
348
}