001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.geronimo.specs.activation;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import org.osgi.framework.Bundle;
025    import org.osgi.framework.BundleActivator;
026    import org.osgi.framework.BundleContext;
027    import org.osgi.framework.ServiceRegistration;
028    import org.osgi.framework.ServiceReference;
029    import org.osgi.service.log.LogService;
030    import org.osgi.util.tracker.BundleTracker;
031    import org.osgi.util.tracker.ServiceTracker;
032    import org.osgi.util.tracker.ServiceTrackerCustomizer;
033    
034    /**
035     * The activator that starts and manages the tracking of
036     * JAF activation command maps
037     */
038    public class Activator extends org.apache.geronimo.osgi.locator.Activator {
039        // tracker to watch for bundle updates
040        protected BundleTracker bt;
041        // service tracker for a logging service
042        protected ServiceTracker lst;
043        // an array of all active logging services.
044        List<LogService> logServices = new ArrayList<LogService>();
045    
046    
047        @Override
048        public synchronized void start(final BundleContext context) throws Exception {
049            super.start(context);
050            lst = new LogServiceTracker(context, LogService.class.getName(), null);
051            lst.open();
052                bt = new BundleTracker(context, Bundle.ACTIVE, new CommandMapBundleTrackerCustomizer(this, context.getBundle()));
053                bt.open();
054            }
055    
056        @Override
057            public synchronized void stop(BundleContext context) throws Exception {
058                bt.close();
059                lst.close();
060            super.stop(context);
061            }
062    
063            void log(int level, String message) {
064                synchronized (logServices) {
065                    for (LogService log : logServices) {
066                        log.log(level, message);
067                    }
068            }
069            }
070    
071            void log(int level, String message, Throwable th) {
072            synchronized (logServices) {
073                for (LogService log : logServices) {
074                    log.log(level, message, th);
075                }
076            }
077        }
078    
079            private final class LogServiceTracker extends ServiceTracker {
080            private LogServiceTracker(BundleContext context, String clazz,
081                    ServiceTrackerCustomizer customizer) {
082                super(context, clazz, customizer);
083            }
084    
085            @Override
086            public Object addingService(ServiceReference reference) {
087                Object svc = super.addingService(reference);
088                if (svc instanceof LogService) {
089                    synchronized (logServices) {
090                        logServices.add((LogService) svc);
091                    }
092                }
093                return svc;
094            }
095    
096            @Override
097            public void removedService(ServiceReference reference, Object service) {
098                synchronized (logServices) {
099                    logServices.remove(service);
100                }
101                super.removedService(reference, service);
102            }
103        }
104    }