Debian Codegraph @

subprojects/groovy-bsf/src/spec/doc/integrating-bsf.adoc

raw
1 //////////////////////////////////////////
2
3 Licensed to the Apache Software Foundation (ASF) under one
4 or more contributor license agreements. See the NOTICE file
5 distributed with this work for additional information
6 regarding copyright ownership. The ASF licenses this file
7 to you under the Apache License, Version 2.0 (the
8 "License"); you may not use this file except in compliance
9 with the License. You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing,
14 software distributed under the License is distributed on an
15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 KIND, either express or implied. See the License for the
17 specific language governing permissions and limitations
18 under the License.
19
20 //////////////////////////////////////////
21
22 = Bean Scripting Framework
23
24 WARNING: The http://commons.apache.org/proper/commons-bsf/[Bean Scripting Framework] is an attempt to create an API
25 to allow calling scripting languages from Java. It hasn't been updated for long and abandoned in favor of the
26 standard <<jsr223,JSR-223>> API.
27
28 The BSF engine for Groovy is implemented by the `org.codehaus.groovy.bsf.GroovyEngine` class. However, that fact is
29 normally hidden away by the BSF APIs. You just treat Groovy like any of the other scripting languages via the BSF
30 API.
31
32 NOTE: Since Groovy has its own native support for integration with Java, you only need to worry about BSF if you also
33 want to also be able to call other languages, e.g. http://jruby.org/[JRuby] or if you want to remain very
34 loosely coupled from your scripting language.
35
36 == Getting started
37
38 Provided you have Groovy and BSF jars in your classpath, you can use the
39 following Java code to run a sample Groovy script:
40
41 [source,java]
42 -------------------------------------------------------------------------------
43 include::{rootProjectDir}/subprojects/groovy-bsf/src/spec/test/BSFSpecTest.java[tags=bsf_simple,indent=0]
44 -------------------------------------------------------------------------------
45
46 == Passing in variables
47
48 BSF lets you pass beans between Java and your scripting language. You
49 can _register_/_unregister_ beans which makes them known to BSF. You can
50 then use BSF methods to _lookup_ beans as required. Alternatively, you
51 can _declare_/_undeclare_ beans. This will register them but also make
52 them available for use directly in your scripting language. This second
53 approach is the normal approach used with Groovy. Here is an example:
54
55 [source,java]
56 -----------------------------------------------------------------------
57 include::{rootProjectDir}/subprojects/groovy-bsf/src/spec/test/BSFSpecTest.java[tags=bsf_variable_passing,indent=0]
58 -----------------------------------------------------------------------
59
60 == Other calling options
61
62 The previous examples used the _eval_ method. BSF makes multiple methods
63 available for your use (see the
64 http://commons.apache.org/proper/commons-bsf/manual.html[BSF documentation] for more
65 details). One of the other available methods is _apply_. It allows you
66 to define an anonymous function in your scripting language and apply
67 that function to arguments. Groovy supports this function using
68 closures. Here is an example:
69
70 [source,java]
71 --------------------------------------------------------------------------------
72 include::{rootProjectDir}/subprojects/groovy-bsf/src/spec/test/BSFSpecTest.java[tags=bsf_apply,indent=0]
73 --------------------------------------------------------------------------------
74
75 == Access to the scripting engine
76
77 Although you don’t normally need it, BSF does provide a hook that lets
78 you get directly to the scripting engine. One of the functions which the
79 engine can perform is to invoke a single method call on an object. Here
80 is an example:
81
82 [source,java]
83 --------------------------------------------------------------------------
84 include::{rootProjectDir}/subprojects/groovy-bsf/src/spec/test/BSFSpecTest.java[tags=bsf_access,indent=0]
85 --------------------------------------------------------------------------
86