View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.visualiser;
25  
26  import java.util.*;
27  import zeus.util.*;
28  import zeus.concepts.*;
29  import zeus.actors.*;
30  
31  public class VisualiserModel {
32    public static final int ADDRESS_KEY   = 0;
33    public static final int MESSAGE_KEY   = 1;
34    public static final int REPORT_KEY    = 2;
35  
36    public static final int CANCEL_SUBSCRIPTION = 0;
37    public static final int DO_NOTHING          = 1;
38  
39    protected Hashtable sessionList         = new Hashtable();
40    protected Hashtable agentList           = new Hashtable();
41    protected Hashtable messageRuleDb       = new Hashtable();
42    protected Hashtable subscriptionDb      = new Hashtable();
43    protected Hashtable subscriptionContent = new Hashtable();
44  
45    protected AgentContext context = null;
46    public    String[]     keys = new String[3];
47  
48    public VisualiserModel(AgentContext context) {
49       this.context = context;
50  
51       for(int i = 0; i < keys.length; i++ )
52          keys[i] = context.newId("VisualiserModel");
53  
54       subscriptionContent.put(keys[ADDRESS_KEY],"log_address");
55       subscriptionContent.put(keys[MESSAGE_KEY],"log_message");
56       subscriptionContent.put(keys[REPORT_KEY],"log_report");
57    }
58  
59    public String getSubscriptionContent(String key) {
60       return (String)subscriptionContent.get(key);
61    }
62  
63  
64    public void addAgent(String name, String type) {
65      if (name == null || type == null) return;
66      agentList.put(name,type);
67    }
68    
69  
70    public void addAgents(String[] agent) {
71      if (agent == null) return;
72      String type = SystemProps.getProperty("agent.names.agent");
73      for(int i = 0; i < agent.length; i++ )
74         if ( !agentList.containsKey(agent[i]) )
75            agentList.put(agent[i],type);
76    }
77  
78    public String[] getAgents() {
79       Enumeration keys = agentList.keys();
80       String[] data = new String[agentList.size()];
81       for(int i = 0; keys.hasMoreElements(); i++)
82          data[i] = (String)keys.nextElement();
83       return data;
84    }
85  
86    public String[] getAgents(String agent_type) {
87        Enumeration keys = agentList.keys();
88        Vector List = new Vector();
89        while(keys.hasMoreElements()) {
90          String agent = (String)keys.nextElement();
91          String type = (String)agentList.get(agent);
92          if (type.equals(agent_type))
93             List.addElement(agent);
94        }
95        return Misc.stringArray(List);
96    }
97  
98  
99    //-------------------------------------------------------
100   // Agent Name Server List Methods
101 
102   public void addNameservers(String[] agent) {
103     if (agent == null) return;
104     String type = SystemProps.getProperty("agent.names.nameserver");
105     for(int i = 0; i < agent.length; i++ ) {
106        agentList.put(agent[i],type);
107     }
108   }
109 
110   public void addNameserver(String agent) {
111      if (agent == null) return;
112      String type = SystemProps.getProperty("agent.names.nameserver");
113      agentList.put(agent,type);
114   }
115 
116   public String[] getNameservers() {
117     return getAgents(SystemProps.getProperty("agent.names.nameserver"));
118   }
119 
120   //-------------------------------------------------------
121   // Database Proxy List Methods
122 
123   public void addDbProxy(String agent) {
124     if (agent == null) return;
125     String type = SystemProps.getProperty("agent.names.dbProxy");
126     agentList.put(agent,type);
127   }
128 
129   public void addDbProxys(String[] agent) {
130     if (agent == null) return;
131     String type = SystemProps.getProperty("agent.names.dbProxy");
132     for(int i = 0; i < agent.length; i++ )
133        agentList.put(agent[i],type);
134   }
135 
136   public String[] getDbProxys() {
137     return getAgents(SystemProps.getProperty("agent.names.dbProxy"));
138   }
139   //-------------------------------------------------------
140 
141   public void addDbSession(String type, String agent, String sessionId) {
142     Hashtable outer = (Hashtable)sessionList.get(type);
143     if ( outer == null ) {
144        outer = new Hashtable();
145        sessionList.put(type,outer);
146     }
147     HSet inner = (HSet)outer.get(agent);
148     if ( inner == null ) {
149        inner = new HSet();
150        outer.put(agent,inner);
151     }
152     inner.add(sessionId);
153   }
154 
155   public void addDbSessions(String type, Hashtable input) {
156     Hashtable outer = (Hashtable)sessionList.get(type);
157     if ( outer == null ) {
158        outer = new Hashtable();
159        sessionList.put(type,outer);
160     }
161 
162     String agent;
163     HSet input_inner, inner;
164     Enumeration keys = input.keys();
165     while( keys.hasMoreElements() ) {
166        agent = (String)keys.nextElement();
167        input_inner = (HSet)input.get(agent);
168 
169        inner = (HSet)outer.get(agent);
170        if ( inner == null ) {
171           inner = new HSet();
172           outer.put(agent,inner);
173        }
174        inner.add(input_inner);
175     }
176   }
177 
178   public Hashtable getDbSessions(String type) {
179     // return value
180     Hashtable outer = (Hashtable)sessionList.get(type);
181     if ( outer == null ) {
182        outer = new Hashtable();
183        sessionList.put(type,outer);
184     }
185     // Now, make sure all dbProxys have an entry of <type>
186     String[] agent = getDbProxys();
187     for(int i = 0; i < agent.length; i++ )
188        if ( !outer.containsKey(agent[i]) )
189           outer.put(agent[i],new HSet());
190     return outer;
191   }
192 
193   //-------------------------------------------------------
194   // Message management
195 
196   public String getMessageRule(String key, String agent,
197                                Object target, String method) {
198      MessageInfo info = new MessageInfo(key,agent,target,method);
199      return (String)messageRuleDb.get(info);
200   }
201   
202   
203   public String removeMessageRule(String key, String agent,
204                                   Object target, String method) {
205      MessageInfo info = new MessageInfo(key,agent,target,method);
206      return (String)messageRuleDb.remove(info);
207   }
208   
209   
210   public void addMessageRule(String key, String agent,
211                              Object target, String method, String ruleId) {
212      MessageInfo info = new MessageInfo(key,agent,target,method);
213      Core.ERROR(messageRuleDb.put(info,ruleId) == null, 1, this);
214   }
215   
216 
217   public Hashtable removeAllMessageRulesTo(Object target) {
218      String ruleId;
219      MessageInfo info;
220      Hashtable output = new Hashtable();
221      Enumeration keys = messageRuleDb.keys();
222      while( keys.hasMoreElements() ) {
223         info = (MessageInfo)keys.nextElement();
224         if ( info.target == target ) {
225            ruleId = (String)messageRuleDb.remove(info);
226            output.put(info,ruleId);
227         }
228      }
229      return output;
230   }
231 
232 
233 
234   public boolean isAlreadySubscribed(String key, String agent, String ruleId) {
235      SubscriptionInfo info = new SubscriptionInfo(key,agent);
236      Vector List = (Vector)subscriptionDb.get(info);
237      return List != null && List.contains(ruleId);
238   }
239   
240   
241 
242   public void subscribe(String key, String agent, String ruleId) {
243      SubscriptionInfo info = new SubscriptionInfo(key,agent);
244      Vector List = (Vector)subscriptionDb.get(info);
245      if ( List == null ) {
246         List = new Vector();
247         subscriptionDb.put(info,List);
248      }
249      Core.ERROR(!List.contains(ruleId),2,this);
250      List.addElement(ruleId);
251   }
252 
253 
254   public int unsubscribe(String key, String agent, String ruleId) {
255      SubscriptionInfo info = new SubscriptionInfo(key,agent);
256      Vector List = (Vector)subscriptionDb.get(info);
257      Core.ERROR(List,3,this);
258      Core.ERROR(List.removeElement(ruleId),4,this);
259      if ( List.isEmpty() ) {
260         subscriptionDb.remove(info);
261         return CANCEL_SUBSCRIPTION;
262      }
263      return DO_NOTHING;
264   }
265 }