ModuleContext.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2005, 2007, 2010 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.expr;
  4. import gnu.mapping.*;
  5. import java.util.*;
  6. import gnu.kawa.util.AbstractWeakHashTable;
  7. /** Maps modules to module instances.
  8. * Given a class, species a specific instance object for that class.
  9. */
  10. public class ModuleContext
  11. {
  12. static ModuleContext global = new ModuleContext(ModuleManager.instance);
  13. ModuleManager manager;
  14. public static int IN_HTTP_SERVER = 1;
  15. public static int IN_SERVLET = 2;
  16. int flags;
  17. public int getFlags () { return flags; }
  18. public void setFlags(int flags) { this.flags = flags; }
  19. public void addFlags(int flags) { this.flags |= flags; }
  20. public ModuleContext (ModuleManager manager)
  21. {
  22. this.manager = manager;
  23. }
  24. /** For now returns the shared global ModuleContext.
  25. * Later provide a means for thread-specific overriding. */
  26. public static ModuleContext getContext ()
  27. {
  28. return global;
  29. }
  30. public ModuleManager getManager ()
  31. {
  32. return manager;
  33. }
  34. private ClassToInstanceMap table = new ClassToInstanceMap();
  35. /** If there is no instance of the argument's class, allocate one. */
  36. public synchronized Object findInstance(ModuleInfo info)
  37. {
  38. Class clas;
  39. try
  40. {
  41. clas = info.getModuleClass();
  42. }
  43. catch (java.lang.ClassNotFoundException ex)
  44. {
  45. String cname = info.getClassName();
  46. throw new WrappedException("cannot find module " + cname, ex);
  47. }
  48. return findInstance(clas);
  49. }
  50. public synchronized Object searchInstance (Class clas)
  51. {
  52. return table.get(clas);
  53. }
  54. public Object findInstance(Class clas) {
  55. synchronized (this) {
  56. Object inst = table.get(clas);
  57. if (inst == null) {
  58. try {
  59. try {
  60. inst = clas.getDeclaredField("$instance").get(null);
  61. } catch (NoSuchFieldException ex) {
  62. // Not a static module - create a new instance.
  63. inst = clas.newInstance();
  64. }
  65. } catch (Exception ex) {
  66. throw new WrappedException
  67. ("exception while initializing module " + clas.getName(), ex);
  68. }
  69. setInstance(inst);
  70. }
  71. return inst;
  72. }
  73. }
  74. public synchronized void setInstance (Object instance)
  75. {
  76. table.put(instance.getClass(), instance);
  77. }
  78. public ModuleInfo findFromInstance (Object instance)
  79. {
  80. Class instanceClass = instance.getClass();
  81. synchronized (this)
  82. {
  83. ModuleInfo info = manager.findWithClass(instanceClass);
  84. setInstance(instance);
  85. return info;
  86. }
  87. }
  88. /** Remove all entries.
  89. * This can be used to avoids memory leaks.
  90. */
  91. public synchronized void clear ()
  92. {
  93. table.clear();
  94. }
  95. static class ClassToInstanceMap extends AbstractWeakHashTable<Class,Object>
  96. {
  97. protected Class getKeyFromValue (Object instance)
  98. {
  99. return instance.getClass();
  100. }
  101. protected boolean matches (Class oldValue, Class newValue)
  102. {
  103. return oldValue == newValue;
  104. }
  105. }
  106. }