1
2
3
4
5 package net.sf.wfnm;
6
7 /***
8 * A factory that manage navigation manager context instances.<br>
9 * This class uses a ThreadLocal design pattern to collect navigation context instances each tied to a browser
10 * request.
11 *
12 * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
13 * @version 1.0.6
14 */
15 public class NavigationContextFactory {
16 /***
17 * The statit contexts.
18 */
19 private static ThreadLocal contexts = new ThreadLocal();
20
21 /***
22 * Get the navigation context instance tied to the current thread.
23 *
24 * @return the navigation context instance tied to the current thread
25 */
26 public static NavigationContext getInstance() {
27 NavigationContext navigationContext = (NavigationContext) contexts.get();
28
29 if (navigationContext == null) {
30 Class contextClass = Config.getInstance().getContext();
31
32 try {
33 navigationContext = (NavigationContext) contextClass.newInstance();
34 } catch (Exception e) {
35 throw new ConfigException("Unable to instantiate the class " + contextClass.getName(), e);
36 }
37
38 contexts.set(navigationContext);
39 }
40
41 return navigationContext;
42 }
43
44 /***
45 * Restore the navigation context instance tied to the current thread.
46 */
47 public static void restoreInstance() {
48 contexts.set(null);
49 }
50 }