KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > script > ScriptCore


1 package rero.script;
2
3 import sleep.runtime.*;
4 import sleep.engine.*;
5
6 import sleep.error.*;
7 import sleep.parser.*;
8
9 import sleep.interfaces.*;
10
11 import java.util.*;
12
13 import rero.ircfw.*;
14 import rero.bridges.*;
15
16 import rero.client.DataStructures;
17
18 import rero.config.*;
19 import text.*; // for the burco constants
20

21 import java.io.*;
22
23 public class ScriptCore implements Loadable
24 {
25    protected ScriptLoader scriptLoader; /* script loader baby! */
26    protected Hashtable environment; /* shared script environment */
27    protected ScriptVariables variables; /* variables shared amongst loaded scripts */
28    protected GlobalVariables globalData; /* global variable data */
29
30    protected BridgeKeeper bridges; /* all of our nifty bridges ... */
31
32    protected static Scalar GLOBAL_HASH; /* a global hash for all... :) */
33
34    public ScriptCore()
35    {
36       if (GLOBAL_HASH == null)
37       {
38          GLOBAL_HASH = SleepUtils.getHashScalar();
39       }
40
41       scriptLoader = new ScriptLoader();
42       environment = new Hashtable();
43       globalData = new GlobalVariables();
44       globalData.putScalar("%GLOBAL", GLOBAL_HASH);
45
46       variables = new ScriptVariables(globalData);
47
48       bridges = new BridgeKeeper();
49
50       scriptLoader.setCharsetConversion(false); // tell sleep to NOT convert characters inside of a script file
51
scriptLoader.setGlobalCache(true); // tell sleep to globally cache all script blocks... makes loading/reloading quicker for multiple servers + it saves memory :)
52
scriptLoader.addSpecificBridge(this);
53
54       CodeGenerator.installEscapeConstant('B', AttributedString.bold+"");
55       CodeGenerator.installEscapeConstant('U', AttributedString.underline+"");
56       CodeGenerator.installEscapeConstant('R', AttributedString.reverse+"");
57       CodeGenerator.installEscapeConstant('C', AttributedString.color+"");
58       CodeGenerator.installEscapeConstant('O', AttributedString.cancel+"");
59
60       CodeGenerator.installEscapeConstant('b', AttributedString.bold+"");
61       CodeGenerator.installEscapeConstant('u', AttributedString.underline+"");
62       CodeGenerator.installEscapeConstant('r', AttributedString.reverse+"");
63       CodeGenerator.installEscapeConstant('c', AttributedString.color+"");
64       CodeGenerator.installEscapeConstant('o', AttributedString.cancel+"");
65
66       SleepUtils.addKeyword("wait"); // register certain scripting keywords with the parser
67
SleepUtils.addKeyword("on");
68       SleepUtils.addKeyword("alias");
69       SleepUtils.addKeyword("bind");
70
71       Loadable blist[] = bridges.getScriptBridges();
72       for (int x = 0; x < blist.length; x++)
73       {
74          addBridge(blist[x]);
75       }
76    }
77
78    // === Process Imports ===================================================================================
79

80    public void announceFramework(ChatFramework ircfw)
81    {
82       bridges.announceFramework(ircfw);
83    }
84
85    public void addBridge(Loadable l)
86    {
87       scriptLoader.addGlobalBridge(l);
88    }
89
90    // === Export Data Structures ============================================================================
91

92    public void storeDataStructures(WeakHashMap centralDataRepository)
93    {
94       centralDataRepository.put("scriptVariables", variables);
95       centralDataRepository.put("globalVariables", globalData);
96
97       centralDataRepository.put(DataStructures.ScriptLoader, scriptLoader);
98       centralDataRepository.put(DataStructures.SharedEnv, environment);
99
100       bridges.storeDataStructures(centralDataRepository);
101    }
102
103    // === Export Capabilities ===============================================================================
104

105    public Scalar callFunction(String JavaDoc function, Stack parameters)
106    {
107        if (function.charAt(0) != '&')
108        {
109           function = '&' + function;
110        }
111
112        ScriptInstance si = (ScriptInstance)scriptLoader.getScripts().getFirst();
113
114        return si.callFunction(function, parameters);
115    }
116
117    /** convienence function for running some code and installing some local variables */
118    public static void runCode(ScriptInstance owner, Block code, HashMap locals)
119    {
120        synchronized (owner.getScriptVariables())
121        {
122           ScriptVariables vars = owner.getScriptVariables();
123
124           vars.pushLocalLevel();
125
126           LocalVariables localLevel = (LocalVariables)vars.getLocalVariables();
127
128           if (locals != null)
129           {
130               localLevel.setDataSource(locals);
131           }
132
133           //
134
// execute the block of code
135
//
136
SleepUtils.runCode(code, owner.getScriptEnvironment());
137
138           vars.popLocalLevel();
139        }
140    }
141
142    // === Implement Interfaces ==============================================================================
143

144    public boolean scriptLoaded(ScriptInstance si)
145    {
146       si.setScriptVariables(variables);
147       ClientState.getClientState().fireChange("loaded.scripts", null);
148       return true;
149    }
150    
151    public boolean scriptUnloaded(ScriptInstance si)
152    {
153       ClientState.getClientState().fireChange("loaded.scripts", null);
154       return true;
155    }
156 }
157
Popular Tags