KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > jnlplauncher > InstalledFileLocatorImpl


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.apisupport.jnlplauncher;
21
22 import java.io.File JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import org.openide.modules.InstalledFileLocator;
27 import org.openide.util.NbBundle;
28
29 /**
30  * Special locator for JNLP mode.
31  * Currently just locates JARs with the special META-INF/clusterpath/$relpath
32  * entry inserted by common.xml -> <makenjnlp>.
33  * @author Jesse Glick
34  */

35 public class InstalledFileLocatorImpl extends InstalledFileLocator {
36
37     public InstalledFileLocatorImpl() {}
38
39     public File JavaDoc locate(String JavaDoc relativePath, String JavaDoc codeNameBase, boolean localized) {
40         if (localized) {
41             int i = relativePath.lastIndexOf('.');
42             String JavaDoc baseName, ext;
43             if (i == -1 || i < relativePath.lastIndexOf('/')) {
44                 baseName = relativePath;
45                 ext = "";
46             } else {
47                 baseName = relativePath.substring(0, i);
48                 ext = relativePath.substring(i);
49             }
50             Iterator JavaDoc<String JavaDoc> it = NbBundle.getLocalizingSuffixes();
51             while (it.hasNext()) {
52                 String JavaDoc locName = baseName + it.next() + ext;
53                 File JavaDoc f = locate(locName, codeNameBase, false);
54                 if (f != null) {
55                     return f;
56                 }
57             }
58         } else {
59             String JavaDoc userdir = System.getProperty("netbeans.user");
60             if (userdir != null) {
61                 File JavaDoc f = new File JavaDoc(userdir, relativePath.replace('/', File.separatorChar));
62                 if (f.exists()) {
63                     return f;
64                 }
65             }
66             String JavaDoc resource = "META-INF/clusterpath/" + relativePath;
67             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
68             URL JavaDoc found = loader.getResource(resource);
69             if (found != null) {
70                 String JavaDoc foundS = found.toExternalForm();
71                 String JavaDoc prefix = "jar:";
72                 String JavaDoc suffix = "!/" + resource;
73                 if (foundS.startsWith(prefix) && foundS.endsWith(suffix)) {
74                     String JavaDoc infix = foundS.substring(prefix.length(), foundS.length() - suffix.length());
75                     if (infix.startsWith("file:")) {
76                         File JavaDoc jar = new File JavaDoc(URI.create(infix));
77                         assert jar.isFile();
78                         return jar;
79                     }
80                 }
81             }
82         }
83         return null;
84     }
85
86 }
87
Popular Tags