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.felix.framework;
020    
021    import java.lang.ref.WeakReference;
022    import java.net.MalformedURLException;
023    import java.net.URL;
024    import java.security.CodeSource;
025    import java.security.Permission;
026    import java.security.ProtectionDomain;
027    import java.security.cert.Certificate;
028    
029    import org.apache.felix.moduleloader.IModule;
030    
031    public class BundleProtectionDomain extends ProtectionDomain
032    {
033        private final WeakReference m_felix;
034        private final WeakReference m_bundle;
035        private final int m_hashCode;
036        private final String m_toString;
037        private final WeakReference m_module;
038    
039        // TODO: SECURITY - This should probably take a module, not a bundle.
040        BundleProtectionDomain(Felix felix, BundleImpl bundle)
041            throws MalformedURLException
042        {
043            super(
044                new CodeSource(
045                    Felix.m_secureAction.createURL(
046                        Felix.m_secureAction.createURL(null, "location:", new FakeURLStreamHandler()), 
047                        bundle._getLocation(),
048                        new FakeURLStreamHandler()
049                        ), 
050                    (Certificate[]) null), 
051                null);
052            m_felix = new WeakReference(felix);
053            m_bundle = new WeakReference(bundle);
054            m_module = new WeakReference(bundle.getCurrentModule());
055            m_hashCode = bundle.hashCode();
056            m_toString = "[" + bundle + "]";
057        }
058    
059        IModule getModule() 
060        {
061            return (IModule) m_module.get();
062        }
063    
064        public boolean implies(Permission permission)
065        {
066            Felix felix = (Felix) m_felix.get();
067            return (felix != null) ? 
068                felix.impliesBundlePermission(this, permission, false) : false;
069        }
070    
071        public boolean impliesDirect(Permission permission)
072        {
073            Felix felix = (Felix) m_felix.get();
074            return (felix != null) ? 
075                felix.impliesBundlePermission(this, permission, true) : false;
076        }
077    
078        BundleImpl getBundle()
079        {
080            return (BundleImpl) m_bundle.get();
081        }
082    
083        public int hashCode()
084        {
085            return m_hashCode;
086        }
087    
088        public boolean equals(Object other)
089        {
090            if ((other == null) || (other.getClass() != BundleProtectionDomain.class))
091            {
092                return false;
093            }
094            if (m_hashCode != other.hashCode())
095            {
096                return false;
097            }
098            return m_bundle.get() == ((BundleProtectionDomain) other).m_bundle.get();
099        }
100    
101        public String toString()
102        {
103            return m_toString;
104        }
105    }