You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2020/03/21 17:04:30 UTC

[jspwiki] 28/36: extract isJSPWikiPermission to PermissionChecks class in order to break some class cycles

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit b7597bf5a5e514aa39dbf977501b70765d31b6c8
Author: juanpablo <ju...@apache.org>
AuthorDate: Sat Mar 21 14:33:26 2020 +0100

    extract isJSPWikiPermission to PermissionChecks class in order to break some class cycles
---
 .../wiki/auth/permissions/AllPermission.java       | 111 ++++++++-------------
 .../auth/permissions/AllPermissionCollection.java  |  14 +--
 .../wiki/auth/permissions/PermissionChecks.java    |  42 ++++++++
 3 files changed, 91 insertions(+), 76 deletions(-)

diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermission.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermission.java
index 3ebfda0..2096f6a 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermission.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermission.java
@@ -28,78 +28,72 @@ import java.security.PermissionCollection;
  * </p>
  * @since 2.3.80
  */
-public final class AllPermission extends Permission implements Serializable
-{
-    private static final long   serialVersionUID = 1L;
+public final class AllPermission extends Permission implements Serializable {
 
-    private static final String WILDCARD         = "*";
+    private static final long serialVersionUID = 1L;
 
-    private final String        m_wiki;
+    private static final String WILDCARD = "*";
+
+    private final String m_wiki;
 
     /** For serialization purposes. */
-    protected AllPermission()
-    {
-        this(null);
+    protected AllPermission() {
+        this( null );
     }
-    
+
     /**
      * Creates a new AllPermission for the given wikis.
-     * 
+     *
      * @param wiki the wiki to which the permission should apply.  If null, will
      *             apply to all wikis.
      */
-    public AllPermission( String wiki )
-    {
+    public AllPermission( final String wiki ) {
         super( wiki );
         m_wiki = ( wiki == null ) ? WILDCARD : wiki;
     }
 
     /**
      * Two AllPermission objects are considered equal if their wikis are equal.
-     * @see java.lang.Object#equals(java.lang.Object)
-     * 
-     * @return {@inheritDoc}
+     *
      * @param obj {@inheritDoc}
+     * @return {@inheritDoc}
+     * @see java.lang.Object#equals(java.lang.Object)
      */
-    public boolean equals( Object obj )
-    {
-        if ( !( obj instanceof AllPermission ) )
-        {
+    public boolean equals( final Object obj ) {
+        if( !( obj instanceof AllPermission ) ) {
             return false;
         }
-        AllPermission p = (AllPermission) obj;
+        final AllPermission p = ( AllPermission )obj;
         return p.m_wiki != null && p.m_wiki.equals( m_wiki );
     }
 
     /**
      * No-op; always returns <code>null</code>
-     * @see java.security.Permission#getActions()
      *
      * @return Always null.
+     * @see java.security.Permission#getActions()
      */
-    public String getActions()
-    {
+    public String getActions() {
         return null;
     }
 
     /**
      * Returns the name of the wiki containing the page represented by this
      * permission; may return the wildcard string.
+     *
      * @return The wiki
      */
-    public String getWiki()
-    {
+    public String getWiki() {
         return m_wiki;
     }
 
     /**
      * Returns the hash code for this WikiPermission.
-     * @see java.lang.Object#hashCode()
-     * 
+     *
      * @return {@inheritDoc}
+     * @see java.lang.Object#hashCode()
      */
-    public int hashCode()
-    {
+    public int hashCode() {
         return m_wiki.hashCode();
     }
 
@@ -108,35 +102,29 @@ public final class AllPermission extends Permission implements Serializable
      * types are implied. One WikiPermission implies another if all of the other
      * WikiPermission's actions are equal to, or a subset of, those for this
      * permission.
+     *
      * @param permission the permission which may (or may not) be implied by
-     *            this instance
+     *                   this instance
      * @return <code>true</code> if the permission is implied,
-     *         <code>false</code> otherwise
+     * <code>false</code> otherwise
      * @see java.security.Permission#implies(java.security.Permission)
      */
-    public boolean implies( Permission permission )
-    {
+    public boolean implies( final Permission permission ) {
         // Permission must be a JSPWiki permission, PagePermission or AllPermission
-        if ( !isJSPWikiPermission( permission ) )
-        {
+        if( !PermissionChecks.isJSPWikiPermission( permission ) ) {
             return false;
         }
         String wiki = null;
-        if ( permission instanceof AllPermission )
-        {
-            wiki = ( (AllPermission) permission ).getWiki();
+        if( permission instanceof AllPermission ) {
+            wiki = ( ( AllPermission )permission ).getWiki();
+        } else if( permission instanceof PagePermission ) {
+            wiki = ( ( PagePermission )permission ).getWiki();
         }
-        else if ( permission instanceof PagePermission )
-        {
-            wiki = ( (PagePermission) permission ).getWiki();
+        if( permission instanceof WikiPermission ) {
+            wiki = ( ( WikiPermission )permission ).getWiki();
         }
-        if ( permission instanceof WikiPermission )
-        {
-            wiki = ( (WikiPermission) permission ).getWiki();
-        }
-        if ( permission instanceof GroupPermission )
-        {
-            wiki = ( (GroupPermission) permission ).getWiki();
+        if( permission instanceof GroupPermission ) {
+            wiki = ( ( GroupPermission )permission ).getWiki();
         }
 
         // If the wiki is implied, it's allowed
@@ -145,37 +133,22 @@ public final class AllPermission extends Permission implements Serializable
 
     /**
      * Returns a new {@link AllPermissionCollection}.
-     * @see java.security.Permission#newPermissionCollection()
-     * 
+     *
      * @return {@inheritDoc}
+     * @see java.security.Permission#newPermissionCollection()
      */
-    public PermissionCollection newPermissionCollection()
-    {
+    public PermissionCollection newPermissionCollection() {
         return new AllPermissionCollection();
     }
 
     /**
      * Prints a human-readable representation of this permission.
-     * @see java.lang.Object#toString()
+     *
      * @return {@inheritDoc}
+     * @see java.lang.Object#toString()
      */
-    public String toString()
-    {
+    public String toString() {
         return "(\"" + this.getClass().getName() + "\",\"" + m_wiki + "\")";
     }
 
-    /**
-     *  Checks if the given permission is one of ours.
-     *  
-     *  @param permission Permission to check
-     *  @return true, if the permission is a JSPWiki permission; false otherwise.
-     */
-    protected static boolean isJSPWikiPermission( Permission permission )
-    {
-        return   permission instanceof WikiPermission ||
-                 permission instanceof PagePermission ||
-                 permission instanceof GroupPermission ||
-                 permission instanceof AllPermission;
-    }
-
 }
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermissionCollection.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermissionCollection.java
index 2af45cb..965a1fd 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermissionCollection.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/AllPermissionCollection.java
@@ -35,7 +35,7 @@ public class AllPermissionCollection extends PermissionCollection
 
     private boolean           m_readOnly      = false;
 
-    protected final Hashtable<Permission, Permission> m_permissions    = new Hashtable<Permission, Permission>();
+    protected final Hashtable<Permission, Permission> m_permissions    = new Hashtable<>();
 
     /**
      * Adds an AllPermission object to this AllPermissionCollection. If this
@@ -47,9 +47,9 @@ public class AllPermissionCollection extends PermissionCollection
      * @param permission {@inheritDoc}
      */
     @Override
-    public void add( Permission permission )
+    public void add( final Permission permission )
     {
-        if ( !AllPermission.isJSPWikiPermission( permission ) )
+        if ( !PermissionChecks.isJSPWikiPermission( permission ) )
         {
             throw new IllegalArgumentException(
                     "Permission must be of type org.apache.wiki.permissions.*Permission." );
@@ -96,7 +96,7 @@ public class AllPermissionCollection extends PermissionCollection
      * 
      * @return {@inheritDoc}
      */
-    public boolean implies( Permission permission )
+    public boolean implies( final Permission permission )
     {
         // If nothing in the collection yet, fail fast
         if ( !m_notEmpty )
@@ -105,16 +105,16 @@ public class AllPermissionCollection extends PermissionCollection
         }
 
         // If not one of our permission types, it's not implied
-        if ( !AllPermission.isJSPWikiPermission( permission ) )
+        if ( !PermissionChecks.isJSPWikiPermission( permission ) )
         {
             return false;
         }
 
         // Step through each AllPermission
-        Enumeration<Permission> permEnum = m_permissions.elements();
+        final Enumeration<Permission> permEnum = m_permissions.elements();
         while( permEnum.hasMoreElements() )
         {
-            Permission storedPermission = permEnum.nextElement();
+            final Permission storedPermission = permEnum.nextElement();
             if ( storedPermission.implies( permission ) )
             {
                 return true;
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/PermissionChecks.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/PermissionChecks.java
new file mode 100644
index 0000000..7eceb96
--- /dev/null
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/PermissionChecks.java
@@ -0,0 +1,42 @@
+/*
+    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.wiki.auth.permissions;
+
+import java.security.Permission;
+
+
+/**
+ * Ususal permission checks
+ */
+class PermissionChecks {
+
+    /**
+     *  Checks if the given permission is one of ours.
+     *
+     *  @param permission Permission to check
+     *  @return true, if the permission is a JSPWiki permission; false otherwise.
+     */
+    static boolean isJSPWikiPermission( final Permission permission ) {
+        return permission instanceof WikiPermission  ||
+               permission instanceof PagePermission  ||
+               permission instanceof GroupPermission ||
+               permission instanceof AllPermission;
+    }
+
+}