All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.flowable.osgi.Activator Maven / Gradle / Ivy

There is a newer version: 7.0.1
Show newest version
/* 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.flowable.osgi;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

import org.apache.felix.fileinstall.ArtifactListener;
import org.apache.felix.fileinstall.ArtifactUrlTransformer;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.url.URLStreamHandlerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * OSGi Activator
 *
 * @author Guillaume Nodet
 */
public class Activator implements BundleActivator {

    private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);

    private List callbacks = new ArrayList<>();

    @Override
    public void start(BundleContext context) throws Exception {
        callbacks.add(new Service(context, URLStreamHandlerService.class.getName(), new BpmnURLHandler(), props("url.handler.protocol", "bpmn")));
        callbacks.add(new Service(context, URLStreamHandlerService.class.getName(), new BarURLHandler(), props("url.handler.protocol", "bar")));
        try {
            callbacks.add(new Service(context, new String[]{ArtifactUrlTransformer.class.getName(), ArtifactListener.class.getName()}, new BpmnDeploymentListener(), null));
            callbacks.add(new Service(context, new String[]{ArtifactUrlTransformer.class.getName(), ArtifactListener.class.getName()}, new BarDeploymentListener(), null));
        } catch (NoClassDefFoundError e) {
            LOGGER.warn("FileInstall package is not available, disabling fileinstall support");
            LOGGER.debug("FileInstall package is not available, disabling fileinstall support", e);
        }
        callbacks.add(new Tracker(new Extender(context)));
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        for (Runnable r : callbacks) {
            r.run();
        }
    }

    private static Dictionary props(String... args) {
        Dictionary props = new Hashtable<>();
        for (int i = 0; i < args.length / 2; i++) {
            props.put(args[2 * i], args[2 * i + 1]);
        }
        return props;
    }

    @SuppressWarnings({"rawtypes"})
    private static class Service implements Runnable {

        private final ServiceRegistration registration;

        public Service(BundleContext context, String clazz, Object service, Dictionary props) {
            this.registration = context.registerService(clazz, service, props);
        }

        public Service(BundleContext context, String[] clazz, Object service, Dictionary props) {
            this.registration = context.registerService(clazz, service, props);
        }

        @Override
        public void run() {
            registration.unregister();
        }
    }

    private static class Tracker implements Runnable {

        private final Extender extender;

        private Tracker(Extender extender) {
            this.extender = extender;
            this.extender.open();
        }

        @Override
        public void run() {
            extender.close();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy