]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/endpoint/endpointMeter/EndpointServiceMetric.java
720eb008eaca15d7622b7427ca81aff9568030bc
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / endpoint / endpointMeter / EndpointServiceMetric.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
12  *  2. Redistributions in binary form must reproduce the above copyright notice, 
13  *     this list of conditions and the following disclaimer in the documentation 
14  *     and/or other materials provided with the distribution.
15  *  
16  *  3. The end-user documentation included with the redistribution, if any, must 
17  *     include the following acknowledgment: "This product includes software 
18  *     developed by Sun Microsystems, Inc. for JXTA(TM) technology." 
19  *     Alternately, this acknowledgment may appear in the software itself, if 
20  *     and wherever such third-party acknowledgments normally appear.
21  *  
22  *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must 
23  *     not be used to endorse or promote products derived from this software 
24  *     without prior written permission. For written permission, please contact 
25  *     Project JXTA at http://www.jxta.org.
26  *  
27  *  5. Products derived from this software may not be called "JXTA", nor may 
28  *     "JXTA" appear in their name, without prior written permission of Sun.
29  *  
30  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
32  *  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN 
33  *  MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
34  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
35  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
36  *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
37  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
38  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
39  *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
44  *  Please see the license information page at :
45  *  <http://www.jxta.org/project/www/license.html> for instructions on use of 
46  *  the license in source files.
47  *  
48  *  ====================================================================
49  *  
50  *  This software consists of voluntary contributions made by many individuals 
51  *  on behalf of Project JXTA. For more information on Project JXTA, please see 
52  *  http://www.jxta.org.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.endpoint.endpointMeter;
58
59
60 import net.jxta.document.Element;
61 import net.jxta.document.TextElement;
62 import net.jxta.endpoint.EndpointAddress;
63 import net.jxta.id.IDFactory;
64 import net.jxta.meter.MonitorResources;
65 import net.jxta.meter.ServiceMetric;
66 import net.jxta.platform.ModuleClassID;
67 import net.jxta.util.documentSerializable.DocumentSerializableUtilities;
68 import net.jxta.util.documentSerializable.DocumentSerializationException;
69
70 import java.net.URI;
71 import java.net.URISyntaxException;
72 import java.util.Enumeration;
73 import java.util.Iterator;
74 import java.util.LinkedList;
75
76
77 /**
78  * Basic Service Metric EndpointService Monitoring
79  */
80 public class EndpointServiceMetric implements ServiceMetric {
81     private LinkedList<InboundMetric> inboundMetrics = new LinkedList<InboundMetric>();
82     private LinkedList<OutboundMetric> outboundMetrics = new LinkedList<OutboundMetric>();
83     private LinkedList<PropagationMetric> propagationMetrics = new LinkedList<PropagationMetric>();
84     private EndpointMetric endpointMetric;
85     private ModuleClassID moduleClassID = MonitorResources.endpointServiceMonitorClassID;
86
87     public EndpointServiceMetric() {}
88
89     public EndpointServiceMetric(ModuleClassID moduleClassID) {
90         this.moduleClassID = moduleClassID;
91     }
92
93     public void init(ModuleClassID moduleClassID) {
94         this.moduleClassID = moduleClassID;
95     }
96
97     public ModuleClassID getModuleClassID() {
98         return moduleClassID;
99     }
100
101     void addInboundMetric(InboundMetric inboundMetric) {
102         inboundMetrics.add(inboundMetric);
103     }
104
105     public Iterator<InboundMetric> getInboundMetrics() {
106         return inboundMetrics.iterator();
107     }
108
109     public InboundMetric getInboundMetric(String serviceName, String serviceParam) {
110         for (InboundMetric inboundMetric : inboundMetrics) {
111             if (inboundMetric.matches(serviceName, serviceParam)) {
112                 return inboundMetric;
113             }
114         }
115
116         return null;
117     }
118
119     public Iterator<PropagationMetric> getPropagationMetrics() {
120         return propagationMetrics.iterator();
121     }
122
123     public PropagationMetric getPropagationMetric(String serviceName, String serviceParam) {
124         for (PropagationMetric propagationMetric : propagationMetrics) {
125             if (propagationMetric.matches(serviceName, serviceParam)) {
126                 return propagationMetric;
127             }
128         }
129
130         return null;
131     }
132
133     void addPropagationMetric(PropagationMetric propagationMetric) {
134         propagationMetrics.add(propagationMetric);
135     }
136
137     void addOutboundMetric(OutboundMetric outboundMetric) {
138         outboundMetrics.add(outboundMetric);
139     }
140
141     public Iterator<OutboundMetric> getOutboundMetrics() {
142         return outboundMetrics.iterator();
143     }
144
145     public OutboundMetric getOutboundMetric(EndpointAddress endpointAddress) {
146         for (OutboundMetric outboundMetric : outboundMetrics) {
147             if (outboundMetric.matches(endpointAddress)) {
148                 return outboundMetric;
149             }
150         }
151
152         return null;
153     }
154
155     public EndpointMetric getEndpointMetric() {
156         return endpointMetric;
157     }
158
159     void setEndpointMetric(EndpointMetric endpointMetric) {
160         this.endpointMetric = endpointMetric;
161     }
162
163     public void serializeTo(Element element) throws DocumentSerializationException {
164         for (Object outboundMetric1 : outboundMetrics) {
165             OutboundMetric outboundMetric = (OutboundMetric) outboundMetric1;
166
167             DocumentSerializableUtilities.addDocumentSerializable(element, "outboundMetric", outboundMetric);
168         }
169         for (Object inboundMetric1 : inboundMetrics) {
170             InboundMetric inboundMetric = (InboundMetric) inboundMetric1;
171
172             DocumentSerializableUtilities.addDocumentSerializable(element, "inboundMetric", inboundMetric);
173         }
174         for (Object propagationMetric1 : propagationMetrics) {
175             PropagationMetric propagationMetric = (PropagationMetric) propagationMetric1;
176
177             DocumentSerializableUtilities.addDocumentSerializable(element, "propagationMetric", propagationMetric);
178         }
179         if (endpointMetric != null) {
180             DocumentSerializableUtilities.addDocumentSerializable(element, "endpointMetric", endpointMetric);
181         }
182
183         if (moduleClassID != null) {
184             DocumentSerializableUtilities.addString(element, "moduleClassID", moduleClassID.toString());
185         }
186     }
187
188     public void initializeFrom(Element element) throws DocumentSerializationException {
189         for (Enumeration e = element.getChildren(); e.hasMoreElements();) {
190             Element childElement = (TextElement) e.nextElement();
191             String tagName = (String) childElement.getKey();
192
193             if (tagName.equals("inboundMetric")) {
194                 InboundMetric inboundMetric = (InboundMetric) DocumentSerializableUtilities.getDocumentSerializable(childElement
195                         ,
196                         InboundMetric.class);
197
198                 inboundMetrics.add(inboundMetric);
199             }
200             if (tagName.equals("outboundMetric")) {
201                 OutboundMetric outboundMetric = (OutboundMetric) DocumentSerializableUtilities.getDocumentSerializable(
202                         childElement, OutboundMetric.class);
203
204                 outboundMetrics.add(outboundMetric);
205             }
206             if (tagName.equals("propagationMetric")) {
207                 PropagationMetric propagationMetric = (PropagationMetric) DocumentSerializableUtilities.getDocumentSerializable(
208                         childElement, PropagationMetric.class);
209
210                 propagationMetrics.add(propagationMetric);
211             }
212             if (tagName.equals("endpointMetric")) {
213                 endpointMetric = (EndpointMetric) DocumentSerializableUtilities.getDocumentSerializable(childElement
214                         ,
215                         EndpointMetric.class);
216             }
217             try {
218                 if (tagName.equals("moduleClassID")) {
219                     moduleClassID = (ModuleClassID) IDFactory.fromURI(
220                             new URI(DocumentSerializableUtilities.getString(childElement)));
221                 }
222             } catch (URISyntaxException jex) {
223                 throw new DocumentSerializationException("Can't decipher ModuleClassID", jex);
224             }
225         }
226     }
227
228     public void mergeMetrics(ServiceMetric otherOne) {
229         mergeMetrics(otherOne, true, true, true, true);
230     }
231
232     /**
233      * Make a deep copy of this metric only including the portions designated in the Filter
234      * The resulting metric is Safe to modify without danger to the underlying Monitor Metrics
235      *
236      * @param endpointServiceMonitorFilter Filter designates constituant parts to be included
237      * @return a copy of this metric with references to the designated parts
238      */
239     public EndpointServiceMetric deepCopy(EndpointServiceMonitorFilter endpointServiceMonitorFilter) {
240         EndpointServiceMetric serviceMetric = new EndpointServiceMetric();
241
242         serviceMetric.moduleClassID = moduleClassID;
243
244         serviceMetric.mergeMetrics(this, true, endpointServiceMonitorFilter.isIncludeInboundMetrics()
245                 ,
246                 endpointServiceMonitorFilter.isIncludeOutboundMetrics(), endpointServiceMonitorFilter.isIncludePropagateMetrics());
247         return serviceMetric;
248     }
249
250     public void mergeMetrics(ServiceMetric otherOne, boolean includeEndpointMetrics, boolean includeInboundMetrics, boolean includeOutboundEndpointMetrics, boolean includePropagationMetrics) {
251         EndpointServiceMetric otherEndpointServiceMetric = (EndpointServiceMetric) otherOne;
252
253         if (includeEndpointMetrics) {
254             EndpointMetric otherEndpointMetric = otherEndpointServiceMetric.getEndpointMetric();
255
256             if ((endpointMetric == null) && (otherEndpointMetric != null)) {
257                 endpointMetric = new EndpointMetric(otherEndpointMetric);
258             }
259
260             if (otherEndpointMetric != null) {
261                 endpointMetric.mergeMetrics(otherEndpointMetric);
262             }
263         }
264
265         if (includeInboundMetrics) {
266             for (Iterator<InboundMetric> i = otherEndpointServiceMetric.getInboundMetrics(); i.hasNext();) {
267                 InboundMetric otherInboundMetric = i.next();
268                 InboundMetric inboundMetric = getInboundMetric(otherInboundMetric.getServiceName()
269                         ,
270                         otherInboundMetric.getServiceParameter());
271
272                 if (inboundMetric == null) {
273                     inboundMetric = new InboundMetric(otherInboundMetric);
274                     addInboundMetric(inboundMetric);
275                 }
276
277                 inboundMetric.mergeMetrics(otherInboundMetric);
278             }
279         }
280
281         if (includeOutboundEndpointMetrics) {
282             for (Iterator<OutboundMetric> i = otherEndpointServiceMetric.getOutboundMetrics(); i.hasNext();) {
283                 OutboundMetric otherOutboundMetric = i.next();
284                 OutboundMetric outboundMetric = getOutboundMetric(otherOutboundMetric.getEndpointAddress());
285
286                 if (outboundMetric == null) {
287                     outboundMetric = new OutboundMetric(otherOutboundMetric);
288                     addOutboundMetric(outboundMetric);
289                 }
290
291                 outboundMetric.mergeMetrics(otherOutboundMetric);
292             }
293         }
294
295         if (includeOutboundEndpointMetrics) {
296             for (Iterator<PropagationMetric> i = otherEndpointServiceMetric.getPropagationMetrics(); i.hasNext();) {
297                 PropagationMetric otherPropagationMetric = i.next();
298                 PropagationMetric propagationMetric = getPropagationMetric(otherPropagationMetric.getServiceName()
299                         ,
300                         otherPropagationMetric.getServiceParameter());
301
302                 if (propagationMetric == null) {
303                     propagationMetric = new PropagationMetric(otherPropagationMetric);
304                     addPropagationMetric(propagationMetric);
305                 }
306
307                 propagationMetric.mergeMetrics(otherPropagationMetric);
308             }
309         }
310     }
311
312     /**
313      * Make a shallow copy of this metric only including the portions designated in the Filter
314      * <P> Note: since this is a shallow copy it is dangerous to modify the submetrics
315      *
316      * @param endpointServiceMonitorFilter Filter designates constituant parts to be included
317      * @return a copy of this metric with references to the designated parts
318      */
319     public EndpointServiceMetric shallowCopy(EndpointServiceMonitorFilter endpointServiceMonitorFilter) {
320         EndpointServiceMetric endpointServiceMetric = new EndpointServiceMetric(moduleClassID);
321
322         endpointServiceMetric.endpointMetric = endpointMetric;
323
324         if (endpointServiceMonitorFilter.isIncludeInboundMetrics()) {
325             for (Iterator<InboundMetric> i = getInboundMetrics(); i.hasNext();) {
326                 InboundMetric inboundMetric = i.next();
327
328                 endpointServiceMetric.addInboundMetric(inboundMetric);
329             }
330         }
331
332         if (endpointServiceMonitorFilter.isIncludeOutboundMetrics()) {
333             for (Iterator<OutboundMetric> i = getOutboundMetrics(); i.hasNext();) {
334                 OutboundMetric outboundMetric = i.next();
335
336                 endpointServiceMetric.addOutboundMetric(outboundMetric);
337             }
338         }
339
340         if (endpointServiceMonitorFilter.isIncludePropagateMetrics()) {
341             for (Iterator<PropagationMetric> i = getPropagationMetrics(); i.hasNext();) {
342                 PropagationMetric propagationMetric = i.next();
343
344                 endpointServiceMetric.addPropagationMetric(propagationMetric);
345             }
346         }
347
348         return endpointServiceMetric;
349     }
350
351     public void diffMetrics(ServiceMetric otherOne) {
352         throw new RuntimeException("Not Supported");
353     }
354
355     @Override
356     public Object clone() throws CloneNotSupportedException {
357         return super.clone();
358     }
359 }