Source Code Cross Referenced for AccessControlContext.java in  » 6.0-JDK-Modules » j2me » 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 » 6.0 JDK Modules » j2me » java.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)AccessControlContext.java	1.40 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package java.security;
029:
030:        import java.util.Vector;
031:        import sun.security.util.Debug;
032:        import sun.security.util.SecurityConstants;
033:
034:        /** 
035:         * An AccessControlContext is used to make system resource access decisions
036:         * based on the context it encapsulates.
037:         * 
038:         * <p>More specifically, it encapsulates a context and
039:         * has a single method, <code>checkPermission</code>,
040:         * that is equivalent to the <code>checkPermission</code> method
041:         * in the AccessController class, with one difference: The AccessControlContext
042:         * <code>checkPermission</code> method makes access decisions based on the 
043:         * context it encapsulates,
044:         * rather than that of the current execution thread.
045:         * 
046:         * <p>Thus, the purpose of AccessControlContext is for those situations where
047:         * a security check that should be made within a given context
048:         * actually needs to be done from within a
049:         * <i>different</i> context (for example, from within a worker thread).
050:         * 
051:         * <p> An AccessControlContext is created by calling the 
052:         * <code>AccessController.getContext</code> method. 
053:         * The <code>getContext</code> method takes a "snapshot"
054:         * of the current calling context, and places
055:         * it in an AccessControlContext object, which it returns. A sample call is
056:         * the following:
057:         * 
058:         * <pre>
059:         * 
060:         *   AccessControlContext acc = AccessController.getContext()
061:         * 
062:         * </pre>
063:         * 
064:         * <p>
065:         * Code within a different context can subsequently call the
066:         * <code>checkPermission</code> method on the
067:         * previously-saved AccessControlContext object. A sample call is the
068:         * following:
069:         * 
070:         * <pre>
071:         * 
072:         *   acc.checkPermission(permission)
073:         * 
074:         * </pre> 
075:         * 
076:         * @see AccessController
077:         *
078:         * @author Roland Schemers
079:         */
080:
081:        public final class AccessControlContext {
082:
083:            private ProtectionDomain context[];
084:            private boolean isPrivileged;
085:            private AccessControlContext privilegedContext;
086:            private DomainCombiner combiner = null;
087:
088:            private static boolean debugInit = false;
089:            private static Debug debug = null;
090:
091:            static Debug getDebug() {
092:                if (debugInit)
093:                    return debug;
094:                else {
095:                    if (Policy.isSet()) {
096:                        debug = Debug.getInstance("access");
097:                        debugInit = true;
098:                    }
099:                    return debug;
100:                }
101:            }
102:
103:            /**
104:             * Create an AccessControlContext with the given set of ProtectionDomains.
105:             * Context must not be null. Duplicate domains will be removed from the
106:             * context.
107:             *
108:             * @param context the ProtectionDomains associated with this context.
109:             */
110:
111:            public AccessControlContext(ProtectionDomain context[]) {
112:                if (context.length == 0) {
113:                    this .context = null;
114:                } else if (context.length == 1) {
115:                    this .context = (ProtectionDomain[]) context.clone();
116:                } else {
117:                    Vector v = new Vector(context.length);
118:                    for (int i = 0; i < context.length; i++) {
119:                        if ((context[i] != null) && (!v.contains(context[i])))
120:                            v.addElement(context[i]);
121:                    }
122:                    this .context = new ProtectionDomain[v.size()];
123:                    v.copyInto(this .context);
124:                }
125:            }
126:
127:            /**
128:             * Create a new <code>AccessControlContext</code> with the given
129:             * <code>AccessControlContext</code> and <code>DomainCombiner</code>.
130:             * This constructor associates the provided
131:             * <code>DomainCombiner</code> with the provided
132:             * <code>AccessControlContext</code>.
133:             *
134:             * <p>
135:             *
136:             * @param acc the <code>AccessControlContext</code> associated
137:             *		with the provided <code>DomainCombiner</code>. <p>
138:             *
139:             * @param combiner the <code>DomainCombiner</code> to be associated
140:             *		with the provided <code>AccessControlContext</code>.
141:             *
142:             * @exception NullPointerException if the provided
143:             *		<code>context</code> is <code>null</code>. <p>
144:             *
145:             * @exception SecurityException if the caller does not have permission
146:             *		to invoke this constructor.
147:             */
148:            public AccessControlContext(AccessControlContext acc,
149:                    DomainCombiner combiner) {
150:
151:                SecurityManager sm = System.getSecurityManager();
152:                if (sm != null) {
153:                    sm.checkPermission(SecurityConstants.CREATE_ACC_PERMISSION);
154:                }
155:
156:                if (acc == null) {
157:                    throw new NullPointerException(
158:                            "null AccessControlContext was provided");
159:                }
160:
161:                this .context = acc.context;
162:
163:                // we do not need to run the combine method on the
164:                // provided ACC.  it was already "combined" when the
165:                // context was originally retrieved.
166:                //
167:                // at this point in time, we simply throw away the old
168:                // combiner and use the newly provided one.
169:                this .combiner = combiner;
170:            }
171:
172:            private AccessControlContext(ProtectionDomain context[],
173:                    DomainCombiner combiner) {
174:                this .context = (ProtectionDomain[]) context.clone();
175:                this .combiner = combiner;
176:            }
177:
178:            /**
179:             * package private constructor for AccessController
180:             */
181:
182:            AccessControlContext(ProtectionDomain context[],
183:                    boolean isPrivileged, AccessControlContext privilegedContext) {
184:                this .context = context;
185:                this .isPrivileged = isPrivileged;
186:                this .privilegedContext = privilegedContext;
187:            }
188:
189:            private AccessControlContext(ProtectionDomain context[],
190:                    boolean isPrivileged) {
191:                this (context, isPrivileged, null);
192:            }
193:
194:            /**
195:             * Returns true if this context is privileged.
196:             */
197:            boolean isPrivileged() {
198:                return isPrivileged;
199:
200:            }
201:
202:            /**
203:             * Get the <code>DomainCombiner</code> associated with this
204:             * <code>AccessControlContext</code>.
205:             *
206:             * <p>
207:             *
208:             * @return the <code>DomainCombiner</code> associated with this
209:             *		<code>AccessControlContext</code>, or <code>null</code>
210:             *		if there is none.
211:             *
212:             * @exception SecurityException if the caller does not have permission
213:             *		to get the <code>DomainCombiner</code> associated with this
214:             *		<code>AccessControlContext</code>.
215:             */
216:            public DomainCombiner getDomainCombiner() {
217:
218:                SecurityManager sm = System.getSecurityManager();
219:                if (sm != null) {
220:                    sm
221:                            .checkPermission(SecurityConstants.GET_COMBINER_PERMISSION);
222:                }
223:                return combiner;
224:            }
225:
226:            /** 
227:             * Determines whether the access request indicated by the
228:             * specified permission should be allowed or denied, based on
229:             * the security policy currently in effect, and the context in
230:             * this object.
231:             * <p>
232:             * This method quietly returns if the access request
233:             * is permitted, or throws a suitable AccessControlException otherwise. 
234:             *
235:             * @param perm the requested permission.
236:             * 
237:             * @exception AccessControlException if the specified permission
238:             * is not permitted, based on the current security policy and the
239:             * context encapsulated by this object.
240:             * @exception NullPointerException if the permission to check for is null.
241:             */
242:            public void checkPermission(Permission perm)
243:                    throws AccessControlException {
244:                if (perm == null) {
245:                    throw new NullPointerException("permission can't be null");
246:                }
247:                if (getDebug() != null) {
248:                    if (Debug.isOn("stack"))
249:                        Thread.currentThread().dumpStack();
250:                    if (Debug.isOn("domain")) {
251:                        if (context == null) {
252:                            debug.println("domain (context is null)");
253:                        } else {
254:                            for (int i = 0; i < context.length; i++) {
255:                                debug.println("domain " + i + " " + context[i]);
256:                            }
257:                        }
258:                    }
259:                }
260:
261:                /*
262:                 * iterate through the ProtectionDomains in the context.
263:                 * Stop at the first one that doesn't allow the
264:                 * requested permission (throwing an exception).
265:                 *
266:                 */
267:
268:                /* if ctxt is null, all we had on the stack were system domains,
269:                   or the first domain was a Privileged system domain. This
270:                   is to make the common case for system code very fast */
271:
272:                if (context == null)
273:                    return;
274:
275:                for (int i = 0; i < context.length; i++) {
276:                    if (context[i] != null && !context[i].implies(perm)) {
277:                        if (debug != null) {
278:                            debug.println("access denied " + perm);
279:                            if (Debug.isOn("failure")) {
280:                                Thread.currentThread().dumpStack();
281:                                final ProtectionDomain pd = context[i];
282:                                final Debug db = debug;
283:                                AccessController
284:                                        .doPrivileged(new PrivilegedAction() {
285:                                            public Object run() {
286:                                                db
287:                                                        .println("domain that failed "
288:                                                                + pd);
289:                                                return null;
290:                                            }
291:                                        });
292:                            }
293:                        }
294:                        throw new AccessControlException("access denied "
295:                                + perm, perm);
296:                    }
297:                }
298:
299:                // allow if all of them allowed access
300:                if (debug != null)
301:                    debug.println("access allowed " + perm);
302:
303:                return;
304:            }
305:
306:            /**
307:             * Take the stack-based context (this) and combine it with the
308:             * privileged or inherited context, if need be.
309:             */
310:            AccessControlContext optimize() {
311:                // the assigned (privileged or inherited) context
312:                AccessControlContext acc;
313:                if (isPrivileged) {
314:                    acc = privilegedContext;
315:                } else {
316:                    acc = AccessController.getInheritedAccessControlContext();
317:                }
318:
319:                // this.context could be null if only system code is on the stack;
320:                // in that case, ignore the stack context
321:                boolean skipStack = (context == null);
322:
323:                // acc.context could be null if only system code was involved;
324:                // in that case, ignore the assigned context
325:                boolean skipAssigned = (acc == null || acc.context == null);
326:
327:                // optimization: if neither have contexts; return acc if possible
328:                // rather than this, because acc might have a combiner
329:                if (skipAssigned && skipStack) {
330:                    return (acc != null) ? acc : this ;
331:                }
332:
333:                if (acc != null && acc.combiner != null) {
334:                    // let the assigned acc's combiner do its thing
335:                    return goCombiner(context, acc);
336:                }
337:
338:                // optimization: if there is no stack context; there is no reason
339:                // to compress the assigned context, it already is compressed
340:                if (skipStack) {
341:                    return acc;
342:                }
343:
344:                int slen = context.length;
345:
346:                // optimization: if there is no assigned context and the stack length
347:                // is less then or equal to two; there is no reason to compress the
348:                // stack context, it already is
349:                if (skipAssigned && slen <= 2) {
350:                    return this ;
351:                }
352:
353:                // optimization: if there is a single stack domain and that domain
354:                // is already in the assigned context; no need to combine
355:                if ((slen == 1) && (context[0] == acc.context[0])) {
356:                    return acc;
357:                }
358:
359:                int n = (skipAssigned) ? 0 : acc.context.length;
360:
361:                // now we combine both of them, and create a new context
362:                ProtectionDomain pd[] = new ProtectionDomain[slen + n];
363:
364:                // first copy in the assigned context domains, no need to compress
365:                if (!skipAssigned) {
366:                    System.arraycopy(acc.context, 0, pd, 0, n);
367:                }
368:
369:                // now add the stack context domains, discarding nulls and duplicates
370:                outer: for (int i = 0; i < context.length; i++) {
371:                    ProtectionDomain sd = context[i];
372:                    if (sd != null) {
373:                        for (int j = 0; j < n; j++) {
374:                            if (sd == pd[j]) {
375:                                continue outer;
376:                            }
377:                        }
378:                        pd[n++] = sd;
379:                    }
380:                }
381:
382:                // if length isn't equal, we need to shorten the array
383:                if (n != pd.length) {
384:                    // optimization: if we didn't really combine anything
385:                    if (!skipAssigned && n == acc.context.length) {
386:                        return acc;
387:                    } else if (skipAssigned && n == slen) {
388:                        return this ;
389:                    }
390:                    ProtectionDomain tmp[] = new ProtectionDomain[n];
391:                    System.arraycopy(pd, 0, tmp, 0, n);
392:                    pd = tmp;
393:                }
394:
395:                return new AccessControlContext(pd, false);
396:            }
397:
398:            private AccessControlContext goCombiner(ProtectionDomain[] current,
399:                    AccessControlContext assigned) {
400:
401:                // the assigned ACC's combiner is not null --
402:                // let the combiner do its thing
403:
404:                // We could add optimizations to 'current' here ...
405:
406:                if (getDebug() != null) {
407:                    debug.println("AccessControlContext invoking the Combiner");
408:                }
409:
410:                ProtectionDomain[] combinedPds = assigned.combiner
411:                        .combine(current == null ? null
412:                                : (ProtectionDomain[]) current.clone(),
413:                                assigned.context == null ? null
414:                                        : (ProtectionDomain[]) assigned.context
415:                                                .clone());
416:
417:                // return the new ACC
418:                return new AccessControlContext(combinedPds, assigned.combiner);
419:            }
420:
421:            /**
422:             * Checks two AccessControlContext objects for equality. 
423:             * Checks that <i>obj</i> is
424:             * an AccessControlContext and has the same set of ProtectionDomains
425:             * as this context.
426:             * <P>
427:             * @param obj the object we are testing for equality with this object.
428:             * @return true if <i>obj</i> is an AccessControlContext, and has the 
429:             * same set of ProtectionDomains as this context, false otherwise.
430:             */
431:            public boolean equals(Object obj) {
432:                if (obj == this )
433:                    return true;
434:
435:                if (!(obj instanceof  AccessControlContext))
436:                    return false;
437:
438:                AccessControlContext that = (AccessControlContext) obj;
439:
440:                if (context == null) {
441:                    return (that.context == null);
442:                }
443:
444:                if (that.context == null)
445:                    return false;
446:
447:                if (!(this .containsAllPDs(that) && that.containsAllPDs(this )))
448:                    return false;
449:
450:                if (this .combiner == null)
451:                    return (that.combiner == null);
452:
453:                if (that.combiner == null)
454:                    return false;
455:
456:                if (!this .combiner.equals(that.combiner))
457:                    return false;
458:
459:                return true;
460:
461:            }
462:
463:            private boolean containsAllPDs(AccessControlContext that) {
464:                boolean match = false;
465:                //
466:                // ProtectionDomains within an ACC currently cannot be null
467:                // and this is enforced by the contructor and the various
468:                // optimize methods. However, historically this logic made attempts
469:                // to support the notion of a null PD and therefore this logic continues
470:                // to support that notion.
471:                for (int i = 0; i < context.length; i++) {
472:                    match = false;
473:                    if (context[i] == null) {
474:                        for (int j = 0; (j < that.context.length) && !match; j++) {
475:                            match = (that.context[j] == null);
476:                        }
477:                    } else {
478:                        for (int j = 0; (j < that.context.length) && !match; j++) {
479:                            if (that.context[j] != null) {
480:                                match = ((context[i].getClass() == that.context[j]
481:                                        .getClass()) && (context[i]
482:                                        .equals(that.context[j])));
483:                            }
484:                        }
485:                    }
486:                    if (!match)
487:                        return false;
488:                }
489:                return match;
490:            }
491:
492:            /**
493:             * Returns the hash code value for this context. The hash code
494:             * is computed by exclusive or-ing the hash code of all the protection
495:             * domains in the context together.
496:             * 
497:             * @return a hash code value for this context.
498:             */
499:
500:            public int hashCode() {
501:                int hashCode = 0;
502:
503:                if (context == null)
504:                    return hashCode;
505:
506:                for (int i = 0; i < context.length; i++) {
507:                    if (context[i] != null)
508:                        hashCode ^= context[i].hashCode();
509:                }
510:                return hashCode;
511:            }
512:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.