1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.backsource.utils.resource;
19
20 import org.backsource.utils.lang.ClassContextLocal;
21 import org.backsource.utils.lang.InheritableClassContextLocal;
22 /***
23 * <p>A dynamic singleton used by URI to lookup an URIFactory.
24 *
25 * <p>See {@link URIFactory} for more information.
26 *
27 *
28 * @author Peter Antman
29 * @version $Revision: 1.1.1.1 $
30 */
31
32 public class URIFactoryContext {
33
34 private static URIFactory global= new URIFactory();
35 private static InheritableClassContextLocal inheritClassLocal= null;
36 private static ClassContextLocal classLocal = null;
37 private static ThreadLocal threadLocal = null;
38
39
40 private URIFactoryContext() {
41
42 }
43
44 /***
45 * <p>Get an URIFactory.
46 *
47 * <p>Does a bottom up search. The order is: threadLocal, classLocal, inheritableClassLocal and last the static global instance of URIFactory.
48 */
49 public static URIFactory get() {
50 URIFactory fac = null;
51 if (threadLocal != null &&
52 (fac = (URIFactory)threadLocal.get()) != null)
53 return fac;
54 else if (classLocal != null &&
55 (fac = (URIFactory)classLocal.get()) != null)
56 return fac;
57 else if (inheritClassLocal != null &&
58 (fac = (URIFactory)inheritClassLocal.get()) != null)
59 return fac;
60 return global;
61 }
62
63 /***
64 * Set a URIFactory available globaly in the JVM.
65 */
66 public static synchronized void set(URIFactory factory) {
67 global = factory;
68 }
69
70 /***
71 * Set an URIFactory available in the current context class loader it is
72 set and in all its children classloaders.
73 */
74 public static synchronized void setInheritClassLocal(URIFactory factory) {
75 if (inheritClassLocal == null)
76 inheritClassLocal = new InheritableClassContextLocal();
77
78 inheritClassLocal.set(factory);
79 }
80
81 /***
82 * Set an URIFactory only available in the context classloader it is
83 set.
84 */
85 public static synchronized void setClassLocal(URIFactory factory) {
86 if (classLocal == null)
87 classLocal = new ClassContextLocal();
88
89 classLocal.set(factory);
90 }
91
92 /***
93 * Set a URIFactory available in the current thread.
94 */
95 public static synchronized void setThreadLocal(URIFactory factory) {
96 if (threadLocal == null)
97 threadLocal = new ThreadLocal();
98
99 threadLocal.set(factory);
100 }
101 }