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.File;
20 import java.io.Serializable;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.gwe.drivers.netAccess.ShellCommand;
25 import org.gwe.utils.IOUtils;
26
27
28
29
30
31 public class DaemonInstallation implements Serializable {
32
33 private static Log log = LogFactory.getLog(DaemonInstallation.class);
34
35 public static void createIfNonExistent(String dirPathStr) {
36 File dirPath = new File(dirPathStr);
37 if (!dirPath.exists()) dirPath.mkdirs();
38 }
39
40 public static final String CONFIGURATION_FILE = "config.ser";
41 public static final String PORT_FILE = "port.txt";
42
43 private String installPath;
44 private String workspacePath = null;
45 private String allocsWorkspacePath = null;
46 private String ordersWorkspacePath = null;
47 private String installWorkspacePath = null;
48 private String allocsDisposedWorkspacePath = null;
49
50 public DaemonInstallation() {}
51
52 public DaemonInstallation(String installPath) {
53 setInstallPath(installPath);
54 }
55
56 public String getInstallPath() {
57 return installPath;
58 }
59
60 public void setInstallPath(String installPath) {
61 this.installPath = installPath;
62 initialize();
63 }
64
65 private void initialize() {
66 if (workspacePath != null) return;
67 this.workspacePath = OSAppFolder.WORKSPACE.getRelativeTo(installPath);
68 this.allocsWorkspacePath = IOUtils.concatenatePaths(workspacePath, "allocs");
69 this.allocsDisposedWorkspacePath = IOUtils.concatenatePaths(workspacePath, "allocs-disposed");
70 this.ordersWorkspacePath = IOUtils.concatenatePaths(workspacePath, "orders");
71 this.installWorkspacePath = IOUtils.concatenatePaths(workspacePath, "install");
72 }
73
74 public String getWorkspacePath() {
75 initialize();
76 return workspacePath;
77 }
78
79 public String getAllocsWorkspacePath() {
80 initialize();
81 return allocsWorkspacePath;
82 }
83
84 public String getAllocsDisposedWorkspacePath() {
85 return allocsDisposedWorkspacePath;
86 }
87
88 public String getOrdersWorkspacePath() {
89 initialize();
90 return ordersWorkspacePath;
91 }
92
93 public String getInstallationWorkspacePath() {
94 initialize();
95 return installWorkspacePath;
96 }
97
98 public String getAllocsWorkspacePath(int id) {
99 return IOUtils.concatenatePaths(getAllocsWorkspacePath(), id);
100 }
101
102 public String getOrdersWorkspacePath(int id) {
103 return IOUtils.concatenatePaths(getOrdersWorkspacePath(), id);
104 }
105
106 public void disposeAllocFolder(int id) {
107 try {
108 String allocDispWSP = getAllocsDisposedWorkspacePath();
109 String allocWS = getAllocsWorkspacePath(id);
110 new File(allocDispWSP).mkdirs();
111 ShellCommand.runLocally(ShellCommand.moveFile(allocWS, allocDispWSP));
112 } catch(Exception e) {}
113 }
114
115 public void cleanupDisposedAllocFolder() {
116 try {
117 String cmd = ShellCommand.delete(getAllocsDisposedWorkspacePath());
118 ShellCommand.runLocally(cmd);
119 log.info("Disposed allocations folder deleted with command '" + cmd + "'");
120 } catch(Exception e) {
121 log.warn("Couldn't clean up disposed allocations folder");
122 }
123 }
124
125 public String getConfigurationFilePath() {
126 return getDBFilePath(CONFIGURATION_FILE);
127 }
128
129 public String getPortFilePath() {
130 return getDBFilePath(PORT_FILE);
131 }
132
133 public String getDBFilePath(String fileName) {
134 return IOUtils.concatenatePaths(OSAppFolder.DATABASE.getRelativeTo(installPath), fileName);
135 }
136
137 public void createDirectories() {
138 createIfNonExistent(getWorkspacePath());
139 createIfNonExistent(getAllocsWorkspacePath());
140 createIfNonExistent(getOrdersWorkspacePath());
141 createIfNonExistent(getInstallationWorkspacePath());
142 }
143 }
144