Posts tagged howto
Import scripts architecture, pt. II
1First post of this series ended with code snippet, which invokates importAction itself. It’s not still the importing itself, but we are almost there. This post will be about what happenes, what hapened before and what is going to happen :-). But first things first, let’s have a look on Factory and Proxy pattern, which are used while calling imports.
Factory pattern is based on idea instead of creating a new object with new operator, factory is asked, which returns a new instance. This is way standard Spring’s bean factory works. It may looks like following diagram (image is from oodesign.com):
To hide newly created instance behind an interface facade Proxy pattern is used. This functionality is imho basic of AOP programming, because instead of calling original object, something else may be called. Have a look on following diagram (again from oodesign.com):
Now it’s possible to put those patterns together and get real importAction functionality: importAction is just an interface, which hides importActionImpl newly created for every request. This can be seen on following snippet of application context configuration.
<bean id="ImportAction" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="targetName" value="importAction"/> <property name="singleton" value="false"/> <property name="proxyInterfaces"> <list> <value>cz.shmoula.imports.ImportAction</value> </list> </property> </bean> <bean id="importAction" class="cz.shmoula.imports.ImportActionImpl" scope="prototype" />
As I wrote a while before – all of this is done due to posibility of calling something else. But back to implementation, for details refer to some AOP documentation. Method importAction.invocateImportScript() is a pointcut, on which two advisors are bound. Have a look at following diagram.>
This configuration can be achieved by NameMatchMethodPointcutAdvisor, see following snippet for ImportContentAdvisor bean definition and updated ImportAction itself:
<bean id="importContentAdvice" class="cz.shmoula.imports.ImportContentAdvice" /> <bean id="ImportContentAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="importContentAdvice"/> <property name="mappedNames"> <list> <value>invocateImportScript</value> </list> </property> </bean> <bean id="ImportAction" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="targetName" value="importAction"/> <property name="singleton" value="false"/> <property name="proxyInterfaces"> <list> <value>cz.shmoula.imports.ImportAction</value> </list> </property> <property name="interceptorNames"> <list> <value>ImportContentAdvisor</value> <value>DeployContentAdvisor</value> </list> </property> </bean> <bean id="importActionTask" class="cz.shmoula.imports.ImportActionTask" scope="prototype"> <property name="importAction" ref="ImportAction"/> </bean>
Last few lines is definition of importActionTask I wrote in previous post. In this class importAction.invocateImportScript() method is called. This method has one parameter – map of properties of config file (in format <QName, Serializable>) to read them once and have them all the time and not need to call nodeService.getProperties() again and again – in advisors or importAction class. When this method is called, proxy of importAction catches this calling and calls invoke() method on ImportContentAdvice and DeployContentAdvice. Let’s have a look on invoke() method in ImportContentAdvice:
public Object invoke(MethodInvocation invocation) throws Throwable { Object[] objects = invocation.getArguments(); Object result = null; Map<QName, Serializable> propertiesMap = __resolve_arguments_to_map_(objects); String lockToken = (String) propertiesMap.get(ContentModel.PROP_NODE_UUID); setTokenOnTarget(invocation, lockToken); try { AuthenticationUtil.setRunAsUser(AuthenticationUtil.getSystemUserName()); misLockingService.createLock(lockToken); object = invocation.proceed(); misLockingService.releaseLock(lockToken); } finally { AuthenticationUtil.clearCurrentSecurityContext(); } return result; }
All what ImportActionAdvice does can be seen on previous snippet. It locks used config file to prevent access from another thread, using node uuid as a locking token. Then it authenticates a system user (mainImportThread runs outside any security context) and invocates next chain (DeployContentAdvisor and after then invocateImportScript(), if everything goes ok). After return from invocation lock is released and security context is cleared. If anything happens inside, lock release is skipped, so some rescue mechanisms are automaticaly initiated after some time. Simple, yet effective, i hope.
Import scripts architecture
1It has been some time since last post about import scripts from external sources to alfresco repository, I was busy with debugging and threading, but now I have some little time to write some notes on that topic again. I’d like to introduce „architecture“ of my solution, at least a first part of it, so lets start with an image.
MainImportingThread is Runnable implementation, which is initiated on init. Main thread sleeps some time and searches for config files in repository, while not sleep. If found some, ImportActionTask instance is created for each config in separate thread. ImportActionTask checks, if is it possible to run script described by config. If so, run script method on ImportAction is executed. That’s in few words functionality of the first part. Now in more details.
First version used quartz jobs triggered by org.alfresco.util.CronTriggerBean every 10 seconds, but that was temporarily solution, too heavy in my eyes. In second version I tried to use BatchProcessor, but spent few days without got it worked as I want :-(. Finally I used ThreadPoolTaskExecutor with combination of java.lang.Runnable in main thread and it worked like a charm! As I wrote a while ago, MainImportingThread has its own separate thread, which cyclically looks for configuration files in repository. It looks like following snippet:
public void init() { taskExecutor.execute(new Runnable() { @Override public void run() { while(keepAlive) { Thread.sleep(threadSleepMs); for(ResultSetRow row : resultSet) { Object oTarget = pool.makeObject(); if(oTarget instanceof ImportActionTask) { ImportActionTask importActionTask = (ImportActionTask) oTarget; importActionTask.setScriptRef(row.getNodeRef()); taskExecutor.execute(importActionTask); } pool.destroyObject(oTarget); } } taskExecutor.shutdown(); pool.destroy(); } }); }
For every row in result set PoolableObjectFactory creates a new instance of ImportActionTask and pass it to ThreadPoolTaskExecutor to execute. After all that – object is destroyed (destroyObject method is there just for sure – in time this method is called action still runs).
ImportActionTask is Runnable implementation, so every action runs its own thread. After execution isTimeToRun() method is called and action runs only when it’s its time and action is enabled, as it defined in data model illustrated on following diagram.
isTimeToRun() method uses fields importCron, importLastAction and isImportActive for its work. Its body looks similar to following simplified code snippet:
private boolean isTimeToRun() { if(!model.isImportActive()) return false; String cronString = model.getImportCron(); Date lastAction = model.getLastAction(); CronExpression cronExpression = new CronExpression(cronString); if(cronExpression.getNextValidTimeAfter(lastAction).getTime() > System.currentTimeMillis()) return false; return true; }
Previous snippet checks cron expression against time of last successfully executed action using org.quartz.CronExpression and in case of success (ie lastAction+cron < current time) importAction itself may be triggered. But more on this next time, right now just another snippet from ImportActionTask, which calls importAction (received from ProxyFactoryBean):
public void run() { if(isTimeToRun) { try { importAction.invocateImportScript(); } finally { importAction.clean(); } } }
Custom configs in Alfresco
0While creating import services I want some configuration mechanism for (not just) BatchProcessor. I like Alfresco’s *-config-custom.xml files (aka Spring framework XMLConfigService), so I did simple custom config service.
Anyone, who customized Alfresco, knows format of this file (web-client-config-custom.xml). I put that structure to new file – mis-config-custom.xml:
<alfresco-config> <config evaluator="string-compare" condition="imports"> <processor> <worker-threads-num>3</worker-threads-num> <batch-size-num>6</batch-size-num> <split-transactions>false</split-transactions> <logging-interval>1000</logging-interval> </processor> <token> <time-refresh>1000</time-refresh> <time-expiration>20000</time-expiration> </token> </config> </alfresco-config>
Please note those constants are just for ilustration purposes now, more on them in scripting series. My MisConfigService extends org.springframework.extensions.config.xml.XMLConfigService, but uses nothing from that class (except init method in bootstrap), I just added some statics inside to keep things together:
public class MisConfigService extends XMLConfigService { public MisConfigService(ConfigSource configSource) { super(configSource); } public static String getChildValue(Config config, String elementId, String childName, String defaultValue) { ConfigElement myConfig = config.getConfigElement(elementId); String result = myConfig.getChildValue(childName); return (result==null ? defaultValue : result); } public static Integer getChildValue(Config config, String elementId, String childName, Integer defaultValue) { Integer result = defaultValue; try { String number = getChildValue(config, elementId, childName, defaultValue.toString()); result = Integer.parseInt(number); } finally { /* O:-) */ } return result; } . . . }
To instantiate this service just add bean definition with UrlConfigSource(-s), which defines path to custom xml configuration:
<bean id="misConfigService" class="cz.mis.service.MisConfigService" init-method="init"> <constructor-arg> <bean class="org.springframework.extensions.config.source.UrlConfigSource"> <constructor-arg> <list> <value>classpath:alfresco/extension/mis-config-custom.xml</value> </list> </constructor-arg> </bean> </constructor-arg> </bean>
Now it’s possible to access those properties in code like this:
org.springframework.extensions.config.Config config = configService.getConfig(imports"); Boolean splitTransactions = MisConfigService.getChildValue(config, "processor", "split-transactions", false);
It’s also possible to access attributes of nodes in that config:
ConfigElement myConfig = config.getConfigElement("config-element"); for(ConfigElement child : myConfig.getChildren()) { // child.getAttributes() // child.getName() // child.getValue() }
That’s all! And now for something completely different! I also spent some time trying to get properties files working and putting those properties into Spring configurations. There’s nice solved thread in Alfresco forums. Prefix helps:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:alfresco/extension/mis-settings.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true" /> <property name="placeholderPrefix" value="${mis:" /> <property name="placeholderSuffix" value="}" /> </bean> <!-- and I can use stuff from properties file with prefix --> <bean class="foo.bar.Example"> <property name="fooAttribute"> <value>${mis:some.sample.property}</value> </property> </bean>
More on invoking scripts in Alfresco
0In my last blogpost on this topic I wrote about invoking rhino scripts programatically from Alfresco and about passing its model into freemarker templates. After that I realized that alfresco templateService is unable to render more complex models (nested classes and collections) if called this way (please note on Alfresco webscripts invoked standard way this works correctly!). I think the problem is – no wrapper is used while passing model. I spent lot of time trying many things, read a lot of posts and topics on that, played with built-ins, trying to find that part in Alfresco source, but still no result, just exceptions. But solution is VERY SIMPLE – to write own templateService :-). Well, maybe this post is more about Freemarker and Rhino than Alfresco, sorry about that!
So, what I want to achieve? Suppose some simple script like this one:
model.bar="foo"; model.foo = "bar"; model.obj = {q:1, w:3, e:9}; model.arr = new Array(); model.arr[0] = {a:1, b:8, c:9, d:{d:66}}; model.arr[1] = {a:2, b:7, c:10, d:{d:66}}; model.arr[2] = {a:3, b:6, c:11, d:{d:66}}; model.arr[3] = {a:4, b:5, c:12, d:{d:66}};
and a Freemarker template simple as that:
q attribute: ${obj.q} <#list arr?keys as value> - ${value} = ${arr[value].a} = ${arr[value].b} = ${arr[value].c} <#assign obj_d = arr[value].d> + d: ${obj_d.d} </#list>
Now after calling templateService.processTemplate( . . . ) the output is Exception this and Exception that… That happens, because Rhino creates NativeObjects for those objects and after passing those objects to Freemarker (through Java – without any ObjectWrapper), which doesn’t know how to handle them. The same situation happens when passing a collection with generics like <String, Object> into model – those Objects are standard Java objects and in Freemarker acts like them, they really don’t have attributes like q, a, b, etc. :-). Similar situation can happen with collections – they needs to be wrapped too. But stop with theory, back to practise. For those interested in wrapping and so, follow Freemarker documentation on wrappers.
Before wrapping – when I printed out keys of model, there were’nt my attributes defined in Javascript. What I found inside is on following listing, note my attributes are also there – bar, foo, arr, obj:
<#assign keys = model?keys> <#list keys as key> + ${key} </#list> (** output **) + getClass + clone + put + remove + get + equals + entrySet + class + hashCode + foo + keySet + size + clear + isEmpty + containsKey + values + arr + containsValue + empty + toString + putAll + bar + obj
Those properties belongs to hashMap and is possible to get rid of them using simpleMapWrapper, this technique is described well in Rizwan Ahmed post.
What about my templateService implementation? First I tried to use JSONtoFmModel and convert javascript objects to strings, but that was no way forward. That was before I discovered wrappers :-). There is RhinoWrapper class in Freemarker and it solves all my problems! It can be used as simple as this:
Configuration cfg = new Configuration(); RhinoWrapper rhinoWrapper = new RhinoWrapper(); rhinoWrapper.setSimpleMapWrapper(true); cfg.setObjectWrapper(rhinoWrapper);
Previous listing creates new Freemarker configuration instance and rhino wrapper instance and binds them together. With that configuration it’s possible to create template instance, which takes reader with template content, let’s read it from repository:
NodeRef nodeRef = new NodeRef("workspace://SpacesStore/56ba2237-f776-494a-939b-d259b68c021a"); ContentReader contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); InputStream inputStream = contentReader.getContentInputStream(); Template template = new Template("my_template", new InputStreamReader(inputStream), cfg);
That’s all we need to render. Rendering takes one more parameter – model map, which is HashMap defined in my previous blogpost. To keep it simple (and post a little shorter) I’m writing output to sysout:
OutputStreamWriter streamWriter = new OutputStreamWriter(java.lang.System.out); template.process(rootMap, streamWriter);
Those few lines and everything works fine now! But I got some silver hairs with trying to solve that, now I can feel like a boss, so I achieved new „Alfresco guru badge“ :-). Also I’m thinking about some public GIT repository with my Alfresco inventions, what you think about it?
PostScriptum: Can anyone explain me why Alfresco uses Rhino version 1.6R7 which is from year 2007? :-O
Invoking scripts in Alfresco programatically
4I’m working on universal CRON runner, which runs scripts saved somewhere in repository (more on this next time). Running webscripts isn’t so easy, as I thought, there is no scriptService.runScriptWithThisDescription(path) method, so I did some research and got some results, which I’d like to share.
It’s possible to run java backed actions from js-webscripts. Everything you need is just a bean extending ActionExecuterAbstractBase with definition parent=“action-executer“ and it’s possible to access that bean from webscript like actions.create(„that_bean_id“). But not in reverse, at least not so simple.
First think I found was post on forum and advice to look at unit tests of webscripts, but everything I found were invocations through http. So I went deeper and figured out that Alfresco uses Rhino JavaScript implementation for scripts. At Rhino pages are good examples, so I had first working script soon. At following codelist I firstly load content of node (script body) from repository and then invocate script itself.
NodeRef nodeRef = new NodeRef("workspace://SpacesStore/4a96aaaa-bb80-eeee-aaaa-800a43fcddb8"); ContentReader contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); InputStreamReader isr = new InputStreamReader(contentReader.getContentInputStream()); BufferedReader reader = new BufferedReader(isr); Context cx = Context.enter(); try { Scriptable scope = cx.initStandardObjects(); Object result = cx.evaluateReader(scope, reader, "scriptInvocation", 1, null); } finally { Context.exit(); }
But I wanted something more – some model in and out and something simplier. So I found ScriptService. Now all those long lines can be ommited, because there is executeScript method!
Map<String, Object> model = new HashMap<String, Object>(); model.put("foo", "bar"); Object result = scriptService.executeScript(scriptRef, model);
Template rendering (I’m using FreeMarker’s ftls) is done the same way, but through TemplateService and processTemplate(nodeRef, model) method, which accepts the same model (altered by script running).
The most beautiful thing is ability of script (and also template) to call java methods, which classes are „injected“ through model, let’s see an example.
Map<String, Object> model = new HashMap<String, Object>(); model.put("something", new Something()); model.put("bar", "foo"); Object result = scriptService.executeScript(scriptRef, model); String templateResult = templateService.processTemplate("workspace://SpacesStore/noderef", model); . . . public class Something { public String text = "aaa"; public String getText() { return "bbb"; } }
Now it’s possible to use script / template:
(** script **) bar = something.text; (** template **) ${something.text} ?=? ${bar}
What is output? Is in templateResult „aaa ?=? aaa“? Nope! Script accesses class attributes directly, on the other side template accesses methods. So output is „bbb ?=? aaa“!!!
One more important thing: this JavaScript implementation is just a stub, so every alfresco service needs to be „injected“ through model, so for example it’s impossible to run stuff like this without modifications:
var connector = remote.connect("http"); var result = connector.call("http://www.google.com/");
Remote object has to be injected (this script is from Share/Surf), more on this in this blogpost. You have to add ScriptRemote to model and create spring-webscripts-config-custom.xml with endpoints defined, see that post. Also another services needs to be injected – to get access to repository, search service… good starting point is Alfresco Wiki.
Edit: Native Java API Access described in wiki is wrong, IT IS POSSIBLE to access maps in JavaScript like this:
var value = javaMap.key;