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-2006 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.modules.projectimport.jbuilder.parsing;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.Collection;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.logging.Logger;
050: import org.netbeans.modules.java.j2seplatform.wizard.NewJ2SEPlatform;
051: import org.netbeans.modules.projectimport.j2seimport.AbstractProject;
052: import org.netbeans.modules.projectimport.j2seimport.LoggerFactory;
053: import org.netbeans.spi.java.platform.PlatformInstall;
054: import org.openide.cookies.InstanceCookie;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.filesystems.Repository;
058: import org.openide.loaders.DataObject;
059: import org.openide.loaders.DataObjectNotFoundException;
060:
061: /**
062: *
063: * @author Radek Matous
064: */
065: public final class JdkSupport {
066: static final String INSTALLER_REGISTRY_FOLDER = "org-netbeans-api-java/platform/installers"; // NOI18N
067: private static final Logger logger = LoggerFactory.getDefault()
068: .createLogger(JdkSupport.class);
069:
070: /** Creates a new instance of JDKSupport */
071: public static File getJKDDirectory(final String jdkLibraryName,
072: File projectDir) {
073: AbstractProject.UserLibrary aulb = null;
074: try {
075: projectDir = (projectDir == null) ? UserLibrarySupport
076: .getUserHomeLib() : projectDir;
077: Collection installers = getPlatformInstallers();
078: if (installers.size() == 0) {
079: logger.finest("No registered PlatformInstall");//NOI18N
080: }
081:
082: if (installers.size() > 0 && projectDir != null) {
083: aulb = UserLibrarySupport.getInstance(jdkLibraryName,
084: projectDir);
085: }
086:
087: if (aulb != null) {
088: Iterator it = aulb.getLibraries().iterator();
089: if (it.hasNext()) {
090: AbstractProject.Library al = (AbstractProject.Library) it
091: .next();
092: File arc = al.getArchiv();
093: if (arc == null) {
094: logger
095: .finest("user library: "
096: + aulb.getName()
097: + " contains no archiv (reference is null) ");//NOI18N
098: }
099:
100: for (Iterator instances = installers.iterator(); instances
101: .hasNext();) {
102: PlatformInstall pi = (PlatformInstall) instances
103: .next();
104: for (File toTest = arc; toTest != null; toTest = toTest
105: .getParentFile()) {
106: FileObject foToTest = FileUtil
107: .toFileObject(toTest);
108: if (foToTest != null && pi.accept(foToTest)) {
109: NewJ2SEPlatform platform = NewJ2SEPlatform
110: .create(foToTest);
111: platform.run();
112: if (platform.isValid()) {
113: return toTest;
114: }
115: } else {
116: if (foToTest == null) {
117: logger.finest("for archiv: " + arc
118: + " toTest: " + toTest);//NOI18
119: } else if (!pi.accept(foToTest)) {
120: logger.finest("foToTest: "
121: + foToTest
122: + " not accepted by "
123: + pi.getClass());//NOI18
124: }
125: }
126: }
127:
128: }
129: }
130:
131: }
132: } catch (IOException iex) {
133: org.openide.ErrorManager.getDefault().notify(iex);
134: } /*catch (SAXException sax) {
135: org.openide.ErrorManager.getDefault().notify(sax);
136: }*/
137:
138: return null;
139: }
140:
141: private static Collection getPlatformInstallers() {
142: Collection result = new HashSet();
143: FileObject fo = Repository.getDefault().getDefaultFileSystem()
144: .findResource(INSTALLER_REGISTRY_FOLDER);
145: assert fo != null;
146: assert fo.isFolder();
147: FileObject[] children = fo.getChildren();
148: for (int i = 0; i < children.length; i++) {
149: try {
150: DataObject doi = DataObject.find(children[i]);
151: InstanceCookie ic = (InstanceCookie) doi
152: .getCookie(InstanceCookie.class);
153: if (ic != null) {
154: if (PlatformInstall.class.isAssignableFrom(ic
155: .instanceClass())) {
156: result.add(ic.instanceCreate());
157: }
158: }
159: } catch (DataObjectNotFoundException dex) {
160: org.openide.ErrorManager.getDefault().notify(dex);
161: continue;
162: } catch (IOException iex) {
163: org.openide.ErrorManager.getDefault().notify(iex);
164: continue;
165: } catch (ClassNotFoundException cex) {
166: org.openide.ErrorManager.getDefault().notify(cex);
167: continue;
168: }
169: }
170:
171: return result;
172: }
173: }
|