1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule; |
12 | |
|
13 | |
import org.mule.util.FileUtils; |
14 | |
|
15 | |
import java.io.File; |
16 | |
import java.io.IOException; |
17 | |
|
18 | |
import javax.management.MBeanServer; |
19 | |
import javax.management.MalformedObjectNameException; |
20 | |
import javax.management.ObjectName; |
21 | |
import javax.naming.InitialContext; |
22 | |
import javax.transaction.TransactionManager; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
|
27 | 0 | public class ManagementContext |
28 | |
{ |
29 | |
|
30 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
31 | |
|
32 | |
protected String jmxDomainName; |
33 | |
protected File workingDir; |
34 | |
protected TransactionManager transactionManager; |
35 | |
protected MBeanServer mBeanServer; |
36 | |
protected InitialContext namingContext; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public String getJmxDomainName() |
49 | |
{ |
50 | 0 | return jmxDomainName; |
51 | |
} |
52 | |
|
53 | |
public void setJmxDomainName(String jmxDomainName) |
54 | |
{ |
55 | 0 | this.jmxDomainName = jmxDomainName; |
56 | 0 | } |
57 | |
|
58 | |
public File getWorkingDir() |
59 | |
{ |
60 | 0 | return workingDir; |
61 | |
} |
62 | |
|
63 | |
public void setWorkingDir(File workingDir) |
64 | |
{ |
65 | 0 | this.workingDir = workingDir; |
66 | 0 | } |
67 | |
|
68 | |
public TransactionManager getTransactionManager() |
69 | |
{ |
70 | 0 | return transactionManager; |
71 | |
} |
72 | |
|
73 | |
public void setTransactionManager(TransactionManager transactionManager) |
74 | |
{ |
75 | 0 | this.transactionManager = transactionManager; |
76 | 0 | } |
77 | |
|
78 | |
public MBeanServer getMBeanServer() |
79 | |
{ |
80 | 0 | return mBeanServer; |
81 | |
} |
82 | |
|
83 | |
public void setMBeanServer(MBeanServer mBeanServer) |
84 | |
{ |
85 | 0 | this.mBeanServer = mBeanServer; |
86 | 0 | } |
87 | |
|
88 | |
public InitialContext getNamingContext() |
89 | |
{ |
90 | 0 | return namingContext; |
91 | |
} |
92 | |
|
93 | |
public void setNamingContext(InitialContext namingContext) |
94 | |
{ |
95 | 0 | this.namingContext = namingContext; |
96 | 0 | } |
97 | |
|
98 | |
public ObjectName createMBeanName(String componentName, String type, String name) |
99 | |
{ |
100 | |
try |
101 | |
{ |
102 | 0 | StringBuffer sb = new StringBuffer(); |
103 | 0 | sb.append(getJmxDomainName()).append(':'); |
104 | 0 | if (componentName != null) |
105 | |
{ |
106 | 0 | sb.append("component=").append(validateString(componentName)); |
107 | 0 | sb.append(','); |
108 | |
} |
109 | 0 | sb.append("type=").append(validateString(type)); |
110 | 0 | if (name != null) |
111 | |
{ |
112 | 0 | sb.append(','); |
113 | 0 | sb.append("name=").append(validateString(name)); |
114 | |
} |
115 | 0 | return new ObjectName(sb.toString()); |
116 | |
} |
117 | 0 | catch (MalformedObjectNameException e) |
118 | |
{ |
119 | 0 | logger.error("Could not create component mbean name", e); |
120 | 0 | return null; |
121 | |
} |
122 | |
} |
123 | |
|
124 | |
private String validateString(String str) |
125 | |
{ |
126 | 0 | str = str.replace(':', '_'); |
127 | 0 | str = str.replace('/', '_'); |
128 | 0 | str = str.replace('\\', '_'); |
129 | 0 | return str; |
130 | |
} |
131 | |
|
132 | |
public static final String TEMP_DIR = "temp"; |
133 | |
public static final String COMPONENTS_DIR = "components"; |
134 | |
public static final String LIBRARIES_DIR = "libraries"; |
135 | |
public static final String ASSEMBLIES_DIR = "assemblies"; |
136 | |
public static final String INSTALL_DIR = "install"; |
137 | |
public static final String DEPLOY_DIR = "deploy"; |
138 | |
public static final String PROCESSED_DIR = "processed"; |
139 | |
public static final String WORKSPACE_DIR = "workspace"; |
140 | |
|
141 | |
private int counter; |
142 | |
|
143 | |
public synchronized File getNewTempDir(File rootDir) |
144 | |
{ |
145 | |
while (true) |
146 | |
{ |
147 | 0 | String s = Integer.toHexString(++counter); |
148 | 0 | while (s.length() < 8) |
149 | |
{ |
150 | 0 | s = "0" + s; |
151 | |
} |
152 | 0 | File f = FileUtils.newFile(rootDir, File.separator + TEMP_DIR + File.separator + s); |
153 | 0 | if (!f.exists()) |
154 | |
{ |
155 | 0 | return f; |
156 | |
} |
157 | |
} |
158 | |
} |
159 | |
|
160 | |
public File getComponentInstallDir(File rootDir, String name) |
161 | |
{ |
162 | 0 | return FileUtils.newFile(rootDir, COMPONENTS_DIR + File.separator + validateString(name)); |
163 | |
} |
164 | |
|
165 | |
public File getComponentWorkspaceDir(File rootDir, String name) |
166 | |
{ |
167 | 0 | return FileUtils.newFile(rootDir, WORKSPACE_DIR + File.separator + validateString(name)); |
168 | |
} |
169 | |
|
170 | |
public File getLibraryInstallDir(File rootDir, String name) |
171 | |
{ |
172 | 0 | return FileUtils.newFile(rootDir, LIBRARIES_DIR + File.separator + validateString(name)); |
173 | |
} |
174 | |
|
175 | |
public File getAssemblyInstallDir(File rootDir, String name) |
176 | |
{ |
177 | 0 | return FileUtils.newFile(rootDir, ASSEMBLIES_DIR + File.separator + validateString(name)); |
178 | |
} |
179 | |
|
180 | |
public static File getAutoInstallDir(File rootDir) |
181 | |
{ |
182 | 0 | return FileUtils.newFile(rootDir, INSTALL_DIR); |
183 | |
} |
184 | |
|
185 | |
public File getAutoInstallProcessedDir(File rootDir) |
186 | |
{ |
187 | 0 | return FileUtils.newFile(rootDir, INSTALL_DIR + File.separator + PROCESSED_DIR); |
188 | |
} |
189 | |
|
190 | |
public File getAutoDeployDir(File rootDir) |
191 | |
{ |
192 | 0 | return FileUtils.newFile(rootDir, DEPLOY_DIR); |
193 | |
} |
194 | |
|
195 | |
public File getAutoDeployProcessedDir(File rootDir) |
196 | |
{ |
197 | 0 | return FileUtils.newFile(rootDir, DEPLOY_DIR + File.separator + PROCESSED_DIR); |
198 | |
} |
199 | |
|
200 | |
public void deleteMarkedDirectories(File dir) |
201 | |
{ |
202 | 0 | if (dir != null && dir.isDirectory()) |
203 | |
{ |
204 | 0 | if (FileUtils.newFile(dir, ".delete").isFile()) |
205 | |
{ |
206 | 0 | deleteDir(dir); |
207 | |
} |
208 | |
else |
209 | |
{ |
210 | 0 | File[] children = dir.listFiles(); |
211 | 0 | for (int i = 0; i < children.length; i++) |
212 | |
{ |
213 | 0 | if (children[i].isDirectory()) |
214 | |
{ |
215 | 0 | deleteMarkedDirectories(children[i]); |
216 | |
} |
217 | |
} |
218 | |
} |
219 | |
} |
220 | 0 | } |
221 | |
|
222 | |
public void deleteDir(String dir) |
223 | |
{ |
224 | 0 | deleteDir(FileUtils.newFile(dir)); |
225 | 0 | } |
226 | |
|
227 | |
public void deleteDir(File dir) |
228 | |
{ |
229 | 0 | FileUtils.deleteTree(dir); |
230 | 0 | if (dir.isDirectory()) |
231 | |
{ |
232 | |
try |
233 | |
{ |
234 | 0 | FileUtils.newFile(dir, ".delete").createNewFile(); |
235 | |
} |
236 | 0 | catch (IOException e) |
237 | |
{ |
238 | 0 | logger.warn("Could not mark directory to be deleted", e); |
239 | 0 | } |
240 | |
} |
241 | 0 | } |
242 | |
|
243 | |
public void createDirectories(File rootDir) throws IOException |
244 | |
{ |
245 | 0 | FileUtils.createFile(rootDir.getAbsolutePath()); |
246 | 0 | FileUtils.createFile(new File(rootDir, COMPONENTS_DIR).getAbsolutePath()); |
247 | 0 | FileUtils.createFile(new File(rootDir, WORKSPACE_DIR).getAbsolutePath()); |
248 | 0 | FileUtils.createFile(new File(rootDir, LIBRARIES_DIR).getAbsolutePath()); |
249 | 0 | FileUtils.createFile(new File(rootDir, ASSEMBLIES_DIR).getAbsolutePath()); |
250 | 0 | FileUtils.createFile(getAutoInstallDir(rootDir).getAbsolutePath()); |
251 | 0 | FileUtils.createFile(getAutoDeployDir(rootDir).getAbsolutePath()); |
252 | 0 | FileUtils.createFile(getAutoDeployProcessedDir(rootDir).getAbsolutePath()); |
253 | 0 | } |
254 | |
} |