KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > petalsadmin > bean > MonitorBean


1 package org.objectweb.petals.tools.petalsadmin.bean;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.ListIterator JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Random JavaDoc;
10
11 import org.objectweb.petals.tools.petalsadmin.utils.JBIEntity;
12 import org.objectweb.petals.tools.petalsadmin.utils.JMXUtils;
13
14 public class MonitorBean {
15     
16     private String JavaDoc serverHost;
17     private String JavaDoc jmxPort;
18     private String JavaDoc jmxUser;
19     private String JavaDoc jmxPasswd;
20     
21     public MonitorBean() {
22     }
23     
24     public List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> bulkData(Map JavaDoc<String JavaDoc, String JavaDoc> serverConf, long sinceTime)
25         throws Exception JavaDoc
26     {
27         try {
28             JMXUtils utilsJmx = new JMXUtils(serverHost,jmxPort,jmxUser,jmxPasswd);
29             return utilsJmx.bulkData(serverConf,sinceTime);
30         }
31         catch (Exception JavaDoc e) {
32             throw new Exception JavaDoc(e);
33         }
34     }
35     
36     /*
37      * List<Map<String,Object>> : liste des captures d'un echange
38      * List<List<Map<String,Object>>> : liste des listes de captures
39      */

40     public List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> getExchanges(Map JavaDoc<String JavaDoc, String JavaDoc> serverConf, long sinceTime, String JavaDoc component)
41         throws Exception JavaDoc
42     {
43         //recuperation des echanges
44
List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc, Object JavaDoc>>> bulkDatas;
45         try {
46             bulkDatas = this.bulkData(serverConf, sinceTime);
47             //creation de la liste des echanges a retourner
48
List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> componentExchanges = new ArrayList JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>>();
49         
50             //parcours des echanges et tri
51
ListIterator JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> parcExchanges = bulkDatas.listIterator();
52             while(parcExchanges.hasNext())
53             {
54                 //booleen pour indiquer que l'echange correspond
55
boolean concernComponent=false;
56                 //recuperation de la liste des captures de l'echange
57
List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>> currentExchange = parcExchanges.next();
58                 //parcours des captures pour trouver celles qui correspondent au composant demande
59
ListIterator JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>> parcCaptures = currentExchange.listIterator();
60                 while (parcCaptures.hasNext())
61                 {
62                     //recuperation de la capture en cours
63
Map JavaDoc<String JavaDoc,Object JavaDoc> capture = parcCaptures.next();
64                     if (capture.get("component").equals(component) && !concernComponent)
65                     {
66                         //la capture correspond au composant demande
67
//->on ajoute l'echange a la liste a retourner
68
concernComponent=true;
69                     }
70                 }
71                 //une capture contenue par le composant concerne le composant demande
72
//-> ajout du composant dans la liste a retourner
73
if (concernComponent)
74                 {
75                     componentExchanges.add(currentExchange);
76                 }
77             }
78             return componentExchanges;
79         } catch (Exception JavaDoc e) {
80             throw new Exception JavaDoc(e);
81         }
82         
83     }
84
85     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getStartedServers() throws Exception JavaDoc
86     {
87         try {
88             JMXUtils utilsJmx = new JMXUtils(serverHost,jmxPort,jmxUser,jmxPasswd);
89             return utilsJmx.getStartedServers();
90         }
91         catch (Exception JavaDoc e) {
92             throw new Exception JavaDoc(e);
93         }
94     }
95     
96     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getAllServers() throws Exception JavaDoc
97     {
98         try {
99             JMXUtils utilsJmx = new JMXUtils(serverHost,jmxPort,jmxUser,jmxPasswd);
100             return utilsJmx.getAllServers();
101         }
102         catch (Exception JavaDoc e) {
103             throw new Exception JavaDoc(e);
104         }
105     }
106     
107     public Map JavaDoc<String JavaDoc, JBIEntity> listComponents(String JavaDoc phost, String JavaDoc pport, String JavaDoc pusername, String JavaDoc ppassword)
108     throws Exception JavaDoc {
109         try {
110             JMXUtils utilsJmx = new JMXUtils(phost,pport,pusername,ppassword);
111             return utilsJmx.listComponents(phost, pport, pusername, ppassword);
112         }
113         catch (Exception JavaDoc e) {
114             throw new Exception JavaDoc(e);
115         }
116     }
117     
118     public boolean monitor(String JavaDoc phost, String JavaDoc pport, String JavaDoc pusername, String JavaDoc ppassword, String JavaDoc action)
119     throws Exception JavaDoc {
120         try {
121             JMXUtils utilsJmx = new JMXUtils(phost,pport,pusername,ppassword);
122             boolean boolAction;
123             if (action.equals("start"))
124                 boolAction=true;
125             else
126                 boolAction=false;
127             return utilsJmx.monitor(phost,pport,pusername,ppassword,boolAction);
128         }
129         catch (Exception JavaDoc e) {
130             throw new Exception JavaDoc(e);
131         }
132     }
133     
134     public boolean monitorAllServers(List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> startedServers, String JavaDoc action)
135     throws Exception JavaDoc {
136         try {
137             JMXUtils utilsJmx = new JMXUtils(serverHost,jmxPort,jmxUser,jmxPasswd);
138             boolean boolAction;
139             boolean result=true;
140             if (action.equals("start"))
141                 boolAction=true;
142             else
143                 boolAction=false;
144             if (startedServers!=null && startedServers.size()>0)
145             {
146                 ListIterator JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> parcServers = startedServers.listIterator();
147                 while (parcServers.hasNext())
148                 {
149                     Map JavaDoc<String JavaDoc, String JavaDoc> currentServer = parcServers.next();
150                     String JavaDoc serverHost = currentServer.get("host");
151                     String JavaDoc serverPort = currentServer.get("jmxPort");
152                     String JavaDoc serverLogin = currentServer.get("jmxLogin");
153                     String JavaDoc serverPasswd = currentServer.get("jmxPassword");
154                     utilsJmx.monitor(serverHost,serverPort,serverLogin,serverPasswd,boolAction);
155                 }
156             }
157             return result;
158         }
159         catch (Exception JavaDoc e) {
160             return false;
161         }
162     }
163     
164     public String JavaDoc getAttribute(Map JavaDoc<String JavaDoc, String JavaDoc> serverConf, String JavaDoc attributeName)
165     throws Exception JavaDoc {
166         try {
167             String JavaDoc serverHost = serverConf.get("host");
168             String JavaDoc serverPort = serverConf.get("jmxPort");
169             String JavaDoc serverLogin = serverConf.get("jmxLogin");
170             String JavaDoc serverPasswd = serverConf.get("jmxPassword");
171             JMXUtils utilsJmx = new JMXUtils(serverHost,serverPort,serverLogin,serverPasswd);
172             return utilsJmx.getAttribute(serverHost,serverPort,serverLogin,serverPasswd,attributeName);
173         }
174         catch (Exception JavaDoc e) {
175             throw new Exception JavaDoc(e);
176         }
177     }
178     
179     
180     /**************************************************************************/
181     /** FAUSSES FONCTIONS **/
182     /**************************************************************************/
183     
184     /**
185      * return a set of exchanges
186      * @param sinceTime : time of first exchange to return
187      * @return ArrayList<HashMap> set of HashMap
188      * each HashMap is a <id,datas> representation of an exchange
189      * datas are <id, value> HashMap too : time, compo, role , type, status
190      */

191     public List JavaDoc<Map.Entry JavaDoc<String JavaDoc, List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>>> oldbulkData(long sinceTime)
192     {
193         List JavaDoc<Map.Entry JavaDoc<String JavaDoc, List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>>> list_exchanges = new ArrayList JavaDoc<Map.Entry JavaDoc<String JavaDoc, List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>>>();
194         Random JavaDoc rd = new Random JavaDoc();
195
196         //recuperation de la liste des composants
197
ArrayList JavaDoc<String JavaDoc> compo_list = this.getComponents("all");
198
199         //creation d'un echange
200
for (int i=1;i<=rd.nextInt(50);i++)
201         {
202             Map JavaDoc<String JavaDoc, List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> exchange = new HashMap JavaDoc<String JavaDoc, List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>>();
203
204             String JavaDoc id = new String JavaDoc("ex_"+Math.abs(rd.nextLong())); //id
205

206             //ensemble des captures d'un echange
207
List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>> captureList = new ArrayList JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>();
208             //objet capture reutilisable
209
Map JavaDoc<String JavaDoc,Object JavaDoc> capture = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
210             //creation de 4 captures par echange
211
for (int j=1;j<=4;j++)
212             {
213                 capture = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
214                 
215                 Long JavaDoc t1 = new Date JavaDoc().getTime();
216                 
217                 //selection d'un composant au hasard
218
String JavaDoc component = new String JavaDoc(compo_list.get(rd.nextInt(compo_list.size())));
219
220                 //selection d'un role au hasard
221
String JavaDoc[] roles_list = new String JavaDoc[2];
222                 roles_list[0]="consumer";
223                 roles_list[1]="provider";
224                 String JavaDoc role = roles_list[rd.nextInt(2)];
225                 
226                 //selection d'un type au hasard
227
String JavaDoc[] types_list = new String JavaDoc[4];
228                 types_list[0]="in";
229                 types_list[1]="out";
230                 types_list[2]="fault";
231                 types_list[3]="ANY";
232                 String JavaDoc type=types_list[rd.nextInt(4)];
233                 
234                 //selection d'un statut au hasard
235
String JavaDoc[] status_list= new String JavaDoc[3];
236                 status_list[0]="active";
237                 status_list[1]="done";
238                 status_list[2]="error";
239                 String JavaDoc status=status_list[rd.nextInt(3)];
240                 
241                 //ajout des donnees dans la capture
242
capture.put("time", t1);
243                 capture.put("component",component);
244                 capture.put("role",role);
245                 capture.put("type", type);
246                 capture.put("status", status);
247                 
248                 //ajout de la capture dans la liste des captures
249
captureList.add(capture);
250             }
251             
252             //ajout de l'id et de la liste des captures a l'echange
253
exchange.put(id, captureList);
254             
255             //ajout de l'echange dans la liste d'echanges
256
list_exchanges.add(exchange.entrySet().iterator().next());
257         }
258         return list_exchanges;
259     }
260
261     /**
262      *
263      * @param group
264      * @return
265      */

266     public ArrayList JavaDoc<String JavaDoc> getComponents(String JavaDoc group) {
267         ArrayList JavaDoc<String JavaDoc> components = new ArrayList JavaDoc<String JavaDoc>();
268         if (group=="BC")
269         {
270             components.add("FileTransfer");
271             components.add("JMS");
272             components.add("XQuare");
273         }
274         else
275         {
276             if (group=="SE")
277             {
278                 components.add("Clock");
279                 components.add("CSV");
280                 components.add("HelloWorld");
281                 components.add("SampleClient");
282                 components.add("XSLT");
283             }
284             else
285             {
286                 if (group=="all")
287                 {
288                     components.add("FileTransfer");
289                     components.add("JMS");
290                     components.add("XQuare");
291                     components.add("Clock");
292                     components.add("CSV");
293                     components.add("HelloWorld");
294                     components.add("SampleClient");
295                     components.add("XSLT");
296                 }
297                 else
298                 {
299                     components.add("none");
300                 }
301             }
302         }
303         return components;
304     }
305
306     public String JavaDoc getJmxPasswd() {
307         return jmxPasswd;
308     }
309
310     public void setJmxPasswd(String JavaDoc jmxPasswd) {
311         this.jmxPasswd = jmxPasswd;
312     }
313
314     public String JavaDoc getJmxPort() {
315         return jmxPort;
316     }
317
318     public void setJmxPort(String JavaDoc jmxPort) {
319         this.jmxPort = jmxPort;
320     }
321
322     public String JavaDoc getJmxUser() {
323         return jmxUser;
324     }
325
326     public void setJmxUser(String JavaDoc jmxUser) {
327         this.jmxUser = jmxUser;
328     }
329
330     public String JavaDoc getServerHost() {
331         return serverHost;
332     }
333
334     public void setServerHost(String JavaDoc serverHost) {
335         this.serverHost = serverHost;
336     }
337     
338 }
339
Popular Tags