Author: reto
Date: Fri Jul 16 20:27:50 2010
New Revision: 964940

URL: http://svn.apache.org/viewvc?rev=964940&view=rev
Log:
enhanced permissionparser no longer to rely on osgi class to be able to parse 
the toString version of permission besides the osgi style with quotes around 
arguments

Added:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/test/java/org/apache/clerezza/utils/security/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/test/java/org/apache/clerezza/utils/security/PermissionParserTest.java
Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/pom.xml
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/main/java/org/apache/clerezza/utils/security/PermissionParser.java

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/pom.xml?rev=964940&r1=964939&r2=964940&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/pom.xml
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/pom.xml
 Fri Jul 16 20:27:50 2010
@@ -26,10 +26,6 @@
                        <artifactId>junit</artifactId>
                        <scope>test</scope>
                </dependency>
-               <dependency>
-                       <groupId>org.osgi</groupId>
-                       <artifactId>org.osgi.core</artifactId>
-               </dependency>
        </dependencies>
        <build>
                <plugins>

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/main/java/org/apache/clerezza/utils/security/PermissionParser.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/main/java/org/apache/clerezza/utils/security/PermissionParser.java?rev=964940&r1=964939&r2=964940&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/main/java/org/apache/clerezza/utils/security/PermissionParser.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/main/java/org/apache/clerezza/utils/security/PermissionParser.java
 Fri Jul 16 20:27:50 2010
@@ -18,12 +18,14 @@
  */
 package org.apache.clerezza.utils.security;
 
+import java.io.IOException;
+import java.io.PushbackReader;
+import java.io.StringReader;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.security.Permission;
 
 
-import org.osgi.service.permissionadmin.PermissionInfo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -57,15 +59,13 @@ public class PermissionParser {
         * @return
         */
        public static Permission getPermission(String permissionDescription, 
ClassLoader classLoader) {
-               PermissionInfo permissionInfo = new PermissionInfo(
-                                       permissionDescription);
-
+               PermissionInfo permissionInfo = parse(permissionDescription);
                try {
-                       Class clazz = 
classLoader.loadClass(permissionInfo.getType());
+                       Class clazz = 
classLoader.loadClass(permissionInfo.className);
                        Constructor<?> constructor = clazz.getConstructor(
                                        String.class, String.class);
                        return (Permission) constructor.newInstance(
-                                       permissionInfo.getName(), 
permissionInfo.getActions());
+                                       permissionInfo.name, 
permissionInfo.actions);
                } catch (InstantiationException ie) {
                        logger.warn("{}", ie);
                        throw new RuntimeException(ie);
@@ -83,4 +83,106 @@ public class PermissionParser {
                        throw new RuntimeException(iae);
                }
        }
+
+       private static PermissionInfo parse(String permissionDescription) {
+               StringReader reader = new StringReader(permissionDescription);
+               try {
+                       return parse(reader);
+               } catch (IOException ex) {
+                       throw new RuntimeException(ex);
+               }
+       }
+
+       private static PermissionInfo parse(StringReader reader) throws 
IOException {
+               PermissionInfo result = new PermissionInfo();
+               for (int ch = reader.read(); ch != -1; ch = reader.read()) {
+                       if (ch == ' ') {
+                               continue;
+                       }
+                       if (ch =='(') {
+                               parseFromClassName(reader, result);
+                               break;
+                       } else {
+                               throw new IllegalArgumentException("Permission 
description does not start with '('");
+                       }
+               }
+               for (int ch = reader.read(); ch != -1; ch = reader.read()) {
+                       if (ch != ' ') {
+                               throw new IllegalArgumentException("Unparsable 
characters after closing ')'");
+                       }
+               }
+               return result;
+       }
+
+       private static void parseFromClassName(StringReader StringReader, 
PermissionInfo result) throws IOException {
+               PushbackReader reader = new PushbackReader(StringReader, 1);
+               result.className = readSection(reader);
+               result.name = readSection(reader);
+               result.actions = readSection(reader);
+               byte closingBracketsCount = 0;
+               for (int ch = reader.read(); ch != -1; ch = reader.read()) {
+                       if (ch == ' ')  {
+                               continue;
+                       }
+                       if (ch == ')')  {
+                               closingBracketsCount++;
+                               if (closingBracketsCount > 1) {
+                                       throw new 
IllegalArgumentException("more than 1 closing bracket");
+                               }
+                               continue;
+                       }
+                       else {
+                               throw new IllegalArgumentException("illegal 
character at this position: "+ch);
+                       }
+               }
+       }
+
+       private static String readSection(PushbackReader reader) throws 
IOException {
+               for (int ch = reader.read(); ch != -1; ch = reader.read()) {
+                       if (ch == ' ')  {
+                               continue;
+                       } else {
+                               reader.unread(ch);
+                               return readSectionWithNoHeadingSpace(reader);
+                       }
+               }
+               return null;
+       }
+
+       private static String readSectionWithNoHeadingSpace(PushbackReader 
reader) throws IOException {
+               StringBuilder sectionWriter = new StringBuilder();
+               for (int ch = reader.read(); ch != -1; ch = reader.read()) {
+                       if (ch == '"')  {
+                               if (sectionWriter.length() > 0) {
+                                       throw new 
IllegalArgumentException("Quote at wrong position, characters before quote: 
"+sectionWriter.toString());
+                               }
+                               sectionWriter = null;
+                               return readTillQuote(reader);
+                       }
+                       if (ch == ' ') {
+                               return sectionWriter.toString();
+                       }
+                       if (ch  == ')') {
+                               reader.unread(ch);
+                               return sectionWriter.toString();
+                       }
+                       sectionWriter.append((char)ch);
+               }
+               throw new IllegalArgumentException("missing closing bracket 
(')')");
+       }
+
+       private static String readTillQuote(PushbackReader reader) throws 
IOException {
+               StringBuilder sectionWriter = new StringBuilder();
+               for (int ch = reader.read(); ch != -1; ch = reader.read()) {
+                       if (ch == '"')  {
+                               return sectionWriter.toString();
+                       }
+                       sectionWriter.append((char)ch);
+               }
+               throw new IllegalArgumentException("missing closing quote 
('=')");
+       }
+
+       private static class PermissionInfo {
+               String className, name, actions;
+       }
 }

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/test/java/org/apache/clerezza/utils/security/PermissionParserTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/test/java/org/apache/clerezza/utils/security/PermissionParserTest.java?rev=964940&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/test/java/org/apache/clerezza/utils/security/PermissionParserTest.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.utils/src/test/java/org/apache/clerezza/utils/security/PermissionParserTest.java
 Fri Jul 16 20:27:50 2010
@@ -0,0 +1,51 @@
+/*
+ * 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.clerezza.utils.security;
+
+import java.io.FilePermission;
+import java.security.AllPermission;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author reto
+ */
+public class PermissionParserTest {
+       
+       @Test
+       public void parsing() {
+               final FilePermission filePermission = new FilePermission("/", 
"read");
+               Assert.assertEquals(filePermission,
+                               PermissionParser.getPermission(
+                               "(java.io.FilePermission \"/\" \"read\")"));
+               Assert.assertEquals(filePermission,
+                               PermissionParser.getPermission(
+                               filePermission.toString()));
+               Assert.assertEquals(new AllPermission(),
+                               PermissionParser.getPermission(
+                               "(java.security.AllPermission)"));
+               Assert.assertEquals(new AllPermission(),
+                               PermissionParser.getPermission(
+                               "(java.security.AllPermission \"\")"));
+               Assert.assertEquals(new AllPermission(),
+                               PermissionParser.getPermission(
+                               "(java.security.AllPermission \"\" \"\")"));
+       }
+}


Reply via email to