BlogAlfresco custom bootstrap context

Anyone who knows a thing or two about Alfresco, also knows the whole system was developed with customization in mind. From data models, through authentication system, to the way permissions are set up, everything can be customized. More often that not, these customizations only require editing particular XML configuration file, but if needed, writing your own code is always a possibility.

One of those configuration files is called bootstrap-context.xml. Its name basically gives away what it does: it sets up what needs to be done (and in what order) during the Alfresco startup. So, if one would want to do something during that time, that would be the place to look at. Of course, best practices should always be followed, so the proper way would be to create your own custom-bootstrap-context.xml file, place it in the extensions folder, while leaving the original file alone.

In our particular case, while working on our ECM solution that sits on top of Alfresco, we had the need to query an external database, and use that information to set up certain parts of application – before it became accessible to the end user. Obviously we needed to write our own code for that, and we needed to have it executed during the startup. While we won’t get into details about our own implementation, we will see what exactly needs to be done to achieve this in broad terms.

First step is to implement logic that is important to you. While we don’t know what exactly that is, we do know that in order to have it run during bootstrap you have to:

  • Create your own bean that extends the AbstractLifecycleBean class
  • Implement its onBootstrap and onShutdown methods

And that’s basically that. Take a look below how that might look like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.springframework.context.ApplicationEvent;
import org.springframework.extensions.surf.util.AbstractLifecycleBean;
 
public class TestBootstrapBean extends AbstractLifecycleBean {
 
	@Override
	protected void onBootstrap(ApplicationEvent arg0) {
		doWork();
	}
 
	@Override
	protected void onShutdown(ApplicationEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	protected void doWork() {
 
		// Hello Alfresco!
 
	}
 
}

Naturally, we can inject Alfresco services we need to implement our logic. For instance, if we need to use nodeService to add an aspect to a node, we’d add a setter, and follow that up on custom-bootstrap-context.xml (we’ll show this in the next step).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.springframework.context.ApplicationEvent;
import org.springframework.extensions.surf.util.AbstractLifecycleBean;
 
public class TestBootstrapBean extends AbstractLifecycleBean {
 
	private NodeService nodeService;
 
	@Override
	protected void onBootstrap(ApplicationEvent arg0) {
		doWork();
	}
 
	@Override
	protected void onShutdown(ApplicationEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	protected void doWork() {
 
		NodeRef node = new NodeRef("yourNodeRef");
		nodeService.addAspect(node, ContentModel.ASPECT_VERSIONABLE, null);
 
	}
 
	public NodeService getNodeService() {
		return nodeService;
	}
 
	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}
 
}

After we’re done with this part, we still have to remember to create our XML configuration file. It too is nothing too complicated, rather a simple matter of knowing name of the class we created, and dependency we created through the use of nodeService.

1
2
3
4
5
6
7
8
9
<beans>
 
    <bean id="TestBootstrapBean" class="com.test.TestBootstrapBean" >
       <property name="nodeService">
           <ref bean="NodeService"/>
       </property>    
    </bean>
 
</beans>

If you’re looking into how to achieve the same, but through JavaScript, we won’t cover that this time around. But, take a look at the ScriptBootstrap addon which does that exact thing.

Tags: Alfresco, bootstrap, customization, java