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.servicemix.specs.activation;
020    
021    import java.io.BufferedReader;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    import java.net.URL;
025    import java.util.Map;
026    import java.util.concurrent.ConcurrentHashMap;
027    
028    import javax.activation.CommandMap;
029    
030    import org.osgi.framework.Bundle;
031    import org.osgi.framework.BundleContext;
032    
033    /**
034     * A bundle activator for activation framework
035     */
036    public class Activator extends org.apache.servicemix.specs.locator.Activator {
037    
038        private Map<Long, MailCap> mailcaps = new ConcurrentHashMap<Long, MailCap>();
039    
040        @Override
041        public void start(BundleContext bundleContext) throws Exception {
042            super.start(bundleContext);
043        }
044    
045        @Override
046        public void stop(BundleContext bundleContext) throws Exception {
047            super.stop(bundleContext);
048            CommandMap.setDefaultCommandMap(null);
049        }
050    
051        @Override
052        protected void register(Bundle bundle) {
053            debugPrintln("checking bundle " + bundle.getBundleId());
054            URL url = bundle.getResource("/META-INF/mailcap");
055            if (url != null) {
056                debugPrintln("found mailcap at " + url);
057                mailcaps.put(bundle.getBundleId(), new MailCap(bundle, url));
058                rebuildCommandMap();
059            }
060        }
061    
062        @Override
063        protected void unregister(long bundleId) {
064            MailCap mailcap = mailcaps.remove(bundleId);
065            if (mailcap != null ){
066                debugPrintln("removing mailcap at " + mailcap.url);
067                rebuildCommandMap();
068            }
069        }
070    
071        private void rebuildCommandMap() {
072            OsgiMailcapCommandMap commandMap = new OsgiMailcapCommandMap();
073            for (MailCap mailcap : mailcaps.values()) {
074                try {
075                    InputStream is = mailcap.url.openStream();
076                    try {
077                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
078                        String line;
079                        while ((line = br.readLine()) != null) {
080                            commandMap.addMailcap(line, mailcap.bundle);
081                        }
082                    } finally {
083                        is.close();
084                    }
085                } catch (Exception e) {
086                    // Ignore
087                }
088            }
089            CommandMap.setDefaultCommandMap(commandMap);
090        }
091    
092        private static class MailCap {
093            Bundle bundle;
094            URL url;
095    
096            private MailCap(Bundle bundle, URL url) {
097                this.bundle = bundle;
098                this.url = url;
099            }
100        }
101    
102    }