View Javadoc

1   package org.catacomb.interlish.service;
2   
3   import java.util.HashMap;
4   
5   import org.catacomb.act.ScriptStubs;
6   import org.catacomb.be.Instantiator;
7   import org.catacomb.report.E;
8   
9   
10  
11  public class ScriptManager {
12  
13  
14  
15      static ScriptManager instance;
16  
17      private HashMap<String, ScriptSource> sourceHM;
18  
19      private ScriptMerger scriptMerger;
20      private ScriptChecker scriptChecker;
21      private ScriptInfo scriptInfo;
22  
23      private Instantiator typeInstantiator;
24      private Instantiator modelInstantiator;
25  
26  
27  
28      public static ScriptManager get() {
29          if (instance == null) {
30              instance = new ScriptManager();
31          }
32          return instance;
33      }
34  
35  
36      public ScriptManager() {
37          sourceHM = new HashMap<String, ScriptSource>();
38      }
39  
40  
41      public void addUtility(Object obj) {
42          if (obj instanceof ScriptMerger) {
43              scriptMerger = (ScriptMerger)obj;
44  
45          } else if (obj instanceof ScriptChecker) {
46              scriptChecker = (ScriptChecker)obj;
47  
48          } else if (obj instanceof ScriptInfo) {
49              scriptInfo = (ScriptInfo)obj;
50  
51          } else {
52              E.warning("unknown utility? " + obj);
53          }
54      }
55  
56  
57      public ScriptInfo getScriptInfo() {
58          return scriptInfo;
59      }
60  
61  
62      public void setModelInstantiator(Instantiator inst) {
63          modelInstantiator = inst;
64          //    E.info("set model instantiator " + inst);
65      }
66  
67      public Instantiator getModelInstantiator() {
68          if (modelInstantiator == null) {
69              E.error("asked for instantiator before it is set?");
70          }
71          return modelInstantiator;
72      }
73  
74      public void setTypeInstantiator(Instantiator inst) {
75          typeInstantiator = inst;
76      }
77  
78      public Instantiator getTypeInstantiator() {
79          if (typeInstantiator == null) {
80              E.error("asked for instantiator before it is set?");
81          }
82          return typeInstantiator;
83      }
84  
85  
86      public ScriptReport checkScripts(Object obj) {
87          ScriptReport ret = null;
88          if (scriptChecker == null) {
89              // return an error report?
90          } else {
91              ret = scriptChecker.checkScript(obj);
92          }
93          return ret;
94      }
95  
96  
97  
98      public void addScriptSource(Object hostExample, ScriptSource p) {
99          String hcnm = null;
100         if (hostExample instanceof String) {
101             hcnm = (String)hostExample;
102         } else {
103             hcnm = hostExample.getClass().getName();
104         }
105         sourceHM.put(hcnm, p);
106     }
107 
108     private ScriptSource getScriptSource(Object host, int role) {
109         ScriptSource ss = null;
110         String hcl = host.getClass().getName();
111         if (sourceHM.containsKey(hcl)) {
112             ss = sourceHM.get(hcl);
113         } else {
114             E.error("need to generate code for " + hcl + " but no suitable script source is registered");
115         }
116         return ss;
117     }
118 
119     public ScriptStubs getStubs(Object host, int role) {
120         ScriptStubs ret = null;
121         ScriptSource ss = getScriptSource(host, role);
122         if (ss != null) {
123             ret = ss.getStubs(host, role);
124         }
125         return ret;
126     }
127 
128     public String getStubInfo(Object host, int role) {
129         String ret = null;
130         ScriptSource ss = getScriptSource(host, role);
131         if (ss != null) {
132             ret = ss.getScriptInfo(host, role);
133         }
134         return ret;
135     }
136 
137     public String mergeStubs(String oldscript, String stubs) {
138         return scriptMerger.mergeStubs(oldscript, stubs);
139     }
140 
141 
142     public void writeScripts(Object obj, int role) {
143         ScriptSource ss = getScriptSource(obj, role);
144         if (ss != null) {
145             ss.writeScripts(obj);
146         }
147     }
148 
149 
150 
151 }