KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > jbuilder > parsing > JdkSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport.jbuilder.parsing;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import org.netbeans.modules.java.j2seplatform.wizard.NewJ2SEPlatform;
29 import org.netbeans.modules.projectimport.j2seimport.AbstractProject;
30 import org.netbeans.modules.projectimport.j2seimport.LoggerFactory;
31 import org.netbeans.spi.java.platform.PlatformInstall;
32 import org.openide.cookies.InstanceCookie;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.filesystems.Repository;
36 import org.openide.loaders.DataObject;
37 import org.openide.loaders.DataObjectNotFoundException;
38
39
40 /**
41  *
42  * @author Radek Matous
43  */

44 public final class JdkSupport {
45     static final String JavaDoc INSTALLER_REGISTRY_FOLDER = "org-netbeans-api-java/platform/installers"; // NOI18N
46
private static final Logger JavaDoc logger =
47             LoggerFactory.getDefault().createLogger(JdkSupport.class);
48     
49     
50     /** Creates a new instance of JDKSupport */
51     public static File JavaDoc getJKDDirectory(final String JavaDoc jdkLibraryName, File JavaDoc projectDir) {
52         AbstractProject.UserLibrary aulb = null;
53         try {
54             projectDir = (projectDir == null) ? UserLibrarySupport.getUserHomeLib() : projectDir;
55             Collection JavaDoc installers = getPlatformInstallers();
56             if (installers.size() == 0) {
57                 logger.finest("No registered PlatformInstall");//NOI18N
58
}
59
60             if (installers.size() > 0 && projectDir != null) {
61                 aulb = UserLibrarySupport.getInstance(jdkLibraryName, projectDir);
62             }
63
64             if (aulb != null) {
65                 Iterator JavaDoc it = aulb.getLibraries().iterator();
66                 if (it.hasNext()) {
67                     AbstractProject.Library al = (AbstractProject.Library)it.next();
68                     File JavaDoc arc = al.getArchiv();
69                     if (arc == null) {
70                         logger.finest("user library: "+aulb.getName()+ " contains no archiv (reference is null) ");//NOI18N
71
}
72                     
73                     for (Iterator JavaDoc instances = installers.iterator(); instances.hasNext();) {
74                         PlatformInstall pi = (PlatformInstall)instances.next();
75                         for (File JavaDoc toTest = arc; toTest != null; toTest = toTest.getParentFile()) {
76                             FileObject foToTest = FileUtil.toFileObject(toTest);
77                             if (foToTest != null && pi.accept(foToTest)) {
78                                 NewJ2SEPlatform platform = NewJ2SEPlatform.create(foToTest);
79                                 platform.run();
80                                 if (platform.isValid()) {
81                                     return toTest;
82                                 }
83                             } else {
84                                 if (foToTest == null) {
85                                     logger.finest("for archiv: " + arc + " toTest: " + toTest);//NOI18
86
} else if (!pi.accept(foToTest)) {
87                                     logger.finest("foToTest: " + foToTest + " not accepted by " + pi.getClass());//NOI18
88
}
89                             }
90                         }
91
92                     }
93                 }
94
95             }
96         } catch (IOException JavaDoc iex) {
97                 org.openide.ErrorManager.getDefault().notify(iex);
98         } /*catch (SAXException sax) {
99                 org.openide.ErrorManager.getDefault().notify(sax);
100         }*/

101         
102
103         return null;
104     }
105             
106     private static Collection JavaDoc getPlatformInstallers() {
107         Collection JavaDoc result = new HashSet JavaDoc();
108         FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(INSTALLER_REGISTRY_FOLDER);
109         assert fo != null;
110         assert fo.isFolder();
111         FileObject[] children = fo.getChildren();
112         for (int i = 0; i < children.length; i++) {
113             try {
114                 DataObject doi = DataObject.find(children[i]);
115                 InstanceCookie ic = (InstanceCookie)doi.getCookie(InstanceCookie.class);
116                 if (ic != null) {
117                     if (PlatformInstall.class.isAssignableFrom(ic.instanceClass())) {
118                         result.add(ic.instanceCreate());
119                     }
120                 }
121             } catch(DataObjectNotFoundException dex) {
122                 org.openide.ErrorManager.getDefault().notify(dex);
123                 continue;
124             } catch (IOException JavaDoc iex) {
125                 org.openide.ErrorManager.getDefault().notify(iex);
126                 continue;
127             } catch (ClassNotFoundException JavaDoc cex) {
128                 org.openide.ErrorManager.getDefault().notify(cex);
129                 continue;
130             }
131         }
132         
133         return result;
134     }
135 }
136
Popular Tags