KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > UnresolvedPermission


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.core;
13
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.security.Permission JavaDoc;
16 import java.security.PermissionCollection JavaDoc;
17 import org.eclipse.osgi.framework.debug.Debug;
18
19 /**
20  * Holds permissions which are of an unknown type when a
21  * policy file is read
22  *
23  */

24 final class UnresolvedPermission extends Permission JavaDoc {
25     private static final long serialVersionUID = 3546358422783079475L;
26     /**
27      * The type of permission this will be
28      */

29     private String JavaDoc type;
30     /**
31      * the action string
32      */

33     private String JavaDoc actions;
34     /**
35      * name of the permission
36      */

37     private String JavaDoc name;
38
39     private static Class JavaDoc[] constructorArgs;
40
41     static {
42         Class JavaDoc string = String JavaDoc.class;
43         constructorArgs = new Class JavaDoc[] {string, string};
44     }
45
46     /**
47      * Constructs a new instance of this class with its
48      * type, name, and certificates set to the arguments
49      * by definition, actions are ignored
50      *
51      * @param type the type class of permission object
52      * @param name the name of the permission
53      * @param actions the actions
54      */

55     UnresolvedPermission(String JavaDoc type, String JavaDoc name, String JavaDoc actions) {
56         super(type);
57         this.name = name;
58         this.type = type;
59         this.actions = actions;
60     }
61
62     /**
63      * Compares the argument to the receiver, and answers true
64      * if they represent the <em>same</em> object using a class
65      * specific comparison. In this case, the receiver and the
66      * object must have the same class, permission name,
67      * actions, and certificates
68      *
69      * @param obj the object to compare with this object
70      * @return <code>true</code>
71      * if the object is the same as this object
72      * <code>false</code>
73      * if it is different from this object
74      */

75     public boolean equals(Object JavaDoc obj) {
76         if (this == obj) {
77             return true;
78         }
79         if (!(obj instanceof UnresolvedPermission)) {
80             return false;
81         }
82
83         UnresolvedPermission perm = (UnresolvedPermission) obj;
84
85         return type.equals(perm.type) && name.equals(perm.name) && actions.equals(perm.actions);
86     }
87
88     /**
89      * Indicates whether the argument permission is implied
90      * by the receiver. UnresolvedPermission objects imply
91      * nothing because nothing is known about them yet.
92      *
93      * @return boolean always replies false
94      * @param p java.security.Permission
95      * the permission to check
96      */

97     public boolean implies(Permission JavaDoc p) {
98         return false;
99     }
100
101     /**
102      * Answers a new PermissionCollection for holding permissions
103      * of this class. Answer null if any permission collection can
104      * be used.
105      *
106      * @return a new PermissionCollection or null
107      *
108      */

109     public PermissionCollection JavaDoc newPermissionCollection() {
110         return new UnresolvedPermissionCollection();
111     }
112
113     /**
114      * Answers the actions associated with the receiver.
115      * Since UnresolvedPermission objects have no actions, answer
116      * the empty string.
117      *
118      * @return String
119      * the actions associated with the receiver.
120      */

121     public String JavaDoc getActions() {
122         return ""; //$NON-NLS-1$
123
}
124
125     /**
126      * Answers an integer hash code for the receiver. Any two
127      * objects which answer <code>true</code> when passed to
128      * <code>equals</code> must answer the same value for this
129      * method.
130      *
131      * @return int
132      * the receiver's hash
133      *
134      */

135     public int hashCode() {
136         return toString().hashCode();
137     }
138
139     /**
140      * Answers a string containing a concise, human-readable
141      * description of the receiver.
142      *
143      * @return String
144      * a printable representation for the receiver.
145      */

146     public String JavaDoc toString() {
147         return "(unresolved " + type + " " + name + " " + actions + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
148
}
149
150     Permission JavaDoc resolve(Class JavaDoc clazz) {
151         if (clazz.getName().equals(type)) {
152             try {
153                 Constructor JavaDoc constructor = clazz.getConstructor(constructorArgs);
154
155                 Permission JavaDoc permission = (Permission JavaDoc) constructor.newInstance(new Object JavaDoc[] {name, actions});
156
157                 if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
158                     Debug.println("Resolved " + this); //$NON-NLS-1$
159
}
160
161                 return permission;
162             } catch (Exception JavaDoc e) {
163                 /* Ignore any error trying to resolve the permission */
164                 if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
165                     Debug.println("Exception trying to resolve permission"); //$NON-NLS-1$
166
Debug.printStackTrace(e);
167                 }
168             }
169         }
170
171         return null;
172     }
173 }
174
Popular Tags