/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.felix.framework;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.Permission;
import java.security.ProtectionDomain;
import java.security.cert.Certificate;

public class BundleProtectionDomain extends ProtectionDomain
{
    private final Felix m_felix;
    private final BundleImpl m_bundle;

    // TODO: SECURITY - This should probably take a module, not a bundle.
    BundleProtectionDomain(Felix felix, BundleImpl bundle)
        throws MalformedURLException
    {
        super(new CodeSource(new URL(new URL(null, "location:", 
            new FakeURLStreamHandler()), bundle._getLocation(),
            new FakeURLStreamHandler()), (Certificate[]) null), null);
        m_felix = felix;
        m_bundle = bundle;
    }

    public boolean implies(Permission permission)
    {
        return m_felix.impliesBundlePermission(this, permission, false);
    }

    public boolean impliesDirect(Permission permission)
    {
        return m_felix.impliesBundlePermission(this, permission, true);
    }

    BundleImpl getBundle()
    {
        return m_bundle;
    }

    public int hashCode()
    {
        return m_bundle.hashCode();
    }

    public boolean equals(Object other)
    {
        if ((other == null) || other.getClass() != BundleProtectionDomain.class)
        {
            return false;
        }
        return m_bundle == ((BundleProtectionDomain) other).m_bundle;
    }

    public String toString()
    {
        return "[" + m_bundle + "]";
    }
}
@@ -838,16 +838,52 @@
 ework;%0A%0A
+import java.lang.ref.WeakReference;%0A
 import j
@@ -1160,21 +1160,29 @@
 e final 
-Felix
+WeakReference
  m_felix
@@ -1205,27 +1205,101 @@
 nal 
-BundleImpl m_bundle
+WeakReference m_bundle;%0A    private final int m_hashCode;%0A    private final String m_toString
 ;%0A%0A 
@@ -1691,21 +1691,40 @@
 felix = 
+new WeakReference(
 felix
+)
 ;%0A      
@@ -1740,96 +1740,270 @@
 e = 
-bundle;%0A    %7D%0A%0A    public boolean implies(Permission permission)%0A    %7B%0A        return m_
+new WeakReference(bundle);%0A        m_hashCode = bundle.hashCode();%0A        m_toString = %22%5B%22 + bundle + %22%5D%22;%0A    %7D%0A%0A    public boolean implies(Permission permission)%0A    %7B%0A        Felix felix = (Felix) m_felix.get();%0A        return (felix != null) ? %0A            
 feli
@@ -2052,16 +2052,24 @@
 , false)
+ : false
 ;%0A    %7D%0A
@@ -2139,25 +2139,99 @@
         
-return m_
+Felix felix = (Felix) m_felix.get();%0A        return (felix != null) ? %0A            
 felix.im
@@ -2275,16 +2275,24 @@
 n, true)
+ : false
 ;%0A    %7D%0A
@@ -2331,32 +2331,45 @@
 %7B%0A        return
+ (BundleImpl)
  m_bundle;%0A    %7D
@@ -2353,32 +2353,38 @@
 leImpl) m_bundle
+.get()
 ;%0A    %7D%0A%0A    pub
@@ -2425,31 +2425,24 @@
 eturn m_
-bundle.
 hashCode
 ();%0A    
@@ -2429,26 +2429,24 @@
 n m_hashCode
-()
 ;%0A    %7D%0A%0A   
@@ -2519,16 +2519,17 @@
 ull) %7C%7C 
+(
 other.ge
@@ -2569,16 +2569,107 @@
 n.class)
+)%0A        %7B%0A            return false;%0A        %7D%0A        if (m_hashCode != other.hashCode())
 %0A       
@@ -2730,16 +2730,22 @@
 m_bundle
+.get()
  == ((Bu
@@ -2777,24 +2777,30 @@
 er).m_bundle
+.get()
 ;%0A    %7D%0A%0A   
@@ -2850,28 +2850,18 @@
 urn 
-%22%5B%22 + m_bundle + %22%5D%22
+m_toString
 ;%0A  

Fix a memory leak when stopping and restarting felix by using weakreferences inside the bundle protection domain. (FELIX-1170)