KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > Capabilities


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34 package bsh;
35
36 import java.util.Hashtable JavaDoc;
37
38 /**
39     The map of extended features supported by the runtime in which we live.
40     <p>
41
42     This class should be independent of all other bsh classes!
43     <p>
44
45     Note that tests for class existence here do *not* use the
46     BshClassManager, as it may require other optional class files to be
47     loaded.
48 */

49 public class Capabilities
50 {
51     private static boolean accessibility = false;
52
53     public static boolean haveSwing() {
54         // classExists caches info for us
55
return classExists( "javax.swing.JButton" );
56     }
57
58     public static boolean canGenerateInterfaces() {
59         // classExists caches info for us
60
return classExists( "java.lang.reflect.Proxy" );
61     }
62
63     /**
64         If accessibility is enabled
65         determine if the accessibility mechanism exists and if we have
66         the optional bsh package to use it.
67         Note that even if both are true it does not necessarily mean that we
68         have runtime permission to access the fields... Java security has
69         a say in it.
70         @see bsh.ReflectManager
71     */

72     public static boolean haveAccessibility()
73     {
74         return accessibility;
75     }
76
77     public static void setAccessibility( boolean b )
78         throws Unavailable
79     {
80         if ( b == false )
81         {
82             accessibility = false;
83             return;
84         }
85
86         if ( !classExists( "java.lang.reflect.AccessibleObject" )
87             || !classExists("bsh.reflect.ReflectManagerImpl")
88         )
89             throw new Unavailable( "Accessibility unavailable" );
90
91         // test basic access
92
try {
93             String JavaDoc.class.getDeclaredMethods();
94         } catch ( SecurityException JavaDoc e ) {
95             throw new Unavailable("Accessibility unavailable: "+e);
96         }
97
98         accessibility = true;
99     }
100
101     private static Hashtable JavaDoc classes = new Hashtable JavaDoc();
102     /**
103         Use direct Class.forName() to test for the existence of a class.
104         We should not use BshClassManager here because:
105             a) the systems using these tests would probably not load the
106             classes through it anyway.
107             b) bshclassmanager is heavy and touches other class files.
108             this capabilities code must be light enough to be used by any
109             system **including the remote applet**.
110     */

111     public static boolean classExists( String JavaDoc name )
112     {
113         Object JavaDoc c = classes.get( name );
114
115         if ( c == null ) {
116             try {
117                 /*
118                     Note: do *not* change this to
119                     BshClassManager plainClassForName() or equivalent.
120                     This class must not touch any other bsh classes.
121                 */

122                 c = Class.forName( name );
123             } catch ( ClassNotFoundException JavaDoc e ) { }
124
125             if ( c != null )
126                 classes.put(c,"unused");
127         }
128
129         return c != null;
130     }
131
132     /**
133         An attempt was made to use an unavailable capability supported by
134         an optional package. The normal operation is to test before attempting
135         to use these packages... so this is runtime exception.
136     */

137     public static class Unavailable extends UtilEvalError
138     {
139         public Unavailable(String JavaDoc s ){ super(s); }
140     }
141 }
142
143
144
Popular Tags