How to create a Java Map in Javascript
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2010 06:42 AM
Does anyone know how I can create a Java Map in Javascript, it seems like this should work:
But it doesn't work because although the new sentence runs flawlessly "a" is just null.
I see that Alfresco's scripts use always the "ScriptableHashMap" type as a result type for many of their functions, so I tried:
But with the same sad result.
Is there any reason why Rhino or Alfresco's Rhino integration doesn't allow the creation of maps ? Something security policyes related or somehting like that ?
var a = new java.util.HashMap();a.put("key1", "value1");a.put("key2", "value2");
But it doesn't work because although the new sentence runs flawlessly "a" is just null.
I see that Alfresco's scripts use always the "ScriptableHashMap" type as a result type for many of their functions, so I tried:
var a = Packages.org.alfresco.repo.jscript.ScriptableHashMap();
But with the same sad result.
Is there any reason why Rhino or Alfresco's Rhino integration doesn't allow the creation of maps ? Something security policyes related or somehting like that ?
Labels:
- Labels:
-
Archive
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2010 07:25 PM
Hi,
in second example you should use new operator
but why not to use simple JavaScript Array?
Thanks,
smicyk
in second example you should use new operator
var a = new Packages.org.alfresco.repo.jscript.ScriptableHashMap();
but why not to use simple JavaScript Array?
var a = new Array();a["key1"] = "value1";a["key2"] = "value2";
Thanks,
smicyk
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2010 03:49 AM
The lack of the "new" was a typo in the post, I have tried with the new but no luck.
I want to use a Map because I've exposed the ruleService as a JavaScript object and it requires a Map in some of its methods, and the javascript array does not work as a replacement.
Thanks.
I want to use a Map because I've exposed the ruleService as a JavaScript object and it requires a Map in some of its methods, and the javascript array does not work as a replacement.
Thanks.

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2013 01:11 PM
Hey, sorry I'm like three years late
But anyway if someone else stumbles upon this, I've written a simple wrapper around native hash table to match the functionality of Java Map. Note that Java Map isnt the same as Java Hash Map, cause Map has a linear seek of keys by value, and a HashMap has a hash table of value to key structure, so seek time is so much faster.
Anyway you can find Javascript Map class on this link: http://stamat.wordpress.com/2013/06/23/javascript-map-class/
Dont forget to leave feedback! Thanks for giving it a chance

Anyway you can find Javascript Map class on this link: http://stamat.wordpress.com/2013/06/23/javascript-map-class/
Dont forget to leave feedback! Thanks for giving it a chance

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2015 04:52 PM
I've to use reflection for using native QNname Property Map.
<javascript>
//imports
ContextLoader = Packages.org.springframework.web.context.ContextLoader;
NamespaceService = Packages.org.alfresco.service.namespace.NamespaceService;
QName = Packages.org.alfresco.service.namespace.QName;
Class = java.lang.Class;
/**
* Gets the Bean instance object. You neet to declare a global variable e.g. NamespaceService = Packages.org.alfresco.service.namespace.NamespaceService;
*/
function srv(bname) {
return ContextLoader.getCurrentWebApplicationContext().getBean(bname, this[bname]);
}
function main(){
// Class reflection to a PropertyMap Alfresco implementation
// PropertyMap extends HashMap<QName, Serializable> so it avoids the HashMap<T> definitions
var c = Class.forName("org.alfresco.util.PropertyMap");
// Get a Method Object Reflexion for use the natural HashMap#put implementation
var putMethod = c.getMethod("put", Class.forName("java.lang.Object"), Class.forName("java.lang.Object"));
// ~ var propMap = {};
var propMap = c.newInstance();
// ~ propMap ['custom
rop'] = 'some val';
putMethod.invoke(propMap, QName.createQName('custom
rop', srv("NamespaceService")), 'some val');
}
main();
</javascript>
<javascript>
//imports
ContextLoader = Packages.org.springframework.web.context.ContextLoader;
NamespaceService = Packages.org.alfresco.service.namespace.NamespaceService;
QName = Packages.org.alfresco.service.namespace.QName;
Class = java.lang.Class;
/**
* Gets the Bean instance object. You neet to declare a global variable e.g. NamespaceService = Packages.org.alfresco.service.namespace.NamespaceService;
*/
function srv(bname) {
return ContextLoader.getCurrentWebApplicationContext().getBean(bname, this[bname]);
}
function main(){
// Class reflection to a PropertyMap Alfresco implementation
// PropertyMap extends HashMap<QName, Serializable> so it avoids the HashMap<T> definitions
var c = Class.forName("org.alfresco.util.PropertyMap");
// Get a Method Object Reflexion for use the natural HashMap#put implementation
var putMethod = c.getMethod("put", Class.forName("java.lang.Object"), Class.forName("java.lang.Object"));
// ~ var propMap = {};
var propMap = c.newInstance();
// ~ propMap ['custom

putMethod.invoke(propMap, QName.createQName('custom

}
main();
</javascript>
