cancel
Showing results for 
Search instead for 
Did you mean: 

Connect to Alfresco Repository remotelly (via RMI) SOLVED

sergey_khariton
Champ in-the-making
Champ in-the-making
Hi!

I got a problem with deletion large folder with a lot of subfolders and files. Using Alfresco Explorer or WebScripts did not help due to large amount of files and folders. It locked server  and finished with time out exception.
After some investigation I found that Alfresco allows RMI connection to core services.
I tried and had success.
1. Create spring configuration in ctl-context.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
    <!– Client Ticket Holder –>
    <bean id="clientTicketHolder" class="org.alfresco.repo.remote.ClientTicketHolderGlobal"/>

    <!– Remote AVM interface –>
    <bean id="avmRemote" class="org.alfresco.repo.remote.AVMRemoteImpl">
        <property name="avmRemoteTransport">
            <ref bean="avmRemoteTransport"/>
        </property>
        <property name="clientTicketHolder">
            <ref bean="clientTicketHolder"/>
        </property>
    </bean>
   
    <bean id="avmRemoteTransport" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl">
            <value>rmi://localhost:50500/avm</value>
        </property>
        <property name="serviceInterface">
            <value>org.alfresco.service.cmr.remote.AVMRemoteTransport</value>
        </property>
        <property name="refreshStubOnConnectFailure">
            <value>true</value>
        </property>
    </bean>

   <bean id="avmSyncService" class="org.alfresco.repo.remote.AVMSyncServiceRemote">
       <property name="avmSyncServiceTransport">
          <ref bean="avmSyncServiceTransport"/>
       </property>
       <property name="clientTicketHolder">
           <ref bean="clientTicketHolder"/>
       </property>
   </bean>

    <bean id="avmSyncServiceTransport" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl">
            <value>rmi://localhost:50500/avmsync</value>
        </property>
        <property name="serviceInterface">
            <value>org.alfresco.service.cmr.remote.AVMSyncServiceTransport</value>
        </property>
        <property name="refreshStubOnConnectFailure">
            <value>true</value>
        </property>
    </bean>   
   
    <bean id="authenticationService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl">
            <value>rmi://localhost:50500/authentication</value>
        </property>
        <property name="serviceInterface">
            <value>org.alfresco.service.cmr.security.AuthenticationService</value>
        </property>
        <property name="refreshStubOnConnectFailure">
            <value>true</value>
        </property>
    </bean>
   
    <bean id="repoRemoteTransport" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl">
            <value>rmi://localhost:50500/repo</value>
        </property>
        <property name="serviceInterface">
            <value>org.alfresco.service.cmr.remote.RepoRemoteTransport</value>
        </property>
        <property name="refreshStubOnConnectFailure">
            <value>true</value>
        </property>
    </bean>
   
    <bean id="repoRemote" class="org.alfresco.repo.remote.RepoRemoteImpl">
        <property name="repoRemoteTransport">
            <ref bean="repoRemoteTransport"/>
        </property>
        <property name="clientTicketHolder">
            <ref bean="clientTicketHolder"/>
        </property>
    </bean>
</beans>
2. Create simple java class

package ru.eurekabpo.alfresco.rmi;

import java.util.Map;

import org.alfresco.repo.remote.ClientTicketHolderGlobal;
import org.alfresco.service.cmr.remote.RepoRemote;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.util.Pair;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AlfrescoRMI {
   private static int counter = 10;

   private static void processChildren(RepoRemote repoRemote, NodeRef nodeRef) {
      counter–;
      if (counter < 0) {
         return;
      }      
      Map<String, Pair<NodeRef, Boolean>> children = repoRemote
            .getListing(nodeRef);
      for (String key : children.keySet()) {
         Pair<NodeRef, Boolean> pair = children.get(key);
         System.out.println(key+":"+pair.getFirst().toString()+" "+pair.getSecond());
         processChildren(repoRemote,pair.getFirst());
      }
   }

   /**
    * @param args
    */
   public static void main(String[] args) {

      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "clt-context.xml");

      AuthenticationService authService = (AuthenticationService) context
            .getBean("authenticationService");

      authService.authenticate("admin", "admin".toCharArray());

      ClientTicketHolderGlobal ticketHolder = (ClientTicketHolderGlobal) context
            .getBean("clientTicketHolder");
      ticketHolder.setTicket(authService.getCurrentTicket());
      
      RepoRemote repoRemote = (RepoRemote) context.getBean("repoRemote");

      NodeRef nodeRef = repoRemote.getRoot();
      System.out.println(nodeRef.toString());
      processChildren(repoRemote, nodeRef);

      
      ;

   }
}
3. Import these jars into project:
alfresco-core-3.4.d.jar
alfresco-data-model-3.4.d.jar
alfresco-remote-api-3.4.d.jar
alfresco-repository-3.4.d.jar
aopalliance.jar
commons-logging-1.1.jar
log4j-1.2.16.jar
org.springframework.aop-3.0.0.jar
org.springframework.asm-3.0.0.jar
org.springframework.beans-3.0.0.jar
org.springframework.context-3.0.0.jar
org.springframework.core-3.0.0.jar
org.springframework.expression-3.0.0.jar
4. Run AlfrescoRMI from Eclipse (clt-context.xml should be on the class path)
You will get something like this:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
workspace://SpacesStore/927ba2d4-4e02-46a9-8408-5a69b35a5c42
10229.tif:workspace://SpacesStore/bc25e9b5-eb5d-472f-a749-4cc9f43b5ea3 false
doclib:workspace://SpacesStore/d36d8829-2d8f-4425-adab-ee4d83adfb30 false
imgpreview:workspace://SpacesStore/5abc7eb8-83cd-4264-870f-774f8c5e73dd false
Data Dictionary:workspace://SpacesStore/df13fd65-638e-4c9a-9ffe-b50a301ad89f true
Email Actions:workspace://SpacesStore/61cab3d8-0642-44b0-a6fa-17a18a61fa22 true
search:workspace://SpacesStore/5e4ecb30-a26a-42f8-8c3b-2956009773d3 true
Email Templates:workspace://SpacesStore/b11c5fb1-1e8a-4a52-b562-0debf4458882 true
Invite Email Templates:workspace://SpacesStore/4c701b8a-2fc1-4cb7-89a4-315dd14e4c31 true
invite_user_email.ftl:workspace://SpacesStore/b70f9728-039f-4246-af48-08d981ca5b6d false
Notify Email Templates:workspace://SpacesStore/d344e020-c969-4f08-b83e-a73330c572fd true
invite:workspace://SpacesStore/a88c673f-2cc2-46ca-beda-df40334b1d3c true
Imap Configs:workspace://SpacesStore/cda37cbc-7c51-4bff-80e1-ef90246c790b true
LINK:workspace://SpacesStore/c40f88f1-95a0-4031-9948-adb92f77cb58 true
Messages:workspace://SpacesStore/071cac25-5d08-4f5c-afb2-b1f4a41ff26a true
Models:workspace://SpacesStore/a9a9f477-5c57-11dc-ad6c-5136d620963c true
Presentation Templates:workspace://SpacesStore/a4a5f921-a900-4c29-b332-54e20d974e82 true
RSS Templates:workspace://SpacesStore/4889d932-04f8-48d9-9edb-1e75771e2eb7 true
Rendering Actions Space:workspace://SpacesStore/rendering_actions_space true
Replication Actions Space:workspace://SpacesStore/replication_actions_space true
Saved Searches:workspace://SpacesStore/930de7c5-6216-4e56-86ae-4ec5c228f3cd true
Scheduled Actions:workspace://SpacesStore/aa8e70b9-e153-49ac-82f0-9dbc511c9923 true
Scripts:workspace://SpacesStore/93a6baeb-d237-44ef-bf28-993b00f537a9 true
Space Templates:workspace://SpacesStore/d378cac7-f39f-4e75-8118-0244b97c8a4f true
Transfers:workspace://SpacesStore/97146956-ec05-440c-9d1f-768877bd90b5 true
Web Client Extension:workspace://SpacesStore/58f53330-8da7-4be6-a13a-8ffe171a7666 true
Web Forms:workspace://SpacesStore/17179791-8ff7-4dc8-b345-4108b6efef95 true
Web Scripts:workspace://SpacesStore/693c4c6e-1d9b-4757-8ead-6e151e13e58f true
Web Scripts Extensions:workspace://SpacesStore/14f315ca-b051-4d39-9d1c-ca6e730ca47b true
Workflow Definitions:workspace://SpacesStore/05590cd0-607e-11dc-af48-8b100325f217 true
Guest Home:workspace://SpacesStore/9e4afa73-a480-4a9e-9574-65eb6985ce70 true
Sites:workspace://SpacesStore/1f3e368d-a6a9-4f5e-b791-d4da37f2a8bb true
Test Folder:workspace://SpacesStore/1e7f84c6-91b1-47da-b3ca-3c0a836e21f4 true
User Homes:workspace://SpacesStore/1f32dbb1-b11d-41ee-9e4f-405b94bdf307 true
Web Deployed:workspace://SpacesStore/4f6b1ed5-bbcf-4698-a8d3-86963a322ebf true
Web Projects:workspace://SpacesStore/8d51cd48-c17d-44e2-8de7-14672f2e21f5 true
file.pdf:workspace://SpacesStore/59e156f0-478a-4643-96af-5022daab2aa8 false
file50.pdf:workspace://SpacesStore/fb6c36d5-de9b-4f2b-9f71-a6b7517b30e5 false
filenull.pdf:workspace://SpacesStore/5b04e855-af95-40df-b04c-ef3b9d1cdb70 false
indexing-min.js:workspace://SpacesStore/bafd4ec7-019c-4e9e-80a4-f555e1c84424 false
sites.acp:workspace://SpacesStore/7c1e6274-954d-41dc-a828-e35ad22ae17f false
5. This code allows to delete folders remotelly without stopping server
3 REPLIES 3

shmoula
Champ in-the-making
Champ in-the-making
Hi Sergey,
  thanks for showing way to do this, but unfortunetly I'm unable to call a method on that proxy. I'm getting following exception while invoking authService.authenticate(…):

Exception in thread "main" org.springframework.remoting.RemoteProxyFailureException: No matching RMI stub method found for: public abstract void org.alfresco.service.cmr.security.AuthenticationService.authenticate(java.lang.String,char[]) throws org.alfresco.repo.security.authentication.AuthenticationException; nested exception is java.lang.NoSuchMethodException: sun.rmi.registry.RegistryImpl_Stub.authenticate(java.lang.String, [C)
   at org.springframework.remoting.rmi.RmiClientInterceptorUtils.invokeRemoteMethod(RmiClientInterceptorUtils.java:122)
   at org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:363)
   at org.springframework.remoting.rmi.RmiClientInterceptor.invoke(RmiClientInterceptor.java:259)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy0.authenticate(Unknown Source)
   at cz.shmoula.alfresco.AlfrescoRMI.main(AlfrescoRMI.java:48)
Caused by: java.lang.NoSuchMethodException: sun.rmi.registry.RegistryImpl_Stub.authenticate(java.lang.String, [C)
   at java.lang.Class.getMethod(Class.java:1605)
   at org.springframework.remoting.rmi.RmiClientInterceptorUtils.invokeRemoteMethod(RmiClientInterceptorUtils.java:114)
   … 6 more

Where can be problem? I'm using Alfresco community 3.4.0.d.

Thanks in advance!

sergey_khariton
Champ in-the-making
Champ in-the-making
I guess, you have different versions of jars in Alfresco and in RMI client side.

shmoula
Champ in-the-making
Champ in-the-making
Hi Sergey, thanks for answer. I checked version twice, problem was somewhere else: I had to specify server hostname in a form like in alfresco virtualization server while starting tomcat:

-Djava.rmi.server.hostname=172-31-149-197.ip.alfrescodemo.net

Now it works okay, so thank for your time and have a nice weekend!

PS OMG, that's slooooooww!!! 😮