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.mail;
020    
021    import java.net.URL;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.List;
026    
027    import org.osgi.framework.Bundle;
028    import org.osgi.framework.BundleActivator;
029    import org.osgi.framework.BundleContext;
030    import org.osgi.framework.ServiceRegistration;
031    import org.osgi.framework.ServiceReference;
032    import org.osgi.service.log.LogService;
033    import org.osgi.util.tracker.BundleTracker;
034    import org.osgi.util.tracker.ServiceTracker;
035    import org.osgi.util.tracker.ServiceTrackerCustomizer;
036    
037    /**
038     * The activator that starts and manages the tracking of
039     * JAF activation command maps
040     */
041    public class Activator extends org.apache.geronimo.osgi.locator.Activator {
042        // tracker to watch for bundle updates
043        protected BundleTracker bt;
044        // service tracker for a logging service
045        protected ServiceTracker lst;
046        // an array of all active logging services.
047        protected List<LogService> logServices = new ArrayList<LogService>();
048    
049        @Override
050        public synchronized void start(final BundleContext context) throws Exception {
051            super.start(context);
052            lst = new LogServiceTracker(context, LogService.class.getName(), null);
053            lst.open();
054                bt = new BundleTracker(context, Bundle.ACTIVE, new MailProviderBundleTrackerCustomizer(this, context.getBundle()));
055                bt.open();
056            }
057    
058        @Override
059            public synchronized void stop(BundleContext context) throws Exception {
060                bt.close();
061                lst.close();
062            super.stop(context);
063            }
064    
065            void log(int level, String message) {
066                synchronized (logServices) {
067                    for (LogService log : logServices) {
068                        log.log(level, message);
069                    }
070            }
071            }
072    
073            void log(int level, String message, Throwable th) {
074            synchronized (logServices) {
075                for (LogService log : logServices) {
076                    log.log(level, message, th);
077                }
078            }
079        }
080    
081            private final class LogServiceTracker extends ServiceTracker {
082            private LogServiceTracker(BundleContext context, String clazz,
083                    ServiceTrackerCustomizer customizer) {
084                super(context, clazz, customizer);
085            }
086    
087            @Override
088            public Object addingService(ServiceReference reference) {
089                Object svc = super.addingService(reference);
090                if (svc instanceof LogService) {
091                    synchronized (logServices) {
092                        logServices.add((LogService) svc);
093                    }
094                }
095                return svc;
096            }
097    
098            @Override
099            public void removedService(ServiceReference reference, Object service) {
100                synchronized (logServices) {
101                    logServices.remove(service);
102                }
103                super.removedService(reference, service);
104            }
105        }
106    }