View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: ResolverContext.java,v 1.1.1.1 2004/05/19 12:07:31 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.utils.xml;
21  import org.backsource.utils.lang.ClassContextLocal;
22  import org.backsource.utils.lang.InheritableClassContextLocal;
23  /***
24   * Resolver context is a flexible static class to allow setting of EntityResolver and URIResolver when using all the static methods in these utility classes.
25   *
26   * @see org.backsource.utils.lang.ClassContextLocal
27   * @see org.backsource.utils.lang.InheritableClassContextLocal
28   * @author <a href="mailto:pra@tim.se"></a>
29   * @version $Revision: 1.1.1.1 $
30   */
31  
32  public class ResolverContext {
33     private static Resolver global= new Resolver();
34     private static InheritableClassContextLocal inheritClassLocal= null;
35     private static ClassContextLocal classLocal = null;
36     private static ThreadLocal threadLocal = null;
37     
38     private ResolverContext (){
39        
40     }
41     /***
42       * <p>Get a Resolver.
43       *
44       * <p>Does a bottom up search. The order is: threadLocal, classLocal, inheritableClassLocal and last the static global instance of URIFactory.
45       */ 
46      public static Resolver get() {
47  	Resolver fac = null;
48  	if (threadLocal != null && 
49  	    (fac = (Resolver)threadLocal.get()) != null)
50  	    return fac;
51  	else if (classLocal != null && 
52  	    (fac = (Resolver)classLocal.get()) != null)
53  	    return fac;
54  	else if (inheritClassLocal != null && 
55  	    (fac = (Resolver)inheritClassLocal.get()) != null)
56  	    return fac;
57  	return global;
58      }
59  
60      /***
61       * Set a Resolver available globaly in the JVM.
62       */
63      public static synchronized void set(Resolver factory) {
64  	global = factory;
65      }
66  
67      /***
68       * Set an Resolver available in the current context class loader it is
69       set and in all its children classloaders.
70       */
71      public static synchronized void setInheritClassLocal(Resolver factory) {
72  	if (inheritClassLocal == null)
73  	    inheritClassLocal = new InheritableClassContextLocal();
74  
75  	inheritClassLocal.set(factory);
76      }
77      
78      /***
79       * Set an Resolver only available in the context classloader it is 
80       set.
81       */
82      public static synchronized void setClassLocal(Resolver factory) {
83  	if (classLocal == null)
84  	    classLocal = new ClassContextLocal();
85  
86  	classLocal.set(factory);
87      }
88      
89      /***
90       * Set a Resolver available in the current thread. 
91       */
92      public static synchronized void setThreadLocal(Resolver factory) {
93  	if (threadLocal == null)
94  	    threadLocal = new ThreadLocal();
95  
96  	threadLocal.set(factory);
97      }
98     
99  }// ResolverContext