Using logging. Also some JDK 5 cleanups. last_stable_pre_nb60 newDownloderIntegration_root
authorjglick@netbeans.org
Tue, 03 Oct 2006 23:28:43 +0000
changeset 2124f70da526c4b
parent 211 7239ec94cec5
child 213 eab3ec754211
Using logging. Also some JDK 5 cleanups.
openide.util/src/org/openide/util/lookup/MetaInfServicesLookup.java
     1.1 --- a/openide.util/src/org/openide/util/lookup/MetaInfServicesLookup.java	Tue Oct 03 21:46:03 2006 +0000
     1.2 +++ b/openide.util/src/org/openide/util/lookup/MetaInfServicesLookup.java	Tue Oct 03 23:28:43 2006 +0000
     1.3 @@ -28,12 +28,13 @@
     1.4  import java.util.Collection;
     1.5  import java.util.Enumeration;
     1.6  import java.util.HashSet;
     1.7 -import java.util.Iterator;
     1.8  import java.util.LinkedHashSet;
     1.9  import java.util.List;
    1.10  import java.util.Map;
    1.11  import java.util.Set;
    1.12  import java.util.WeakHashMap;
    1.13 +import java.util.logging.Level;
    1.14 +import java.util.logging.Logger;
    1.15  import org.openide.util.Lookup;
    1.16  import org.openide.util.SharedClassObject;
    1.17  import org.openide.util.WeakSet;
    1.18 @@ -52,8 +53,9 @@
    1.19   * @see "#14722"
    1.20   */
    1.21  final class MetaInfServicesLookup extends AbstractLookup {
    1.22 -    // Better not to use ErrorManager here - EM.gD will use this class, might cause cycles etc.
    1.23 -    private static final boolean DEBUG = Boolean.getBoolean("org.openide.util.lookup.MetaInfServicesLookup.DEBUG"); // NOI18N
    1.24 +
    1.25 +    private static final Logger LOGGER = Logger.getLogger(MetaInfServicesLookup.class.getName());
    1.26 +
    1.27      private static final Map<Class,Object> knownInstances = new WeakHashMap<Class,Object>();
    1.28  
    1.29      /** A set of all requested classes.
    1.30 @@ -79,9 +81,7 @@
    1.31      public MetaInfServicesLookup(ClassLoader loader) {
    1.32          this.loader = loader;
    1.33  
    1.34 -        if (DEBUG) {
    1.35 -            System.err.println("Created: " + this); // NOI18N
    1.36 -        }
    1.37 +        LOGGER.log(Level.FINE, "Created: {0}", this);
    1.38      }
    1.39  
    1.40      public String toString() {
    1.41 @@ -98,7 +98,7 @@
    1.42          synchronized (this) {
    1.43              if (classes.add(c)) {
    1.44                  // Added new class, search for it.
    1.45 -                LinkedHashSet<Pair<?>> arr = getPairsAsLHS();
    1.46 +                LinkedHashSet<AbstractLookup.Pair<?>> arr = getPairsAsLHS();
    1.47                  search(c, arr);
    1.48  
    1.49                  // listeners are notified under while holding lock on class c, 
    1.50 @@ -118,13 +118,13 @@
    1.51       * @param clazz class to find
    1.52       * @param result collection to add Pair to
    1.53       */
    1.54 -    private void search(Class<?> clazz, Collection<Pair<?>> result) {
    1.55 -        if (DEBUG) {
    1.56 -            System.err.println("Searching for " + clazz.getName() + " in " + clazz.getClassLoader() + " from " + this); // NOI18N
    1.57 +    private void search(Class<?> clazz, Collection<AbstractLookup.Pair<?>> result) {
    1.58 +        if (LOGGER.isLoggable(Level.FINER)) {
    1.59 +            LOGGER.log(Level.FINER, "Searching for " + clazz.getName() + " in " + clazz.getClassLoader() + " from " + this);
    1.60          }
    1.61  
    1.62          String res = "META-INF/services/" + clazz.getName(); // NOI18N
    1.63 -        Enumeration en;
    1.64 +        Enumeration<URL> en;
    1.65  
    1.66          try {
    1.67              en = loader.getResources(res);
    1.68 @@ -170,14 +170,14 @@
    1.69                  if (realMcCoy != clazz) {
    1.70                      // Either the interface class is not available at all in our loader,
    1.71                      // or it is not the same version as we expected. Don't provide results.
    1.72 -                    if (DEBUG) {
    1.73 +                    if (LOGGER.isLoggable(Level.FINER)) {
    1.74                          if (realMcCoy != null) {
    1.75 -                            System.err.println(
    1.76 +                            LOGGER.log(Level.FINER,
    1.77                                  clazz.getName() + " is not the real McCoy! Actually found it in " +
    1.78                                  realMcCoy.getClassLoader()
    1.79                              ); // NOI18N
    1.80                          } else {
    1.81 -                            System.err.println(clazz.getName() + " could not be found in " + loader); // NOI18N
    1.82 +                            LOGGER.log(Level.FINER, clazz.getName() + " could not be found in " + loader); // NOI18N
    1.83                          }
    1.84                      }
    1.85  
    1.86 @@ -185,7 +185,7 @@
    1.87                  }
    1.88              }
    1.89  
    1.90 -            URL url = (URL) en.nextElement();
    1.91 +            URL url = en.nextElement();
    1.92              Item currentItem = null;
    1.93  
    1.94              try {
    1.95 @@ -206,7 +206,8 @@
    1.96                          // is it position attribute?
    1.97                          if (line.startsWith("#position=")) {
    1.98                              if (currentItem == null) {
    1.99 -                                assert false : "Found line '" + line + "' but there is no item to associate it with!";
   1.100 +                                LOGGER.log(Level.WARNING, "Found line '{0}' in {1} but there is no item to associate it with", new Object[] {line, url});
   1.101 +                                continue;
   1.102                              }
   1.103  
   1.104                              try {
   1.105 @@ -257,10 +258,6 @@
   1.106                          }
   1.107  
   1.108                          if (!clazz.isAssignableFrom(inst)) {
   1.109 -                            if (DEBUG) {
   1.110 -                                System.err.println("Not a subclass"); // NOI18N
   1.111 -                            }
   1.112 -
   1.113                              throw new ClassNotFoundException(inst.getName() + " not a subclass of " + clazz.getName()); // NOI18N
   1.114                          }
   1.115  
   1.116 @@ -283,30 +280,17 @@
   1.117                      is.close();
   1.118                  }
   1.119              } catch (ClassNotFoundException ex) {
   1.120 -                // do not use ErrorManager because we are in the startup code
   1.121 -                // and ErrorManager might not be ready
   1.122 -                ex.printStackTrace();
   1.123 +                LOGGER.log(Level.WARNING, null, ex);
   1.124              } catch (IOException ex) {
   1.125 -                // do not use ErrorManager because we are in the startup code
   1.126 -                // and ErrorManager might not be ready
   1.127 -                ex.printStackTrace();
   1.128 +                LOGGER.log(Level.WARNING, null, ex);
   1.129              }
   1.130          }
   1.131  
   1.132 -        if (DEBUG) {
   1.133 -            System.err.println(
   1.134 -                "Found impls of " + clazz.getName() + ": " + foundClasses + " and removed: " + removeClasses +
   1.135 -                " from: " + this
   1.136 -            ); // NOI18N
   1.137 -        }
   1.138 +        LOGGER.log(Level.FINER, "Found impls of {0}: {1} and removed: {2} from: {3}", new Object[] {clazz.getName(), foundClasses, removeClasses, this});
   1.139  
   1.140          foundClasses.removeAll(removeClasses);
   1.141  
   1.142 -        Iterator it = foundClasses.iterator();
   1.143 -
   1.144 -        while (it.hasNext()) {
   1.145 -            Item item = (Item) it.next();
   1.146 -
   1.147 +        for (Item item : foundClasses) {
   1.148              if (removeClasses.contains(item.clazz)) {
   1.149                  continue;
   1.150              }
   1.151 @@ -327,13 +311,9 @@
   1.152          }
   1.153  
   1.154          int index = -1;
   1.155 -        Iterator it = list.iterator();
   1.156 -
   1.157 -        while (it.hasNext()) {
   1.158 +        for (Item i : list) {
   1.159              index++;
   1.160  
   1.161 -            Item i = (Item) it.next();
   1.162 -
   1.163              if (i.position == -1) {
   1.164                  list.add(index, item);
   1.165  
   1.166 @@ -353,6 +333,10 @@
   1.167      private static class Item {
   1.168          private Class clazz;
   1.169          private int position = -1;
   1.170 +        @Override
   1.171 +        public String toString() {
   1.172 +            return "MetaInfServicesLookup.Item[" + clazz.getName() + "]"; // NOI18N
   1.173 +        }
   1.174      }
   1.175  
   1.176      /** Pair that holds name of a class and maybe the instance.
   1.177 @@ -400,7 +384,7 @@
   1.178              return c.isAssignableFrom(clazz());
   1.179          }
   1.180  
   1.181 -        public Class<? extends Object> getType() {
   1.182 +        public Class<?> getType() {
   1.183              return clazz();
   1.184          }
   1.185  
   1.186 @@ -437,9 +421,7 @@
   1.187                          // could see and return immediately.
   1.188                          object = o;
   1.189                      } catch (Exception ex) {
   1.190 -                        // do not use ErrorManager because we are in the startup code
   1.191 -                        // and ErrorManager might not be ready
   1.192 -                        ex.printStackTrace();
   1.193 +                        LOGGER.log(Level.WARNING, null, ex);
   1.194                          object = null;
   1.195                      }
   1.196                  }