cancel
Showing results for 
Search instead for 
Did you mean: 

How to invoke a Java class from WebScripts

anwar501pasha
Champ in-the-making
Champ in-the-making
Hi All,

I have a business requirement where I need to call a Java Class from a Webscript.

Please can anyone help me how I can call a Java class from webscripts or link which can help me to call the Java class from webscripts.

Thanks & Regards,
Anwar
6 REPLIES 6

mrogers
Star Contributor
Star Contributor
Look at script-services-context.xml which is where you define which classes are available to the script API.

Basically what you do is give your class a name which is then available in the scripting environment.

anwar501pasha
Champ in-the-making
Champ in-the-making
Hi Morgers,

Thanks for the quick reply.

I have gone through the 'script-services-context.xml' but was not able to exactly pickup exactly how it can be done.

If possible can you please provide me the sample code which will adress my issue.

Thanks & Regards,
Anwar

tara_b
Champ in-the-making
Champ in-the-making
Your java class has to be in the classpath. I put mine in WEB-INF/classes/org/alfresco/sample, with the appropriate package name(org.alfresco.sample). You need a compiled .class in there, not a .java.

Then in the script-services-context.xml file, you add in a new bean of your new class, and call it what you want:

  
 <bean id="searchCustomScript" parent="baseJavaScriptExtension" class="org.alfresco.sample.CustomSearch">
         <property name="extensionName">
              <value>customSearch</value>
         </property>
    </bean>

Depending what your java class will do, you might also want to inject some properties, such as:
        
<property name="serviceRegistry">
            <ref bean="ServiceRegistry"/>
        </property>
        <property name="storeUrl">
            <value>${spaces.store}</value>
        </property>

In your web script, you'll be able to call customSearch.blah(…), much the same as you call logger.log(….).

norgan
Champ in-the-making
Champ in-the-making
Hei Anwar, a big point for Tara please Smiley Happy

(besides - I never checked, is this mentioned in the Wiki already, Tara, or can you add it ?)

Norgan

jck
Champ in-the-making
Champ in-the-making
With Cocoon which used, and still uses, a js rhino implementation (the same as Alfresco I think), it was possible to call java class directly from js code, with a special syntax like noted in this html page:
* http://cocoon.apache.org/2.1/userdocs/flow/java.html

Could it be possible to use such a mechanism?

JC

rmorant
Champ in-the-making
Champ in-the-making
With Cocoon which used, and still uses, a js rhino implementation (the same as Alfresco I think), it was possible to call java class directly from js code..
JSScriptProcessor disables this behavior :


      // remove security issue related objects - this ensures the script may not access
            // unsecure java.* libraries or import any other classes for direct access - only
            // the configured root host objects will be available to the script writer
            this.secureScope.delete("Packages");
            this.secureScope.delete("getClass");
            this.secureScope.delete("java");

bad luck JCK.

Tara, do you have some code example?
(org.alfresco.sample.CustomSearch)

I'm fighting with spring-surf M3, the parent="baseJavaScriptExtension" has been changed by "baseScriptExtension" in spring-surf-script-services-context.xml
but I can't find any implementation example.
The only way I found is override "webscripts.abstractcontainer" bean, but i prefer extend than override.
Any suggestions?
Thanks.