KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > FilteredServiceListener


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.core;
13
14 import org.eclipse.osgi.framework.debug.Debug;
15 import org.osgi.framework.*;
16
17 public class FilteredServiceListener implements ServiceListener {
18     /** Filter for listener. */
19     private final FilterImpl filter;
20     /** Real listener. */
21     private final ServiceListener listener;
22     // The bundle context
23
private final BundleContextImpl context;
24     // is this an AllServiceListener
25
private final boolean allservices;
26     // an objectClass required by the filter
27
private final String JavaDoc objectClass;
28
29     /**
30      * Constructor.
31      *
32      * @param filterstring filter for this listener.
33      * @param listener real listener.
34      * @exception InvalidSyntaxException if the filter is invalid.
35      */

36     protected FilteredServiceListener(String JavaDoc filterstring, ServiceListener listener, BundleContextImpl context) throws InvalidSyntaxException {
37         if (filterstring == null) {
38             this.filter = null;
39             this.objectClass = null;
40         }
41         else {
42             FilterImpl filterImpl = new FilterImpl(filterstring);
43             String JavaDoc clazz = filterImpl.getRequiredObjectClass();
44             if (clazz == null) {
45                 this.objectClass = null;
46                 this.filter = filterImpl;
47             }
48             else {
49                 this.objectClass = clazz.intern(); /*intern the name for future identity comparison */
50                 String JavaDoc objectClassFilter = FilterImpl.getObjectClassFilterString(this.objectClass);
51                 this.filter = (objectClassFilter.equals(filterstring)) ? null : filterImpl;
52             }
53         }
54         this.listener = listener;
55         this.context = context;
56         this.allservices = (listener instanceof AllServiceListener);
57     }
58
59     /**
60      * Receive notification that a service has had a
61      * change occur in it's lifecycle.
62      *
63      * @param event The ServiceEvent.
64      */

65     public void serviceChanged(ServiceEvent event) {
66         ServiceReferenceImpl reference = (ServiceReferenceImpl) event.getServiceReference();
67
68         // first check if we can short circuit the filter match if the required objectClass does not match the event
69
objectClassCheck:
70         if (objectClass != null) {
71             String JavaDoc[] classes = reference.getClasses();
72             int size = classes.length;
73             for (int i = 0; i < size; i++) {
74                 if (classes[i] == objectClass) // objectClass strings have previously been interned for identity comparison
75
break objectClassCheck;
76             }
77             return; // no class in this event matches a required part of the filter; we do not need to deliver this event
78
}
79
80         if (!context.hasListenServicePermission(event))
81             return;
82
83         if (Debug.DEBUG && Debug.DEBUG_EVENTS) {
84             String JavaDoc listenerName = this.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(this)); //$NON-NLS-1$
85
Debug.println("filterServiceEvent(" + listenerName + ", \"" + filter + "\", " + reference.registration.properties + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
86
}
87         
88         if ((filter == null || filter.match(reference)) && (allservices || context.isAssignableTo(reference))) {
89             if (Debug.DEBUG && Debug.DEBUG_EVENTS) {
90                 String JavaDoc listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); //$NON-NLS-1$
91
Debug.println("dispatchFilteredServiceEvent(" + listenerName + ")"); //$NON-NLS-1$ //$NON-NLS-2$
92
}
93
94             listener.serviceChanged(event);
95         }
96     }
97
98     /**
99      * Get the filter string used by this Filtered listener.
100      *
101      * @return The filter string used by this listener.
102      */

103     public String JavaDoc toString() {
104         return filter == null ? listener.toString() : filter.toString();
105     }
106
107 }
108
Popular Tags