File: TVPermission.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (358 lines) | stat: -rw-r--r-- 10,185 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright (c) 1999, 2015, 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.
 *
 * 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 TVJar;

import java.security.Permission;
import java.security.PermissionCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringJoiner;
import java.util.StringTokenizer;

public class TVPermission extends Permission {

    /**
     * Watch
     */
    private final static int WATCH = 0x1;

    /**
     * Preview
     */
    private final static int PREVIEW = 0x2;

    /**
     * No actions
     */
    private final static int NONE = 0x0;

    /**
     * All actions
     */
    private final static int ALL = WATCH | PREVIEW;

    // the actions mask
    private int mask;

    // the actions string
    private String actions;

    // the canonical name of the channel
    private String cname;

    // true if the channelname is a wildcard
    private boolean wildcard;

    // num range on channel
    private int[] numrange;

    // various num constants
    private final static int NUM_MIN = 1;
    private final static int NUM_MAX = 128;

    public TVPermission(String channel, String action) {
        this(channel, getMask(action));
    }

    TVPermission(String channel, int mask) {
        super(channel);
        init(channel, mask);
    }

    private synchronized int[] parseNum(String num)
            throws Exception {

        if (num == null || num.equals("") || num.equals("*")) {
            wildcard = true;
            return new int[]{NUM_MIN, NUM_MAX};
        }

        int dash = num.indexOf('-');

        if (dash == -1) {
            int p = 0;
            try {
                p = Integer.parseInt(num);
            } catch (NumberFormatException nfe) {
                throw new IllegalArgumentException("invalid input" + num);
            }
            return new int[]{p, p};
        } else {
            String low = num.substring(0, dash);
            String high = num.substring(dash + 1);
            int l, h;

            if (low.equals("")) {
                l = NUM_MIN;
            } else {
                try {
                    l = Integer.parseInt(low);
                } catch (NumberFormatException nfe) {
                    throw new IllegalArgumentException("invalid input" + num);
                }
            }

            if (high.equals("")) {
                h = NUM_MAX;
            } else {
                try {
                    h = Integer.parseInt(high);
                } catch (NumberFormatException nfe) {
                    throw new IllegalArgumentException("invalid input" + num);
                }
            }
            if (h < l || l < NUM_MIN || h > NUM_MAX) {
                throw new IllegalArgumentException("invalid num range");
            }

            return new int[]{l, h};
        }
    }

    /**
     * Initialize the TVPermission object.
     */
    private synchronized void init(String channel, int mask) {

        // Parse the channel name.
        int sep = channel.indexOf(':');

        if (sep != -1) {
            String num = channel.substring(sep + 1);
            cname = channel.substring(0, sep);
            try {
                numrange = parseNum(num);
            } catch (Exception e) {
                throw new IllegalArgumentException("invalid num range: " + num);
            }
        } else {
            numrange = new int[]{NUM_MIN, NUM_MAX};
        }
    }

    /**
     * Convert an action string to an integer actions mask.
     *
     * @param action the action string
     * @return the action mask
     */
    private synchronized static int getMask(String action) {
        int mask = NONE;

        if (action == null) {
            return mask;
        }

        StringTokenizer st = new StringTokenizer(action.toLowerCase(), ",");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (token.equals("watch")) {
                mask |= WATCH;
            } else if (token.equals("preview")) {
                mask |= PREVIEW;
            } else {
                throw new IllegalArgumentException("invalid TV permission: " + token);
            }
        }
        return mask;
    }

    @Override
    public boolean implies(Permission p) {
        if (!(p instanceof TVPermission)) {
            return false;
        }

        if (this.wildcard) {
            return true;
        }

        TVPermission that = (TVPermission) p;

        if ((this.mask & that.mask) != that.mask) {
            System.out.println("Masks are not ok this = "
                    + this.mask + "THat = " + that.mask);
            return false;
        }

        if ((this.numrange[0] > that.numrange[0])
                || (this.numrange[1] < that.numrange[1])) {

            System.out.println("This 0= " + this.numrange[0]
                    + " 1 = " + this.numrange[1]);
            System.out.println("That 0= " + that.numrange[0]
                    + " 1 = " + that.numrange[1]);
            return false;
        }
        return true;
    }

    /**
     * Checks two TVPermission objects for equality.
     * <p>
     * @param obj the object we are testing for equality.
     * @return true if obj is a TVPermission, and has the same channelname and
     * action mask as this TVPermission object.
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof TVPermission)) {
            return false;
        }

        TVPermission that = (TVPermission) obj;

        // check the mask first
        if (this.mask != that.mask) {
            return false;
        }

        // now check the num range...
        if ((this.numrange[0] != that.numrange[0])
                || (this.numrange[1] != that.numrange[1])) {
            return false;
        }

        return this.getName().equals(that.getName());
    }

    /**
     * Returns the hash code value for this object.
     *
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        return this.getName().hashCode();
    }

    /**
     * Return the canonical string representation of the actions. Always returns
     * actions in the following order: watch,preview.
     *
     * @param mask a specific integer action mask to translate into a string
     * @return the canonical string representation of the actions
     */
    private synchronized static String getActions(int mask) {
        StringJoiner sj = new StringJoiner(",");
        if ((mask & WATCH) == WATCH) {
            sj.add("watch");
        }
        if ((mask & PREVIEW) == PREVIEW) {
            sj.add("preview");
        }
        return sj.toString();
    }

    /**
     * Return the canonical string representation of the actions. Always returns
     * actions in the following order: watch,preview.
     *
     * @return the canonical string representation of the actions.
     */
    @Override
    public String getActions() {
        if (actions == null) {
            actions = getActions(this.mask);
        }

        return actions;
    }

    @Override
    public String toString() {
        return super.toString() + "\n"
                + "cname = " + cname + "\n"
                + "wildcard = " + wildcard + "\n"
                + "numrange = " + numrange[0] + "," + numrange[1] + "\n";

    }

    @Override
    public PermissionCollection newPermissionCollection() {
        return new TVPermissionCollection();
    }
}

final class TVPermissionCollection extends PermissionCollection {

    /**
     * The TVPermissions for this set.
     */
    private final ArrayList<TVPermission> permissions = new ArrayList<>();

    /**
     * Adds a permission to the TVPermissions. The key for the hash is the name
     * in the case of wildcards, or all the IP addresses.
     *
     * @param permission the Permission object to add.
     */
    @Override
    public void add(Permission permission) {
        if (!(permission instanceof TVPermission)) {
            throw new IllegalArgumentException("invalid permission: " + permission);
        }
        permissions.add((TVPermission) permission);
    }

    /**
     * Check and see if this collection of permissions implies the permissions
     * expressed in "permission".
     *
     * @param p the Permission object to compare
     *
     * @return true if "permission" is a proper subset of a permission in the
     * collection, false if not.
     */
    @Override
    public boolean implies(Permission p) {
        if (!(p instanceof TVPermission)) {
            return false;
        }

        Iterator<TVPermission> i = permissions.iterator();
        while (i.hasNext()) {
            if (((TVPermission) i.next()).implies(p)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns an enumeration of all the TVPermission objects in the container.
     *
     * @return an enumeration of all the TVPermission objects.
     */
    @Override
    public Enumeration elements() {
        return Collections.enumeration(permissions);
    }

}