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

org.ow2.cmi.agent.Activator Maven / Gradle / Ivy

There is a newer version: 2.2.6
Show newest version
/**
 * CMI : Cluster Method Invocation
 * Copyright (C) 2008 Bull S.A.S.
 * Contact: [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * --------------------------------------------------------------------------
 * $Id: Activator.java 1978 2008-08-01 04:07:39Z eyindanga $
 * --------------------------------------------------------------------------
 */
package org.ow2.cmi.agent;

import java.text.DateFormat;
import java.util.Date;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogListener;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.LogService;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;


/**
 * Activator for the bundle agent. This agent may start some bundles.
 * @author eyindanga
 *
 */
public class Activator implements BundleActivator {

    /**
     * Logger.
     */
    private static Log logger = LogFactory.getLog(Activator.class);

    /**
     * List of bundles to be started by the agent.
     * WARNING: Sorting matters !
     */
    private static final String[] BUNDLE_SYMBOLIC_NAMES = {
    };

    /**
     * Starts the Cmi bundle in the right order.
     * {@inheritDoc}
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    @SuppressWarnings("unchecked")
    public void start(final BundleContext bc) throws Exception {

        // Lookup logreaderservice and register a simpleloglistener
        ServiceReference logServiceReference = bc.getServiceReference(LogReaderService.class.getName());

        if (logServiceReference != null){
            LogReaderService logReaderService = (LogReaderService) bc.getService(logServiceReference);
            logReaderService.addLogListener(new SimpleLogListenerImpl());
        }

        Bundle[] bundles = bc.getBundles();

        // Start Bundles in the array order
        for (int i = 0; i < BUNDLE_SYMBOLIC_NAMES.length; i++) {

            // 1. Start the bundle with the right symbolic name
            Bundle bundle = null;
            for (int j = 0; j < bundles.length; j++) {
                bundle = bundles[j];
                if (BUNDLE_SYMBOLIC_NAMES[i].equals(bundle.getSymbolicName())) {
                    logger.debug("Starting Bundle", bundle.getSymbolicName());
                    try {
                        if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.INSTALLED) {
                            bundle.start();
                            break;
                        }
                    }catch (Exception e) {
                        logger.error("Exception occurred when starting Bundle: " + bundle.getSymbolicName(), e);
                    }
                }
            }
        }
    }

    /**
     * Stops the CMI bundle in the right order. {@inheritDoc}
     *
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(final BundleContext bc) throws Exception {
        Bundle[] bundles = bc.getBundles();

        // Traverse the array starting by the end
        for (int i = BUNDLE_SYMBOLIC_NAMES.length - 1; i >= 0; i--) {
            Bundle bundle = null;
            for (int j = 0; j < bundles.length && bundle == null; j++) {
                if (BUNDLE_SYMBOLIC_NAMES[i].equals(bundles[j].getSymbolicName())) {
                    bundle = bundles[j];

                    // TODO Should use standard LogService
                    try {
                        logger.debug("Stopping Bundle", bundle.getSymbolicName());
                        bundle.stop();
                    } catch (Exception e) {
                        logger.error("Stopping Bundle", bundle.getSymbolicName(), e);
                    }
                }
            }
        }
    }


    static class SimpleLogListenerImpl implements LogListener {

        /*
         * (non-Javadoc)
         *
         * @see org.osgi.service.log.LogListener#logged(org.osgi.service.log.LogEntry)
         */
        public void logged(final LogEntry entry) {
            String logLevel = null;
            switch (entry.getLevel()) {
            case LogService.LOG_DEBUG:
                logLevel = "DEBUG";
                break;

            case LogService.LOG_INFO:
                logLevel = "INFO";
                break;
            case LogService.LOG_WARNING:
                logLevel = "WARNING";
                break;
            case LogService.LOG_ERROR:
                logLevel = "ERROR";
                break;
            default:
                logLevel = "INFO";
            }

            DateFormat dateFormat = DateFormat.getTimeInstance();
            System.out.println(logLevel
                    + " - "
                    + dateFormat.format(new Date(entry.getTime()))
                    + " ["
                    + (entry.getServiceReference() != null ? "S: "
                            + entry.getServiceReference().getProperty(Constants.SERVICE_PID) : "B: "
                                + entry.getBundle().getSymbolicName()) + "] - " + entry.getMessage() + ".");
            if (entry.getException() != null) {
                System.out.println(" Exception: " + entry.getException().getMessage());
                entry.getException().printStackTrace(System.out);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy