001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander V. Astapchuk
020: * @version $Revision$
021: */package java.security;
022:
023: /**
024: * This class represents a domain in which classes from the same source (URL)
025: * and signed by the same keys are stored. All the classes inside are given the
026: * same permissions.
027: * <p>
028: * Note: a class can only belong to one and only one protection domain.
029: */
030: public class ProtectionDomain {
031:
032: // CodeSource for this ProtectionDomain
033: private CodeSource codeSource;
034:
035: // Static permissions for this ProtectionDomain
036: private PermissionCollection permissions;
037:
038: // ClassLoader
039: private ClassLoader classLoader;
040:
041: // Set of principals associated with this ProtectionDomain
042: private Principal[] principals;
043:
044: // false if this ProtectionDomain was constructed with static
045: // permissions, true otherwise.
046: private boolean dynamicPerms;
047:
048: /**
049: * Constructs a protection domain from the given code source and the
050: * permissions that that should be granted to the classes which are
051: * encapsulated in it.
052: * @param cs
053: * @param permissions
054: */
055: public ProtectionDomain(CodeSource cs,
056: PermissionCollection permissions) {
057: this .codeSource = cs;
058: if (permissions != null) {
059: permissions.setReadOnly();
060: }
061: this .permissions = permissions;
062: //this.classLoader = null;
063: //this.principals = null;
064: //dynamicPerms = false;
065: }
066:
067: /**
068: * Constructs a protection domain from the given code source and the
069: * permissions that that should be granted to the classes which are
070: * encapsulated in it.
071: *
072: * This constructor also allows the association of a ClassLoader and group
073: * of Principals.
074: *
075: * @param cs
076: * the CodeSource associated with this domain
077: * @param permissions
078: * the Permissions associated with this domain
079: * @param cl
080: * the ClassLoader associated with this domain
081: * @param principals
082: * the Principals associated with this domain
083: */
084: public ProtectionDomain(CodeSource cs,
085: PermissionCollection permissions, ClassLoader cl,
086: Principal[] principals) {
087: this .codeSource = cs;
088: if (permissions != null) {
089: permissions.setReadOnly();
090: }
091: this .permissions = permissions;
092: this .classLoader = cl;
093: if (principals != null) {
094: this .principals = new Principal[principals.length];
095: System.arraycopy(principals, 0, this .principals, 0,
096: this .principals.length);
097: }
098: dynamicPerms = true;
099: }
100:
101: /**
102: * Returns the ClassLoader associated with the ProtectionDomain
103: *
104: * @return ClassLoader associated ClassLoader
105: */
106: public final ClassLoader getClassLoader() {
107: return classLoader;
108: }
109:
110: /**
111: * Answers the code source of this domain.
112: *
113: * @return java.security.CodeSource the code source of this domain
114: */
115: public final CodeSource getCodeSource() {
116: return codeSource;
117: }
118:
119: /**
120: * Answers the permissions that should be granted to the classes which are
121: * encapsulated in this domain.
122: *
123: * @return java.security.PermissionCollection collection of permissions
124: * associated with this domain.
125: */
126: public final PermissionCollection getPermissions() {
127: return permissions;
128: }
129:
130: /**
131: * Returns the Principals associated with this ProtectionDomain. A change to
132: * the returned array will not impact the ProtectionDomain.
133: *
134: * @return Principals[] Principals associated with the ProtectionDomain.
135: */
136: public final Principal[] getPrincipals() {
137: if (principals == null) {
138: return new Principal[0];
139: }
140: Principal[] tmp = new Principal[principals.length];
141: System.arraycopy(principals, 0, tmp, 0, tmp.length);
142: return tmp;
143: }
144:
145: /**
146: * Determines whether the permission collection of this domain implies the
147: * argument permission.
148: *
149: *
150: * @return boolean true if this permission collection implies the argument
151: * and false otherwise.
152: * @param permission
153: * java.security.Permission the permission to check.
154: */
155: public boolean implies(Permission permission) {
156: // First, test with the Policy, as the default Policy.implies()
157: // checks for both dynamic and static collections of the
158: // ProtectionDomain passed...
159: if (dynamicPerms
160: && Policy.getAccessiblePolicy().implies(this ,
161: permission)) {
162: return true;
163: }
164:
165: // ... and we get here if
166: // either the permissions are static
167: // or Policy.implies() did not check for static permissions
168: // or the permission is not implied
169: return permissions == null ? false : permissions
170: .implies(permission);
171: }
172:
173: /**
174: * Answers a string containing a concise, human-readable description of the
175: * receiver.
176: *
177: * @return String a printable representation for the receiver.
178: */
179: public String toString() {
180: //FIXME: 1.5 use StreamBuilder here
181: StringBuffer buf = new StringBuffer(200);
182: buf.append("ProtectionDomain\n"); //$NON-NLS-1$
183: buf
184: .append("CodeSource=").append( //$NON-NLS-1$
185: codeSource == null ? "<null>" : codeSource.toString()).append( //$NON-NLS-1$
186: "\n"); //$NON-NLS-1$
187: buf
188: .append("ClassLoader=").append( //$NON-NLS-1$
189: classLoader == null ? "<null>" : classLoader.toString()) //$NON-NLS-1$
190: .append("\n"); //$NON-NLS-1$
191: if (principals == null || principals.length == 0) {
192: buf.append("<no principals>\n"); //$NON-NLS-1$
193: } else {
194: buf.append("Principals: <\n"); //$NON-NLS-1$
195: for (int i = 0; i < principals.length; i++) {
196: buf
197: .append("\t").append( //$NON-NLS-1$
198: principals[i] == null ? "<null>" : principals[i] //$NON-NLS-1$
199: .toString()).append(
200: "\n"); //$NON-NLS-1$
201: }
202: buf.append(">"); //$NON-NLS-1$
203: }
204:
205: //permissions here
206: buf.append("Permissions:\n"); //$NON-NLS-1$
207: if (permissions == null) {
208: buf.append("\t\t<no static permissions>\n"); //$NON-NLS-1$
209: } else {
210: buf
211: .append("\t\tstatic: ").append(permissions.toString()).append( //$NON-NLS-1$
212: "\n"); //$NON-NLS-1$
213: }
214:
215: if (dynamicPerms) {
216: if (Policy.isSet()) {
217: PermissionCollection perms;
218: perms = Policy.getAccessiblePolicy().getPermissions(
219: this );
220: if (perms == null) {
221: buf.append("\t\t<no dynamic permissions>\n"); //$NON-NLS-1$
222: } else {
223: buf
224: .append("\t\tdynamic: ").append(perms.toString()) //$NON-NLS-1$
225: .append("\n"); //$NON-NLS-1$
226: }
227: } else {
228: buf.append("\t\t<no dynamic permissions>\n"); //$NON-NLS-1$
229: }
230: }
231: return buf.toString();
232: }
233: }
|