View Javadoc

1   /*
2    * Copyright (c) 2001 Peter Antman Tim <peter.antman@tim.se>
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2 of the License, or (at your option) any later version
8    * 
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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();//The default
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 } // URIFactoryContext