Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/rmi/rmid/ExecOptionPermission.java
38923 views
/*1* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.rmi.rmid;2627import java.security.*;28import java.io.*;29import java.util.*;3031/**32* The ExecOptionPermission class represents permission for rmid to use33* a specific command-line option when launching an activation group.34* <P>35*36* @author Ann Wollrath37*38* @serial exclude39*/40public final class ExecOptionPermission extends Permission41{42/**43* does this permission have a wildcard at the end?44*/45private transient boolean wildcard;4647/**48* the name without the wildcard on the end49*/50private transient String name;5152/**53* UID for serialization54*/55private static final long serialVersionUID = 5842294756823092756L;5657public ExecOptionPermission(String name) {58super(name);59init(name);60}6162public ExecOptionPermission(String name, String actions) {63this(name);64}6566/**67* Checks if the specified permission is "implied" by68* this object.69* <P>70* More specifically, this method returns true if:<p>71* <ul>72* <li> <i>p</i>'s class is the same as this object's class, and<p>73* <li> <i>p</i>'s name equals or (in the case of wildcards)74* is implied by this object's75* name. For example, "a.b.*" implies "a.b.c", and76* "a.b=*" implies "a.b=c"77* </ul>78*79* @param p the permission to check against.80*81* @return true if the passed permission is equal to or82* implied by this permission, false otherwise.83*/84public boolean implies(Permission p) {85if (!(p instanceof ExecOptionPermission))86return false;8788ExecOptionPermission that = (ExecOptionPermission) p;8990if (this.wildcard) {91if (that.wildcard) {92// one wildcard can imply another93return that.name.startsWith(name);94} else {95// make sure p.name is longer so a.b.* doesn't imply a.b96return (that.name.length() > this.name.length()) &&97that.name.startsWith(this.name);98}99} else {100if (that.wildcard) {101// a non-wildcard can't imply a wildcard102return false;103} else {104return this.name.equals(that.name);105}106}107}108109/**110* Checks two ExecOptionPermission objects for equality.111* Checks that <i>obj</i>'s class is the same as this object's class112* and has the same name as this object.113* <P>114* @param obj the object we are testing for equality with this object.115* @return true if <i>obj</i> is an ExecOptionPermission, and has the same116* name as this ExecOptionPermission object, false otherwise.117*/118public boolean equals(Object obj) {119if (obj == this)120return true;121122if ((obj == null) || (obj.getClass() != getClass()))123return false;124125ExecOptionPermission that = (ExecOptionPermission) obj;126127return this.getName().equals(that.getName());128}129130131/**132* Returns the hash code value for this object.133* The hash code used is the hash code of the name, that is,134* <code>getName().hashCode()</code>, where <code>getName</code> is135* from the Permission superclass.136*137* @return a hash code value for this object.138*/139public int hashCode() {140return this.getName().hashCode();141}142143/**144* Returns the canonical string representation of the actions.145*146* @return the canonical string representation of the actions.147*/148public String getActions() {149return "";150}151152/**153* Returns a new PermissionCollection object for storing154* ExecOptionPermission objects.155* <p>156* A ExecOptionPermissionCollection stores a collection of157* ExecOptionPermission permissions.158*159* <p>ExecOptionPermission objects must be stored in a manner that allows160* them to be inserted in any order, but that also enables the161* PermissionCollection <code>implies</code> method162* to be implemented in an efficient (and consistent) manner.163*164* @return a new PermissionCollection object suitable for165* storing ExecOptionPermissions.166*/167public PermissionCollection newPermissionCollection() {168return new ExecOptionPermissionCollection();169}170171/**172* readObject is called to restore the state of the ExecOptionPermission173* from a stream.174*/175private synchronized void readObject(java.io.ObjectInputStream s)176throws IOException, ClassNotFoundException177{178s.defaultReadObject();179// init is called to initialize the rest of the values.180init(getName());181}182183/**184* Initialize a ExecOptionPermission object. Common to all constructors.185* Also called during de-serialization.186*/187private void init(String name)188{189if (name == null)190throw new NullPointerException("name can't be null");191192if (name.equals("")) {193throw new IllegalArgumentException("name can't be empty");194}195196if (name.endsWith(".*") || name.endsWith("=*") || name.equals("*")) {197wildcard = true;198if (name.length() == 1) {199this.name = "";200} else {201this.name = name.substring(0, name.length()-1);202}203} else {204this.name = name;205}206}207208/**209* A ExecOptionPermissionCollection stores a collection210* of ExecOptionPermission permissions. ExecOptionPermission objects211* must be stored in a manner that allows them to be inserted in any212* order, but enable the implies function to evaluate the implies213* method in an efficient (and consistent) manner.214*215* A ExecOptionPermissionCollection handles comparing a permission like216* "a.b.c.d.e" * with a Permission such as "a.b.*", or "*".217*218* @serial include219*/220private static class ExecOptionPermissionCollection221extends PermissionCollection222implements java.io.Serializable223{224225private Hashtable<String, Permission> permissions;226private boolean all_allowed; // true if "*" is in the collection227private static final long serialVersionUID = -1242475729790124375L;228229/**230* Create an empty ExecOptionPermissionCollection.231*/232public ExecOptionPermissionCollection() {233permissions = new Hashtable<>(11);234all_allowed = false;235}236237/**238* Adds a permission to the collection. The key for the hash is239* permission.name.240*241* @param permission the Permission object to add.242*243* @exception IllegalArgumentException - if the permission is not a244* ExecOptionPermission245*246* @exception SecurityException - if this ExecOptionPermissionCollection247* object has been marked readonly248*/249250public void add(Permission permission)251{252if (! (permission instanceof ExecOptionPermission))253throw new IllegalArgumentException("invalid permission: "+254permission);255if (isReadOnly())256throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");257258ExecOptionPermission p = (ExecOptionPermission) permission;259260permissions.put(p.getName(), permission);261if (!all_allowed) {262if (p.getName().equals("*"))263all_allowed = true;264}265}266267/**268* Check and see if this set of permissions implies the permissions269* expressed in "permission".270*271* @param p the Permission object to compare272*273* @return true if "permission" is a proper subset of a permission in274* the set, false if not.275*/276public boolean implies(Permission permission)277{278if (! (permission instanceof ExecOptionPermission))279return false;280281ExecOptionPermission p = (ExecOptionPermission) permission;282283// short circuit if the "*" Permission was added284if (all_allowed)285return true;286287// strategy:288// Check for full match first. Then work our way up the289// name looking for matches on a.b.*290291String pname = p.getName();292293Permission x = permissions.get(pname);294295if (x != null)296// we have a direct hit!297return x.implies(permission);298299300// work our way up the tree...301int last, offset;302303offset = pname.length() - 1;304305while ((last = pname.lastIndexOf(".", offset)) != -1) {306307pname = pname.substring(0, last+1) + "*";308x = permissions.get(pname);309310if (x != null) {311return x.implies(permission);312}313offset = last - 1;314}315316// check for "=*" wildcard match317pname = p.getName();318offset = pname.length() - 1;319320while ((last = pname.lastIndexOf("=", offset)) != -1) {321322pname = pname.substring(0, last+1) + "*";323x = permissions.get(pname);324325if (x != null) {326return x.implies(permission);327}328offset = last - 1;329}330331// we don't have to check for "*" as it was already checked332// at the top (all_allowed), so we just return false333return false;334}335336/**337* Returns an enumeration of all the ExecOptionPermission objects in the338* container.339*340* @return an enumeration of all the ExecOptionPermission objects.341*/342343public Enumeration<Permission> elements()344{345return permissions.elements();346}347}348}349350351