KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > ModuleContext


1 // Copyright (c) 2005 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.expr;
5 import gnu.mapping.*;
6 import java.util.*;
7
8 /** Maps modules to module instances.
9  * Given a class, species a specific instance object for that class.
10  */

11
12 public class ModuleContext
13 {
14   static ModuleContext global = new ModuleContext(ModuleManager.instance);
15   ModuleManager manager;
16
17   public ModuleContext (ModuleManager manager)
18   {
19     this.manager = manager;
20   }
21
22   /** For now returns the shared global ModuleContext.
23    * Later provide a means for thread-specific overriding. */

24   public static ModuleContext getContext ()
25   {
26     return global;
27   }
28
29   public ModuleManager getManager ()
30   {
31     return manager;
32   }
33
34   Hashtable table = new Hashtable();
35
36   public Object JavaDoc checkInstance (ModuleInfo info)
37   {
38     return table.get(info.className);
39   }
40
41   /** Allocate a new instance of the class corresponding to the argument. */
42   public Object JavaDoc makeInstance (ModuleInfo info)
43   {
44     String JavaDoc cname = info.className;
45     Class JavaDoc clas;
46     try
47       {
48         clas = info.getModuleClass();
49       }
50     catch (java.lang.ClassNotFoundException JavaDoc ex)
51       {
52         throw new WrappedException("cannot find module " + cname, ex);
53       }
54
55     Object JavaDoc inst;
56     try
57       {
58         try
59           {
60             inst = clas.getDeclaredField("$instance").get(null);
61           }
62         catch (NoSuchFieldException JavaDoc ex)
63           {
64             // Not a static module - create a new instance.
65
inst = clas.newInstance();
66           }
67       }
68     catch (Throwable JavaDoc ex)
69       {
70         throw new WrappedException
71           ("exception while initializing module " + cname, ex);
72       }
73     setInstance(info, inst);
74     return inst;
75   }
76
77   /** If there is no instance of the argument's class, allocated one. */
78   public Object JavaDoc findInstance (ModuleInfo info)
79   {
80     Object JavaDoc inst = table.get(info.className);
81     if (inst == null || info.moduleClass == null
82         || ! info.moduleClass.isInstance(inst))
83       inst = makeInstance(info);
84     return inst;
85   }
86
87   public void setInstance (ModuleInfo info, Object JavaDoc instance)
88   {
89     table.put(info.className, instance);
90   }
91
92   public ModuleInfo findFromInstance (Object JavaDoc instance)
93   {
94     Class JavaDoc instanceClass = instance.getClass();
95     String JavaDoc className = instanceClass.getName();
96     ModuleInfo info = manager.findWithClassName(className);
97     info.moduleClass = instanceClass;
98     setInstance(info, instance);
99     return info;
100   }
101
102 }
103
Popular Tags