KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > monitoring > RouterMonitorImpl


1 /*
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2007 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Adrien LOUIS
21  * --------------------------------------------------------------------------
22  * $Id: R
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.monitoring;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
35 import org.objectweb.fractal.fraclet.annotation.Interface;
36 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
37 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
38 import org.objectweb.fractal.fraclet.annotation.Monolog;
39 import org.objectweb.fractal.fraclet.annotation.Provides;
40 import org.objectweb.fractal.fraclet.annotation.Requires;
41 import org.objectweb.util.monolog.api.Logger;
42
43 import org.objectweb.petals.jbi.component.context.ComponentContextImpl;
44 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl;
45 import org.objectweb.petals.jbi.routing.Router;
46 import org.objectweb.petals.jbi.routing.RoutingException;
47 import org.objectweb.petals.util.LoggingUtil;
48
49 /**
50  * @version $Rev: 617 $ $Date: 2006-06-19 17:28:41 +0200 (lun, 19 jun 2006) $
51  * @since Petals 1.1
52  * @author alouis
53  */

54 @FractalComponent
55 @Provides(interfaces = {
56     @Interface(name = "service", signature = org.objectweb.petals.jbi.routing.Router.class),
57     @Interface(name = "monitoring-service", signature = org.objectweb.petals.monitoring.RouterMonitor.class)})
58 public class RouterMonitorImpl implements Router, RouterMonitor {
59
60     private boolean monitoring = false;
61
62     @Requires(name = "router", signature = org.objectweb.petals.jbi.routing.Router.class)
63     private Router router;
64
65     @Monolog(name="logger")
66     protected Logger logger;
67     
68     protected LoggingUtil log;
69     
70     private HashMap JavaDoc<String JavaDoc, ExchangeStateReport> exchangeReports = new HashMap JavaDoc<String JavaDoc, ExchangeStateReport>();
71
72     private TimeComparator timeComparator = new TimeComparator();
73
74     ////////////////////////////////////////////
75
// Router implementation
76
////////////////////////////////////////////
77

78     public void send(ComponentContextImpl source, MessageExchangeImpl exchange,
79         long timeOut) throws RoutingException {
80
81         if (monitoring) {
82             report(exchange);
83         }
84
85         router.send(source, exchange, timeOut);
86     }
87
88     public void receive(MessageExchangeImpl exchange) throws RoutingException {
89
90         if (monitoring) {
91             report(exchange);
92         }
93
94         router.receive(exchange);
95     }
96
97     private void report(MessageExchangeImpl exchange) {
98         ExchangeStateReport report = exchangeReports.get(exchange
99             .getExchangeId());
100         if (report == null) {
101             report = new ExchangeStateReport(exchange);
102             exchangeReports.put(exchange.getExchangeId(), report);
103         }
104         report.reportState(exchange);
105
106     }
107
108     ////////////////////////////////////////////
109
// Monitor implementation
110
////////////////////////////////////////////
111

112     public boolean isMonitoring() {
113         return monitoring;
114     }
115
116     /**
117      * if set to false, the reports are reset.
118      */

119     public void setMonitoring(boolean monitoring) {
120         this.monitoring = monitoring;
121         if (! monitoring) {
122             exchangeReports = new HashMap JavaDoc<String JavaDoc, ExchangeStateReport>();
123         }
124     }
125     
126     
127     /**
128      * Return the reports for all exchange since the specified date, sorted by
129      * date
130      *
131      * @param sinceDate
132      * date (in 'time' format)
133      * @return a map of reports. not null, may be empty
134      */

135     public List JavaDoc<ExchangeStateReport> getReports(long sinceDate) {
136         log.start();
137         // 1. get reports since the specified date
138
List JavaDoc<ExchangeStateReport> list = new ArrayList JavaDoc<ExchangeStateReport>();
139
140         for (ExchangeStateReport report : exchangeReports.values()) {
141             if (report.getReportList().size() == 0
142                 || report.getReportList().get(0).time >= sinceDate) {
143                 list.add(report);
144             }
145         }
146         // 2. sort the list by date
147
Collections.sort(list, timeComparator);
148
149         // 3. return
150
log.end();
151         return list;
152     }
153
154     class TimeComparator implements Comparator JavaDoc<ExchangeStateReport> {
155
156         /**
157          * Compare the two exchange-reports by comparing the date of their
158          * respective first state-capture.
159          */

160         public int compare(ExchangeStateReport o1, ExchangeStateReport o2) {
161             List JavaDoc<ExchangeStateCapture> l1 = o1.getReportList();
162             List JavaDoc<ExchangeStateCapture> l2 = o2.getReportList();
163
164             int comp = 0;
165             if (l1.size() == 0 || l2.size() == 0) {
166                 // one of the exchange has no capture, so it is more recent than
167
// the other exchange
168
comp = l1.size() - l2.size();
169             } else {
170                 comp = (int) (l2.get(0).time - l1.get(0).time);
171             }
172
173             return comp;
174         }
175
176     }
177     
178     
179     
180 // ----------------------------------------------------------------------
181
// Fractal LifecycleController implementation
182
// ----------------------------------------------------------------------
183

184     @LifeCycle(on=LifeCycleType.START)
185     protected void start() {
186         log = new LoggingUtil(logger);
187     }
188 }
189
Popular Tags