KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > ManagementContext


1 /*
2  * $Id: ManagementContext.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MalformedObjectNameException JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.naming.InitialContext JavaDoc;
20 import javax.transaction.TransactionManager JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.mule.util.FileUtils;
25
26 public class ManagementContext
27 {
28
29     protected transient Log logger = LogFactory.getLog(getClass());
30
31     protected String JavaDoc jmxDomainName;
32     protected File JavaDoc workingDir;
33     protected TransactionManager JavaDoc transactionManager;
34     protected MBeanServer JavaDoc mBeanServer;
35     protected InitialContext JavaDoc namingContext;
36
37     // protected Registry registry;
38

39     // public Registry getRegistry() {
40
// return registry;
41
// }
42

43     // public void setRegistry(Registry registry) {
44
// this.registry = registry;
45
// }
46

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