package org.geoint.security.spi;

import java.net.URL;
import java.security.CodeSource;
import java.security.Permission;
import java.security.ProtectionDomain;
import org.geoint.security.SnitchSecurityManager;

/**
 * SPI interface that logs/reports which operations were requested through the
 * {@link SnitchSecurityManager}.
 */
public abstract class SnitchReporter {

    public abstract void permission(Permission perm, ProtectionDomain pd);
    
//    protected String format(Permission p, Object context) {
//        StringBuilder sb = new StringBuilder();
//        sb.append(p.getClass().getName())
//                .append(" \"")
//                .append(p.getName())
//                .append("\" \"")
//                .append(p.getActions())
//                .append("\";\n");
//        return sb.toString();
//    }
    
    protected String format (Permission p, ProtectionDomain pd) {
        final CodeSource cs = pd.getCodeSource();

        if ( null == cs ) {
            return null;
        }
        final URL url = cs.getLocation();
        if( null == url ) {
           return null;
        }
        
        final StringBuilder sb = new StringBuilder();
        sb.append("grant codeBase \"");
        sb.append(url.toString());
        sb.append("\" {");
        sb.append("permission ");
        sb.append(" ");
        sb.append(p.getClass().getName());
        sb.append(" ");
        sb.append("\"");

        /* Some complex permissions have quoted strings embedded or
        literal carriage returns that must be escaped.  */

        final String permissionName = p.getName();
        final String escapedPermissionName = permissionName.replace("\"","\\\"").replace("\r","\\\r");

        sb.append(escapedPermissionName);
        sb.append("\", ");
        sb.append("\"");
        sb.append(p.getActions());
        sb.append("\";");
        sb.append("};");
        return sb.toString();
    }
}

@@ -444,15 +444,9 @@
 d);%0A
-    %0A//
+%0A
     
@@ -474,429 +474,52 @@
 at(P
-ermission p, Object context) %7B%0A//        StringBuilder sb = new StringBuilder();%0A//        sb.append(p.getClass().getName())%0A//                .append(%22 %5C%22%22)%0A//                .append(p.getName())%0A//                .append(%22%5C%22 %5C%22%22)%0A//                .append(p.getActions())%0A//                .append(%22%5C%22;%5Cn%22);%0A//        return sb.toString();%0A//    %7D%0A    %0A    protected String format (Permission p, ProtectionDomain pd
+rotectionDomain pd, Permission... perms
 ) %7B%0A
+%0A
     
@@ -564,17 +564,16 @@
 urce();%0A
-%0A
         
@@ -576,17 +576,16 @@
     if (
- 
 null == 
@@ -586,17 +586,16 @@
 ll == cs
- 
 ) %7B%0A    
@@ -681,10 +681,10 @@
   if
-(
  
+(
 null
@@ -694,13 +694,13 @@
  url
- 
 ) %7B%0A
+ 
     
@@ -729,24 +729,16 @@
       %7D%0A
-        
 %0A       
@@ -905,16 +905,84 @@
 append(%22
+%5Cn%22);%0A        for (Permission p : perms) %7B%0A            sb.append(%22%5Ct
 permissi
@@ -984,24 +984,28 @@
 mission %22);%0A
+    
         sb.a
@@ -1016,32 +1016,36 @@
 d(%22 %22);%0A        
+    
 sb.append(p.getC
@@ -1063,32 +1063,36 @@
 ame());%0A        
+    
 sb.append(%22 %22);%0A
@@ -1091,32 +1091,36 @@
 d(%22 %22);%0A        
+  
+  
 sb.append(%22%5C%22%22);
@@ -1117,24 +1117,28 @@
 end(%22%5C%22%22);%0A%0A
+    
         /* S
@@ -1193,16 +1193,21 @@
 dded or%0A
+     
         
@@ -1257,17 +1257,20 @@
 ed.  */%0A
-%0A
+    
         
@@ -1308,24 +1308,28 @@
 .getName();%0A
+    
         fina
@@ -1389,16 +1389,17 @@
 ce(%22%5C%22%22,
+ 
 %22%5C%5C%5C%22%22).
@@ -1411,16 +1411,17 @@
 ce(%22%5Cr%22,
+ 
 %22%5C%5C%5Cr%22);
@@ -1418,24 +1418,28 @@
 , %22%5C%5C%5Cr%22);%0A%0A
+    
         sb.a
@@ -1468,32 +1468,36 @@
 nName);%0A        
+    
 sb.append(%22%5C%22, %22
@@ -1499,32 +1499,36 @@
 %5C%22, %22);%0A        
+ 
+   
 sb.append(%22%5C%22%22);
@@ -1524,24 +1524,28 @@
 pend(%22%5C%22%22);%0A
+    
         sb.a
@@ -1567,32 +1567,36 @@
 ons());%0A        
+   
+ 
 sb.append(%22%5C%22;%22)
@@ -1593,24 +1593,63 @@
 end(%22%5C%22;%22);%0A
+            sb.append(%22%5Cn%22);%0A        %7D%0A
         sb.a

format method supports multiple permissions for a ProtectionDomain, listing each permission as expected by the policy file