This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 13847
Collapse All | Expand All

(-)openide/src/org/openide/util/NbBundle.java (+98 lines)
Lines 15-20 Link Here
15
15
16
import java.io.*;
16
import java.io.*;
17
import java.lang.ref.*;
17
import java.lang.ref.*;
18
import java.lang.reflect.Method;
18
import java.net.URL;
19
import java.net.URL;
19
import java.util.*;
20
import java.util.*;
20
import java.util.jar.Attributes;
21
import java.util.jar.Attributes;
Lines 321-326 Link Here
321
        return getBundle(baseName, locale, getLoader());
322
        return getBundle(baseName, locale, getLoader());
322
    }
323
    }
323
324
325
    private static final boolean USE_C_PROPERTIES = Boolean.getBoolean("NbBundle.use.cproperties");
326
    
324
    /** Get a resource bundle the hard way.
327
    /** Get a resource bundle the hard way.
325
    * @param baseName bundle basename
328
    * @param baseName bundle basename
326
    * @param locale the locale to use
329
    * @param locale the locale to use
Lines 330-335 Link Here
330
    */
333
    */
331
    public static final ResourceBundle getBundle(String baseName, Locale locale,
334
    public static final ResourceBundle getBundle(String baseName, Locale locale,
332
            ClassLoader loader) throws MissingResourceException {
335
            ClassLoader loader) throws MissingResourceException {
336
        if (USE_C_PROPERTIES) {
337
            ResourceBundle b = getCBundle(baseName, locale, loader);
338
            if (b != null) {
339
                return b;
340
            }
341
        }
333
        if (USE_DEBUG_LOADER) loader = DebugLoader.get (loader);
342
        if (USE_DEBUG_LOADER) loader = DebugLoader.get (loader);
334
        String t;
343
        String t;
335
        if (brandingToken == null)
344
        if (brandingToken == null)
Lines 407-412 Link Here
407
        }
416
        }
408
    }
417
    }
409
418
419
    private static final Object MISSING = new String("MISSING"); // NOI18N
420
    private static final Map cBundleCache = new WeakHashMap(); // Map<ClassLoader,Map<String,Reference<ResourceBundle>|MISSING>>
421
    private static synchronized ResourceBundle getCBundle(String name, Locale locale, ClassLoader loader) {
422
        Map m = (Map)cBundleCache.get(loader); // Map<String,Reference<ResourceBundle>|MISSING>
423
        if (m == null) {
424
            cBundleCache.put(loader, m = new HashMap());
425
        }
426
        String key = name + '.' + (brandingToken != null ? brandingToken : "") + '.' + locale;
427
        Object o = m.get(key);
428
        if (o == MISSING) {
429
            return null;
430
        }
431
        ResourceBundle b = (o != null) ? (ResourceBundle)((Reference)o).get() : null;
432
        if (b != null) {
433
            return b;
434
        } else {
435
            b = loadCBundle(name, locale, loader);
436
            m.put(key, (b != null) ? new SoftReference(b) : MISSING);
437
            return b;
438
        }
439
    }
440
    private static boolean foundCPropertiesReader = false;
441
    private static Method cPropertiesReader = null;
442
    private static ResourceBundle loadCBundle(String name, Locale locale, ClassLoader loader) {
443
        if (cPropertiesReader == null) {
444
            if (foundCPropertiesReader) {
445
                return null;
446
            }
447
            foundCPropertiesReader = true;
448
            try {
449
                cPropertiesReader = Class.forName("sun.misc.BinaryProperties").getMethod("read", new Class[] {InputStream.class, Map.class});
450
            } catch (ClassNotFoundException e) {
451
                // ignore
452
            } catch (Exception e) {
453
                ErrorManager.getDefault().notify(e);
454
            }
455
            if (cPropertiesReader == null) {
456
                return null;
457
            }
458
        }
459
        String sname = name.replace('.', '/');
460
        Iterator it = new LocaleIterator(locale);
461
        LinkedList l = new LinkedList();
462
        while (it.hasNext()) {
463
            l.addFirst(it.next());
464
        }
465
        it = l.iterator();
466
        Map m = new HashMap(); // Map<String,String>
467
        boolean first = true;
468
        while (it.hasNext()) {
469
            String res = sname + (String)it.next() + ".cproperties";
470
            InputStream is = loader.getResourceAsStream(res);
471
            if (is != null) {
472
                //System.err.println("Loading " + res);
473
                try {
474
                    try {
475
                        cPropertiesReader.invoke(null, new Object[] {is, m});
476
                    } finally {
477
                        is.close();
478
                    }
479
                } catch (Exception e) { // IOException, InvocationTargetException<IOException>
480
                    ErrorManager.getDefault().notify(e);
481
                    return null;
482
                }
483
            } else if (first) {
484
                return null;
485
            }
486
            first = false;
487
        }
488
        return new CBundle(m, locale);
489
    }
490
    private static final class CBundle extends ResourceBundle {
491
        private final Map m; // Map<String,String>
492
        private final Locale locale;
493
        public CBundle(Map m, Locale locale) {
494
            this.m = m;
495
            this.locale = locale;
496
        }
497
        public Enumeration getKeys() {
498
            return Collections.enumeration(m.keySet());
499
        }
500
        protected Object handleGetObject(String key) {
501
            return m.get(key);
502
        }
503
        public Locale getLocale() {
504
            return locale;
505
        }
506
    }
507
    
410
    //
508
    //
411
    // Helper methods to simplify localization of messages
509
    // Helper methods to simplify localization of messages
412
    //
510
    //

Return to bug 13847