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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Alexey V. Varlamov
23 package org.apache.harmony.security;
26 import java.security.CodeSigner;
27 import java.security.CodeSource;
28 import java.security.Permission;
29 import java.security.Principal;
30 import java.util.Collection;
31 import java.util.Collections;
32 import org.apache.harmony.security.fortress.PolicyUtils;
35 * This class represents an elementary block of a security policy. It associates
36 * a CodeSource of an executable code, Principals allowed to execute the code,
37 * and a set of granted Permissions.
39 * @see org.apache.harmony.security.fortress.DefaultPolicy
41 public class PolicyEntry {
44 private final CodeSource cs;
46 // Array of principals
47 private final Principal[] principals;
49 // Permissions collection
50 private final Collection<Permission> permissions;
53 * Constructor with initialization parameters. Passed collections are not
54 * referenced directly, but copied.
56 public PolicyEntry(CodeSource cs, Collection<? extends Principal> prs,
57 Collection<? extends Permission> permissions) {
58 this.cs = (cs != null) ? normalizeCodeSource(cs) : null;
59 this.principals = (prs == null || prs.isEmpty()) ? null
60 : (Principal[]) prs.toArray(new Principal[prs.size()]);
61 this.permissions = (permissions == null || permissions.isEmpty()) ? null
62 : Collections.unmodifiableCollection(permissions);
66 * Checks if passed CodeSource matches this PolicyEntry. Null CodeSource of
67 * PolicyEntry implies any CodeSource; non-null CodeSource forwards to its
70 public boolean impliesCodeSource(CodeSource codeSource) {
75 if (codeSource == null) {
78 return cs.implies(normalizeCodeSource(codeSource));
81 private CodeSource normalizeCodeSource(CodeSource codeSource) {
82 URL codeSourceURL = PolicyUtils.normalizeURL(codeSource.getLocation());
83 CodeSource result = codeSource;
85 if (codeSourceURL != codeSource.getLocation()) {
86 // URL was normalized - recreate codeSource with new URL
87 CodeSigner[] signers = codeSource.getCodeSigners();
88 if (signers == null) {
89 result = new CodeSource(codeSourceURL, codeSource
92 result = new CodeSource(codeSourceURL, signers);
99 * Checks if specified Principals match this PolicyEntry. Null or empty set
100 * of Principals of PolicyEntry implies any Principals; otherwise specified
101 * array must contain all Principals of this PolicyEntry.
103 public boolean impliesPrincipals(Principal[] prs) {
104 return PolicyUtils.matchSubset(principals, prs);
108 * Returns unmodifiable collection of permissions defined by this
109 * PolicyEntry, may be <code>null</code>.
111 public Collection<Permission> getPermissions() {
116 * Returns true if this PolicyEntry defines no Permissions, false otherwise.
118 public boolean isVoid() {
119 return permissions == null || permissions.size() == 0;