boot/src/main/java/org/netbeans/html/boot/impl/JsPkgCache.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 485 e9d3d44aba23
child 790 30f20d9c0986
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@459
     1
/**
jaroslav@459
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@459
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@459
     5
 *
jaroslav@459
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@459
     7
 * Other names may be trademarks of their respective owners.
jaroslav@459
     8
 *
jaroslav@459
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@459
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@459
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@459
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@459
    13
 * License. You can obtain a copy of the License at
jaroslav@459
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@459
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@459
    16
 * specific language governing permissions and limitations under the
jaroslav@459
    17
 * License.  When distributing the software, include this License Header
jaroslav@459
    18
 * Notice in each file and include the License file at
jaroslav@459
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@459
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@459
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@459
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@459
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@459
    24
 * your own identifying information:
jaroslav@459
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@459
    26
 *
jaroslav@459
    27
 * Contributor(s):
jaroslav@459
    28
 *
jaroslav@459
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@459
    31
 *
jaroslav@459
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@459
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@459
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@459
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@459
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@459
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@459
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@459
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@459
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@459
    41
 * made subject to such option by the copyright holder.
jaroslav@459
    42
 */
jaroslav@459
    43
package org.netbeans.html.boot.impl;
jaroslav@459
    44
jaroslav@459
    45
import java.io.BufferedReader;
jaroslav@459
    46
import java.io.IOException;
jaroslav@459
    47
import java.io.InputStream;
jaroslav@459
    48
import java.io.InputStreamReader;
jaroslav@459
    49
import java.net.URL;
jaroslav@459
    50
import java.util.Collections;
jaroslav@459
    51
import java.util.Enumeration;
jaroslav@459
    52
import java.util.Map;
jaroslav@459
    53
import java.util.Set;
jaroslav@459
    54
import java.util.TreeSet;
jaroslav@459
    55
import java.util.WeakHashMap;
jaroslav@459
    56
import java.util.logging.Level;
jaroslav@459
    57
import java.util.logging.Logger;
jaroslav@459
    58
jaroslav@459
    59
/**
jaroslav@459
    60
 *
jaroslav@459
    61
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@459
    62
 */
jaroslav@459
    63
final class JsPkgCache {
jaroslav@459
    64
    private final Map<String,Set<String>> props = new WeakHashMap<String, Set<String>>();
jaroslav@459
    65
    private static final Map<ClassLoader, JsPkgCache> CACHE = new WeakHashMap<ClassLoader, JsPkgCache>();
jaroslav@459
    66
    private static final Set<String> NONE = Collections.emptySet();
jaroslav@459
    67
jaroslav@459
    68
    public static boolean process(ClassLoader l, String className) {
jaroslav@485
    69
        if (className.equals("org.netbeans.html.boot.impl.Test")) { // NOI18N
jaroslav@459
    70
            return true;
jaroslav@459
    71
        }
jaroslav@459
    72
        Set<String> p;
jaroslav@459
    73
        JsPkgCache c;
jaroslav@459
    74
        String pkgName;
jaroslav@459
    75
        synchronized (CACHE) {
jaroslav@459
    76
            c = CACHE.get(l);
jaroslav@459
    77
            if (c == null) {
jaroslav@459
    78
                c = new JsPkgCache();
jaroslav@459
    79
                CACHE.put(l, c);
jaroslav@459
    80
            }
jaroslav@459
    81
            int lastDot = className.lastIndexOf('.');
jaroslav@459
    82
            pkgName = className.substring(0, lastDot + 1).replace('.', '/');
jaroslav@459
    83
            p = c.props.get(pkgName);
jaroslav@459
    84
            if (p == NONE) {
jaroslav@459
    85
                return false;
jaroslav@459
    86
            } else if (p != null) {
jaroslav@459
    87
                return p.contains(className);
jaroslav@459
    88
            }
jaroslav@459
    89
        }
jaroslav@459
    90
        final String res = pkgName + "net.java.html.js.classes";
jaroslav@459
    91
        
jaroslav@459
    92
        Enumeration<URL> en;
jaroslav@459
    93
        try {
jaroslav@459
    94
            en = l.getResources(res);
jaroslav@459
    95
        } catch (IOException ex) {
jaroslav@459
    96
            en = null;
jaroslav@459
    97
        }
jaroslav@459
    98
        if (en == null || !en.hasMoreElements()) synchronized (CACHE) {
jaroslav@459
    99
            c.props.put(pkgName, NONE);
jaroslav@459
   100
            return false;
jaroslav@459
   101
        }
jaroslav@459
   102
jaroslav@459
   103
        try {
jaroslav@459
   104
            Set<String> arr = new TreeSet<String>();
jaroslav@459
   105
            while (en.hasMoreElements()) {
jaroslav@459
   106
                URL u = en.nextElement();
jaroslav@459
   107
                BufferedReader r = new BufferedReader(
jaroslav@459
   108
                    new InputStreamReader(u.openStream(), "UTF-8")
jaroslav@459
   109
                );
jaroslav@459
   110
                for (;;) {
jaroslav@459
   111
                    String line = r.readLine();
jaroslav@459
   112
                    if (line == null) {
jaroslav@459
   113
                        break;
jaroslav@459
   114
                    }
jaroslav@459
   115
                    arr.add(line);
jaroslav@459
   116
                }
jaroslav@459
   117
                r.close();
jaroslav@459
   118
            }
jaroslav@459
   119
            p = arr;
jaroslav@459
   120
        } catch (IOException ex) {
jaroslav@459
   121
            LOG.log(Level.WARNING, "Can't read " + res, ex);
jaroslav@459
   122
            p = NONE;
jaroslav@459
   123
        }
jaroslav@459
   124
        
jaroslav@459
   125
        synchronized (CACHE) {
jaroslav@459
   126
            c.props.put(pkgName, p);
jaroslav@459
   127
            return p.contains(className);
jaroslav@459
   128
        }
jaroslav@459
   129
        
jaroslav@459
   130
    }
jaroslav@459
   131
    private static final Logger LOG = Logger.getLogger(JsPkgCache.class.getName());
jaroslav@459
   132
}