KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > util > SecureAction


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.util;
13
14 import java.io.*;
15 import java.net.*;
16 import java.security.*;
17 import java.util.Properties JavaDoc;
18 import java.util.zip.ZipFile JavaDoc;
19 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
20 import org.osgi.framework.*;
21 import org.osgi.util.tracker.ServiceTracker;
22
23 /**
24  * Utility class to execute common privileged code.
25  * @since 3.1
26  */

27 public class SecureAction {
28     // make sure we use the correct controlContext;
29
private AccessControlContext controlContext;
30     
31     // This ClassLoader is used in loadSystemClass if System.getClassLoader() returns null
32
static final ClassLoader JavaDoc bootClassLoader = (ClassLoader JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
33         public Object JavaDoc run() {
34             return new ClassLoader JavaDoc(null) {
35                 // parentless ClassLoader
36
};
37         }
38     });
39
40     /*
41      * Package privaet constructor a new SecureAction object.
42      * The constructed SecureAction object uses the caller's AccessControlContext
43      * to perform security checks
44      */

45     SecureAction() {
46         // save the control context to be used.
47
this.controlContext = AccessController.getContext();
48     }
49
50     /**
51      * Creates a privileged action that can be used to construct a SecureAction object.
52      * The recommended way to construct a SecureAction object is the following: <p>
53      * <pre>
54      * SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction());
55      * </pre>
56      * @return a privileged action object that can be used to construct a SecureAction object.
57      */

58     public static PrivilegedAction createSecureAction() {
59         return new PrivilegedAction() {
60             public Object JavaDoc run() {
61                 return new SecureAction();
62             }
63         };
64     }
65
66     /**
67      * Returns a system property. Same as calling
68      * System.getProperty(String).
69      * @param property the property key.
70      * @return the value of the property or null if it does not exist.
71      */

72     public String JavaDoc getProperty(final String JavaDoc property) {
73         if (System.getSecurityManager() == null)
74             return FrameworkProperties.getProperty(property);
75         return (String JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
76             public Object JavaDoc run() {
77                 return FrameworkProperties.getProperty(property);
78             }
79         }, controlContext);
80     }
81
82     /**
83      * Returns a system property. Same as calling
84      * System.getProperty(String,String).
85      * @param property the property key.
86      * @param def the default value if the property key does not exist.
87      * @return the value of the property or the def value if the property
88      * does not exist.
89      */

90     public String JavaDoc getProperty(final String JavaDoc property, final String JavaDoc def) {
91         if (System.getSecurityManager() == null)
92             return FrameworkProperties.getProperty(property, def);
93         return (String JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
94             public Object JavaDoc run() {
95                 return FrameworkProperties.getProperty(property, def);
96             }
97         }, controlContext);
98     }
99
100     /**
101      * Returns the system properties. Same as calling
102      * System.getProperties().
103      * @return the system properties.
104      */

105     public Properties JavaDoc getProperties() {
106         if (System.getSecurityManager() == null)
107             return FrameworkProperties.getProperties();
108         return (Properties JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
109             public Object JavaDoc run() {
110                 return FrameworkProperties.getProperties();
111             }
112         }, controlContext);
113     }
114
115     /**
116      * Creates a FileInputStream from a File. Same as calling
117      * new FileInputStream(File).
118      * @param file the File to craete a FileInputStream from.
119      * @return The FileInputStream.
120      * @throws FileNotFoundException if the File does not exist.
121      */

122     public FileInputStream getFileInputStream(final File JavaDoc file) throws FileNotFoundException {
123         if (System.getSecurityManager() == null)
124             return new FileInputStream(file);
125         try {
126             return (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
127                 public Object JavaDoc run() throws FileNotFoundException {
128                     return new FileInputStream(file);
129                 }
130             }, controlContext);
131         } catch (PrivilegedActionException e) {
132             if (e.getException() instanceof FileNotFoundException)
133                 throw (FileNotFoundException) e.getException();
134             throw (RuntimeException JavaDoc) e.getException();
135         }
136     }
137
138     /**
139      * Creates a FileInputStream from a File. Same as calling
140      * new FileOutputStream(File,boolean).
141      * @param file the File to create a FileOutputStream from.
142      * @param append indicates if the OutputStream should append content.
143      * @return The FileOutputStream.
144      * @throws FileNotFoundException if the File does not exist.
145      */

146     public FileOutputStream getFileOutputStream(final File JavaDoc file, final boolean append) throws FileNotFoundException {
147         if (System.getSecurityManager() == null)
148             return new FileOutputStream(file.getAbsolutePath(), append);
149         try {
150             return (FileOutputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
151                 public Object JavaDoc run() throws FileNotFoundException {
152                     return new FileOutputStream(file.getAbsolutePath(), append);
153                 }
154             }, controlContext);
155         } catch (PrivilegedActionException e) {
156             if (e.getException() instanceof FileNotFoundException)
157                 throw (FileNotFoundException) e.getException();
158             throw (RuntimeException JavaDoc) e.getException();
159         }
160     }
161
162     /**
163      * Returns the length of a file. Same as calling
164      * file.length().
165      * @param file a file object
166      * @return the length of a file.
167      */

168     public long length(final File JavaDoc file) {
169         if (System.getSecurityManager() == null)
170             return file.length();
171         return ((Long JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
172             public Object JavaDoc run() {
173                 return new Long JavaDoc(file.length());
174             }
175         }, controlContext)).longValue();
176     }
177
178     /**
179      * Returns true if a file exists, otherwise false is returned. Same as calling
180      * file.exists().
181      * @param file a file object
182      * @return true if a file exists, otherwise false
183      */

184     public boolean exists(final File JavaDoc file) {
185         if (System.getSecurityManager() == null)
186             return file.exists();
187         return ((Boolean JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
188             public Object JavaDoc run() {
189                 return file.exists() ? Boolean.TRUE : Boolean.FALSE;
190             }
191         }, controlContext)).booleanValue();
192     }
193
194     /**
195      * Returns true if a file is a directory, otherwise false is returned. Same as calling
196      * file.isDirectory().
197      * @param file a file object
198      * @return true if a file is a directory, otherwise false
199      */

200     public boolean isDirectory(final File JavaDoc file) {
201         if (System.getSecurityManager() == null)
202             return file.isDirectory();
203         return ((Boolean JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
204             public Object JavaDoc run() {
205                 return file.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
206             }
207         }, controlContext)).booleanValue();
208     }
209
210     /**
211      * Returns a file's last modified stamp. Same as calling
212      * file.lastModified().
213      * @param file a file object
214      * @return a file's last modified stamp.
215      */

216     public long lastModified(final File JavaDoc file) {
217         if (System.getSecurityManager() == null)
218             return file.lastModified();
219         return ((Long JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
220             public Object JavaDoc run() {
221                 return new Long JavaDoc(file.lastModified());
222             }
223         }, controlContext)).longValue();
224     }
225
226     /**
227      * Returns a file's list. Same as calling
228      * file.list().
229      * @param file a file object
230      * @return a file's list.
231      */

232     public String JavaDoc[] list(final File JavaDoc file) {
233         if (System.getSecurityManager() == null)
234             return file.list();
235         return (String JavaDoc[]) AccessController.doPrivileged(new PrivilegedAction() {
236             public Object JavaDoc run() {
237                 return file.list();
238             }
239         }, controlContext);
240     }
241
242     /**
243      * Returns a ZipFile. Same as calling
244      * new ZipFile(file)
245      * @param file the file to get a ZipFile for
246      * @return a ZipFile
247      * @throws IOException if an error occured
248      */

249     public ZipFile JavaDoc getZipFile(final File JavaDoc file) throws IOException {
250         if (System.getSecurityManager() == null)
251             return new ZipFile JavaDoc(file);
252         try {
253             return (ZipFile JavaDoc) AccessController.doPrivileged(new PrivilegedExceptionAction() {
254                 public Object JavaDoc run() throws IOException {
255                     return new ZipFile JavaDoc(file);
256                 }
257             }, controlContext);
258         } catch (PrivilegedActionException e) {
259             if (e.getException() instanceof IOException)
260                 throw (IOException) e.getException();
261             throw (RuntimeException JavaDoc) e.getException();
262         }
263     }
264
265     /**
266      * Gets a URL. Same a calling
267      * {@link URL#URL(java.lang.String, java.lang.String, int, java.lang.String, java.net.URLStreamHandler)}
268      * @param protocol the protocol
269      * @param host the host
270      * @param port the port
271      * @param file the file
272      * @param handler the URLStreamHandler
273      * @return a URL
274      * @throws MalformedURLException
275      */

276     public URL getURL(final String JavaDoc protocol, final String JavaDoc host, final int port, final String JavaDoc file, final URLStreamHandler handler) throws MalformedURLException {
277         if (System.getSecurityManager() == null)
278             return new URL(protocol, host, port, file, handler);
279         try {
280             return (URL) AccessController.doPrivileged(new PrivilegedExceptionAction() {
281                 public Object JavaDoc run() throws MalformedURLException {
282                     return new URL(protocol, host, port, file, handler);
283                 }
284             }, controlContext);
285         } catch (PrivilegedActionException e) {
286             if (e.getException() instanceof MalformedURLException)
287                 throw (MalformedURLException) e.getException();
288             throw (RuntimeException JavaDoc) e.getException();
289         }
290     }
291
292     /**
293      * Creates a new Thread from a Runnable. Same as calling
294      * new Thread(target,name).
295      * @param target the Runnable to create the Thread from.
296      * @param name The name of the Thread.
297      * @return The new Thread
298      */

299     public Thread JavaDoc createThread(final Runnable JavaDoc target, final String JavaDoc name) {
300         if (System.getSecurityManager() == null)
301             return new Thread JavaDoc(target, name);
302         return (Thread JavaDoc) AccessController.doPrivileged(new PrivilegedAction() {
303             public Object JavaDoc run() {
304                 return new Thread JavaDoc(target, name);
305             }
306         }, controlContext);
307     }
308
309     /**
310      * Gets a service object. Same as calling
311      * context.getService(reference)
312      * @param reference the ServiceReference
313      * @param context the BundleContext
314      * @return a service object
315      */

316     public Object JavaDoc getService(final ServiceReference reference, final BundleContext context) {
317         if (System.getSecurityManager() == null)
318             return context.getService(reference);
319         return AccessController.doPrivileged(new PrivilegedAction() {
320             public Object JavaDoc run() {
321                 return context.getService(reference);
322             }
323         }, controlContext);
324     }
325
326     /**
327      * Returns a Class. Same as calling
328      * Class.forName(name)
329      * @param name the name of the class.
330      * @return a Class
331      * @throws ClassNotFoundException
332      */

333     public Class JavaDoc forName(final String JavaDoc name) throws ClassNotFoundException JavaDoc {
334         if (System.getSecurityManager() == null)
335             return Class.forName(name);
336         try {
337             return (Class JavaDoc) AccessController.doPrivileged(new PrivilegedExceptionAction() {
338                 public Object JavaDoc run() throws Exception JavaDoc {
339                     return Class.forName(name);
340                 }
341             }, controlContext);
342         } catch (PrivilegedActionException e) {
343             if (e.getException() instanceof ClassNotFoundException JavaDoc)
344                 throw (ClassNotFoundException JavaDoc) e.getException();
345             throw (RuntimeException JavaDoc) e.getException();
346         }
347     }
348
349     /**
350      * Returns a Class.
351      * Tries to load a class from the System ClassLoader or if that doesn't exist tries the boot ClassLoader
352      * @param name the name of the class.
353      * @return a Class
354      * @throws ClassNotFoundException
355      */

356     public Class JavaDoc loadSystemClass(final String JavaDoc name) throws ClassNotFoundException JavaDoc {
357         if (System.getSecurityManager() == null) {
358             ClassLoader JavaDoc systemClassLoader = ClassLoader.getSystemClassLoader();
359             return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : bootClassLoader.loadClass(name);
360         }
361         try {
362             return (Class JavaDoc) AccessController.doPrivileged(new PrivilegedExceptionAction() {
363                 public Object JavaDoc run() throws Exception JavaDoc {
364                     ClassLoader JavaDoc systemClassLoader = ClassLoader.getSystemClassLoader();
365                     return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : bootClassLoader.loadClass(name);
366                 }
367             }, controlContext);
368         } catch (PrivilegedActionException e) {
369             if (e.getException() instanceof ClassNotFoundException JavaDoc)
370                 throw (ClassNotFoundException JavaDoc) e.getException();
371             throw (RuntimeException JavaDoc) e.getException();
372         }
373     }
374             
375     /**
376      * Opens a ServiceTracker. Same as calling tracker.open()
377      * @param tracker the ServiceTracker to open.
378      */

379     public void open(final ServiceTracker tracker) {
380         if (System.getSecurityManager() == null) {
381             tracker.open();
382             return;
383         }
384         AccessController.doPrivileged(new PrivilegedAction() {
385             public Object JavaDoc run() {
386                 tracker.open();
387                 return null;
388             }
389         }, controlContext);
390     }
391
392     /**
393      * Starts a bundle.
394      * @param bundle the bundle to start
395      * @param options the start options
396      * @throws BundleException
397      */

398     public void start(final Bundle bundle, final int options) throws BundleException {
399         if (System.getSecurityManager() == null) {
400             bundle.start(options);
401             return;
402         }
403         try {
404             AccessController.doPrivileged(new PrivilegedExceptionAction() {
405                 public Object JavaDoc run() throws BundleException {
406                     bundle.start(options);
407                     return null;
408                 }
409             }, controlContext);
410             return;
411         } catch (PrivilegedActionException e) {
412             if (e.getException() instanceof BundleException)
413                 throw (BundleException) e.getException();
414             throw (RuntimeException JavaDoc) e.getException();
415         }
416     }
417
418     /**
419      * Starts a bundle
420      * @param bundle
421      * @throws BundleException
422      */

423     public void start(final Bundle bundle) throws BundleException {
424         start(bundle, 0);
425     }
426 }
427
Popular Tags