Source Code Cross Referenced for ProtectionDomain.java in  » Apache-Harmony-Java-SE » java-package » java » security » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.