[prev in list] [next in list] [prev in thread] [next in thread] 

List:       wsf-c-dev
Subject:    Re: [Dev] ESB script mediator - inject Java class as a JS native object
From:       Malaka Silva <malaka () wso2 ! com>
Date:       2015-03-31 11:30:14
Message-ID: CANy7KD29Aeb4rDo5uafbCM6HfP-o2qfoXXj=BkTrFco13N8h0w () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi Akila,

You can try following,

        <script language="js"><![CDATA[
             importPackage(Packages.org.......JSUtils);
             ......................
             var result = JSUtils.parseJSON(someJsonVar);
 ]]></script>


On Tue, Mar 31, 2015 at 3:31 PM, Akila Ravihansa Perera <ravihansa@wso2.com>
wrote:

> Hi,
>
> Is it possible to do $subject? I've been playing around with wso2-synapse
> code base but couldn't find a way around without overriding ScriptMediator
> class which is something I'm trying to avoid.
>
> My requirement is that I've developed some Java Util methods which I would
> like to expose as JS native functions. I tried writing a class mediator and
> do the bindings but couldn't get it working.
>
> ScriptEngineManager manager = new ScriptEngineManager();
> manager.registerEngineExtension("jsEngine", new
> RhinoScriptEngineFactory());
> ScriptEngine scriptEngine = manager.getEngineByExtension("jsEngine");
>
> Bindings bindings = scriptEngine.createBindings();
> bindings.put("jsUtils", new JSUtils());
>
> JSUtils Java class has some string operations that should be accessible
> via JS. Inside JS script mediator I should be able to something like this;
>
> var result = jsUtils.parseJSON(someJsonVar);
>
>
> I know this can be done by purely using the class mediator to evaluate the
> whole JS source. But I'd much rather use built-in script mediator and only
> inject my custom Java classes onto it.
>
> I think this would be a cool feature to have, if it is not already
> supported.
>
> Thanks.
>
> --
> Akila Ravihansa Perera
> Software Engineer, WSO2
>
> Blog: http://ravihansa3000.blogspot.com
>



-- 

Best Regards,

Malaka Silva
Senior Tech Lead
M: +94 777 219 791
Tel : 94 11 214 5345
Fax :94 11 2145300
Skype : malaka.sampath.silva
LinkedIn : http://www.linkedin.com/pub/malaka-silva/6/33/77
Blog : http://mrmalakasilva.blogspot.com/

WSO2, Inc.
lean . enterprise . middleware
http://www.wso2.com/
http://www.wso2.com/about/team/malaka-silva/
<http://wso2.com/about/team/malaka-silva/>

Save a tree -Conserve nature & Save the world for your future. Print this
email only if it is absolutely necessary.

[Attachment #5 (text/html)]

<div dir="ltr">Hi Akila,<div><br></div><div>You can try \
following,</div><div><br></div><div><div style="font-size:13.333333969116211px">      \
&lt;script language=&quot;js&quot;&gt;&lt;![CDATA[<br></div><div \
style="font-size:13.333333969116211px">                    \
importPackage(Packages.org.......JSUtils);</div><div \
style="font-size:13.333333969116211px">                    \
......................</div><div style="font-size:13.333333969116211px">              \
<span style="color:rgb(0,0,0)">var result = \
JSUtils.parseJSON(someJsonVar);</span></div><div \
style="font-size:13.333333969116211px"><span style="white-space:pre-wrap">	</span>  \
]]&gt;&lt;/script&gt;</div><div \
style="font-size:13.333333969116211px"><br></div></div></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 31, 2015 at 3:31 PM, \
Akila Ravihansa Perera <span dir="ltr">&lt;<a href="mailto:ravihansa@wso2.com" \
target="_blank">ravihansa@wso2.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><div dir="ltr"><span \
style="color:rgb(0,0,0);font-size:12.8000001907349px">Hi,</span><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px"><br></div><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px">Is it possible to do $subject? \
I&#39;ve been playing around with wso2-synapse code base but couldn&#39;t find a way \
around without overriding ScriptMediator class which is something I&#39;m trying to \
avoid.</div><div style="color:rgb(0,0,0);font-size:12.8000001907349px"><br></div><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px">My requirement is that I&#39;ve \
developed some Java Util methods which I would like to expose as JS native functions. \
I tried writing a class mediator and do the bindings but couldn&#39;t get it \
working.</div><div style="color:rgb(0,0,0);font-size:12.8000001907349px"><br>ScriptEngineManager \
manager = new ScriptEngineManager();<br>manager.registerEngineExtension(&quot;jsEngine&quot;, \
new RhinoScriptEngineFactory());<br>ScriptEngine scriptEngine = \
manager.getEngineByExtension(&quot;jsEngine&quot;);<br><br>Bindings bindings = \
scriptEngine.createBindings();<br></div><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px">bindings.put(&quot;jsUtils&quot;, \
new JSUtils());</div><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px"><br></div><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px">JSUtils Java class has some \
string operations that should be accessible via JS. Inside JS script mediator I \
should be able to something like this;</div><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px"><br></div><div \
style="color:rgb(0,0,0);font-size:12.8000001907349px">var result = \
jsUtils.parseJSON(someJsonVar);<br><div><br></div><div><br></div><div>I know this can \
be done by purely using the class mediator to evaluate the whole JS source. But \
I&#39;d much rather use built-in script mediator and only inject my custom Java \
classes onto it.</div><div><br></div><div>I think this would be a cool feature to \
have, if it is not already \
supported.</div><div><br></div><div>Thanks.</div></div><div><br></div>-- \
<br><div>Akila Ravihansa Perera<br>Software Engineer, WSO2<br><br>Blog: <a \
href="http://ravihansa3000.blogspot.com" \
target="_blank">http://ravihansa3000.blogspot.com</a></div> </div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div \
class="gmail_signature"><div dir="ltr"><div><br></div><font color="#666666">Best \
Regards,</font><div><font color="#666666"><br>Malaka Silva<br>Senior Tech Lead<br>M: \
+94 777 219 791</font></div><div><span style="color:rgb(102,102,102)">Tel :  \
</span><a value="+94112145345">94 11 214 5345</a><br></div><div><div><font \
color="#666666">Fax :94 11  2145300  </font></div><div><font color="#666666">Skype : \
malaka.sampath.silva</font></div><font color="#666666">LinkedIn :  <a \
href="http://www.linkedin.com/pub/malaka-silva/6/33/77" \
target="_blank">http://www.linkedin.com/pub/malaka-silva/6/33/77</a></font></div><div><span \
style="color:rgb(102,102,102)">Blog :</span>  <a \
href="http://mrmalakasilva.blogspot.com/" \
target="_blank">http://mrmalakasilva.blogspot.com/</a><font \
color="#666666"><br></font></div><div><br></div><div><font color="#666666">WSO2, Inc. \
</font></div><div><font color="#666666">lean . enterprise . middleware  <br></font><a \
href="http://www.wso2.com/" target="_blank">http://www.wso2.com/</a><font \
color="#666666"><br></font></div><div><a \
href="http://wso2.com/about/team/malaka-silva/" \
target="_blank">http://www.wso2.com/about/team/malaka-silva/</a><br></div><div><br></div><div><span \
style="font-family:Tahoma,sans-serif;color:rgb(0,176,80);font-size:8pt">Save a tree \
-Conserve nature &amp; Save the world for your future. Print this email only if it is \
absolutely necessary.</span><br></div></div></div> </div>



_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic