incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling > Adding New Scripting Variables
Date Mon, 25 Jan 2010 20:15:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=SLING&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/SLING/Adding+New+Scripting+Variables">Adding
New Scripting Variables</a></h2>
    <h4>Page  <b>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~justinedelson">Justin
Edelson</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <p>As of r902947, Sling Scripting allows bundles to contribute objects to the
variables available to scripts.</p>

<p>This is done by implementing the interface <tt>org.apache.sling.scripting.api.BindingsValuesProvider</tt>
and exposing the implementation as an OSGi Service. Implementations are able to add one or
more name/value pairs to the <tt>javax.script.Bindings</tt> object, but they <b>cannot</b>
overwrite or remove any of the default scripting variables (defined here: <a href="/confluence/display/SLING/Scripting+variables"
title="Scripting variables">Scripting variables</a>). These custom bindings can be
configured to apply to <b>any</b> scripting language or a specific scripting language.</p>

<p>An example of the binding of a custom object can be found in the commons.json bundle
(<a href="http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/groovy/internal/JSONGroovyBuilderBindingsValuesProvider.java?revision=902962&amp;view=markup"
rel="nofollow">source</a>):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> javax.script.Bindings;

<span class="code-keyword">import</span> org.apache.sling.commons.json.groovy.JSONGroovyBuilder;
<span class="code-keyword">import</span> org.apache.sling.scripting.api.BindingsValuesProvider;

/**
 * BindingsValuesProvider which binds an instance of JSONGroovyBuilder.
 *
 * @scr.component immediate=<span class="code-quote">"<span class="code-keyword">true</span>"</span>
metatype=<span class="code-quote">"no"</span>
 * @scr.service
 *
 * @scr.property name=<span class="code-quote">"service.description"</span> value=<span
class="code-quote">"JSONGroovyBuilder BindingsValuesProvider"</span>
 * @scr.property name=<span class="code-quote">"service.vendor"</span> value=<span
class="code-quote">"The Apache Software Foundation"</span>
 *
 * @scr.property name=<span class="code-quote">"javax.script.name"</span> value=<span
class="code-quote">"groovy"</span>
 */
<span class="code-keyword">public</span> class JSONGroovyBuilderBindingsValuesProvider
<span class="code-keyword">implements</span> BindingsValuesProvider {

    /**
     * {@inheritDoc}
     */
    <span class="code-keyword">public</span> void addBindings(Bindings bindings)
{
        bindings.put(<span class="code-quote">"jsonBuilder"</span>, <span class="code-keyword">new</span>
JSONGroovyBuilder());
    }

}
</pre>
</div></div>

<p>In this example, the <tt>jsonBuilder</tt> key is bound to an instance
of <tt>JSONGroovyBuilder</tt> only for Groovy scripts.</p>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="http://cwiki.apache.org/confluence/display/SLING/Adding+New+Scripting+Variables">View
Online</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/SLING/Adding+New+Scripting+Variables?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message