org.ops4j.pax.wicket.internal.PaxWicketPageTracker Maven / Gradle / Ivy
Show all versions of org.ops4j.pax.wicket.service Show documentation
/**
* Copyright OPS4J
*
* 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.
*/
package org.ops4j.pax.wicket.internal;
import static org.ops4j.pax.wicket.api.Constants.APPLICATION_NAME;
import static org.ops4j.pax.wicket.internal.TrackingUtil.createAllPageFactoryFilter;
import org.ops4j.pax.wicket.api.PageFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class PaxWicketPageTracker extends ServiceTracker {
private static final Logger LOGGER = LoggerFactory.getLogger(PaxWicketPageTracker.class);
private final String applicationName;
private final PaxWicketPageFactory paxWicketPageFactory;
PaxWicketPageTracker(BundleContext context, String applicationName, PaxWicketPageFactory paxWicketPageFactory) {
super(context, createAllPageFactoryFilter(context, applicationName), null);
this.applicationName = applicationName;
this.paxWicketPageFactory = paxWicketPageFactory;
}
/**
* Default implementation of the {@code ServiceTrackerCustomizer.addingService} method.
*
*
* This method is only called when this ServiceTracker
object has been constructed with a
* null ServiceTrackerCustomizer
argument.
*
* The default implementation returns the result of calling getService
, on the
* BundleContext
object with which this ServiceTracker
object was created, passing the
* specified ServiceReference
object.
*
* 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 removedService that will unget the
* service.
*
* @param reference Reference to service being added to this ServiceTracker
object.
*
* @return The service object to be tracked for the service added to this ServiceTracker
object.
*
* @see org.osgi.util.tracker.ServiceTrackerCustomizer
*/
@Override
public final Object addingService(ServiceReference reference) {
PageFactory> pageSource = (PageFactory>) super.addingService(reference);
Class> pageClass = pageSource.getPageClass();
paxWicketPageFactory.add(pageClass, pageSource);
return pageSource;
}
/**
* Default implementation of the ServiceTrackerCustomizer.modifiedService
method.
*
*
* This method is only called when this ServiceTracker
object has been constructed with a
* null ServiceTrackerCustomizer
argument.
*
* The default implementation does nothing.
*
* @param reference Reference to modified service.
* @param service The service object for the modified service.
*
* @see org.osgi.util.tracker.ServiceTrackerCustomizer
*/
@Override
public final void modifiedService(ServiceReference reference, Object service) {
PageFactory> pageSource = (PageFactory>) service;
String appName = (String) reference.getProperty(APPLICATION_NAME);
if (!applicationName.equals(appName)) {
Class> pageClass = pageSource.getPageClass();
paxWicketPageFactory.remove(pageClass);
}
}
/**
* Default implementation of the ServiceTrackerCustomizer.removedService
method.
*
*
* This method is only called when this ServiceTracker
object has been constructed with a
* null ServiceTrackerCustomizer
argument.
*
* The default implementation calls ungetService
, on the BundleContext
object with which
* this ServiceTracker
object was created, passing the specified ServiceReference
object.
*
* This method can be overridden in a subclass. If the default implementation of addingService
method
* was used, this method must unget the service.
*
* @param reference Reference to removed service.
* @param service The service object for the removed service.
*
* @see org.osgi.util.tracker.ServiceTrackerCustomizer
*/
@Override
public final void removedService(ServiceReference reference, Object service) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("removedService( " + reference + ", " + service + ");");
}
PageFactory> pageSource = (PageFactory>) service;
Class> pageclass = pageSource.getPageClass();
paxWicketPageFactory.remove(pageclass);
super.removedService(reference, service);
}
}