cancel
Showing results for 
Search instead for 
Did you mean: 

TransactionService and ServiceRegistry - Spring injection

sergio
Champ in-the-making
Champ in-the-making
Hi all.

I need to write a Java package in order to manipulate Alfresco repository objects in custom ways. I am just creating a custom Java package and I want to have a reference to alfresco services (nodeService, …) inside my Java classes, so I am going to direct inject Alfresco beans into my classes. Because I need to manage all the task in a transactional way, I need a reference to TransactionService into my classes. I was thinking about direct injecting a TransactionService bean into my classes, but it failed.

My questions are the following:
1) how to direct inject TransactionService into my classes (if possible)?
2) Instead of TransactionService is it possible to direct inject the ServiceRegistry bean?

I followed the FirstFoundationClient example, but it creates a new ApplicationContext, on the contrary I want to use the ApplicationContext already created during the Alfresco boot.

I tried the write a Java bean and direct inject the TransactionComponent and the AuthenticationComponentImpl beans. My bean has an init() method as the following:

public void init ()
  {

    try {
         transactionService.getUserTransaction().begin();
      } catch (NotSupportedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (SystemException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      this.authenticationService.authenticate("admin", "admin".toCharArray());
  }

It works fine but I would like to know if this is the correct way to go on or not.

Many thanks to all.

Sergio
4 REPLIES 4

rwetherall
Confirmed Champ
Confirmed Champ
Hi Sergio,

Injecting the ServiceRegistry is a good idea as it gives you easy access to a number of fundamental services.

To inject the service your bean should look something like this …



package com.mystuff;

public MyBean
{
    private ServiceRegistry serviceRegistry;

   public void setServiceRegistry(ServiceRegistry serviceRegistry)
   {
      this.serviceRegistry = serviceRegistry;
   }

   ….

}

.. and the associated Spring config for the bean would look something like this …



   <bean id="myBean" class="com.mystuff.MyBean">
       <property name="serviceRegitry">
         <ref bean="ServiceRegistry"/>
       </property>
     
      …

    </bean>


You wouldn't start a user transaction in the init method of bean since is a singleton instance and makes no sense.

Instead start and stop the user transactions in each method.  Use the TransactionUtil class to help with this.

Cheers,
Roy

sergio
Champ in-the-making
Champ in-the-making
Many thanks Roy.

Hear from you soon,

Sergio

sergio
Champ in-the-making
Champ in-the-making
Hi Roy.

Once I have created a class direct injected with basic Alfresco services, the next step is to be able to use that class as a web service published into Alfresco like any other Alfresco WS.

I tried the following:
1) created a .wsdl file for the class;
2) copied the .class file with its own package into the Alfresco \lib folder;
3) modified the server-config.wsdd Alfresco file including the reference to the new web service;
4) copied the .wsdl file into the \alfresco\wsdl folder;
5) direct injected into the .class file nodeService bean using Spring through a -context.xml file.

The bean is loaded fine, but when I try to call the web service using a client the nodeService - initially correctly loaded - seems to be null and everything goes wrong.

I suppose that when the ws is called by the client, a new instance of the Java class is created, so I am not able to point to the fisrt instance of the Java class initially loaded as a Spring bean. As a consequence, I am not able to use the nodeService (and whatever Alfresco service) via the web service.

This is a big problem for me, and I have no idea how to resolve it. The web service should be able to run inside the same context initially created during the boot of Alfresco. In this way, I would be able to direct inject into my web service whatever parameter I want just only during the Alfresco start-up.

I will appreciate a lot any help.

All the best,

Sergio

rwetherall
Confirmed Champ
Confirmed Champ
Hi Sergio,

What have you done about generating you're server side and client stubs from the WSDL?

Once you have the server stubs you can base your web service implementation on them.  I presume this is how you are working?

Cheers,
Roy