001 /*****************************************************************************
002 * *
003 * This file is part of the BeanShell Java Scripting distribution. *
004 * Documentation and updates may be found at http://www.beanshell.org/ *
005 * *
006 * Sun Public License Notice: *
007 * *
008 * The contents of this file are subject to the Sun Public License Version *
009 * 1.0 (the "License"); you may not use this file except in compliance with *
010 * the License. A copy of the License is available at http://www.sun.com *
011 * *
012 * The Original Code is BeanShell. The Initial Developer of the Original *
013 * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014 * (C) 2000. All Rights Reserved. *
015 * *
016 * GNU Public License Notice: *
017 * *
018 * Alternatively, the contents of this file may be used under the terms of *
019 * the GNU Lesser General Public License (the "LGPL"), in which case the *
020 * provisions of LGPL are applicable instead of those above. If you wish to *
021 * allow use of your version of this file only under the terms of the LGPL *
022 * and not to allow others to use your version of this file under the SPL, *
023 * indicate your decision by deleting the provisions above and replace *
024 * them with the notice and other provisions required by the LGPL. If you *
025 * do not delete the provisions above, a recipient may use your version of *
026 * this file under either the SPL or the LGPL. *
027 * *
028 * Patrick Niemeyer (pat@pat.net) *
029 * Author of Learning Java, O'Reilly & Associates *
030 * http://www.pat.net/~pat/ *
031 * *
032 *****************************************************************************/
033
034 package bsh;
035
036 import java.util.Hashtable;
037
038 /**
039 The map of extended features supported by the runtime in which we live.
040 <p>
041
042 This class should be independent of all other bsh classes!
043 <p>
044
045 Note that tests for class existence here do *not* use the
046 BshClassManager, as it may require other optional class files to be
047 loaded.
048 */
049 public class Capabilities
050 {
051 private static boolean accessibility = false;
052
053 public static boolean haveSwing() {
054 // classExists caches info for us
055 return classExists( "javax.swing.JButton" );
056 }
057
058 public static boolean canGenerateInterfaces() {
059 // classExists caches info for us
060 return classExists( "java.lang.reflect.Proxy" );
061 }
062
063 /**
064 If accessibility is enabled
065 determine if the accessibility mechanism exists and if we have
066 the optional bsh package to use it.
067 Note that even if both are true it does not necessarily mean that we
068 have runtime permission to access the fields... Java security has
069 a say in it.
070 @see bsh.ReflectManager
071 */
072 public static boolean haveAccessibility()
073 {
074 return accessibility;
075 }
076
077 public static void setAccessibility( boolean b )
078 throws Unavailable
079 {
080 if ( b == false )
081 {
082 accessibility = false;
083 return;
084 }
085
086 if ( !classExists( "java.lang.reflect.AccessibleObject" )
087 || !classExists("bsh.reflect.ReflectManagerImpl")
088 )
089 throw new Unavailable( "Accessibility unavailable" );
090
091 // test basic access
092 try {
093 String.class.getDeclaredMethods();
094 } catch ( SecurityException e ) {
095 throw new Unavailable("Accessibility unavailable: "+e);
096 }
097
098 accessibility = true;
099 }
100
101 private static Hashtable classes = new Hashtable();
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 name )
112 {
113 Object 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 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 s ){ super(s); }
140 }
141 }
142
143