Access control in BeanShell classes and methods

Pascal Meheut <[email protected]> Wed, 14 Feb 2007 15:17:07 +0100
Newsgroups gmane.comp.java.beanshell.devel
Message-ID <[email protected]>
Hi, this is my first post in this list and I hope it is appropriate. If 
not, let me know.

I'm trying to integrate BeanShell in a commercial software not only to 
provide scripting but also to support dynamically created methods in 
also dynamically created classes.
For the dynamically created classes, we generate the bytecode and load 
ourself for various reason, mainly easy integration in our screen & 
persistence system.

But on top of normal, stored attributes in these classes, I would like 
to support some formulas too. Basically, the idea is to allow a 
power-user to add its own types in the system and stuff like "field 
c=field a+field b"... Nothing new but helps a lot to make a commercial 
system more flexible.

Instead of parsing and compiling/interpreting the formulas myself, I 
would like to use BeanShell of course. But a lot of access control comes 
in the way.
Here are a few examples:

- I want to check in realtime if the formula the user enters is valid. 
To do so, the system calls a method like this:

    public LabeledBoolean checkFormula()
    {
      if(formula!=null && formula.length()>0)
      {
        Parser parser=new Parser(new StringReader(formula));
        try
        {
          parser.Expression();
        }
        catch (ParseException exception)
        {
          return new LabeledBoolean(false, exception.getMessage());
        }
      }
      return LabeledBoolean.OK;
    }

and display the results near the formula field on the screen. But if I 
want to check more things, for instance the syntaxic tree out of the 
parser to detect any BSHAmbiguousName and checks its name against the 
valid ones, i.e. the ones declared in my dynamic class, I can't.
Because even SimpleNode is package protected, not public.

Isnt'it strange that "popNode" is public but returns a package-only class ?


- I find also difficult to remove the default imports. For instance, I 
do not want the scripts in the system to access Swing. I can of course 
give my own namespace and detect such access but the simplest solution 
would be to extends NameSpace and to override loadDefaultImports.
But even if I do so, I cannot easily put this MyNameSpace as the global 
one.
Because the interpreter constructor does:

        BshClassManager bcm = BshClassManager.createClassManager( this );
        if ( namespace == null )
            this.globalNameSpace = new NameSpace( bcm, "global");

And if I want to pass it MyNameSpace,  it has to be built before and so 
cannot contains the link to BshClassManager.createClassManager( this )...
Ok, I know the BshClassManager is not used a lot in NameSpace and I 
could pass null but will it still work in the future...

- Same thing for getNameResolver and the names Hashtable? Even if I 
found workaround, it would have been nice to have them as protected to 
connect the NameSpace to my own objects (importObject is not enough. 
Works fine for fields and methods but not for bean properties)

I know that moving something from "package" access to "protected" has a 
lot of impact because it becomes part of the contract of the class with 
the external world and cannot be changed easily later on without 
breaking source code compatibility but in some cases, it would also 
extend the useability of BeanShell a lot.

Waiting for your answers and back to coding...

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV