Path: blob/master/test/jdk/java/security/Policy/ExtensiblePolicy/TVJar/TVPermission.java
41159 views
/*1* Copyright (c) 1999, 2015, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package TVJar;2324import java.security.Permission;25import java.security.PermissionCollection;26import java.util.ArrayList;27import java.util.Collections;28import java.util.Enumeration;29import java.util.Iterator;30import java.util.StringJoiner;31import java.util.StringTokenizer;3233public class TVPermission extends Permission {3435/**36* Watch37*/38private final static int WATCH = 0x1;3940/**41* Preview42*/43private final static int PREVIEW = 0x2;4445/**46* No actions47*/48private final static int NONE = 0x0;4950/**51* All actions52*/53private final static int ALL = WATCH | PREVIEW;5455// the actions mask56private int mask;5758// the actions string59private String actions;6061// the canonical name of the channel62private String cname;6364// true if the channelname is a wildcard65private boolean wildcard;6667// num range on channel68private int[] numrange;6970// various num constants71private final static int NUM_MIN = 1;72private final static int NUM_MAX = 128;7374public TVPermission(String channel, String action) {75this(channel, getMask(action));76}7778TVPermission(String channel, int mask) {79super(channel);80init(channel, mask);81}8283private synchronized int[] parseNum(String num)84throws Exception {8586if (num == null || num.equals("") || num.equals("*")) {87wildcard = true;88return new int[]{NUM_MIN, NUM_MAX};89}9091int dash = num.indexOf('-');9293if (dash == -1) {94int p = 0;95try {96p = Integer.parseInt(num);97} catch (NumberFormatException nfe) {98throw new IllegalArgumentException("invalid input" + num);99}100return new int[]{p, p};101} else {102String low = num.substring(0, dash);103String high = num.substring(dash + 1);104int l, h;105106if (low.equals("")) {107l = NUM_MIN;108} else {109try {110l = Integer.parseInt(low);111} catch (NumberFormatException nfe) {112throw new IllegalArgumentException("invalid input" + num);113}114}115116if (high.equals("")) {117h = NUM_MAX;118} else {119try {120h = Integer.parseInt(high);121} catch (NumberFormatException nfe) {122throw new IllegalArgumentException("invalid input" + num);123}124}125if (h < l || l < NUM_MIN || h > NUM_MAX) {126throw new IllegalArgumentException("invalid num range");127}128129return new int[]{l, h};130}131}132133/**134* Initialize the TVPermission object.135*/136private synchronized void init(String channel, int mask) {137138// Parse the channel name.139int sep = channel.indexOf(':');140141if (sep != -1) {142String num = channel.substring(sep + 1);143cname = channel.substring(0, sep);144try {145numrange = parseNum(num);146} catch (Exception e) {147throw new IllegalArgumentException("invalid num range: " + num);148}149} else {150numrange = new int[]{NUM_MIN, NUM_MAX};151}152}153154/**155* Convert an action string to an integer actions mask.156*157* @param action the action string158* @return the action mask159*/160private synchronized static int getMask(String action) {161int mask = NONE;162163if (action == null) {164return mask;165}166167StringTokenizer st = new StringTokenizer(action.toLowerCase(), ",");168while (st.hasMoreTokens()) {169String token = st.nextToken();170if (token.equals("watch")) {171mask |= WATCH;172} else if (token.equals("preview")) {173mask |= PREVIEW;174} else {175throw new IllegalArgumentException("invalid TV permission: " + token);176}177}178return mask;179}180181@Override182public boolean implies(Permission p) {183if (!(p instanceof TVPermission)) {184return false;185}186187if (this.wildcard) {188return true;189}190191TVPermission that = (TVPermission) p;192193if ((this.mask & that.mask) != that.mask) {194System.out.println("Masks are not ok this = "195+ this.mask + "THat = " + that.mask);196return false;197}198199if ((this.numrange[0] > that.numrange[0])200|| (this.numrange[1] < that.numrange[1])) {201202System.out.println("This 0= " + this.numrange[0]203+ " 1 = " + this.numrange[1]);204System.out.println("That 0= " + that.numrange[0]205+ " 1 = " + that.numrange[1]);206return false;207}208return true;209}210211/**212* Checks two TVPermission objects for equality.213* <p>214* @param obj the object we are testing for equality.215* @return true if obj is a TVPermission, and has the same channelname and216* action mask as this TVPermission object.217*/218@Override219public boolean equals(Object obj) {220if (obj == this) {221return true;222}223224if (!(obj instanceof TVPermission)) {225return false;226}227228TVPermission that = (TVPermission) obj;229230// check the mask first231if (this.mask != that.mask) {232return false;233}234235// now check the num range...236if ((this.numrange[0] != that.numrange[0])237|| (this.numrange[1] != that.numrange[1])) {238return false;239}240241return this.getName().equals(that.getName());242}243244/**245* Returns the hash code value for this object.246*247* @return a hash code value for this object.248*/249@Override250public int hashCode() {251return this.getName().hashCode();252}253254/**255* Return the canonical string representation of the actions. Always returns256* actions in the following order: watch,preview.257*258* @param mask a specific integer action mask to translate into a string259* @return the canonical string representation of the actions260*/261private synchronized static String getActions(int mask) {262StringJoiner sj = new StringJoiner(",");263if ((mask & WATCH) == WATCH) {264sj.add("watch");265}266if ((mask & PREVIEW) == PREVIEW) {267sj.add("preview");268}269return sj.toString();270}271272/**273* Return the canonical string representation of the actions. Always returns274* actions in the following order: watch,preview.275*276* @return the canonical string representation of the actions.277*/278@Override279public String getActions() {280if (actions == null) {281actions = getActions(this.mask);282}283284return actions;285}286287@Override288public String toString() {289return super.toString() + "\n"290+ "cname = " + cname + "\n"291+ "wildcard = " + wildcard + "\n"292+ "numrange = " + numrange[0] + "," + numrange[1] + "\n";293294}295296@Override297public PermissionCollection newPermissionCollection() {298return new TVPermissionCollection();299}300}301302final class TVPermissionCollection extends PermissionCollection {303304/**305* The TVPermissions for this set.306*/307private final ArrayList<TVPermission> permissions = new ArrayList<>();308309/**310* Adds a permission to the TVPermissions. The key for the hash is the name311* in the case of wildcards, or all the IP addresses.312*313* @param permission the Permission object to add.314*/315@Override316public void add(Permission permission) {317if (!(permission instanceof TVPermission)) {318throw new IllegalArgumentException("invalid permission: " + permission);319}320permissions.add((TVPermission) permission);321}322323/**324* Check and see if this collection of permissions implies the permissions325* expressed in "permission".326*327* @param p the Permission object to compare328*329* @return true if "permission" is a proper subset of a permission in the330* collection, false if not.331*/332@Override333public boolean implies(Permission p) {334if (!(p instanceof TVPermission)) {335return false;336}337338Iterator<TVPermission> i = permissions.iterator();339while (i.hasNext()) {340if (((TVPermission) i.next()).implies(p)) {341return true;342}343}344return false;345}346347/**348* Returns an enumeration of all the TVPermission objects in the container.349*350* @return an enumeration of all the TVPermission objects.351*/352@Override353public Enumeration elements() {354return Collections.enumeration(permissions);355}356357}358359360