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.FileFilter;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Comparator;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.ListIterator;
052: import java.util.logging.Logger;
053:
054: /**
055: *
056: * @author Milan Kubec
057: */
058: public class UserdirScanner {
059:
060: private static Logger LOGGER = WSProjectOpener.LOGGER;
061:
062: UserdirScanner() {
063: }
064:
065: public static NBInstallation[] suitableNBInstallations(
066: File homeDir, String minVersion, Comparator comp) {
067: File nbUserHome = new File(homeDir, ".netbeans");
068: List list = allNBInstallations(nbUserHome);
069: LOGGER.info("All found NetBeans installations: " + list);
070:
071: NBInstallation devNbi = null;
072: // find dev NBInstallation
073: for (Iterator iter = list.iterator(); iter.hasNext();) {
074: NBInstallation nbi = (NBInstallation) iter.next();
075: // 1.0 version means no version number exists
076: if (nbi.numVersion().equals("1.0")
077: && nbi.releaseType().equals("dev")
078: && nbi.releaseVersion().equals("")) {
079: devNbi = nbi;
080: }
081: }
082: if (minVersion.equals("dev")) {
083: if (devNbi != null) {
084: return new NBInstallation[] { devNbi };
085: }
086: return new NBInstallation[] {};
087: }
088:
089: Collections.sort(list, comp);
090: for (ListIterator listIter = list.listIterator(); listIter
091: .hasNext();) {
092: NBInstallation nbi = (NBInstallation) listIter.next();
093: if (Utils.compareVersions(minVersion, nbi.numVersion()) > 0) { // in case we don't want dev builds -> || nbi.releaseType().equals("dev")) {
094: listIter.remove();
095: }
096: }
097: Collections.reverse(list);
098: // add dev to the end of the list here
099: if (devNbi != null) {
100: list.add(devNbi);
101: }
102: return (NBInstallation[]) list.toArray(new NBInstallation[list
103: .size()]);
104: }
105:
106: // returns all valid installations of NB found in ${HOME}/.netbeans
107: private static List allNBInstallations(File nbUserHome) {
108: File files[] = nbUserHome.listFiles(new FileFilter() {
109: public boolean accept(File f) {
110: if (f.isDirectory()) {
111: return true;
112: }
113: return false;
114: }
115: });
116: List list = new ArrayList();
117: // files might be null here, e.g. if there is no .netbeans folder
118: if (files != null) {
119: for (int i = 0; i < files.length; i++) {
120: // creating NB installation is based on userdir
121: NBInstallation nbi = new NBInstallation(files[i]);
122: if (nbi.isValid()) {
123: list.add(nbi);
124: }
125: }
126: }
127: return list;
128: }
129:
130: }
|