1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.app.client.config;
18
19 import java.io.File;
20 import java.io.Serializable;
21 import java.util.Map;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.gwe.GWEAppContext;
26 import org.gwe.app.client.ProgressTracker;
27 import org.gwe.app.client.SessionsRepository;
28 import org.gwe.p2elv2.macro.PMacroLibrary;
29 import org.gwe.p2elv2.macro.PMacroRepo;
30 import org.gwe.persistence.model.DaemonConfigDesc;
31 import org.gwe.persistence.model.GridInfo;
32 import org.gwe.persistence.model.HeadResourceInfo;
33 import org.gwe.persistence.model.order.OrderDescriptor;
34 import org.gwe.persistence.model.order.p2el.POrderDescriptor;
35 import org.gwe.utils.IOUtils;
36 import org.gwe.utils.rex.REXException;
37 import org.gwe.utils.security.AccountInfo;
38 import org.gwe.utils.security.KeyStore;
39
40
41
42
43
44 public class ClientConfig {
45
46 private static final String MACRO_EXTENSION = ".p2el";
47
48 private static Log log = LogFactory.getLog(ClientConfig.class);
49
50 public static final String GWE_HOME_ENV_VAR = "GWE_HOME";
51
52 public static String extractGWE_HOME() {
53 String gweHome = System.getenv(GWE_HOME_ENV_VAR);
54 return (gweHome == null || "".equals(gweHome)) ? "" : IOUtils.concatenatePaths(gweHome, "");
55 }
56
57 public static InstallationFiles getInstallFiles() {
58 return new InstallationFiles(extractGWE_HOME());
59 }
60
61 protected GWEAppContext appContext;
62
63 private GridInfo grid;
64 private KeyStore keys;
65
66 private PMacroRepo macroRepo = new PMacroRepo();
67 private SessionsRepository sessionsRepository = new SessionsRepository(this);
68
69 public ClientConfig(String host, String daemonRootPath, String user, String password) {
70 this(host, daemonRootPath, KeyStore.createKeyStore(new AccountInfo(user, password), host));
71 }
72
73 public ClientConfig(String host, String daemonRootPath, KeyStore keys) {
74 this(new HeadResourceInfo(host, daemonRootPath), keys);
75 }
76
77 public ClientConfig(HeadResourceInfo clusterHeadNode, KeyStore keys) {
78 this(new GridInfo(clusterHeadNode), keys);
79 }
80
81 public ClientConfig(ClientConfigReader configReader) {
82 this(configReader.getGrid(), configReader.getKeys());
83 }
84
85 public ClientConfig(GridInfo grid, KeyStore keys) {
86 String installPath = extractGWE_HOME();
87 this.appContext = new GWEAppContext(ClientConfig.class, installPath, installPath);
88 keys.test();
89 this.keys = keys;
90 this.grid = grid;
91
92 String version = appContext.getDistribution().getVersion();
93 for (HeadResourceInfo headRes : grid.getHeadResources()) headRes.setVersion(version);
94 grid.validate();
95
96 loadMacros(new File(getInstallFiles().getConfigFilePath("gwe-macros")).listFiles());
97 log.info("Confguration read!");
98
99 }
100
101 private void addShutdownHooks() {
102 Thread processReapersShutdown = new Thread(new Runnable() {
103 public void run() {
104 Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
105 for (Thread thread : allStackTraces.keySet())
106 if (thread.isDaemon() && thread.getName().contains("process reaper"))
107 thread.interrupt();
108 }
109 });
110 Runtime.getRuntime().addShutdownHook(processReapersShutdown);
111 }
112
113 private void loadMacros(File[] macroFiles) {
114 log.info("Loading P2EL macros...");
115 for (File macroFile : macroFiles) {
116 if (macroFile.getName().endsWith(MACRO_EXTENSION)) {
117 try {
118 String currLibCategory = macroFile.getName().replace(MACRO_EXTENSION, "").toUpperCase();
119 PMacroLibrary currLib = PMacroLibrary.read(macroFile.getCanonicalPath(), true);
120 currLib.setCategory(currLibCategory);
121 macroRepo.add(currLib);
122 } catch (Exception e) {
123 log.warn("Could not load macros from library '" + macroFile + "'", e);
124 }
125 }
126 }
127 log.info("P2EL macros loaded!");
128 }
129
130 public void setTracker(ProgressTracker tracker) {
131 sessionsRepository.setTracker(tracker);
132 }
133
134 public GWEAppContext getAppContext() {
135 return appContext;
136 }
137
138 public void setAppContext(GWEAppContext appContext) {
139 this.appContext = appContext;
140 }
141
142 public GridInfo getGrid() {
143 return grid;
144 }
145
146 public KeyStore getKeys() {
147 return keys;
148 }
149
150 public PMacroRepo getMacroRepo() {
151 return macroRepo;
152 }
153
154 public SessionsRepository getSessionsRepository() {
155 return sessionsRepository;
156 }
157
158 public DaemonConfigDesc createDaemonConfig(HeadResourceInfo headResource) {
159 return new DaemonConfigDesc(headResource, keys, appContext.getDistribution());
160 }
161
162 public OrderDescriptor<Serializable> createOrderDescriptor(String stmt) throws REXException {
163 String parsedStmt = macroRepo.applyMacros(stmt.replaceAll("\\s{1,}", " ").trim());
164 return new POrderDescriptor<Serializable>(parsedStmt);
165 }
166 }
167