1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.app.client;
18
19 import java.util.HashMap;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.gwe.api.ServerAPIConnectionException;
24 import org.gwe.api.Session4ClientAPI;
25 import org.gwe.api.Session4ClientAPIEnhancer;
26 import org.gwe.app.client.admin.ClientDaemonInstaller;
27 import org.gwe.app.client.admin.InstallerException;
28 import org.gwe.app.client.config.ClientConfig;
29 import org.gwe.drivers.bundleManagers.BundleType;
30 import org.gwe.persistence.model.DaemonConfigDesc;
31 import org.gwe.persistence.model.HeadResourceInfo;
32
33
34
35
36
37 public class SessionsRepository extends HashMap<HeadResourceInfo, Session4ClientAPIEnhancer> {
38
39 private static Log log = LogFactory.getLog(SessionsRepository.class);
40
41 private ClientConfig config;
42 private ProgressTracker tracker = ProgressTracker.LOG_TRACKER;;
43
44 public SessionsRepository(ClientConfig config) {
45 this.config = config;
46 }
47
48 public void setTracker(ProgressTracker tracker) {
49 this.tracker = tracker;
50 }
51
52 public Session4ClientAPIEnhancer getSession(HeadResourceInfo daemonInfo) throws InstallerException, ServerAPIConnectionException {
53 return getSession(daemonInfo, false);
54 }
55
56 public Session4ClientAPIEnhancer getSession(HeadResourceInfo daemonInfo, boolean installDaemonIfNeeded) throws InstallerException, ServerAPIConnectionException {
57 return getSession(daemonInfo, installDaemonIfNeeded, BundleType.ZIP);
58 }
59
60 public Session4ClientAPIEnhancer getSession(HeadResourceInfo daemonInfo, boolean installDaemonIfNeeded, BundleType bundleType) throws InstallerException, ServerAPIConnectionException {
61 if (daemonInfo == null) return null;
62 Session4ClientAPIEnhancer result = getTestedSession(daemonInfo);
63 if (result == null) {
64 result = createClientSession(daemonInfo, installDaemonIfNeeded, bundleType);
65 put(daemonInfo, result);
66 }
67 return result;
68 }
69
70 private Session4ClientAPIEnhancer getTestedSession(HeadResourceInfo selectedCluster) {
71 Session4ClientAPIEnhancer result = null;
72 try {
73 result = get(selectedCluster);
74 if (result != null)
75 result.getSession().connect();
76 } catch (ServerAPIConnectionException e) {
77 put(selectedCluster, null);
78 }
79 return result;
80 }
81
82 private Session4ClientAPIEnhancer createClientSession(HeadResourceInfo headResource, boolean installDaemonIfNeeded, BundleType bundleType) throws InstallerException, ServerAPIConnectionException {
83 DaemonConfigDesc daemonConfig = config.createDaemonConfig(headResource);
84 Session4ClientAPI session = new Session4ClientAPI(daemonConfig);
85
86 try {
87 tracker.trackProgress("Connecting to GWE daemon: " + daemonConfig.getConnectionURL() + " ...");
88 session.connect();
89 } catch (ServerAPIConnectionException e) {
90 tracker.trackProgress("Remote daemon not installed or running!");
91 if (!installDaemonIfNeeded) throw e;
92 new ClientDaemonInstaller(tracker, daemonConfig).setupDaemon(bundleType);
93 }
94
95 return new Session4ClientAPIEnhancer(session);
96 }
97 }
98