1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.persistence.model;
18
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.persistence.CascadeType;
29 import javax.persistence.Entity;
30 import javax.persistence.GeneratedValue;
31 import javax.persistence.Id;
32 import javax.persistence.OneToMany;
33 import javax.persistence.Transient;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.gwe.app.daemon.DaemonApp;
38 import org.gwe.drivers.fileSystems.FileHandle;
39 import org.gwe.drivers.netAccess.ShellCommand;
40 import org.gwe.utils.IOUtils;
41 import org.gwe.utils.security.AccountInfo;
42 import org.gwe.utils.security.KeyStore;
43 import org.gwe.utils.security.ProtocolScheme;
44 import org.gwe.utils.security.Realm;
45 import org.gwe.utils.security.ResourceLink;
46 import org.gwe.utils.security.ThinURI;
47 import org.hibernate.annotations.GenericGenerator;
48
49
50
51
52
53
54 @GenericGenerator(name = "headResourceInfoIdGenerator", strategy = "org.gwe.persistence.model.HeadResourceInfoIdGenerator")
55 @Entity
56 public class HeadResourceInfo extends BaseModelInfo<String> {
57
58 private static Log log = LogFactory.getLog(HeadResourceInfo.class);
59
60
61 public static final String BASE_SCRIPT = "gwed-base-script.sh";
62
63 public static ShellCommand createDaemonScriptCommand(String daemonHomePath, Class<?> mainClass, int debugPort) {
64 String binFolder = OSAppFolder.BINARIES.getRelativeTo(daemonHomePath);
65 String baseScript = IOUtils.concatenatePaths(binFolder, BASE_SCRIPT);
66 String debugFlag = "";
67 if (debugPort >= 1000) debugFlag = debugPort + "";
68 String launchCmd = baseScript + " " + debugFlag + " " + mainClass.getName() + " " + daemonHomePath;
69
70
71 ShellCommand cmd = new ShellCommand(launchCmd, daemonHomePath, null);
72 cmd.setInactivityTimeout(120000);
73 return cmd;
74 }
75
76 @Id
77 @GeneratedValue(generator = "headResourceInfoIdGenerator")
78 private String location = null;
79
80 private String host;
81
82 private String name = "";
83
84
85 private int queueSize = -1;
86 private float maxHijackMins = -1;
87 private float maxIdleMins = -1;
88 private float maxWaitMins = -1;
89 private long heartBeatPeriodSecs;
90
91 private String version;
92 private String resourceManager;
93
94 private String installRootPath = "";
95 private String databaseRootPath;
96
97 private String platform;
98
99 @OneToMany(cascade = CascadeType.ALL)
100 private List<VarInfo> vars;
101
102 @Transient
103 private int debugPort = -1;
104
105 @Transient
106 private transient List<OrderInfo> ordersList = new ArrayList<OrderInfo>();
107
108 public HeadResourceInfo() {}
109
110 public HeadResourceInfo(String host, String daemonRootPath) {
111 setHost(host);
112 setInstallRootPath(daemonRootPath);
113 }
114
115 public String getId() {
116 return getLocation();
117 }
118
119 public String getLocation() {
120 return (location == null) ? generateRMIBaseURI() : location;
121 }
122
123 private void setLocation(String location) {
124 this.location = location;
125 }
126
127 public String getHost() {
128 return host;
129 }
130
131 private void setHost(String host) {
132 this.host = host;
133 }
134
135 public String getVersion() {
136 return version;
137 }
138
139 public void setVersion(String version) {
140 this.version = version;
141 }
142
143 public String getResourceManager() {
144 return resourceManager;
145 }
146
147 public void setResourceManager(String resourceManagers) {
148 this.resourceManager = resourceManagers;
149 }
150
151 public String getDatabasePath() {
152 return getGWEPath(getDatabaseRootPath());
153 }
154
155 public String getDatabaseRootPath() {
156 if (databaseRootPath == null || "".equals(databaseRootPath))
157 databaseRootPath = getInstallRootPath();
158
159 return databaseRootPath;
160 }
161
162 public void setDatabaseRootPath(String dbRootPath) {
163 this.databaseRootPath = dbRootPath;
164 }
165
166 public String getInstallRootPath() {
167 return installRootPath;
168 }
169
170 public void setInstallRootPath(String installRootPath) {
171 this.installRootPath = installRootPath;
172 }
173
174 public String getInstallPath() {
175 return getGWEPath(getInstallRootPath());
176 }
177
178 private String getGWEPath(String rootPath) {
179 return IOUtils.concatenatePaths(rootPath, "gwe-daemon", "gwe-" + version);
180 }
181
182 public DaemonInstallation getInstallation() {
183 return new DaemonInstallation(getInstallPath());
184 }
185
186 public String toFileProtocol(String filePath) {
187 return (this.getAccessScheme().equals(ProtocolScheme.SSH)) ?
188 ProtocolScheme.SFTP.toURIStr(host, filePath) : ProtocolScheme.FILE.toURIStr("", filePath);
189 }
190
191 public int getRegistryPort(AccountInfo acct) {
192 try {
193 ThinURI uri = ThinURI.create(toFileProtocol(getInstallation().getPortFilePath()));
194 InputStream fis = new ResourceLink<FileHandle>(uri, acct).createHandle().getInputStream();
195 byte[] portBytes = new byte[8];
196 fis.read(portBytes);
197 return Integer.parseInt(new String(portBytes).trim());
198 } catch (Exception e) {
199 return DaemonApp.DEFAULT_RMI_REGISTRY_PORT;
200 }
201 }
202
203 public void setRegistryPort(int port) {
204 try {
205 FileOutputStream fos = new FileOutputStream(getInstallation().getPortFilePath(), false);
206 byte[] portBytes = Integer.toString(port).getBytes();
207 fos.write(portBytes);
208 } catch (FileNotFoundException e) {
209 } catch (IOException e) {
210 }
211 }
212
213 public String getName() {
214 if ((name == null || "".equals(name)) && location != null)
215 name = location;
216 return name;
217 }
218
219 public void setName(String name) {
220 if (name != null) this.name = name;
221 }
222
223 public String getCompURI() {
224 return getAccessScheme().toURIStr(host);
225 }
226
227 public ProtocolScheme getAccessScheme() {
228 return (!host.equals("localhost")) ? ProtocolScheme.SSH : ProtocolScheme.LOCAL;
229 }
230
231 public String getConnectionURL(KeyStore keys) {
232 Realm realm = resolveRealm(keys);
233 return (getHost() == null || realm == null) ? "" : realm.getAccount().getUser() + "@" + getHost();
234 }
235
236 private Realm resolveRealm(KeyStore keys) {
237 return keys.resolveRealm(getAccessScheme(), getHost());
238 }
239
240 public int getQueueSize() {
241 return queueSize;
242 }
243
244 public void setQueueSize(int queueSize) {
245 this.queueSize = queueSize;
246 }
247
248
249
250
251 public float getMaxHijackMins() {
252 return maxHijackMins;
253 }
254
255 public long getMaxHijackMillis() {
256 return (long)(maxHijackMins * 60000);
257 }
258
259 public void setMaxHijackMins(float value) {
260 this.maxHijackMins = value;
261 }
262
263 public float getMaxIdleMins() {
264 return maxIdleMins;
265 }
266
267 public long getMaxIdleMillis() {
268 return (long)(maxIdleMins * 60000);
269 }
270
271 public void setMaxIdleMins(float value) {
272 this.maxIdleMins = value;
273 }
274
275 public float getMaxWaitMins() {
276 return maxWaitMins;
277 }
278
279 public long getMaxAttachMillis() {
280 return (long)(maxWaitMins * 60000);
281 }
282
283 public void setMaxWaitMins(float maxAttachMins) {
284 this.maxWaitMins = maxAttachMins;
285 }
286
287 public long getHeartBeatPeriodSecs() {
288 return heartBeatPeriodSecs;
289 }
290
291 public void setHeartBeatPeriodSecs(long value) {
292 this.heartBeatPeriodSecs = value;
293 }
294
295
296
297
298 public PlatformType getPlatform() {
299 return PlatformType.getByName(platform);
300 }
301
302 public void setPlatform(PlatformType platform) {
303 this.platform = platform.name();
304 }
305
306 public String getVarValue(String varName) {
307 for (VarInfo var : vars)
308 if (var.getName().equals(varName))
309 return var.getValue();
310
311 return null;
312 }
313
314 public List<VarInfo> getVars() {
315 return vars;
316 }
317
318 public Map<String, Object> getVarsAsMap() {
319 Map<String, Object> result = new HashMap<String, Object>();
320 for (VarInfo var : vars) result.put(var.getName(), var.getValue());
321 return result;
322 }
323
324 public void setVars(List<VarInfo> vars) {
325 this.vars = vars;
326 }
327
328 public void setDebugPort(int debugPort) {
329 this.debugPort = debugPort;
330 }
331
332 public List<OrderInfo> getOrdersList() {
333 if (ordersList == null)
334 ordersList = new ArrayList<OrderInfo>();
335 return ordersList;
336 }
337
338 public void setOrdersList(List<OrderInfo> ordersList) {
339 this.ordersList = ordersList;
340 }
341
342 @Override
343 public String toString() {
344 return name + "->" + getLocation();
345 }
346
347
348
349
350 public String generateRMIBaseURI() {
351 return ProtocolScheme.RMI.toURIStr(getHost(), generateRMIPath());
352 }
353
354 public String generateRMIPath() {
355 return IOUtils.concatenatePaths(getInstallPath(), "DAEMON");
356 }
357
358 public ShellCommand createDaemonLauncherCommand() {
359 ShellCommand cmd = createDaemonScriptCommand(getInstallPath(), DaemonApp.class, debugPort);
360 cmd.setExitToken(DaemonApp.DAEMON_APP_MAIN_COMPLETED_MSG);
361 return cmd;
362 }
363
364 @Override
365 public int hashCode() {
366 return getLocation().hashCode();
367 }
368
369 @Override
370 public boolean equals(Object obj) {
371 if (this == obj) return true;
372 if (obj == null) return false;
373 if (getClass() != obj.getClass()) return false;
374 final HeadResourceInfo other = (HeadResourceInfo) obj;
375 return (getLocation().equals(other.getLocation()));
376 }
377 }
378