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 Thu, 28 Jan 2010 19:47: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>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~justinedelson">Justin
Edelson</a>
    </h4>
     adding information about Map-based providers
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     adding information about Map-based providers<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <p>As of r902947, Sling Scripting allows bundles to contribute objects to the
variables available to scripts by exposing OSGi services.</p>

<p>This can be done by implementing either <tt>java.util.Map</tt> or <tt>org.apache.sling.scripting.api.BindingsValuesProvider</tt>.
Implement <tt>org.apache.sling.scripting.api.BindingsValuesProvider</tt> if you
need access to the current script variables.</p>

<p>In either case, 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 using <tt>java.util.Map</tt>
can be found in the extensions.groovy bundle (<a href="http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/json/internal/JSONGroovyBuilderBindingsValuesProvider.java?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> org.apache.sling.commons.json.groovy.JSONGroovyBuilder;

/**
 * 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">extends</span> HashMap&lt;<span class="code-object">String</span>,<span
class="code-object">Object</span>&gt; {

    <span class="code-keyword">public</span> JSONGroovyBuilderBindingsValuesProvider()
{
        <span class="code-keyword">super</span>();
        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>

<p>An example of the binding of a custom object using <tt>org.apache.sling.scripting.api.BindingsValuesProvider</tt>
can be found in the jcr.resource bundle (<a href="http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/scripting/CurrentNodeBindingsValuesProvider.java?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.jcr.Node;
<span class="code-keyword">import</span> javax.script.Bindings;

<span class="code-keyword">import</span> org.apache.sling.api.resource.Resource;
<span class="code-keyword">import</span> org.apache.sling.scripting.api.BindingsValuesProvider;

/**
 * BindingsValuesProvider <span class="code-keyword">for</span> currentNode object.
 *
 * @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">"CurrentNode 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">"any"</span>
 */
<span class="code-keyword">public</span> class CurrentNodeBindingsValuesProvider
<span class="code-keyword">implements</span> BindingsValuesProvider {

    /**
     * {@inheritDoc}
     */
    <span class="code-keyword">public</span> void addBindings(Bindings bindings)
{
        Resource resource = (Resource) bindings.get(<span class="code-quote">"resource"</span>);
        Node node = resource.adaptTo(Node.class);
        <span class="code-keyword">if</span> (node != <span class="code-keyword">null</span>)
{
            bindings.put(<span class="code-quote">"currentNode"</span>, node);
        }

    }

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


     </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/pages/diffpagesbyversion.action?pageId=12321182&revisedVersion=3&originalVersion=2">View
Change</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