/*============================================================================= Library: CTK Copyright (c) 2010 German Cancer Research Center, Division of Medical and Biological Informatics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #ifndef CTKSERVICETRACKER_H #define CTKSERVICETRACKER_H #include #include "CTKPluginFrameworkExport.h" #include "ctkServiceReference.h" #include "ctkServiceTrackerCustomizer.h" #include "ctkLDAPSearchFilter.h" class ctkPluginContext; class ctkServiceTrackerPrivate; class CTK_PLUGINFW_EXPORT ctkServiceTracker : protected ctkServiceTrackerCustomizer { public: ~ctkServiceTracker(); /** * Create a ServiceTracker on the specified * ServiceReference. * *

* The service referenced by the specified ServiceReference * will be tracked by this ServiceTracker. * * @param context The BundleContext against which the tracking * is done. * @param reference The ServiceReference for the service to be * tracked. * @param customizer The customizer object to call when services are added, * modified, or removed in this ServiceTracker. If * customizer is null, then this * ServiceTracker will be used as the * ServiceTrackerCustomizer and this * ServiceTracker will call the * ServiceTrackerCustomizer methods on itself. */ ctkServiceTracker(ctkPluginContext* context, const ctkServiceReference& reference, ctkServiceTrackerCustomizer* customizer = 0); /** * Create a ServiceTracker on the specified class name. * *

* Services registered under the specified class name will be tracked by * this ServiceTracker. * * @param context The BundleContext against which the tracking * is done. * @param clazz The class name of the services to be tracked. * @param customizer The customizer object to call when services are added, * modified, or removed in this ServiceTracker. If * customizer is null, then this * ServiceTracker will be used as the * ServiceTrackerCustomizer and this * ServiceTracker will call the * ServiceTrackerCustomizer methods on itself. */ ctkServiceTracker(ctkPluginContext* context, const QString& clazz, ctkServiceTrackerCustomizer* customizer = 0); /** * Create a ServiceTracker on the specified Filter * object. * *

* Services which match the specified Filter object will be * tracked by this ServiceTracker. * * @param context The BundleContext against which the tracking * is done. * @param filter The Filter to select the services to be * tracked. * @param customizer The customizer object to call when services are added, * modified, or removed in this ServiceTracker. If * customizer is null, then this ServiceTracker will be * used as the ServiceTrackerCustomizer and this * ServiceTracker will call the * ServiceTrackerCustomizer methods on itself. * @since 1.1 */ ctkServiceTracker(ctkPluginContext* context, const ctkLDAPSearchFilter& filter, ctkServiceTrackerCustomizer* customizer = 0); /** * Open this ServiceTracker and begin tracking services. * *

* Services which match the search criteria specified when this * ServiceTracker was created are now tracked by this * ServiceTracker. * * @throws java.lang.IllegalStateException If the BundleContext * with which this ServiceTracker was created is no * longer valid. * @since 1.3 */ void open(); /** * Close this ServiceTracker. * *

* This method should be called when this ServiceTracker should * end the tracking of services. * *

* This implementation calls {@link #getServiceReferences()} to get the list * of tracked services to remove. */ void close(); /** * Wait for at least one service to be tracked by this * ServiceTracker. This method will also return when this * ServiceTracker is closed. * *

* It is strongly recommended that waitForService is not used * during the calling of the BundleActivator methods. * BundleActivator methods are expected to complete in a short * period of time. * *

* This implementation calls {@link #getService()} to determine if a service * is being tracked. * * @param timeout The time interval in milliseconds to wait. If zero, the * method will wait indefinitely. * @return Returns the result of {@link #getService()}. * @throws InterruptedException If another thread has interrupted the * current thread. * @throws IllegalArgumentException If the value of timeout is negative. */ QObject* waitForService(unsigned long timeout); /** * Return an array of ServiceReferences for all services being * tracked by this ServiceTracker. * * @return Array of ServiceReferences or null if * no services are being tracked. */ QList getServiceReferences() const; /** * Returns a ServiceReference for one of the services being * tracked by this ServiceTracker. * *

* If multiple services are being tracked, the service with the highest * ranking (as specified in its service.ranking property) is * returned. If there is a tie in ranking, the service with the lowest * service ID (as specified in its service.id property); that * is, the service that was registered first is returned. This is the same * algorithm used by BundleContext.getServiceReference. * *

* This implementation calls {@link #getServiceReferences()} to get the list * of references for the tracked services. * * @return A ServiceReference or null if no * services are being tracked. * @since 1.1 */ ctkServiceReference getServiceReference() const; /** * Returns the service object for the specified * ServiceReference if the specified referenced service is * being tracked by this ServiceTracker. * * @param reference The reference to the desired service. * @return A service object or null if the service referenced * by the specified ServiceReference is not being * tracked. */ QObject* getService(const ctkServiceReference& reference) const; /** * Return an array of service objects for all services being tracked by this * ServiceTracker. * *

* This implementation calls {@link #getServiceReferences()} to get the list * of references for the tracked services and then calls * {@link #getService(ServiceReference)} for each reference to get the * tracked service object. * * @return An array of service objects or null if no services * are being tracked. */ QList getServices() const; /** * Returns a service object for one of the services being tracked by this * ServiceTracker. * *

* If any services are being tracked, this implementation returns the result * of calling getService(getServiceReference()). * * @return A service object or null if no services are being * tracked. */ QObject* getService() const; /** * Remove a service from this ServiceTracker. * * The specified service will be removed from this * ServiceTracker. If the specified service was being tracked * then the ServiceTrackerCustomizer.removedService method will * be called for that service. * * @param reference The reference to the service to be removed. */ void remove(const ctkServiceReference& reference); /** * Return the number of services being tracked by this * ServiceTracker. * * @return The number of services being tracked. */ int size() const; /** * Returns the tracking count for this ServiceTracker. * * The tracking count is initialized to 0 when this * ServiceTracker is opened. Every time a service is added, * modified or removed from this ServiceTracker, the tracking * count is incremented. * *

* The tracking count can be used to determine if this * ServiceTracker has added, modified or removed a service by * comparing a tracking count value previously collected with the current * tracking count value. If the value has not changed, then no service has * been added, modified or removed from this ServiceTracker * since the previous tracking count was collected. * * @since 1.2 * @return The tracking count for this ServiceTracker or -1 if * this ServiceTracker is not open. */ int getTrackingCount() const; protected: /** * Default implementation of the * ServiceTrackerCustomizer.addingService method. * *

* This method is only called when this ServiceTracker has been * constructed with a null ServiceTrackerCustomizer argument. * *

* This implementation returns the result of calling getService * on the BundleContext with which this * ServiceTracker was created passing the specified * ServiceReference. *

* This method can be overridden in a subclass to customize the service * object to be tracked for the service being added. In that case, take care * not to rely on the default implementation of * {@link #removedService(ServiceReference, Object) removedService} to unget * the service. * * @param reference The reference to the service being added to this * ServiceTracker. * @return The service object to be tracked for the service added to this * ServiceTracker. * @see ServiceTrackerCustomizer#addingService(ServiceReference) */ QObject* addingService(const ctkServiceReference& reference); /** * Default implementation of the * ServiceTrackerCustomizer.modifiedService method. * *

* This method is only called when this ServiceTracker has been * constructed with a null ServiceTrackerCustomizer argument. * *

* This implementation does nothing. * * @param reference The reference to modified service. * @param service The service object for the modified service. * @see ServiceTrackerCustomizer#modifiedService(ServiceReference, Object) */ void modifiedService(const ctkServiceReference& reference, QObject* service); /** * Default implementation of the * ServiceTrackerCustomizer.removedService method. * *

* This method is only called when this ServiceTracker has been * constructed with a null ServiceTrackerCustomizer argument. * *

* This implementation calls ungetService, on the * BundleContext with which this ServiceTracker * was created, passing the specified ServiceReference. *

* This method can be overridden in a subclass. If the default * implementation of {@link #addingService(ServiceReference) addingService} * method was used, this method must unget the service. * * @param reference The reference to removed service. * @param service The service object for the removed service. * @see ServiceTrackerCustomizer#removedService(ServiceReference, Object) */ void removedService(const ctkServiceReference& reference, QObject* service); private: friend class ctkTrackedService; Q_DECLARE_PRIVATE(ctkServiceTracker) const QScopedPointer d_ptr; }; #endif // CTKSERVICETRACKER_H