]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bd67b290bb6fc433370860756d45bf2cdf15d69f
[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     public void release() throws Exception {
57         if(initialized == true) {
58             client.disconnect();
59         }
60     }
61
62     private void loadBundles() throws Exception {
63         String bundles = "nuxeo-client/lib/nuxeo-runtime-*:nuxeo-client/lib/nuxeo-*";
64         Collection<File> files = null;
65         if(bundles != null){
66             files = NuxeoApp.getBundleFiles(new File("."), bundles, ":");
67         }
68         if(logger.isDebugEnabled()){
69             logger.debug("loadBundles(): deploying bundles: " + files);
70         }
71         if(files != null){
72             app.deployBundles(files);
73         }
74     }
75
76     private void setProperties() {
77         System.setProperty("org.nuxeo.runtime.server.enabled", Boolean.FALSE.toString());
78         System.setProperty("org.nuxeo.runtime.server.port", "" + NUXEO_PORT);
79         System.setProperty("org.nuxeo.runtime.server.host", "127.0.0.1");
80         //System.setProperty("org.nuxeo.runtime.1.3.3.streaming.port", "3233");
81         System.setProperty("org.nuxeo.runtime.streaming.serverLocator", "socket://127.0.0.1:3233");
82         System.setProperty("org.nuxeo.runtime.streaming.isServer", Boolean.FALSE.toString());
83         System.setProperty("org.nuxeo.client.remote", Boolean.TRUE.toString());
84     }
85
86     public NuxeoClient getClient() throws Exception {
87         if(initialized == true){
88 //            if(client.isConnected()){
89 //                return client;
90 //            }else{
91             //authentication failure error comes when reusing the client
92             //fore connect for now
93                 client.forceConnect(NUXEO_HOST, NUXEO_PORT);
94                 if(logger.isDebugEnabled()){
95                     logger.debug("getClient(): connection successful port=" + NUXEO_PORT);
96                 }
97                 return client;
98 //            }
99         }
100         String msg = "NuxeoConnector is not initialized!";
101         logger.error(msg);
102         throw new IllegalStateException(msg);
103     }
104 }