KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > blocks > scalable > webmonitor > WebMonitorEngine


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47
48  /*=========================================================================================
49    For instructions on how to run this sample please refer to the file
50    sample\blocks\scalable\webmonitor\Readme.txt under the MantaRay installation directory.
51 ===========================================================================================*/

52
53
54 package sample.blocks.scalable.webmonitor;
55
56 import java.net.URL JavaDoc;
57 import java.util.HashMap JavaDoc;
58 import java.util.Iterator JavaDoc;
59
60 import org.mr.api.blocks.ScalableDispatcher;
61 import org.mr.api.blocks.ScalableFactory;
62 import org.mr.api.blocks.ScalableHandler;
63 import org.mr.api.blocks.ScalableStage;
64
65 /**
66  * @author Amir Shevat
67  * this is the engine part of the two part, scalable example application
68  */

69 public class WebMonitorEngine extends Thread JavaDoc implements ScalableHandler {
70     /**
71      * Inbound stage this engine gets its URL to check from this stage
72      */

73     public ScalableStage engineStage;
74     /**
75      * Outbound dispatcher this engine returns a result map on this dispatcher
76      */

77     public ScalableDispatcher guiDispatcher;
78     /**
79      * the result map
80      */

81     public HashMap JavaDoc urlsToStatus = new HashMap JavaDoc();
82
83     /**
84      * The engine gets a reference to the incoming stage and the out going dispatcher,
85      * the distributed parameter is passed to the factory of these objects
86      * @param distributed if true then this sample is running in a distributed environment, the engine is in a different VM
87      */

88     public WebMonitorEngine(boolean distributed){
89         //get the inbound stage and register has handler
90
engineStage = ScalableFactory.getStage("engine", distributed);
91         engineStage.addHandler(this);
92         // get the outbound dispatcher
93
guiDispatcher = ScalableFactory.getDispatcher("gui", distributed);
94     }
95
96     /**
97      * This is an implementation method of ScalableHandler interface
98      * Called by the stage when an event (request) was received from the GUI,
99      * adds the URL to the "to checked" map called urlsToStatus
100      */

101     public void handle(Object JavaDoc event) {
102         // the event is a string like "java.sun.com"
103
synchronized(urlsToStatus){
104             String JavaDoc urlStr = (String JavaDoc) event;
105             if(!urlStr.startsWith("HTTP://")&& !urlStr.startsWith("http://")){
106                 urlStr = "http://"+urlStr;
107             }
108             // return a temporary response
109
urlsToStatus.put(urlStr, "Checking");
110             guiDispatcher.dispatch(urlsToStatus);
111         }
112
113     }
114
115     /**
116      * the engine is a thread that runs and checks the URL and creates a report about status changes
117      */

118     public void run(){
119         while(true){
120             // try to sleep
121
try {
122                 sleep(1500);
123             } catch (InterruptedException JavaDoc e1) {
124                 e1.printStackTrace();
125             }
126             HashMap JavaDoc result = new HashMap JavaDoc();
127             // check URLs
128
synchronized(urlsToStatus){
129                 Iterator JavaDoc urlsToCheck =urlsToStatus.keySet().iterator();
130                 while(urlsToCheck.hasNext()){
131                     String JavaDoc urlStr =(String JavaDoc) urlsToCheck.next();
132
133                     String JavaDoc status = "";
134                     try {
135                         URL JavaDoc url = new URL JavaDoc(urlStr);
136                         url.openStream().read();
137                         status= "OK :)";
138                     } catch (Exception JavaDoc e) {
139                         status = "Not responding";
140                     }
141                     // if the status has changed updated in locally and send to GUI
142
if(!urlsToStatus.get(urlStr).equals(status)){
143                         result.put(urlStr, status);
144                         urlsToStatus.put(urlStr, status);
145                     }
146
147                 }// while
148
// only is result is not emply then send it
149
if(!result.isEmpty())
150                     guiDispatcher.dispatch(urlsToStatus);
151             }// while
152
}// run
153

154
155     }
156
157
158     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
159         // this main will only be called if distributed
160
WebMonitorEngine engine = new WebMonitorEngine(true);
161         engine.start();
162
163     }
164
165 }
166
Popular Tags