]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8ca05c567660a9945e92ad569316799404716e3e
[tmp/jakarta-migration.git] /
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package org.collectionspace.services.nuxeo;
6
7 import java.io.File;
8 import java.util.Collection;
9 import org.nuxeo.ecm.core.client.NuxeoApp;
10 import org.nuxeo.ecm.core.client.NuxeoClient;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 /**
15  * NuxeoConnector is a facade to Nuxeo remoting client
16  * @author 
17  */
18 public class NuxeoConnector {
19     //FIXME: get port and host from configuration
20     public static int NUXEO_PORT = 62474;
21     public static String NUXEO_HOST = "localhost";
22     private Logger logger = LoggerFactory.getLogger(NuxeoConnector.class);
23     private static final NuxeoConnector self = new NuxeoConnector();
24     private NuxeoApp app;
25     private NuxeoClient client;
26     volatile boolean initialized = false; //use volatile for lazy initialization in singleton
27
28     private NuxeoConnector() {
29     }
30
31     public final static NuxeoConnector getInstance() {
32         return self;
33     }
34
35     public void initialize() throws Exception {
36
37         try{
38             if(initialized == false){
39                 setProperties();
40                 app = new NuxeoApp();
41                 app.start();
42                 if(logger.isDebugEnabled()) {
43                     logger.debug("initialize() NuxeoApp started");
44                 }
45                 loadBundles();
46                 client = NuxeoClient.getInstance();
47                 initialized = true;
48             }
49         }catch(Exception e){
50             if(logger.isDebugEnabled()){
51                 e.printStackTrace();
52             }
53         }
54     }
55
56     private void loadBundles() throws Exception {
57         String bundles = "nuxeo-client/lib/nuxeo-runtime-*:nuxeo-client/lib/nuxeo-*";
58         Collection<File> files = null;
59         if(bundles != null){
60             files = NuxeoApp.getBundleFiles(new File("."), bundles, ":");
61         }
62         if(logger.isDebugEnabled()){
63             logger.debug("loadBundles(): deploying bundles: " + files);
64         }
65         if(files != null){
66             app.deployBundles(files);
67         }
68     }
69
70     private void setProperties() {
71         System.setProperty("org.nuxeo.runtime.server.enabled", Boolean.FALSE.toString());
72         System.setProperty("org.nuxeo.runtime.server.port", "" + NUXEO_PORT);
73         System.setProperty("org.nuxeo.runtime.server.host", "127.0.0.1");
74         //System.setProperty("org.nuxeo.runtime.1.3.3.streaming.port", "3233");
75         System.setProperty("org.nuxeo.runtime.streaming.serverLocator", "socket://127.0.0.1:3233");
76         System.setProperty("org.nuxeo.runtime.streaming.isServer", Boolean.FALSE.toString());
77         System.setProperty("org.nuxeo.client.remote", Boolean.TRUE.toString());
78     }
79
80     public NuxeoClient getClient() throws Exception {
81         if(initialized == true){
82 //            if(client.isConnected()){
83 //                return client;
84 //            }else{
85             //authentication failure error comes when reusing the client
86             //fore connect for now
87                 client.forceConnect(NUXEO_HOST, NUXEO_PORT);
88                 if(logger.isDebugEnabled()){
89                     logger.debug("getClient(): connection successful port=" + NUXEO_PORT);
90                 }
91                 return client;
92 //            }
93         }
94         String msg = "NuxeoConnector is not initialized!";
95         logger.error(msg);
96         throw new IllegalStateException(msg);
97     }
98 }