2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
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.
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.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
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.
48 * ====================================================================
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.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.impl.endpoint.endpointMeter;
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;
71 import java.net.URISyntaxException;
72 import java.util.Enumeration;
73 import java.util.Iterator;
74 import java.util.LinkedList;
78 * Basic Service Metric EndpointService Monitoring
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;
87 public EndpointServiceMetric() {}
89 public EndpointServiceMetric(ModuleClassID moduleClassID) {
90 this.moduleClassID = moduleClassID;
93 public void init(ModuleClassID moduleClassID) {
94 this.moduleClassID = moduleClassID;
97 public ModuleClassID getModuleClassID() {
101 void addInboundMetric(InboundMetric inboundMetric) {
102 inboundMetrics.add(inboundMetric);
105 public Iterator<InboundMetric> getInboundMetrics() {
106 return inboundMetrics.iterator();
109 public InboundMetric getInboundMetric(String serviceName, String serviceParam) {
110 for (InboundMetric inboundMetric : inboundMetrics) {
111 if (inboundMetric.matches(serviceName, serviceParam)) {
112 return inboundMetric;
119 public Iterator<PropagationMetric> getPropagationMetrics() {
120 return propagationMetrics.iterator();
123 public PropagationMetric getPropagationMetric(String serviceName, String serviceParam) {
124 for (PropagationMetric propagationMetric : propagationMetrics) {
125 if (propagationMetric.matches(serviceName, serviceParam)) {
126 return propagationMetric;
133 void addPropagationMetric(PropagationMetric propagationMetric) {
134 propagationMetrics.add(propagationMetric);
137 void addOutboundMetric(OutboundMetric outboundMetric) {
138 outboundMetrics.add(outboundMetric);
141 public Iterator<OutboundMetric> getOutboundMetrics() {
142 return outboundMetrics.iterator();
145 public OutboundMetric getOutboundMetric(EndpointAddress endpointAddress) {
146 for (OutboundMetric outboundMetric : outboundMetrics) {
147 if (outboundMetric.matches(endpointAddress)) {
148 return outboundMetric;
155 public EndpointMetric getEndpointMetric() {
156 return endpointMetric;
159 void setEndpointMetric(EndpointMetric endpointMetric) {
160 this.endpointMetric = endpointMetric;
163 public void serializeTo(Element element) throws DocumentSerializationException {
164 for (Object outboundMetric1 : outboundMetrics) {
165 OutboundMetric outboundMetric = (OutboundMetric) outboundMetric1;
167 DocumentSerializableUtilities.addDocumentSerializable(element, "outboundMetric", outboundMetric);
169 for (Object inboundMetric1 : inboundMetrics) {
170 InboundMetric inboundMetric = (InboundMetric) inboundMetric1;
172 DocumentSerializableUtilities.addDocumentSerializable(element, "inboundMetric", inboundMetric);
174 for (Object propagationMetric1 : propagationMetrics) {
175 PropagationMetric propagationMetric = (PropagationMetric) propagationMetric1;
177 DocumentSerializableUtilities.addDocumentSerializable(element, "propagationMetric", propagationMetric);
179 if (endpointMetric != null) {
180 DocumentSerializableUtilities.addDocumentSerializable(element, "endpointMetric", endpointMetric);
183 if (moduleClassID != null) {
184 DocumentSerializableUtilities.addString(element, "moduleClassID", moduleClassID.toString());
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();
193 if (tagName.equals("inboundMetric")) {
194 InboundMetric inboundMetric = (InboundMetric) DocumentSerializableUtilities.getDocumentSerializable(childElement
196 InboundMetric.class);
198 inboundMetrics.add(inboundMetric);
200 if (tagName.equals("outboundMetric")) {
201 OutboundMetric outboundMetric = (OutboundMetric) DocumentSerializableUtilities.getDocumentSerializable(
202 childElement, OutboundMetric.class);
204 outboundMetrics.add(outboundMetric);
206 if (tagName.equals("propagationMetric")) {
207 PropagationMetric propagationMetric = (PropagationMetric) DocumentSerializableUtilities.getDocumentSerializable(
208 childElement, PropagationMetric.class);
210 propagationMetrics.add(propagationMetric);
212 if (tagName.equals("endpointMetric")) {
213 endpointMetric = (EndpointMetric) DocumentSerializableUtilities.getDocumentSerializable(childElement
215 EndpointMetric.class);
218 if (tagName.equals("moduleClassID")) {
219 moduleClassID = (ModuleClassID) IDFactory.fromURI(
220 new URI(DocumentSerializableUtilities.getString(childElement)));
222 } catch (URISyntaxException jex) {
223 throw new DocumentSerializationException("Can't decipher ModuleClassID", jex);
228 public void mergeMetrics(ServiceMetric otherOne) {
229 mergeMetrics(otherOne, true, true, true, true);
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
236 * @param endpointServiceMonitorFilter Filter designates constituant parts to be included
237 * @return a copy of this metric with references to the designated parts
239 public EndpointServiceMetric deepCopy(EndpointServiceMonitorFilter endpointServiceMonitorFilter) {
240 EndpointServiceMetric serviceMetric = new EndpointServiceMetric();
242 serviceMetric.moduleClassID = moduleClassID;
244 serviceMetric.mergeMetrics(this, true, endpointServiceMonitorFilter.isIncludeInboundMetrics()
246 endpointServiceMonitorFilter.isIncludeOutboundMetrics(), endpointServiceMonitorFilter.isIncludePropagateMetrics());
247 return serviceMetric;
250 public void mergeMetrics(ServiceMetric otherOne, boolean includeEndpointMetrics, boolean includeInboundMetrics, boolean includeOutboundEndpointMetrics, boolean includePropagationMetrics) {
251 EndpointServiceMetric otherEndpointServiceMetric = (EndpointServiceMetric) otherOne;
253 if (includeEndpointMetrics) {
254 EndpointMetric otherEndpointMetric = otherEndpointServiceMetric.getEndpointMetric();
256 if ((endpointMetric == null) && (otherEndpointMetric != null)) {
257 endpointMetric = new EndpointMetric(otherEndpointMetric);
260 if (otherEndpointMetric != null) {
261 endpointMetric.mergeMetrics(otherEndpointMetric);
265 if (includeInboundMetrics) {
266 for (Iterator<InboundMetric> i = otherEndpointServiceMetric.getInboundMetrics(); i.hasNext();) {
267 InboundMetric otherInboundMetric = i.next();
268 InboundMetric inboundMetric = getInboundMetric(otherInboundMetric.getServiceName()
270 otherInboundMetric.getServiceParameter());
272 if (inboundMetric == null) {
273 inboundMetric = new InboundMetric(otherInboundMetric);
274 addInboundMetric(inboundMetric);
277 inboundMetric.mergeMetrics(otherInboundMetric);
281 if (includeOutboundEndpointMetrics) {
282 for (Iterator<OutboundMetric> i = otherEndpointServiceMetric.getOutboundMetrics(); i.hasNext();) {
283 OutboundMetric otherOutboundMetric = i.next();
284 OutboundMetric outboundMetric = getOutboundMetric(otherOutboundMetric.getEndpointAddress());
286 if (outboundMetric == null) {
287 outboundMetric = new OutboundMetric(otherOutboundMetric);
288 addOutboundMetric(outboundMetric);
291 outboundMetric.mergeMetrics(otherOutboundMetric);
295 if (includeOutboundEndpointMetrics) {
296 for (Iterator<PropagationMetric> i = otherEndpointServiceMetric.getPropagationMetrics(); i.hasNext();) {
297 PropagationMetric otherPropagationMetric = i.next();
298 PropagationMetric propagationMetric = getPropagationMetric(otherPropagationMetric.getServiceName()
300 otherPropagationMetric.getServiceParameter());
302 if (propagationMetric == null) {
303 propagationMetric = new PropagationMetric(otherPropagationMetric);
304 addPropagationMetric(propagationMetric);
307 propagationMetric.mergeMetrics(otherPropagationMetric);
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
316 * @param endpointServiceMonitorFilter Filter designates constituant parts to be included
317 * @return a copy of this metric with references to the designated parts
319 public EndpointServiceMetric shallowCopy(EndpointServiceMonitorFilter endpointServiceMonitorFilter) {
320 EndpointServiceMetric endpointServiceMetric = new EndpointServiceMetric(moduleClassID);
322 endpointServiceMetric.endpointMetric = endpointMetric;
324 if (endpointServiceMonitorFilter.isIncludeInboundMetrics()) {
325 for (Iterator<InboundMetric> i = getInboundMetrics(); i.hasNext();) {
326 InboundMetric inboundMetric = i.next();
328 endpointServiceMetric.addInboundMetric(inboundMetric);
332 if (endpointServiceMonitorFilter.isIncludeOutboundMetrics()) {
333 for (Iterator<OutboundMetric> i = getOutboundMetrics(); i.hasNext();) {
334 OutboundMetric outboundMetric = i.next();
336 endpointServiceMetric.addOutboundMetric(outboundMetric);
340 if (endpointServiceMonitorFilter.isIncludePropagateMetrics()) {
341 for (Iterator<PropagationMetric> i = getPropagationMetrics(); i.hasNext();) {
342 PropagationMetric propagationMetric = i.next();
344 endpointServiceMetric.addPropagationMetric(propagationMetric);
348 return endpointServiceMetric;
351 public void diffMetrics(ServiceMetric otherOne) {
352 throw new RuntimeException("Not Supported");
356 public Object clone() throws CloneNotSupportedException {
357 return super.clone();