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.util.Map;
022    import java.util.HashMap;
023    
024    import javax.activation.MailcapCommandMap;
025    import javax.activation.CommandInfo;
026    import javax.activation.DataContentHandler;
027    
028    import org.osgi.framework.Bundle;
029    
030    /**
031     * An OSGi enabled MailcapCommandMap
032     */
033    public class OsgiMailcapCommandMap extends MailcapCommandMap {
034    
035        private Bundle currentBundle;
036        private Map<CommandInfo, Bundle> bundles = new HashMap<CommandInfo, Bundle>();
037    
038        public void addMailcap(String line, Bundle bundle) {
039            currentBundle = bundle;
040            addMailcap(line);
041        }
042    
043        protected void addCommand(Map commandList, String mimeType, CommandInfo command) {
044            super.addCommand(commandList, mimeType, command);
045            if (currentBundle != null) {
046                bundles.put(command, currentBundle);
047            }
048        }
049    
050        @Override
051        public DataContentHandler createDataContentHandler(String mimeType) {
052            CommandInfo info = getCommand(mimeType, "content-handler");
053            if (info == null) {
054                return null;
055            }
056    
057            Bundle bundle = bundles.get(info);
058            if (bundle != null) {
059                try {
060                    return (DataContentHandler) bundle.loadClass(info.getCommandClass()).newInstance();
061                } catch (ClassNotFoundException e) {
062                    return null;
063                } catch (IllegalAccessException e) {
064                    return null;
065                } catch (InstantiationException e) {
066                    return null;
067                }
068            }
069    
070            ClassLoader cl = Thread.currentThread().getContextClassLoader();
071            if (cl == null) {
072                cl = getClass().getClassLoader();
073            }
074            try {
075                return (DataContentHandler) cl.loadClass(info.getCommandClass()).newInstance();
076            } catch (ClassNotFoundException e) {
077                return null;
078            } catch (IllegalAccessException e) {
079                return null;
080            } catch (InstantiationException e) {
081                return null;
082            }
083        }
084    }