changeset 33:a60958f44b0c

8055914: DevicePermission exposes API not defined by spec Summary: Methods isValidAction, actionImplies, equals, verifyAndOrderActions moved to a separate class. Reviewed-by: jld Contributed-by: alexey.mironov@oracle.com
author alkonsta
date Tue, 26 Aug 2014 19:21:36 +0400
parents 249f79a848af
children 93cb5cb04f26
files src/share/classes/com/oracle/dio/utils/ActionFactory.java src/share/classes/jdk/dio/DeviceMgmtPermission.java src/share/classes/jdk/dio/DevicePermission.java src/share/classes/jdk/dio/atcmd/ATPermission.java src/share/classes/jdk/dio/gpio/GPIOPinPermission.java src/share/classes/jdk/dio/gpio/GPIOPortPermission.java
diffstat 6 files changed, 186 insertions(+), 190 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/com/oracle/dio/utils/ActionFactory.java	Tue Aug 26 19:21:36 2014 +0400
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.oracle.dio.utils;
+
+import java.security.Permission;
+import jdk.dio.DevicePermission;
+import jdk.dio.DeviceMgmtPermission;
+import jdk.dio.atcmd.ATPermission;
+import jdk.dio.gpio.GPIOPinPermission;
+import jdk.dio.gpio.GPIOPortPermission;
+
+/**
+ * Utility class for device permission class
+ */
+public class ActionFactory {
+    /**
+     * Return true if all actions are applied for permission
+     *
+     * @param actions for validation
+     * @param permission to verify which action implied
+     *
+     * @return true for valid
+     */
+    public static boolean actionImplies(String actions, Permission permission) {
+        int index = actions.indexOf(",");
+        if (index == -1) {
+            return isValidAction(actions, permission) && (permission.getActions().indexOf(actions) != -1);
+        } else {
+            return actionImplies(actions.substring(0, index), permission) &&
+                   actionImplies(actions.substring(index+1), permission);
+        }
+    }
+
+    /**
+     * Return true for permission specified actions
+     *
+     * @param action for validation
+     * @param permission to verify which action implied
+     *
+     * @return true for valid
+     */
+    private static boolean isValidAction(String action, Permission permission) {
+        if (permission instanceof DevicePermission) {
+            if (DevicePermission.OPEN.equals(action)) return true;
+            if (DevicePermission.POWER_MANAGE.equals(action)) return true;
+        }
+        if (permission instanceof DeviceMgmtPermission) {
+            if (DeviceMgmtPermission.REGISTER.equals(action)) return true;
+            if (DeviceMgmtPermission.UNREGISTER.equals(action)) return true;
+            if (DeviceMgmtPermission.OPEN.equals(action)) return true;
+        }
+        if (permission instanceof ATPermission)
+            if (ATPermission.DATA.equals(action)) return true;
+        if (permission instanceof GPIOPinPermission)
+            if (GPIOPinPermission.SET_DIRECTION.equals(action)) return true;
+        if (permission instanceof GPIOPortPermission)
+            if (GPIOPortPermission.SET_DIRECTION.equals(action)) return true;
+
+        return false;
+    }
+
+    /**
+     * Returns action list in spec required order
+     * <p>
+     * It is assumed that actions have the following priority
+     * <ul>
+     *     <li> open </li>
+     *     <li> powermanage </li>
+     *     <li> other peripheral specific actions </li>
+     * </ul>
+     *
+     * @param actions unordered and {@code verified} list
+     * @param permission to verify which action implied
+     *
+     * @return ordered list
+     */
+    public static String verifyAndOrderActions(String actions, Permission permission) {
+        int idx = 0;
+        int prevIdx = 0;
+        while (-1 != (idx = actions.indexOf(',', prevIdx))) {
+            String s1 = actions.substring(prevIdx, idx);
+            // check for duplicate and validity
+            if (!isValidAction(s1, permission) || -1 != actions.indexOf(s1, idx)) {
+                throw new IllegalArgumentException(actions);
+            }
+            prevIdx = idx + 1;
+        }
+        if (!isValidAction(actions.substring(prevIdx), permission) ) {
+                throw new IllegalArgumentException(actions);
+        }
+
+        StringBuilder sb = new StringBuilder(30);
+        if (permission instanceof DeviceMgmtPermission) {
+            if (-1 != actions.indexOf(DeviceMgmtPermission.REGISTER)) {
+                sb.append(DeviceMgmtPermission.REGISTER);
+                idx = 1;
+            }
+            if (-1 != actions.indexOf(DeviceMgmtPermission.UNREGISTER)) {
+                if (idx > 0 ) {
+                    sb.append(',');
+                }
+                sb.append(DeviceMgmtPermission.UNREGISTER);
+                idx = 1;
+            }
+
+            if (-1 != actions.indexOf(DeviceMgmtPermission.OPEN)) {
+                if (idx > 0 ) {
+                    sb.append(',');
+                }
+                sb.append(DeviceMgmtPermission.OPEN);
+                idx = 1;
+            }
+        }
+        if (permission instanceof DevicePermission) {
+            if (-1 != actions.indexOf(DevicePermission.OPEN)) {
+                sb.append(DevicePermission.OPEN);
+                idx = 1;
+            }
+            if (-1 != actions.indexOf(DevicePermission.POWER_MANAGE)) {
+                if (idx > 0 ) {
+                    sb.append(',');
+                }
+                sb.append(DevicePermission.POWER_MANAGE);
+                idx = 1;
+            }
+        }
+        if ((permission instanceof ATPermission) && -1 != actions.indexOf(ATPermission.DATA)) {
+            if (idx > 0 ) {
+                sb.append(',');
+            }
+            sb.append(ATPermission.DATA);
+            idx = 1;
+        }
+        if ((permission instanceof GPIOPinPermission) && -1 != actions.indexOf(GPIOPinPermission.SET_DIRECTION)) {
+            if (idx > 0 ) {
+                sb.append(',');
+            }
+            sb.append(GPIOPinPermission.SET_DIRECTION);
+            idx = 1;
+        }
+        if ((permission instanceof GPIOPortPermission) && -1 != actions.indexOf(GPIOPortPermission.SET_DIRECTION)) {
+            if (idx > 0 ) {
+                sb.append(',');
+            }
+            sb.append(GPIOPortPermission.SET_DIRECTION);
+            idx = 1;
+        }
+        return sb.toString();
+    }
+}
\ No newline at end of file
--- a/src/share/classes/jdk/dio/DeviceMgmtPermission.java	Fri Aug 22 12:44:34 2014 -0400
+++ b/src/share/classes/jdk/dio/DeviceMgmtPermission.java	Tue Aug 26 19:21:36 2014 +0400
@@ -27,6 +27,7 @@
 
 import java.security.Permission;
 import java.security.PermissionCollection;
+import com.oracle.dio.utils.ActionFactory;
 import java.util.Vector;
 import com.oracle.dio.utils.ExceptionMessage;
 
@@ -122,70 +123,13 @@
      *             specified possible actions.
      */
     public DeviceMgmtPermission(String name, String actions) {
-        super(name);
+        super(name.toString());
         if (null == actions) {
             throw new IllegalArgumentException(
                 ExceptionMessage.format(ExceptionMessage.DEVICE_NULL_ACTIONS)
             );
         }
-        myActions = verifyAndOrderActions(actions);
-    }
-
-    /**
-     * Returns action list in spec required order
-     * <p>
-     * It is assumed that actions have the following priority
-     * <ul>
-     *     <li> register </li>
-     *     <li> unregister </li>
-     *     <li> open </li>
-     * </ul>
-     *
-     * @param actions unordered and {@code verified} list
-     *
-     * @return ordered list
-     */
-    private String verifyAndOrderActions(String actions) throws IllegalArgumentException {
-        int idx = 0;
-        int prevIdx = 0;
-        while (-1 != (idx = actions.indexOf(',', prevIdx))) {
-            if (!isValidAction(actions.substring(prevIdx, idx))) {
-                throw new IllegalArgumentException(actions);
-            }
-            prevIdx = idx + 1;
-        }
-        if (!isValidAction(actions.substring(prevIdx)) ) {
-                throw new IllegalArgumentException(actions);
-        }
-
-        StringBuilder sb = new StringBuilder(30);
-        if (-1 != actions.indexOf(REGISTER)) {
-            sb.append(REGISTER);
-            idx = 1;
-        }
-        if (-1 != actions.indexOf(UNREGISTER)) {
-            if (idx > 0 ) {
-                sb.append(',');
-            }
-            sb.append(UNREGISTER);
-            idx = 1;
-        }
-
-        if (-1 != actions.indexOf(OPEN)) {
-            if (idx > 0 ) {
-                sb.append(',');
-            }
-            sb.append(OPEN);
-        }
-        return sb.toString();
-    }
-
-
-    private boolean isValidAction(String action) {
-        if (REGISTER.equals(action)) return true;
-        if (UNREGISTER.equals(action)) return true;
-        if (OPEN.equals(action)) return true;
-        return false;
+        myActions = ActionFactory.verifyAndOrderActions(actions, this);
     }
 
     /**
@@ -223,17 +167,6 @@
         return getName().hashCode();
     }
 
-    /* Copied from DevicePermission. It is cheaper than to introduce new class */
-    protected boolean actionImplies(String actions) {
-        int index = actions.indexOf(",");
-        if (index == -1) {
-            return isValidAction(actions) && (myActions.indexOf(actions) != -1);
-        } else {
-            return actionImplies(actions.substring(0, index)) &&
-                   actionImplies(actions.substring(index+1));
-        }
-    }
-
     /**
      * Checks if this {@code DeviceMgmtPermission} object "implies" the specified permission.
      * <p />
@@ -256,7 +189,7 @@
         if ((permission == null) || (permission.getClass() != getClass()))
             return false;
 
-        if (!actionImplies(permission.getActions())) return false;
+        if (!ActionFactory.actionImplies(permission.getActions(), this)) return false;
 
         int idx = 0;
         String name = permission.getName();
--- a/src/share/classes/jdk/dio/DevicePermission.java	Fri Aug 22 12:44:34 2014 -0400
+++ b/src/share/classes/jdk/dio/DevicePermission.java	Tue Aug 26 19:21:36 2014 +0400
@@ -30,6 +30,7 @@
 import java.util.Enumeration;
 import java.util.Vector;
 import com.oracle.dio.utils.ExceptionMessage;
+import com.oracle.dio.utils.ActionFactory;
 
 /**
  * The {@code DevicePermission} abstract class is the superclass of all device permissions.
@@ -108,7 +109,7 @@
      * Coma-separated action list
      *
      */
-    protected String myActions;
+    private String myActions;
 
     /**
      * Constructs a new {@code DevicePermission} with the specified
@@ -121,7 +122,7 @@
      * @see #getName getName
      */
     public DevicePermission(String name) {
-        super(name);
+        super(checkName(name));
         if(name == null){
             throw new NullPointerException(
                 ExceptionMessage.format(ExceptionMessage.DEVICE_NULL_NAME)
@@ -130,6 +131,10 @@
         myActions = OPEN;
     }
 
+    private static String checkName(String name) {
+        return name;
+    }
+
     /**
      * Constructs a new {@code DevicePermission} instance with the specified target name and
      * action list.
@@ -159,51 +164,7 @@
             );
         }
 
-        myActions = verifyAndOrderActions(actions);
-    }
-
-    /**
-     * Returns action list in spec required order
-     * <p>
-     * It is assumed that actions have the following priority
-     * <ul>
-     *     <li> open </li>
-     *     <li> powermanage </li>
-     *     <li> other peripheral specific actions </li>
-     * </ul>
-     *
-     * @param actions unordered and {@code verified} list
-     *
-     * @return ordered list
-     */
-    protected String verifyAndOrderActions(String actions) {
-        int idx = 0;
-        int prevIdx = 0;
-        while (-1 != (idx = actions.indexOf(',', prevIdx))) {
-            String s1 = actions.substring(prevIdx, idx);
-            // check for duplicate and validity
-            if (!isValidAction(s1) || -1 != actions.indexOf(s1, idx)) {
-                throw new IllegalArgumentException(actions);
-            }
-            prevIdx = idx + 1;
-        }
-        if (!isValidAction(actions.substring(prevIdx)) ) {
-                throw new IllegalArgumentException(actions);
-        }
-
-        StringBuilder sb = new StringBuilder(30);
-        if (-1 != actions.indexOf(OPEN)) {
-            sb.append(OPEN);
-            idx = 1;
-        }
-        if (-1 != actions.indexOf(POWER_MANAGE)) {
-            if (idx > 0 ) {
-                sb.append(',');
-            }
-            sb.append(POWER_MANAGE);
-            idx = 1;
-        }
-        return sb.toString();
+        myActions = ActionFactory.verifyAndOrderActions(actions, this);
     }
 
     /**
@@ -264,24 +225,6 @@
     }
 
 
-    // Override function at child class to add new action
-    protected boolean isValidAction(String action) {
-        if (OPEN.equals(action)) return true;
-        if (POWER_MANAGE.equals(action)) return true;
-        return false;
-    }
-
-    protected boolean actionImplies(String actions) {
-        int index = actions.indexOf(",");
-        if (index == -1) {
-            return isValidAction(actions) && (myActions.indexOf(actions) != -1);
-        } else {
-            return actionImplies(actions.substring(0, index)) &&
-                   actionImplies(actions.substring(index+1));
-        }
-    }
-
-
     /**
      * Checks if this {@code DevicePermission} object "implies" the specified permission.
      * <p />
@@ -304,7 +247,7 @@
         if ((permission == null) || (permission.getClass() != getClass()))
             return false;
 
-        if (!actionImplies(permission.getActions())) return false;
+        if (!ActionFactory.actionImplies(permission.getActions(), this)) return false;
 
         int idx = 0;
         String name = permission.getName();
--- a/src/share/classes/jdk/dio/atcmd/ATPermission.java	Fri Aug 22 12:44:34 2014 -0400
+++ b/src/share/classes/jdk/dio/atcmd/ATPermission.java	Tue Aug 26 19:21:36 2014 +0400
@@ -98,22 +98,4 @@
     public ATPermission(String name, String actions) {
         super(name, actions);
     }
-
-    // verifies AT specific permission
-    protected boolean isValidAction(String action) {
-        if (DATA.equals(action)) return true;
-        return super.isValidAction(action);
-    }
-
-    @Override
-    protected String verifyAndOrderActions(String actions) {
-        String s = super.verifyAndOrderActions(actions);
-        if (-1 != actions.indexOf(DATA)) {
-            if (s.length() > 0) {
-                s += ",";
-            }
-            s += DATA;
-        }
-        return s;
-    }
 }
--- a/src/share/classes/jdk/dio/gpio/GPIOPinPermission.java	Fri Aug 22 12:44:34 2014 -0400
+++ b/src/share/classes/jdk/dio/gpio/GPIOPinPermission.java	Tue Aug 26 19:21:36 2014 +0400
@@ -100,21 +100,4 @@
     public GPIOPinPermission(String name, String actions) {
         super(name, actions);
     }
-
-    protected boolean isValidAction(String action) {
-        if (SET_DIRECTION.equals(action)) return true;
-        return super.isValidAction(action);
-    }
-
-    protected String verifyAndOrderActions(String actions) {
-        String s = super.verifyAndOrderActions(actions);
-        if (-1 != actions.indexOf(SET_DIRECTION)) {
-            if (s.length() > 0) {
-                s += ",";
-            }
-            s = s + SET_DIRECTION;
-        }
-        return s;
-    }
 }
-
--- a/src/share/classes/jdk/dio/gpio/GPIOPortPermission.java	Fri Aug 22 12:44:34 2014 -0400
+++ b/src/share/classes/jdk/dio/gpio/GPIOPortPermission.java	Tue Aug 26 19:21:36 2014 +0400
@@ -98,22 +98,4 @@
     public GPIOPortPermission(String name, String actions) {
         super(name, actions);
     }
-
-    protected boolean isValidAction(String action) {
-        if (SET_DIRECTION.equals(action)) return true;
-        return super.isValidAction(action);
-    }
-
-    @Override
-    protected String verifyAndOrderActions(String actions) {
-        String s = super.verifyAndOrderActions(actions);
-        if (-1 != actions.indexOf(SET_DIRECTION)) {
-            if (s.length() > 0) {
-                s += ",";
-            }
-            s += SET_DIRECTION;
-        }
-        return s;
-    }
-
 }