Source Code Cross Referenced for PackagePermission.java in  » Scripting » Pnuts » pnuts » 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 » Scripting » Pnuts » pnuts.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)PackagePermission.java 1.2 04/12/06
003:         *
004:         * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005:         *
006:         * See the file "LICENSE.txt" for information on usage and redistribution
007:         * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008:         */
009:        package pnuts.security;
010:
011:        import java.io.IOException;
012:        import java.io.ObjectInputStream;
013:        import java.io.Serializable;
014:        import java.security.BasicPermission;
015:        import java.security.Permission;
016:        import java.security.PermissionCollection;
017:        import java.util.Enumeration;
018:        import java.util.Hashtable;
019:
020:        /**
021:         * This class represents access to a Package in Pnuts. A PackagePermission consists
022:         * of a package name and a set of actions.
023:         *
024:         * @see <a href="../../../doc/permission.html">Pnuts User's Guide</a>
025:         * @version 1.1
026:         */
027:        public final class PackagePermission extends BasicPermission {
028:
029:            private static final int WRITE = 1;
030:            private static final int ADD = 2;
031:            private static final int REMOVE = 4;
032:
033:            private transient int mask = 0;
034:            private transient String actions;
035:            private transient boolean wildcard = false;
036:            private transient String path;
037:
038:            public PackagePermission(String name) {
039:                super (name);
040:            }
041:
042:            public PackagePermission(String name, String actions) {
043:                super (name, actions);
044:                this .actions = actions;
045:                init(actions);
046:            }
047:
048:            void init(String actions) {
049:                if (actions.indexOf("write") >= 0) {
050:                    mask |= WRITE;
051:                }
052:                if (actions.indexOf("add") >= 0) {
053:                    mask |= ADD;
054:                }
055:                if (actions.indexOf("remove") >= 0) {
056:                    mask |= REMOVE;
057:                }
058:                String name = getName();
059:                if (name.endsWith("::*")) {
060:                    wildcard = true;
061:                    path = name.substring(0, name.length() - 2);
062:                } else if (name.endsWith(".*")) {
063:                    wildcard = true;
064:                    path = name.substring(0, name.length() - 1);
065:                } else if (name.equals("*")) {
066:                    wildcard = true;
067:                    path = "";
068:                    path = name.substring(0, name.length() - 2);
069:                } else {
070:                    path = name;
071:                }
072:            }
073:
074:            public String getActions() {
075:                String s = "";
076:                boolean first = true;
077:                if ((mask & WRITE) != 0) {
078:                    s += "write";
079:                    first = false;
080:                }
081:                if ((mask & ADD) != 0) {
082:                    if (!first) {
083:                        s += ", ";
084:                    }
085:                    s += "add";
086:                    first = false;
087:                }
088:                if ((mask & REMOVE) != 0) {
089:                    if (!first) {
090:                        s += ", ";
091:                    }
092:                    s += "remove";
093:                }
094:                return s;
095:            }
096:
097:            public boolean implies(Permission p) {
098:                if (!(p instanceof  PackagePermission)) {
099:                    return false;
100:                }
101:                PackagePermission pp = (PackagePermission) p;
102:                int m = pp.getMask();
103:                if ((m & this .mask) != m) {
104:                    return false;
105:                }
106:
107:                if (this .wildcard) {
108:                    if (pp.wildcard) {
109:                        return pp.path.startsWith(path);
110:                    } else {
111:                        return (pp.path.length() > this .path.length())
112:                                && pp.path.startsWith(this .path);
113:                    }
114:                } else {
115:                    if (pp.wildcard) {
116:                        return false;
117:                    } else {
118:                        return this .path.equals(pp.path);
119:                    }
120:                }
121:            }
122:
123:            int getMask() {
124:                return mask;
125:            }
126:
127:            public PermissionCollection newPermissionCollection() {
128:                return new PackagePermissionCollection();
129:            }
130:
131:            private void readObject(ObjectInputStream s) throws IOException,
132:                    ClassNotFoundException {
133:                s.defaultReadObject();
134:                init(actions);
135:            }
136:        }
137:
138:        final class PackagePermissionCollection extends PermissionCollection
139:                implements  Serializable {
140:            private Hashtable permissions;
141:            private boolean all_allowed;
142:
143:            public PackagePermissionCollection() {
144:                permissions = new Hashtable(10);
145:                all_allowed = false;
146:            }
147:
148:            public void add(Permission permission) {
149:                if (!(permission instanceof  PackagePermission)) {
150:                    throw new IllegalArgumentException("invalid permission: "
151:                            + permission);
152:                }
153:                PackagePermission pp = (PackagePermission) permission;
154:                permissions.put(pp.getName(), permission);
155:                if (!all_allowed) {
156:                    if (pp.getName().equals("*")) {
157:                        all_allowed = true;
158:                    }
159:                }
160:            }
161:
162:            public boolean implies(Permission permission) {
163:                if (!(permission instanceof  PackagePermission)) {
164:                    return false;
165:                }
166:
167:                PackagePermission pp = (PackagePermission) permission;
168:                if (all_allowed) {
169:                    return true;
170:                }
171:                String path = pp.getName();
172:                Permission x = (Permission) permissions.get(path);
173:                if (x != null) {
174:                    return x.implies(permission);
175:                }
176:
177:                int offset = path.length() - 1;
178:
179:                while (true) {
180:                    int last1 = path.lastIndexOf("::", offset);
181:                    int last2 = path.lastIndexOf(".", offset);
182:                    if (last1 == -1 && last2 == -1) {
183:                        break;
184:                    }
185:                    int last;
186:                    if (last2 > last1) {
187:                        last = last2;
188:                    } else {
189:                        last = last1;
190:                    }
191:                    path = path.substring(0, last + 1) + "*";
192:                    x = (Permission) permissions.get(path);
193:                    if (x != null) {
194:                        return x.implies(permission);
195:                    }
196:                    offset = last - 1;
197:                }
198:                return false;
199:            }
200:
201:            public Enumeration elements() {
202:                return permissions.elements();
203:            }
204:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.