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, 29 Nov 2010 20:53:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=SLING&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/SLING/Adding+New+Scripting+Variables">Adding
New Scripting Variables</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~justinedelson">Justin
Edelson</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-changed-lines" >As of <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">r902947,</span>
<span class="diff-added-words"style="background-color: #dfd;">version 2.0.14 of</span>
Sling Scripting <span class="diff-added-words"style="background-color: #dfd;">Core,
Sling Scripting</span> allows bundles to contribute objects to the variables available
to scripts by exposing OSGi services. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>This can be done by implementing
either {{java.util.Map}} or {{org.apache.sling.scripting.api.BindingsValuesProvider}}. Implement
{{org.apache.sling.scripting.api.BindingsValuesProvider}} if you need access to the current
script variables. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>As of version 2.0.14 of Sling Scripting Core, 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?revision=904226&amp;view=markup"
class="external-link" 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"
class="external-link" 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="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/SLING/Adding+New+Scripting+Variables">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=12321182&revisedVersion=5&originalVersion=4">View
Changes</a>
                |
        <a href="https://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