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    import java.util.concurrent.ConcurrentMap;
027    import java.util.concurrent.ConcurrentHashMap;
028    
029    import org.osgi.framework.Bundle;
030    import org.osgi.framework.BundleActivator;
031    import org.osgi.framework.BundleContext;
032    import org.osgi.framework.ServiceRegistration;
033    import org.osgi.framework.ServiceReference;
034    import org.osgi.service.log.LogService;
035    import org.osgi.util.tracker.BundleTracker;
036    import org.osgi.util.tracker.ServiceTracker;
037    import org.osgi.util.tracker.ServiceTrackerCustomizer;
038    
039    /**
040     * The activator that starts and manages the tracking of
041     * JAF activation command maps
042     */
043    public class MailProviderRegistry {
044        // a list of all active mail provider config files
045        static ConcurrentMap<Long, URL> providers = new ConcurrentHashMap<Long, URL>();
046        // a list of all active default provider config files
047        static ConcurrentMap<Long, URL> defaultProviders = new ConcurrentHashMap<Long, URL>();
048    
049        /**
050         * Perform the check for an existing mailcap file when
051         * a bundle is registered.
052         *
053         * @param bundle The potential provider bundle.
054         *
055         * @return A URL object if this bundle contains a mailcap file.
056         */
057        static Object registerBundle(Bundle bundle) {
058            // potential tracker return result
059            Object result = null;
060            // a given bundle might have a javamail.providers definition and/or a
061            // default providers definition.
062            URL url = bundle.getResource("META-INF/javamail.providers");
063            if (url != null) {
064                providers.put(bundle.getBundleId(), url);
065                // this indicates our interest
066                result = url;
067            }
068    
069            url = bundle.getResource("META-INF/javamail.default.providers");
070            if (url != null) {
071                defaultProviders.put(bundle.getBundleId(), url);
072                // this indicates our interest
073                result = url;
074            }
075            // the url marks our interest in additional activity for this
076            // bundle.
077            return result;
078        }
079    
080    
081        /**
082         * Remove a bundle from our potential mailcap pool.
083         *
084         * @param bundle The potential source bundle.
085         */
086        static void unregisterBundle(Bundle bundle) {
087            // remove these items
088            providers.remove(bundle.getBundleId());
089            defaultProviders.remove(bundle.getBundleId());
090        }
091    
092        /**
093         * Retrieve any located provider definitions
094         * from bundles.
095         *
096         * @return A collection of the provider definition file
097         *         URLs.
098         */
099        public static Collection<URL> getProviders() {
100            return providers.values();
101        }
102    
103        /**
104         * Retrieve any located default provider definitions
105         * from bundles.
106         *
107         * @return A collection of the default provider definition file
108         *         URLs.
109         */
110        public static Collection<URL> getDefaultProviders() {
111            return defaultProviders.values();
112        }
113    }