1.1 --- a/lookup/src/main/java/org/openide/util/lookup/MetaInfServicesLookup.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,573 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * Contributor(s):
1.28 - *
1.29 - * The Original Software is NetBeans. The Initial Developer of the Original
1.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
1.31 - * Microsystems, Inc. All Rights Reserved.
1.32 - *
1.33 - * If you wish your version of this file to be governed by only the CDDL
1.34 - * or only the GPL Version 2, indicate your decision by adding
1.35 - * "[Contributor] elects to include this software in this distribution
1.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.37 - * single choice of license, a recipient has the option to distribute
1.38 - * your version of this file under either the CDDL, the GPL Version 2 or
1.39 - * to extend the choice of license to its licensees as provided above.
1.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.41 - * Version 2 license, then the option applies only if the new code is
1.42 - * made subject to such option by the copyright holder.
1.43 - */
1.44 -
1.45 -package org.openide.util.lookup;
1.46 -
1.47 -import java.io.BufferedReader;
1.48 -import java.io.IOException;
1.49 -import java.io.InputStream;
1.50 -import java.io.InputStreamReader;
1.51 -import java.lang.ref.Reference;
1.52 -import java.lang.ref.WeakReference;
1.53 -import java.lang.reflect.Method;
1.54 -import java.net.URL;
1.55 -import java.util.ArrayList;
1.56 -import java.util.Collection;
1.57 -import java.util.Enumeration;
1.58 -import java.util.HashSet;
1.59 -import java.util.LinkedHashSet;
1.60 -import java.util.List;
1.61 -import java.util.Map;
1.62 -import java.util.WeakHashMap;
1.63 -import java.util.concurrent.Executor;
1.64 -import java.util.concurrent.Executors;
1.65 -import java.util.logging.Level;
1.66 -import java.util.logging.Logger;
1.67 -import org.openide.util.Lookup;
1.68 -
1.69 -/**
1.70 - * @author Jaroslav Tulach, Jesse Glick
1.71 - * @see Lookups#metaInfServices(ClassLoader,String)
1.72 - * @see "#14722"
1.73 - */
1.74 -final class MetaInfServicesLookup extends AbstractLookup {
1.75 -
1.76 - private static final Logger LOGGER = Logger.getLogger(MetaInfServicesLookup.class.getName());
1.77 - static final Executor RP;
1.78 - static {
1.79 - Executor res = null;
1.80 - try {
1.81 - Class<?> seek = Class.forName("org.openide.util.RequestProcessor");
1.82 - res = (Executor)seek.newInstance();
1.83 - } catch (Throwable t) {
1.84 - res = Executors.newSingleThreadExecutor();
1.85 - }
1.86 - RP = res;
1.87 - }
1.88 - /*TBD: Inject RequestProcessor somehow
1.89 - new RequestProcessor(MetaInfServicesLookup.class.getName(), 1);
1.90 - */
1.91 - private static int knownInstancesCount;
1.92 - private static final List<Reference<Object>> knownInstances;
1.93 - static {
1.94 - knownInstances = new ArrayList<Reference<Object>>();
1.95 - for (int i = 0; i < 512; i++) {
1.96 - knownInstances.add(null);
1.97 - }
1.98 - }
1.99 -
1.100 - /** A set of all requested classes.
1.101 - * Note that classes that we actually succeeded on can never be removed
1.102 - * from here because we hold a strong reference to the loader.
1.103 - * However we also hold classes which are definitely not loadable by
1.104 - * our loader.
1.105 - */
1.106 - private final Map<Class,Object> classes = new WeakHashMap<Class,Object>();
1.107 -
1.108 - /** class loader to use */
1.109 - private final ClassLoader loader;
1.110 - /** prefix to prepend */
1.111 - private final String prefix;
1.112 -
1.113 - /** Create a lookup reading from a specified classloader.
1.114 - */
1.115 - public MetaInfServicesLookup(ClassLoader loader, String prefix) {
1.116 - this.loader = loader;
1.117 - this.prefix = prefix;
1.118 -
1.119 - LOGGER.log(Level.FINE, "Created: {0}", this);
1.120 - }
1.121 -
1.122 - @Override
1.123 - public String toString() {
1.124 - return "MetaInfServicesLookup[" + loader + "]"; // NOI18N
1.125 - }
1.126 -
1.127 - /* Tries to load appropriate resources from manifest files.
1.128 - */
1.129 - @Override
1.130 - protected final void beforeLookup(Lookup.Template t) {
1.131 - Class c = t.getType();
1.132 -
1.133 - Collection<AbstractLookup.Pair<?>> toAdd = null;
1.134 - synchronized (this) {
1.135 - if (classes.get(c) == null) { // NOI18N
1.136 - toAdd = new ArrayList<Pair<?>>();
1.137 - } else {
1.138 - // ok, nothing needs to be done
1.139 - return;
1.140 - }
1.141 - }
1.142 - if (toAdd != null) {
1.143 - search(c, toAdd);
1.144 - }
1.145 - synchronized (this) {
1.146 - if (classes.put(c, "") == null) { // NOI18N
1.147 - // Added new class, search for it.
1.148 - LinkedHashSet<AbstractLookup.Pair<?>> arr = getPairsAsLHS();
1.149 - arr.addAll(toAdd);
1.150 - setPairs(arr, RP);
1.151 - }
1.152 - }
1.153 - }
1.154 -
1.155 - /** Finds all pairs and adds them to the collection.
1.156 - *
1.157 - * @param clazz class to find
1.158 - * @param result collection to add Pair to
1.159 - */
1.160 - private void search(Class<?> clazz, Collection<AbstractLookup.Pair<?>> result) {
1.161 - if (LOGGER.isLoggable(Level.FINER)) {
1.162 - LOGGER.log(Level.FINER, "Searching for " + clazz.getName() + " in " + clazz.getClassLoader() + " from " + this);
1.163 - }
1.164 -
1.165 - String res = prefix + clazz.getName(); // NOI18N
1.166 - Enumeration<URL> en;
1.167 -
1.168 - try {
1.169 - en = loader.getResources(res);
1.170 - } catch (IOException ioe) {
1.171 - // do not use ErrorManager because we are in the startup code
1.172 - // and ErrorManager might not be ready
1.173 - ioe.printStackTrace();
1.174 -
1.175 - return;
1.176 - }
1.177 -
1.178 - // Do not create multiple instances in case more than one JAR
1.179 - // has the same entry in it (and they load to the same class).
1.180 - // Probably would not happen, assuming JARs only list classes
1.181 - // they own, but just in case...
1.182 - List<Item> foundClasses = new ArrayList<Item>();
1.183 - Collection<Class> removeClasses = new ArrayList<Class>();
1.184 -
1.185 - boolean foundOne = false;
1.186 -
1.187 - while (en.hasMoreElements()) {
1.188 - if (!foundOne) {
1.189 - foundOne = true;
1.190 -
1.191 - // Double-check that in fact we can load the *interface* class.
1.192 - // For example, say class I is defined in two JARs, J1 and J2.
1.193 - // There is also an implementation M1 defined in J1, and another
1.194 - // implementation M2 defined in J2.
1.195 - // Classloaders C1 and C2 are made from J1 and J2.
1.196 - // A MetaInfServicesLookup is made from C1. Then the user asks to
1.197 - // lookup I as loaded from C2. J1 has the services line and lists
1.198 - // M1, and we can in fact make it. However it is not of the desired
1.199 - // type to be looked up. Don't do this check, which could be expensive,
1.200 - // unless we expect to be getting some results, however.
1.201 - Class realMcCoy = null;
1.202 -
1.203 - try {
1.204 - realMcCoy = loader.loadClass(clazz.getName());
1.205 - } catch (ClassNotFoundException cnfe) {
1.206 - // our loader does not know about it, OK
1.207 - }
1.208 -
1.209 - if (realMcCoy != clazz) {
1.210 - // Either the interface class is not available at all in our loader,
1.211 - // or it is not the same version as we expected. Don't provide results.
1.212 - if (LOGGER.isLoggable(Level.WARNING)) {
1.213 - if (realMcCoy != null) {
1.214 - LOGGER.log(Level.WARNING,
1.215 - clazz.getName() + " is not the real McCoy! Actually found it in " +
1.216 - realMcCoy.getClassLoader()
1.217 - ); // NOI18N
1.218 - } else {
1.219 - LOGGER.log(Level.WARNING, clazz.getName() + " could not be found in " + loader); // NOI18N
1.220 - }
1.221 - }
1.222 -
1.223 - return;
1.224 - }
1.225 - }
1.226 -
1.227 - URL url = en.nextElement();
1.228 - Item currentItem = null;
1.229 -
1.230 - try {
1.231 - InputStream is = url.openStream();
1.232 -
1.233 - try {
1.234 - BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); // NOI18N
1.235 -
1.236 - while (true) {
1.237 - String line = reader.readLine();
1.238 -
1.239 - if (line == null) {
1.240 - break;
1.241 - }
1.242 -
1.243 - line = line.trim();
1.244 -
1.245 - // is it position attribute?
1.246 - if (line.startsWith("#position=")) {
1.247 - if (currentItem == null) {
1.248 - LOGGER.log(Level.WARNING, "Found line '{0}' in {1} but there is no item to associate it with", new Object[] {line, url});
1.249 - continue;
1.250 - }
1.251 -
1.252 - try {
1.253 - currentItem.position = Integer.parseInt(line.substring(10));
1.254 - } catch (NumberFormatException e) {
1.255 - // do not use ErrorManager because we are in the startup code
1.256 - // and ErrorManager might not be ready
1.257 - e.printStackTrace();
1.258 - }
1.259 - }
1.260 -
1.261 - if (currentItem != null) {
1.262 - insertItem(currentItem, foundClasses);
1.263 - currentItem = null;
1.264 - }
1.265 -
1.266 - // Ignore blank lines and comments.
1.267 - if (line.length() == 0) {
1.268 - continue;
1.269 - }
1.270 -
1.271 - boolean remove = false;
1.272 -
1.273 - if (line.charAt(0) == '#') {
1.274 - if ((line.length() == 1) || (line.charAt(1) != '-')) {
1.275 - continue;
1.276 - }
1.277 -
1.278 - // line starting with #- is a sign to remove that class from lookup
1.279 - remove = true;
1.280 - line = line.substring(2);
1.281 - }
1.282 -
1.283 - Class inst = null;
1.284 -
1.285 - try {
1.286 - // Most lines are fully-qualified class names.
1.287 - inst = Class.forName(line, false, loader);
1.288 - } catch (ClassNotFoundException cnfe) {
1.289 - if (remove) {
1.290 - // if we are removing somthing and the something
1.291 - // cannot be found it is ok to do nothing
1.292 - continue;
1.293 - } else {
1.294 - // but if we are not removing just rethrow
1.295 - throw cnfe;
1.296 - }
1.297 - }
1.298 -
1.299 - if (!clazz.isAssignableFrom(inst)) {
1.300 - throw new ClassNotFoundException(clazzToString(inst) + " not a subclass of " + clazzToString(clazz)); // NOI18N
1.301 - }
1.302 -
1.303 - if (remove) {
1.304 - removeClasses.add(inst);
1.305 - } else {
1.306 - // create new item here, but do not put it into
1.307 - // foundClasses array yet because following line
1.308 - // might specify its position
1.309 - currentItem = new Item();
1.310 - currentItem.clazz = inst;
1.311 - }
1.312 - }
1.313 -
1.314 - if (currentItem != null) {
1.315 - insertItem(currentItem, foundClasses);
1.316 - currentItem = null;
1.317 - }
1.318 - } finally {
1.319 - is.close();
1.320 - }
1.321 - } catch (ClassNotFoundException ex) {
1.322 - LOGGER.log(Level.WARNING, null, ex);
1.323 - } catch (IOException ex) {
1.324 - LOGGER.log(Level.WARNING, null, ex);
1.325 - }
1.326 - }
1.327 -
1.328 - LOGGER.log(Level.FINER, "Found impls of {0}: {1} and removed: {2} from: {3}", new Object[] {clazz.getName(), foundClasses, removeClasses, this});
1.329 -
1.330 - foundClasses.removeAll(removeClasses);
1.331 -
1.332 - for (Item item : foundClasses) {
1.333 - if (removeClasses.contains(item.clazz)) {
1.334 - continue;
1.335 - }
1.336 -
1.337 - result.add(new P(item.clazz));
1.338 - }
1.339 - }
1.340 - private static String clazzToString(Class clazz) {
1.341 - return clazz.getName() + "@" + clazz.getClassLoader() + ":" + clazz.getProtectionDomain().getCodeSource().getLocation(); // NOI18N
1.342 - }
1.343 -
1.344 - /**
1.345 - * Insert item to the list according to item.position value.
1.346 - */
1.347 - private void insertItem(Item item, List<Item> list) {
1.348 - // no position? -> add it to the end
1.349 - if (item.position == -1) {
1.350 - list.add(item);
1.351 -
1.352 - return;
1.353 - }
1.354 -
1.355 - int index = -1;
1.356 - for (Item i : list) {
1.357 - index++;
1.358 -
1.359 - if (i.position == -1) {
1.360 - list.add(index, item);
1.361 -
1.362 - return;
1.363 - } else {
1.364 - if (i.position > item.position) {
1.365 - list.add(index, item);
1.366 -
1.367 - return;
1.368 - }
1.369 - }
1.370 - }
1.371 -
1.372 - list.add(item);
1.373 - }
1.374 -
1.375 - private static class Item {
1.376 - private Class clazz;
1.377 - private int position = -1;
1.378 - @Override
1.379 - public String toString() {
1.380 - return "MetaInfServicesLookup.Item[" + clazz.getName() + "]"; // NOI18N
1.381 - }
1.382 - }
1.383 -
1.384 - /** Pair that holds name of a class and maybe the instance.
1.385 - */
1.386 - private static final class P extends AbstractLookup.Pair<Object> {
1.387 - /** May be one of three things:
1.388 - * 1. The implementation class which was named in the services file.
1.389 - * 2. An instance of it.
1.390 - * 3. Null, if creation of the instance resulted in an error.
1.391 - */
1.392 - private Object object;
1.393 -
1.394 - public P(Class<?> clazz) {
1.395 - this.object = clazz;
1.396 - }
1.397 -
1.398 - /** Finds the class.
1.399 - */
1.400 - private Class<? extends Object> clazz() {
1.401 - Object o = object;
1.402 -
1.403 - if (o instanceof Class) {
1.404 - return (Class<? extends Object>) o;
1.405 - } else if (o != null) {
1.406 - return o.getClass();
1.407 - } else {
1.408 - // Broken.
1.409 - return Object.class;
1.410 - }
1.411 - }
1.412 -
1.413 - @Override
1.414 - public boolean equals(Object o) {
1.415 - if (o instanceof P) {
1.416 - return ((P) o).clazz().equals(clazz());
1.417 - }
1.418 -
1.419 - return false;
1.420 - }
1.421 -
1.422 - @Override
1.423 - public int hashCode() {
1.424 - return clazz().hashCode();
1.425 - }
1.426 -
1.427 - protected boolean instanceOf(Class<?> c) {
1.428 - return c.isAssignableFrom(clazz());
1.429 - }
1.430 -
1.431 - public Class<?> getType() {
1.432 - return clazz();
1.433 - }
1.434 -
1.435 - public Object getInstance() {
1.436 - Object o = object; // keeping local copy to avoid another
1.437 -
1.438 - // thread to modify it under my hands
1.439 - if (o instanceof Class) {
1.440 - synchronized (o) { // o is Class and we will not create
1.441 - // 2 instances of the same class
1.442 -
1.443 - try {
1.444 - Class<?> c = ((Class) o);
1.445 - o = null;
1.446 -
1.447 - synchronized (knownInstances) { // guards only the static cache
1.448 - int size = knownInstances.size();
1.449 - int index = c.hashCode() % size;
1.450 - for (int i = 0; i < size; i++) {
1.451 - Reference<Object> ref = knownInstances.get(index);
1.452 - Object obj = ref == null ? null : ref.get();
1.453 - if (obj == null) {
1.454 - break;
1.455 - }
1.456 - if (c == obj.getClass()) {
1.457 - o = obj;
1.458 - break;
1.459 - }
1.460 - if (++index == size) {
1.461 - index = 0;
1.462 - }
1.463 - }
1.464 - }
1.465 -
1.466 - if (o == null) {
1.467 - o = createInstance(c);
1.468 -
1.469 - synchronized (knownInstances) { // guards only the static cache
1.470 - hashPut(o);
1.471 -
1.472 - int size = knownInstances.size();
1.473 - if (knownInstancesCount > size * 2 / 3) {
1.474 - LOGGER.log(Level.CONFIG, "Cache of size {0} is 2/3 full. Rehashing.", size);
1.475 - HashSet<Reference<Object>> all = new HashSet<Reference<Object>>();
1.476 - all.addAll(knownInstances);
1.477 - for (int i = 0; i < size; i++) {
1.478 - knownInstances.set(i, null);
1.479 - }
1.480 - for (int i = 0; i < size; i++) {
1.481 - knownInstances.add(null);
1.482 - }
1.483 - knownInstancesCount = 0;
1.484 - for (Reference<Object> r : all) {
1.485 - if (r == null) {
1.486 - continue;
1.487 - }
1.488 - Object instance = r.get();
1.489 - if (instance == null) {
1.490 - continue;
1.491 - }
1.492 - hashPut(instance);
1.493 - }
1.494 - }
1.495 -
1.496 - }
1.497 - }
1.498 -
1.499 - // Do not assign to instance var unless there is a complete synch
1.500 - // block between the newInstance and this line. Otherwise we could
1.501 - // be assigning a half-constructed instance that another thread
1.502 - // could see and return immediately.
1.503 - object = o;
1.504 - } catch (Exception ex) {
1.505 - LOGGER.log(Level.WARNING, "Cannot create " + object, ex);
1.506 - object = null;
1.507 - } catch (ExceptionInInitializerError x) { // #174055
1.508 - LOGGER.log(Level.WARNING, "Cannot create " + object, x);
1.509 - object = null;
1.510 - }
1.511 - }
1.512 - }
1.513 -
1.514 - return object;
1.515 - }
1.516 -
1.517 - public String getDisplayName() {
1.518 - return clazz().getName();
1.519 - }
1.520 -
1.521 - public String getId() {
1.522 - return clazz().getName();
1.523 - }
1.524 -
1.525 - protected boolean creatorOf(Object obj) {
1.526 - return obj == object;
1.527 - }
1.528 -
1.529 - private static void hashPut(Object o) {
1.530 - Class<?> c = o.getClass();
1.531 - int size = knownInstances.size();
1.532 - int index = c.hashCode() % size;
1.533 - for (int i = 0; i < size; i++) {
1.534 - Reference<Object> ref = knownInstances.get(index);
1.535 - Object obj = ref == null ? null : ref.get();
1.536 - if (obj == null) {
1.537 - knownInstances.set(index, new WeakReference<Object>(o));
1.538 - knownInstancesCount++;
1.539 - break;
1.540 - }
1.541 - if (++index == size) {
1.542 - index = 0;
1.543 - }
1.544 - }
1.545 - }
1.546 -
1.547 - private static boolean findSharedClassObjectSkip;
1.548 - private static Method findSharedClassObject;
1.549 - /** Basically does c.newInstance(), however the method is complicated
1.550 - * with a special behaviour for old and almost obsoleted NetBeans
1.551 - * class: SharedClassObject.
1.552 - */
1.553 - private static Object createInstance(Class<?> c) throws InstantiationException, IllegalAccessException {
1.554 - if (!findSharedClassObjectSkip) {
1.555 - try {
1.556 - if (findSharedClassObject == null) {
1.557 - Class<?> sco;
1.558 - try {
1.559 - sco = Class.forName("org.openide.util.SharedClassObject"); // NOI18N
1.560 - } catch (ClassNotFoundException ex) {
1.561 - findSharedClassObjectSkip = true;
1.562 - return c.newInstance();
1.563 - }
1.564 - findSharedClassObject = sco.getMethod("findObject", Class.class, boolean.class);
1.565 - }
1.566 - if (findSharedClassObject.getReturnType().isAssignableFrom(c)) {
1.567 - return findSharedClassObject.invoke(null, c, true);
1.568 - }
1.569 - } catch (Exception problem) {
1.570 - throw (InstantiationException)new InstantiationException(problem.getMessage()).initCause(problem);
1.571 - }
1.572 - }
1.573 - return c.newInstance();
1.574 - }
1.575 - }
1.576 -}