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