KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > core > AntSecurityManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.ant.internal.core;
12
13
14 import java.io.FileDescriptor JavaDoc;
15 import java.net.InetAddress JavaDoc;
16 import java.net.SocketPermission JavaDoc;
17 import java.security.Permission JavaDoc;
18 import java.util.PropertyPermission JavaDoc;
19 import org.eclipse.ant.core.AntSecurityException;
20
21 /**
22  * A security manager that always throws an <code>AntSecurityException</code>
23  * if the calling thread attempts to cause the Java Virtual Machine to
24  * exit/halt or if the restricted thread attempts to set a System property.
25  * Otherwise this manager just delegates to the pre-existing manager
26  * passed in the constructor or mimics the default security manager behavior
27  */

28 public class AntSecurityManager extends SecurityManager JavaDoc {
29
30     private SecurityManager JavaDoc fSecurityManager= null;
31     private Thread JavaDoc fRestrictedThread= null;
32     //ensure that the PropertyPermission class is loaded before we
33
//start checking permissions: bug 85908
34
private static final PropertyPermission JavaDoc fgPropertyPermission= new PropertyPermission JavaDoc("*", "write"); //$NON-NLS-1$ //$NON-NLS-2$
35

36     private boolean fAllowSettingSystemProperties= true;
37     
38     public AntSecurityManager(SecurityManager JavaDoc securityManager, Thread JavaDoc restrictedThread, boolean allowSettingProperties) {
39         fSecurityManager= securityManager;
40         fRestrictedThread= restrictedThread;
41         fAllowSettingSystemProperties= allowSettingProperties;
42     }
43     
44     public AntSecurityManager(SecurityManager JavaDoc securityManager, Thread JavaDoc restrictedThread) {
45         this(securityManager, restrictedThread, true);
46     }
47     
48     /* (non-Javadoc)
49      * @see java.lang.SecurityManager#checkExit(int)
50      */

51     public void checkExit(int status) {
52         //no exit allowed from the restricted thread...System.exit is being called
53
//by some ant task...do not want Eclipse to exit if
54
//in the same VM.
55
if (Thread.currentThread() == fRestrictedThread) {
56             throw new AntSecurityException();
57         }
58         if (fSecurityManager != null) {
59             fSecurityManager.checkExit(status);
60         }
61     }
62     /* (non-Javadoc)
63      * @see java.lang.SecurityManager#checkAccept(java.lang.String, int)
64      */

65     public void checkAccept(String JavaDoc host, int port) {
66         if (fSecurityManager != null) {
67             fSecurityManager.checkAccept(host, port);
68         }
69     }
70
71     /* (non-Javadoc)
72      * @see java.lang.SecurityManager#checkAccess(java.lang.Thread)
73      */

74     public void checkAccess(Thread JavaDoc t) {
75         if (fSecurityManager != null) {
76             fSecurityManager.checkAccess(t);
77         }
78     }
79
80     /* (non-Javadoc)
81      * @see java.lang.SecurityManager#checkAccess(java.lang.ThreadGroup)
82      */

83     public void checkAccess(ThreadGroup JavaDoc g) {
84         if (fSecurityManager != null) {
85             fSecurityManager.checkAccess(g);
86         }
87     }
88
89     /* (non-Javadoc)
90      * @see java.lang.SecurityManager#checkAwtEventQueueAccess()
91      */

92     public void checkAwtEventQueueAccess() {
93         if (fSecurityManager != null) {
94             fSecurityManager.checkAwtEventQueueAccess();
95         }
96     }
97
98     /* (non-Javadoc)
99      * @see java.lang.SecurityManager#checkConnect(java.lang.String, int, java.lang.Object)
100      */

101     public void checkConnect(String JavaDoc host, int port, Object JavaDoc context) {
102         if (fSecurityManager != null) {
103             fSecurityManager.checkConnect(host, port, context);
104         }
105     }
106
107     /* (non-Javadoc)
108      * @see java.lang.SecurityManager#checkConnect(java.lang.String, int)
109      */

110     public void checkConnect(String JavaDoc host, int port) {
111         if (fSecurityManager != null) {
112             fSecurityManager.checkConnect(host, port);
113         }
114     }
115
116     /* (non-Javadoc)
117      * @see java.lang.SecurityManager#checkCreateClassLoader()
118      */

119     public void checkCreateClassLoader() {
120         if (fSecurityManager != null) {
121             fSecurityManager.checkCreateClassLoader();
122         }
123     }
124
125     /* (non-Javadoc)
126      * @see java.lang.SecurityManager#checkDelete(java.lang.String)
127      */

128     public void checkDelete(String JavaDoc file) {
129         if (fSecurityManager != null) {
130             fSecurityManager.checkDelete(file);
131         }
132     }
133
134     /* (non-Javadoc)
135      * @see java.lang.SecurityManager#checkExec(java.lang.String)
136      */

137     public void checkExec(String JavaDoc cmd) {
138         if (fSecurityManager != null) {
139             fSecurityManager.checkExec(cmd);
140         }
141     }
142
143     /* (non-Javadoc)
144      * @see java.lang.SecurityManager#checkLink(java.lang.String)
145      */

146     public void checkLink(String JavaDoc lib) {
147         if (fSecurityManager != null) {
148             fSecurityManager.checkLink(lib);
149         }
150     }
151
152     /* (non-Javadoc)
153      * @see java.lang.SecurityManager#checkListen(int)
154      */

155     public void checkListen(int port) {
156         if (fSecurityManager != null) {
157             fSecurityManager.checkListen(port);
158         }
159     }
160
161     /* (non-Javadoc)
162      * @see java.lang.SecurityManager#checkMemberAccess(java.lang.Class, int)
163      */

164     public void checkMemberAccess(Class JavaDoc clazz, int which) {
165         if (fSecurityManager != null) {
166             fSecurityManager.checkMemberAccess(clazz, which);
167         }
168     }
169
170     /**
171      * @see java.lang.SecurityManager#checkMulticast(java.net.InetAddress, byte)
172      * @deprecated
173      */

174     public void checkMulticast(InetAddress JavaDoc maddr, byte ttl) {
175         if (fSecurityManager != null) {
176             String JavaDoc host = maddr.getHostAddress();
177             if (!host.startsWith("[") && host.indexOf(':') != -1) { //$NON-NLS-1$
178
host = "[" + host + "]"; //$NON-NLS-1$ //$NON-NLS-2$
179
}
180             checkPermission(new SocketPermission JavaDoc(host, "accept,connect")); //$NON-NLS-1$
181
}
182     }
183
184     /* (non-Javadoc)
185      * @see java.lang.SecurityManager#checkMulticast(java.net.InetAddress)
186      */

187     public void checkMulticast(InetAddress JavaDoc maddr) {
188         if (fSecurityManager != null) {
189             fSecurityManager.checkMulticast(maddr);
190         }
191     }
192
193     /* (non-Javadoc)
194      * @see java.lang.SecurityManager#checkPackageAccess(java.lang.String)
195      */

196     public void checkPackageAccess(String JavaDoc pkg) {
197         if (fSecurityManager != null) {
198             fSecurityManager.checkPackageAccess(pkg);
199         }
200     }
201
202     /* (non-Javadoc)
203      * @see java.lang.SecurityManager#checkPackageDefinition(java.lang.String)
204      */

205     public void checkPackageDefinition(String JavaDoc pkg) {
206         if (fSecurityManager != null) {
207             fSecurityManager.checkPackageDefinition(pkg);
208         }
209     }
210
211     /* (non-Javadoc)
212      * @see java.lang.SecurityManager#checkPermission(java.security.Permission, java.lang.Object)
213      */

214     public void checkPermission(Permission JavaDoc perm, Object JavaDoc context) {
215         if (fSecurityManager != null) {
216             fSecurityManager.checkPermission(perm, context);
217         }
218     }
219
220     /* (non-Javadoc)
221      * @see java.lang.SecurityManager#checkPermission(java.security.Permission)
222      */

223     public void checkPermission(Permission JavaDoc perm) {
224         if (!fAllowSettingSystemProperties && fgPropertyPermission.implies(perm) && fRestrictedThread == Thread.currentThread()) {
225             //attempting to write a system property
226
throw new AntSecurityException();
227         }
228         if (fSecurityManager != null) {
229             fSecurityManager.checkPermission(perm);
230         }
231     }
232
233     /* (non-Javadoc)
234      * @see java.lang.SecurityManager#checkPrintJobAccess()
235      */

236     public void checkPrintJobAccess() {
237         if (fSecurityManager != null) {
238             fSecurityManager.checkPrintJobAccess();
239         }
240     }
241
242     /* (non-Javadoc)
243      * @see java.lang.SecurityManager#checkPropertiesAccess()
244      */

245     public void checkPropertiesAccess() {
246         if (fSecurityManager != null) {
247             fSecurityManager.checkPropertiesAccess();
248         }
249         super.checkPropertiesAccess();
250     }
251
252     /* (non-Javadoc)
253      * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
254      */

255     public void checkPropertyAccess(String JavaDoc key) {
256         if (fSecurityManager != null) {
257             fSecurityManager.checkPropertyAccess(key);
258         }
259     }
260
261     /* (non-Javadoc)
262      * @see java.lang.SecurityManager#checkRead(java.io.FileDescriptor)
263      */

264     public void checkRead(FileDescriptor JavaDoc fd) {
265         if (fSecurityManager != null) {
266             fSecurityManager.checkRead(fd);
267         }
268     }
269
270     /* (non-Javadoc)
271      * @see java.lang.SecurityManager#checkRead(java.lang.String, java.lang.Object)
272      */

273     public void checkRead(String JavaDoc file, Object JavaDoc context) {
274         if (fSecurityManager != null) {
275             fSecurityManager.checkRead(file, context);
276         }
277     }
278
279     /* (non-Javadoc)
280      * @see java.lang.SecurityManager#checkRead(java.lang.String)
281      */

282     public void checkRead(String JavaDoc file) {
283         if (fSecurityManager != null) {
284             fSecurityManager.checkRead(file);
285         }
286     }
287
288     /* (non-Javadoc)
289      * @see java.lang.SecurityManager#checkSecurityAccess(java.lang.String)
290      */

291     public void checkSecurityAccess(String JavaDoc target) {
292         if (fSecurityManager != null) {
293             fSecurityManager.checkSecurityAccess(target);
294         }
295     }
296
297     /* (non-Javadoc)
298      * @see java.lang.SecurityManager#checkSetFactory()
299      */

300     public void checkSetFactory() {
301         if (fSecurityManager != null) {
302             fSecurityManager.checkSetFactory();
303         }
304     }
305
306     /* (non-Javadoc)
307      * @see java.lang.SecurityManager#checkSystemClipboardAccess()
308      */

309     public void checkSystemClipboardAccess() {
310         if (fSecurityManager != null) {
311             fSecurityManager.checkSystemClipboardAccess();
312         }
313     }
314
315     /* (non-Javadoc)
316      * @see java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
317      */

318     public boolean checkTopLevelWindow(Object JavaDoc window) {
319         if (fSecurityManager != null) {
320             return fSecurityManager.checkTopLevelWindow(window);
321         }
322         return super.checkTopLevelWindow(window);
323     }
324
325     /* (non-Javadoc)
326      * @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
327      */

328     public void checkWrite(FileDescriptor JavaDoc fd) {
329         if (fSecurityManager != null) {
330             fSecurityManager.checkWrite(fd);
331         }
332     }
333
334     /* (non-Javadoc)
335      * @see java.lang.SecurityManager#checkWrite(java.lang.String)
336      */

337     public void checkWrite(String JavaDoc file) {
338         if (fSecurityManager != null) {
339             fSecurityManager.checkWrite(file);
340         }
341     }
342
343     /**
344      * @see java.lang.SecurityManager#getInCheck()
345      * @deprecated
346      */

347     public boolean getInCheck() {
348         if (fSecurityManager != null) {
349             return fSecurityManager.getInCheck();
350         }
351         return super.getInCheck();
352     }
353
354     /* (non-Javadoc)
355      * @see java.lang.SecurityManager#getSecurityContext()
356      */

357     public Object JavaDoc getSecurityContext() {
358         if (fSecurityManager != null) {
359             return fSecurityManager.getSecurityContext();
360         }
361         return super.getSecurityContext();
362     }
363
364     /* (non-Javadoc)
365      * @see java.lang.SecurityManager#getThreadGroup()
366      */

367     public ThreadGroup JavaDoc getThreadGroup() {
368         if (fSecurityManager != null) {
369             fSecurityManager.getThreadGroup();
370         }
371         return super.getThreadGroup();
372     }
373 }
374
Popular Tags