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.osgi.registry;
020    
021    import org.osgi.framework.Bundle;
022    import org.osgi.framework.BundleEvent;
023    import org.osgi.framework.ServiceRegistration;
024    import org.osgi.service.log.LogService;
025    import org.osgi.util.tracker.BundleTrackerCustomizer;
026    
027    public class ProviderBundleTrackerCustomizer implements BundleTrackerCustomizer {
028        // our base Activator (used as a service source)
029        private Activator activator;
030        // the bundle hosting this registry
031        private Bundle registryBundle;
032        // the registry we interact with
033        private ProviderRegistryImpl registry;
034    
035        public ProviderBundleTrackerCustomizer(Activator a, Bundle b, ProviderRegistryImpl r) {
036            activator = a;
037            registryBundle = b;
038            registry = r;
039        }
040    
041        /**
042         * Handle the activation of a new bundle.
043         *
044         * @param bundle The source bundle.
045         * @param event  The bundle event information.
046         *
047         * @return A return object.
048         */
049        @Override
050        public Object addingBundle(Bundle bundle, BundleEvent event) {
051            log(LogService.LOG_DEBUG, "Bundle Considered for class providers: " + bundle.getSymbolicName());
052            if (bundle.equals(registryBundle)) {
053                return null;
054            }
055    
056            return registry.addBundle(bundle);
057        }
058    
059        @Override
060        public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
061            // nothing to do here
062        }
063    
064        @Override
065        public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
066            // have the registry process this
067            registry.removeBundle(bundle, object);
068        }
069    
070        private void log(int level, String message) {
071            activator.log(level, message);
072        }
073    
074        private void log(int level, String message, Throwable th) {
075            activator.log(level, message, th);
076        }
077    }