KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > FixedModule


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;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.MissingResourceException JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.jar.Attributes JavaDoc;
35 import java.util.jar.Manifest JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import org.openide.util.NbBundle;
38
39 /** Object representing one module, possibly installed.
40  * Responsible for opening of module JAR file; reading
41  * manifest; parsing basic information such as dependencies;
42  * and creating a classloader for use by the installer.
43  * Methods not defined in ModuleInfo must be called from within
44  * the module manager's read mutex as a rule.
45  * @author Jesse Glick
46  */

47 final class FixedModule extends Module {
48     
49     /** localized properties, only non-null if requested from disabled module */
50     private Properties JavaDoc localizedProps;
51
52     /** Create a special-purpose "fixed" JAR. */
53     public FixedModule(ModuleManager mgr, Events ev, Manifest JavaDoc manifest, Object JavaDoc history, ClassLoader JavaDoc classloader) throws InvalidException {
54         super(mgr, ev, manifest, history, classloader);
55         loadLocalizedPropsClasspath();
56         parseManifest();
57     }
58     
59     /** Get a localized attribute.
60      * First, if OpenIDE-Module-Localizing-Bundle was given, the specified
61      * bundle file (in all locale JARs as well as base JAR) is searched for
62      * a key of the specified name.
63      * Otherwise, the manifest's main attributes are searched for an attribute
64      * with the specified name, possibly with a locale suffix.
65      * If the attribute name contains a slash, and there is a manifest section
66      * named according to the part before the last slash, then this section's attributes
67      * are searched instead of the main attributes, and for the attribute listed
68      * after the slash. Currently this would only be useful for localized filesystem
69      * names. E.g. you may request the attribute org/foo/MyFileSystem.class/Display-Name.
70      * In the future certain attributes known to be dangerous could be
71      * explicitly suppressed from this list; should only be used for
72      * documented localizable attributes such as OpenIDE-Module-Name etc.
73      */

74     public Object JavaDoc getLocalizedAttribute(String JavaDoc attr) {
75         String JavaDoc locb = getManifest().getMainAttributes().getValue("OpenIDE-Module-Localizing-Bundle"); // NOI18N
76
boolean usingLoader = false;
77         if (locb != null) {
78             if (classloader != null) {
79                 if (locb.endsWith(".properties")) { // NOI18N
80
usingLoader = true;
81                     String JavaDoc basename = locb.substring(0, locb.length() - 11).replace('/', '.');
82                     try {
83                         ResourceBundle JavaDoc bundle = NbBundle.getBundle(basename, Locale.getDefault(), classloader);
84                         try {
85                             return bundle.getString(attr);
86                         } catch (MissingResourceException JavaDoc mre) {
87                             // Fine, ignore.
88
}
89                     } catch (MissingResourceException JavaDoc mre) {
90                         Util.err.log(Level.WARNING, null, mre);
91                     }
92                 } else {
93                     Util.err.warning("cannot efficiently load non-*.properties OpenIDE-Module-Localizing-Bundle: " + locb);
94                 }
95             }
96             if (!usingLoader) {
97                 if (localizedProps != null) {
98                     String JavaDoc val = localizedProps.getProperty(attr);
99                     if (val != null) {
100                         return val;
101                     }
102                 }
103             }
104         }
105         // Try in the manifest now.
106
int idx = attr.lastIndexOf('/'); // NOI18N
107
if (idx == -1) {
108             // Simple main attribute.
109
return NbBundle.getLocalizedValue(getManifest().getMainAttributes(), new Attributes.Name JavaDoc(attr));
110         } else {
111             // Attribute of a manifest section.
112
String JavaDoc section = attr.substring(0, idx);
113             String JavaDoc realAttr = attr.substring(idx + 1);
114             Attributes JavaDoc attrs = getManifest().getAttributes(section);
115             if (attrs != null) {
116                 return NbBundle.getLocalizedValue(attrs, new Attributes.Name JavaDoc(realAttr));
117             } else {
118                 return null;
119             }
120         }
121     }
122     
123     public boolean isFixed() {
124         return true;
125     }
126    
127     /** Similar, but for fixed modules only.
128      * Should be very rarely used: only for classpath modules with a strangely
129      * named OpenIDE-Module-Localizing-Bundle (not *.properties).
130      */

131     private void loadLocalizedPropsClasspath() throws InvalidException {
132         Attributes JavaDoc attr = manifest.getMainAttributes();
133         String JavaDoc locbundle = attr.getValue("OpenIDE-Module-Localizing-Bundle"); // NOI18N
134
if (locbundle != null) {
135             Util.err.fine("Localized props in " + locbundle + " for " + attr.getValue("OpenIDE-Module"));
136             try {
137                 int idx = locbundle.lastIndexOf('.'); // NOI18N
138
String JavaDoc name, ext;
139                 if (idx == -1) {
140                     name = locbundle;
141                     ext = ""; // NOI18N
142
} else {
143                     name = locbundle.substring(0, idx);
144                     ext = locbundle.substring(idx);
145                 }
146                 List JavaDoc<String JavaDoc> suffixes = new ArrayList JavaDoc<String JavaDoc>(10);
147                 Iterator JavaDoc<String JavaDoc> it = NbBundle.getLocalizingSuffixes();
148                 while (it.hasNext()) {
149                     suffixes.add(it.next());
150                 }
151                 Collections.reverse(suffixes);
152                 it = suffixes.iterator();
153                 for (String JavaDoc suffix: suffixes) {
154                     String JavaDoc resource = name + suffix + ext;
155                     InputStream JavaDoc is = classloader.getResourceAsStream(resource);
156                     if (is != null) {
157                         Util.err.fine("Found " + resource);
158                         if (localizedProps == null) {
159                             localizedProps = new Properties JavaDoc();
160                         }
161                         localizedProps.load(is);
162                     }
163                 }
164                 if (localizedProps == null) {
165                     throw new IOException JavaDoc("Could not find localizing bundle: " + locbundle); // NOI18N
166
}
167             } catch (IOException JavaDoc ioe) {
168                 throw (InvalidException) new InvalidException(ioe.toString()).initCause(ioe);
169             }
170         }
171     }
172
173     /** Get all JARs loaded by this module.
174      * Includes the module itself, any locale variants of the module,
175      * any extensions specified with Class-Path, any locale variants
176      * of those extensions.
177      * The list will be in classpath order (patches first).
178      * Currently the temp JAR is provided in the case of test modules, to prevent
179      * sporadic ZIP file exceptions when background threads (like Java parsing) tries
180      * to open libraries found in the library path.
181      * JARs already present in the classpath are <em>not</em> listed.
182      * @return a <code>List&lt;File&gt;</code> of JARs
183      */

184     public List JavaDoc<File JavaDoc> getAllJars() {
185         return Collections.emptyList();
186     }
187
188     /**
189      * This method can be overriden
190      * in subclasses in case they want to change the reloadable semantix
191      * of the fixed modules.
192      *
193      * @throws IllegalStateException as FixedModule cannot be reloaded
194      */

195     public void setReloadable(boolean r) {
196         throw new IllegalStateException JavaDoc();
197     }
198     
199     /** Reload this module. Access from ModuleManager.
200      * If an exception is thrown, the module is considered
201      * to be in an invalid state.
202      *
203      * @throws IllegalStateException as FixedModule cannot be reloaded
204      */

205     public void reload() throws IOException JavaDoc {
206         throw new IOException JavaDoc("Fixed module cannot be reloaded!"); // NOI18N
207
}
208     
209     // Access from ModuleManager:
210
/** Turn on the classloader. Passed a list of parent modules to use.
211      * The parents should already have had their classloaders initialized.
212      */

213     protected void classLoaderUp(Set JavaDoc parents) throws IOException JavaDoc {
214         return; // no need
215
}
216     
217     /** Turn off the classloader and release all resources. */
218     protected void classLoaderDown() {
219         return; // don't touch it
220
}
221     /** Should be called after turning off the classloader of one or more modules & GC'ing. */
222     protected void cleanup() {
223         return; // don't touch it
224
}
225     
226     /** Notify the module that it is being deleted. */
227     protected void destroy() {
228     }
229     
230     /** String representation for debugging. */
231     public String JavaDoc toString() {
232         String JavaDoc s = "FixedModule:" + getCodeNameBase(); // NOI18N
233
if (!isValid()) s += "[invalid]"; // NOI18N
234
return s;
235     }
236 }
237
Popular Tags