KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > spi > SourceProviders


1 /**
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.google.inject.spi;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * Provides access to the default {@link SourceProvider} implementation and
25  * common controls for certain implementations.
26  *
27  * @author crazybob@google.com (Bob Lee)
28  */

29 public class SourceProviders {
30
31   private SourceProviders() {}
32
33   public static final Object JavaDoc UNKNOWN_SOURCE = "[unknown source]";
34   
35   static final SourceProvider DEFAULT_INSTANCE = new StacktraceSourceProvider();
36
37   static Set JavaDoc<String JavaDoc> skippedClassNames = Collections.emptySet();
38
39   static {
40     skip(SourceProviders.class);
41     skip(StacktraceSourceProvider.class);
42   }
43
44   /**
45    * Instructs stacktrace-based providers to skip the given class in the stack
46    * trace when determining the source. Use this to keep the binder from
47    * logging utility methods as the sources of bindings (i.e. it will skip to
48    * the utility methods' callers instead).
49    *
50    * <p>Skipping only takes place after this method is called.
51    */

52   public synchronized static void skip(Class JavaDoc<?> clazz) {
53     Set JavaDoc<String JavaDoc> copy = new HashSet JavaDoc<String JavaDoc>();
54     copy.addAll(skippedClassNames);
55     copy.add(clazz.getName());
56     skippedClassNames = Collections.unmodifiableSet(copy);
57   }
58
59   /**
60    * Gets the set of class names which should be skipped by stacktrace-based
61    * providers.
62    */

63   public synchronized static Set JavaDoc<String JavaDoc> getSkippedClassNames() {
64     return skippedClassNames;
65   }
66
67   static ThreadLocal JavaDoc<SourceProvider[]> localSourceProvider =
68       new ThreadLocal JavaDoc<SourceProvider[]>() {
69     protected SourceProvider[] initialValue() {
70       return new SourceProvider[] { DEFAULT_INSTANCE };
71     }
72   };
73
74   /**
75    * Returns the current source obtained from the default provider.
76    */

77   public static Object JavaDoc defaultSource() {
78     return localSourceProvider.get()[0].source();
79   }
80
81   /**
82    * Sets the default source provider, runs the given command, and then
83    * restores the previous default source provider.
84    */

85   public static void withDefault(
86       SourceProvider sourceProvider, Runnable JavaDoc r) {
87     // We use a holder so we perform only 1 thread local access instead of 3.
88
SourceProvider[] holder = localSourceProvider.get();
89     SourceProvider previous = holder[0];
90     try {
91       holder[0] = sourceProvider;
92       r.run();
93     } finally {
94       holder[0] = previous;
95     }
96   }
97
98   static class StacktraceSourceProvider implements SourceProvider {
99     public Object JavaDoc source() {
100       // Search up the stack until we find a class outside of this one.
101
Set JavaDoc<String JavaDoc> skippedClassNames = getSkippedClassNames();
102       for (final StackTraceElement JavaDoc element : new Throwable JavaDoc().getStackTrace()) {
103         String JavaDoc className = element.getClassName();
104         if (!skippedClassNames.contains(className)) {
105           return element;
106         }
107       }
108       throw new AssertionError JavaDoc();
109     }
110   }
111 }
112
Popular Tags