Home | History | Annotate | Download | only in security
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package java.security;
     19 
     20 
     21 /**
     22  * {@code AllPermission} represents the permission to perform any operation.
     23  * Since its {@link #implies(Permission)} method always returns {@code true},
     24  * granting this permission is equivalent to disabling security.
     25  */
     26 public final class AllPermission extends Permission {
     27 
     28     /**
     29      * @serial
     30      */
     31     private static final long serialVersionUID = -2916474571451318075L;
     32 
     33     // Permission name
     34     private static final String ALL_PERMISSIONS = "<all permissions>";
     35 
     36     // Actions name
     37     private static final String ALL_ACTIONS = "<all actions>";
     38 
     39     /**
     40      * Constructs a new instance of {@code AllPermission}. The two argument
     41      * version is provided for class {@code Policy} so that it has a consistent
     42      * call pattern across all permissions. The name and action list are both
     43      * ignored.
     44      *
     45      * @param name
     46      *            ignored.
     47      * @param actions
     48      *            ignored.
     49      */
     50     public AllPermission(String name, String actions) {
     51         super(ALL_PERMISSIONS);
     52     }
     53 
     54     /**
     55      * Constructs a new instance of {@code AllPermission}.
     56      */
     57     public AllPermission() {
     58         super(ALL_PERMISSIONS);
     59     }
     60 
     61     /**
     62      * Compares the specified object with this {@code AllPermission} for
     63      * equality and returns {@code true} if the specified object is equal,
     64      * {@code false} otherwise. To be equal, the given object needs to be an
     65      * instance of {@code AllPermission}.
     66      *
     67      * @param obj
     68      *            object to be compared for equality with this {@code
     69      *            AllPermission}.
     70      * @return {@code true} if the specified object is equal to this {@code
     71      *         AllPermission}, otherwise {@code false}.
     72      * @see #hashCode
     73      */
     74     @Override
     75     public boolean equals(Object obj) {
     76         return (obj instanceof AllPermission);
     77     }
     78 
     79     /**
     80      * Returns the hash code value for this {@code AllPermission}. Returns the
     81      * same hash code for {@code AllPermission}s that are equal to each other as
     82      * required by the general contract of {@link Object#hashCode}.
     83      *
     84      * @return the hash code value for this {@code AllPermission}.
     85      * @see Object#equals(Object)
     86      * @see AllPermission#equals(Object)
     87      */
     88     @Override
     89     public int hashCode() {
     90         return 1;
     91     }
     92 
     93     /**
     94      * Returns the actions associated with this {@code AllPermission}. Since
     95      * {@code AllPermission} objects allow all actions, this method returns
     96      * always the string "&lt;all actions&gt;".
     97      *
     98      * @return the actions associated with this {@code AllPermission}.
     99      */
    100     @Override
    101     public String getActions() {
    102         return ALL_ACTIONS;
    103     }
    104 
    105     /**
    106      * Indicates whether the given permission is implied by this permission.
    107      * {@code AllPermission} objects imply all other permissions.
    108      *
    109      * @return always {@code true}.
    110      * @param permission
    111      *            the permission to check.
    112      */
    113     @Override
    114     public boolean implies(Permission permission) {
    115         return true;
    116     }
    117 
    118     /**
    119      * Returns a new {@code PermissionCollection} for holding permissions of
    120      * this class.
    121      *
    122      * @return a new {@code PermissionCollection}.
    123      */
    124     @Override
    125     public PermissionCollection newPermissionCollection() {
    126         return new AllPermissionCollection();
    127     }
    128 }
    129