KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > DummyModuleInfo


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.autoupdate;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.*;
25 import java.util.jar.Attributes JavaDoc;
26
27 import org.openide.modules.Dependency;
28 import org.openide.modules.ModuleInfo;
29 import org.openide.modules.SpecificationVersion;
30
31 import org.netbeans.core.startup.AutomaticDependencies;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileStateInvalidException;
34 import org.openide.filesystems.Repository;
35 import org.openide.util.Exceptions;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /** A fake module info class initialized from a manifest but not backed by a real JAR.
39  * Used for purposes of comparisons to real modules and updates and so on.
40  * @author Jesse Glick
41  */

42 final class DummyModuleInfo extends ModuleInfo {
43     
44     private static AutomaticDependencies autoDepsHandler = null;
45     
46     /**
47      * Roughly copied from NbInstaller.refineDependencies.
48      * @see "#29577"
49      */

50     private static synchronized AutomaticDependencies getAutoDepsHandler() {
51         if (autoDepsHandler == null) {
52             FileObject depsFolder = Repository.getDefault().getDefaultFileSystem().findResource("ModuleAutoDeps"); // NOI18N
53
if (depsFolder != null) {
54                 FileObject[] kids = depsFolder.getChildren();
55                 List urls = new ArrayList(Math.max(kids.length, 1)); // List<URL>
56
for (int i = 0; i < kids.length; i++) {
57                     if (kids[i].hasExt("xml")) {
58                         try {
59                             urls.add(kids[i].getURL());
60                         }
61                         catch (FileStateInvalidException e) {
62                             Exceptions.printStackTrace(e);
63                         }
64                     }
65                 }
66                 try {
67                     autoDepsHandler = AutomaticDependencies.parse((URL JavaDoc[])urls.toArray(new URL JavaDoc[urls.size()]));
68                 } catch (IOException JavaDoc e) {
69                     Exceptions.printStackTrace(e);
70                 } catch (SAXException JavaDoc e) {
71                     Exceptions.printStackTrace(e);
72                 }
73             }
74             if (autoDepsHandler == null) {
75                 // Parsing failed, or no files.
76
autoDepsHandler = AutomaticDependencies.empty();
77             }
78         }
79         return autoDepsHandler;
80     }
81     
82     private final Attributes JavaDoc attr;
83     private final Set deps; // Set<Dependency>
84
private final String JavaDoc[] provides;
85     
86     /** Create a new fake module based on manifest.
87      * Only main attributes need be presented, so
88      * only pass these.
89      */

90     public DummyModuleInfo(Attributes JavaDoc attr) throws IllegalArgumentException JavaDoc {
91         this.attr = attr;
92         if (getCodeName() == null) throw new IllegalArgumentException JavaDoc();
93         String JavaDoc cnb = getCodeNameBase();
94         try {
95             getSpecificationVersion();
96         } catch (NumberFormatException JavaDoc nfe) {
97             throw new IllegalArgumentException JavaDoc(nfe.toString() + " from " + cnb); // NOI18N
98
}
99         deps = parseDeps(attr, cnb);
100         getAutoDepsHandler().refineDependencies(cnb, deps); // #29577
101
String JavaDoc providesS = attr.getValue("OpenIDE-Module-Provides"); // NOI18N
102
if (providesS == null) {
103             provides = new String JavaDoc[0];
104         } else {
105             StringTokenizer tok = new StringTokenizer(providesS, ", "); // NOI18N
106
provides = new String JavaDoc[tok.countTokens()];
107             for (int i = 0; i < provides.length; i++) {
108                 provides[i] = tok.nextToken();
109             }
110         }
111         // XXX could do more error checking but this is probably plenty
112
}
113     
114     public boolean isEnabled() {
115         return false;
116     }
117     
118     public SpecificationVersion getSpecificationVersion() {
119         String JavaDoc sv = attr.getValue("OpenIDE-Module-Specification-Version"); // NOI18N
120
return (sv == null ? null : new SpecificationVersion(sv));
121     }
122     
123     public String JavaDoc getCodeName() {
124         return attr.getValue("OpenIDE-Module"); // NOI18N
125
}
126     
127     public int getCodeNameRelease() {
128         String JavaDoc s = getCodeName();
129         int idx = s.lastIndexOf('/'); // NOI18N
130
if (idx == -1) {
131             return -1;
132         } else {
133             return Integer.parseInt(s.substring(idx + 1));
134         }
135     }
136     
137     public String JavaDoc getCodeNameBase() {
138         String JavaDoc s = getCodeName();
139         int idx = s.lastIndexOf('/'); // NOI18N
140
if (idx == -1) {
141             return s;
142         } else {
143             return s.substring(0, idx);
144         }
145     }
146     
147     public Object JavaDoc getLocalizedAttribute(String JavaDoc a) {
148         return attr.getValue(a);
149     }
150     
151     public Object JavaDoc getAttribute(String JavaDoc a) {
152         return attr.getValue(a);
153     }
154     
155     /** Get a list of all dependencies this module has. */
156     public Set getDependencies() {
157         return deps;
158     }
159     
160     private final static Set parseDeps(Attributes JavaDoc attr, String JavaDoc cnb) throws IllegalArgumentException JavaDoc {
161         Set s = new HashSet(); // Set<Dependency>
162
s.addAll(Dependency.create(Dependency.TYPE_MODULE, attr.getValue("OpenIDE-Module-Module-Dependencies"))); // NOI18N
163
s.addAll(Dependency.create(Dependency.TYPE_PACKAGE, attr.getValue("OpenIDE-Module-Package-Dependencies"))); // NOI18N
164
s.addAll(Dependency.create(Dependency.TYPE_IDE, attr.getValue("OpenIDE-Module-IDE-Dependencies"))); // NOI18N
165
s.addAll(Dependency.create(Dependency.TYPE_JAVA, attr.getValue("OpenIDE-Module-Java-Dependencies"))); // NOI18N
166
s.addAll(Dependency.create(Dependency.TYPE_REQUIRES, attr.getValue("OpenIDE-Module-Requires"))); // NOI18N
167
// #24143: treat API dependencies as dependencies on pseudomodule org.openide
168
Iterator it = s.iterator();
169         SpecificationVersion api = null;
170         String JavaDoc impl = null;
171         String JavaDoc major = null;
172         while (it.hasNext()) {
173             Dependency dep = (Dependency)it.next();
174             if (dep.getType() == Dependency.TYPE_IDE) {
175                 if (dep.getComparison() == Dependency.COMPARE_SPEC) {
176                     if (api != null) {
177                         throw new IllegalArgumentException JavaDoc("Duplicate OpenIDE-Module-IDE-Dependencies found!"); // NOI18N
178
}
179                     api = new SpecificationVersion(dep.getVersion());
180                 } else {
181                     // Must be impl comparison.
182
if (impl != null) {
183                         throw new IllegalArgumentException JavaDoc("Duplicate OpenIDE-Module-IDE-Dependencies found!"); // NOI18N
184
}
185                     impl = dep.getVersion();
186                 }
187                 String JavaDoc name = dep.getName();
188                 int index = name.lastIndexOf('/');
189                 String JavaDoc newmajor;
190                 if (index == -1) {
191                     newmajor = ""; // NOI18N
192
} else {
193                     newmajor = name.substring(index);
194                 }
195                 if (major != null && !major.equals(newmajor)) {
196                     throw new IllegalArgumentException JavaDoc("Clashing OpenIDE-Module-IDE-Dependencies found!"); // NOI18N
197
}
198                 major = newmajor;
199                 it.remove();
200             }
201         }
202         if (api != null) {
203             s.addAll(Dependency.create(Dependency.TYPE_MODULE, "org.openide" + major + " > " + api)); // NOI18N
204
}
205         if (impl != null) {
206             s.addAll(Dependency.create(Dependency.TYPE_MODULE, "org.openide" + major + " = " + impl)); // NOI18N
207
}
208         if (api == null && impl == null) {
209             // All modules implicitly depend on openide.
210
// Needed for #29577.
211
s.addAll(Dependency.create(Dependency.TYPE_MODULE, "org.openide/1 > 0")); // NOI18N
212
}
213         return s;
214     }
215     
216     public boolean owns(Class JavaDoc clazz) {
217         return false;
218     }
219     
220     public String JavaDoc[] getProvides() {
221         return provides;
222     }
223 }
224
Popular Tags