io.neba.core.blueprint.SlingOsgiBundleShutdownHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of io.neba.neba-core Show documentation
Show all versions of io.neba.neba-core Show documentation
Contains the entire NEBA core implementation, i.e. the framework that interprets the
NEBA API annotations and provides implementations for the service and lifecycle callback
interfaces provided in the NEBA API. This package must not export anything as
its implementation details are entirely private.
/**
* Copyright 2013 the original author or authors.
*
* 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 io.neba.core.blueprint;
import org.eclipse.gemini.blueprint.context.BundleContextAware;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.SynchronousBundleListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import static org.osgi.framework.BundleEvent.STOPPING;
/**
* Dispatches a {@link org.osgi.framework.Bundle} shutdown event to the injected
* services in a defined order.
* This is a {@link SynchronousBundleListener} since only these listeners are
* notified when a bundle is about to be stopped in a synchronous
* manner. This is required in order to prevent the shutdown event from being handled in the wrong order, e.g.
* after the {@link SlingBeanFactoryPostProcessor} was invoked.
*
* @author Olaf Otto
*/
@Service
public class SlingOsgiBundleShutdownHandler extends ContextShutdownHandler implements SynchronousBundleListener, BundleContextAware {
private BundleContext context;
@Override
@Async
public void bundleChanged(BundleEvent event) {
if (isRemoveEvent(event)) {
event.getBundle().getBundleContext().removeBundleListener(this);
handleStop(event.getBundle());
}
}
private boolean isRemoveEvent(BundleEvent event) {
return event.getType() == STOPPING;
}
@PostConstruct
public void startListening() {
this.context.addBundleListener(this);
}
@PreDestroy
public void stopListening() {
this.context.removeBundleListener(this);
}
@Override
public void setBundleContext(BundleContext bundleContext) {
this.context = bundleContext;
}
}