1 17 package org.apache.servicemix.jbi.container; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Timer ; 24 import java.util.TimerTask ; 25 26 import javax.jbi.JBIException; 27 import javax.management.JMException ; 28 import javax.management.MBeanAttributeInfo ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; 33 import org.apache.servicemix.jbi.management.AttributeInfoHelper; 34 import org.apache.servicemix.jbi.management.BaseSystemService; 35 import org.apache.servicemix.jbi.util.FileUtil; 36 import org.apache.servicemix.jbi.util.FileVersionUtil; 37 38 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 39 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 40 41 57 public class EnvironmentContext extends BaseSystemService implements EnvironmentContextMBean { 58 private static final Log log = LogFactory.getLog(EnvironmentContext.class); 59 60 private File jbiRootDir; 61 private File componentsDir; 62 private File installationDir; 63 private File deploymentDir; 64 private File sharedLibDir; 65 private File serviceAssembliesDir; 66 private File tmpDir; 67 private int statsInterval = 5; 68 private Map envMap = new ConcurrentHashMap(); 69 private AtomicBoolean started = new AtomicBoolean(false); 70 private boolean dumpStats = false; 71 private Timer statsTimer; 72 private TimerTask timerTask; 73 74 75 78 public static String getVersion() { 79 String answer = null; 80 Package p = Package.getPackage("org.apache.servicemix"); 81 if (p != null) { 82 answer = p.getImplementationVersion(); 83 } 84 return answer; 85 } 86 87 91 public String getDescription(){ 92 return "Manages Environment for the Container"; 93 } 94 97 public File getComponentsDir() { 98 return componentsDir; 99 } 100 101 104 public File getInstallationDir() { 105 return installationDir; 106 } 107 108 113 public void setInstallationDir(File installationDir){ 114 this.installationDir = installationDir; 115 } 116 117 118 121 public File getDeploymentDir() { 122 return deploymentDir; 123 } 124 125 128 public void setDeploymentDir(File deploymentDir) { 129 this.deploymentDir = deploymentDir; 130 } 131 132 136 public File getSharedLibDir(){ 137 return sharedLibDir; 138 } 139 140 143 public File getTmpDir() { 144 if (tmpDir != null) { 145 FileUtil.buildDirectory(tmpDir); 146 } 147 return tmpDir; 148 } 149 150 151 154 public File getServiceAssembliesDir(){ 155 return serviceAssembliesDir; 156 } 157 158 159 166 public void init(JBIContainer container, String rootDirPath) throws JBIException { 167 super.init(container); 168 jbiRootDir = new File (rootDirPath); 169 buildDirectoryStructure(); 170 } 171 172 protected Class getServiceMBean() { 173 return EnvironmentContextMBean.class; 174 } 175 176 181 public void start() throws javax.jbi.JBIException { 182 super.start(); 183 if (started.compareAndSet(false, true)) { 184 scheduleStatsTimer(); 185 } 186 } 187 188 193 public void stop() throws javax.jbi.JBIException { 194 if (started.compareAndSet(true, false)) { 195 super.stop(); 196 if (timerTask != null) { 197 timerTask.cancel(); 198 } 199 } 200 } 201 202 207 public void shutDown() throws javax.jbi.JBIException { 208 super.shutDown(); 209 for (Iterator i = envMap.values().iterator();i.hasNext();) { 210 ComponentEnvironment ce = (ComponentEnvironment) i.next(); 211 ce.close(); 212 } 213 if (timerTask != null) { 214 timerTask.cancel(); 215 } 216 if (statsTimer != null) { 217 statsTimer.cancel(); 218 } 219 envMap.clear(); 220 container.getManagementContext().unregisterMBean(this); 221 } 222 223 226 public int getStatsInterval() { 227 return statsInterval; 228 } 229 230 233 public void setStatsInterval(int statsInterval) { 234 this.statsInterval = statsInterval; 235 scheduleStatsTimer(); 236 } 237 238 241 public boolean isDumpStats() { 242 return dumpStats; 243 } 244 245 248 public void setDumpStats(boolean value) { 249 if (dumpStats && !value) { 250 if (timerTask != null) { 251 timerTask.cancel(); 252 } 253 } 254 else if (!dumpStats && value) { 255 dumpStats = value; scheduleStatsTimer(); 257 } 258 dumpStats = value; 259 } 260 261 protected void doDumpStats() { 262 if (isDumpStats()) { 263 for (Iterator i = envMap.values().iterator();i.hasNext();) { 264 ComponentEnvironment ce = (ComponentEnvironment) i.next(); 265 ce.dumpStats(); 266 } 267 } 268 } 269 270 277 public ComponentEnvironment registerComponent(ComponentMBeanImpl connector) throws JBIException { 278 return registerComponent(null, connector); 279 } 280 281 288 public ComponentEnvironment registerComponent(ComponentEnvironment result, 289 ComponentMBeanImpl connector) throws JBIException { 290 if (result == null) { 291 result = new ComponentEnvironment(); 292 } 293 if (!connector.isPojo()) { 294 if (container.isEmbedded()) { 295 throw new JBIException("JBI component can not be installed in embedded mode"); 296 } 297 try { 299 String name = connector.getComponentNameSpace().getName(); 300 if (result.getComponentRoot() == null) { 301 File componentRoot = getComponentRootDir(name); 302 FileUtil.buildDirectory(componentRoot); 303 result.setComponentRoot(componentRoot); 304 } 305 if (result.getWorkspaceRoot() == null) { 306 File privateWorkspace = createWorkspaceDirectory(name); 307 result.setWorkspaceRoot(privateWorkspace); 308 } 309 if (result.getStateFile() == null) { 310 File stateFile = FileUtil.getDirectoryPath(result.getComponentRoot(), "state.xml"); 311 result.setStateFile(stateFile); 312 } 313 } catch (IOException e) { 314 throw new JBIException(e); 315 } 316 } 317 if (result.getStatsFile() == null) { 318 File statsFile = FileUtil.getDirectoryPath(result.getComponentRoot(), "stats.cvs"); 319 result.setStatsFile(statsFile); 320 } 321 result.setLocalConnector(connector); 322 envMap.put(connector, result); 323 return result; 324 } 325 326 333 public File getComponentRootDir(String componentName) { 334 if (getComponentsDir() == null) { 335 return null; 336 } 337 File result = FileUtil.getDirectoryPath(getComponentsDir(), componentName); 338 return result; 339 } 340 341 348 public File createComponentRootDir(String componentName) throws IOException { 349 if (getComponentsDir() == null) { 350 return null; 351 } 352 File result = FileUtil.getDirectoryPath(getComponentsDir(), componentName); 353 return result; 354 } 355 356 363 public File getNewComponentInstallationDir(String componentName) throws IOException { 364 File result = getComponentRootDir(componentName); 365 result = FileVersionUtil.getNewVersionDirectory(result); 367 return result; 368 } 369 370 377 public File getComponentInstallationDir(String componentName) throws IOException { 378 File result = getComponentRootDir(componentName); 379 result = FileVersionUtil.getLatestVersionDirectory(result); 381 return result; 382 } 383 384 public ComponentEnvironment getNewComponentEnvironment(String compName) throws IOException { 385 File rootDir = FileUtil.getDirectoryPath(getComponentsDir(), compName); 386 File instDir = FileVersionUtil.getNewVersionDirectory(rootDir); 387 File workDir = FileUtil.getDirectoryPath(rootDir, "workspace"); 388 File stateFile = FileUtil.getDirectoryPath(rootDir, "state.xml"); 389 File statsFile = FileUtil.getDirectoryPath(rootDir, "stats.cvs"); 390 ComponentEnvironment env = new ComponentEnvironment(); 391 env.setComponentRoot(rootDir); 392 env.setInstallRoot(instDir); 393 env.setWorkspaceRoot(workDir); 394 env.setStateFile(stateFile); 395 env.setStatsFile(statsFile); 396 return env; 397 } 398 399 public ComponentEnvironment getComponentEnvironment(String compName) throws IOException { 400 File rootDir = FileUtil.getDirectoryPath(getComponentsDir(), compName); 401 File instDir = FileVersionUtil.getLatestVersionDirectory(rootDir); 402 File workDir = FileUtil.getDirectoryPath(rootDir, "workspace"); 403 File stateFile = FileUtil.getDirectoryPath(rootDir, "state.xml"); 404 File statsFile = FileUtil.getDirectoryPath(rootDir, "stats.cvs"); 405 ComponentEnvironment env = new ComponentEnvironment(); 406 env.setComponentRoot(rootDir); 407 env.setInstallRoot(instDir); 408 env.setWorkspaceRoot(workDir); 409 env.setStateFile(stateFile); 410 env.setStatsFile(statsFile); 411 return env; 412 } 413 414 public ServiceAssemblyEnvironment getNewServiceAssemblyEnvironment(String saName) throws IOException { 415 File rootDir = FileUtil.getDirectoryPath(getServiceAssembliesDir(), saName); 416 File versDir = FileVersionUtil.getNewVersionDirectory(rootDir); 417 File instDir = FileUtil.getDirectoryPath(versDir, "install"); 418 File susDir = FileUtil.getDirectoryPath(versDir, "sus"); 419 File stateFile = FileUtil.getDirectoryPath(rootDir, "state.xml"); 420 ServiceAssemblyEnvironment env = new ServiceAssemblyEnvironment(); 421 env.setRootDir(rootDir); 422 env.setInstallDir(instDir); 423 env.setSusDir(susDir); 424 env.setStateFile(stateFile); 425 return env; 426 } 427 428 public ServiceAssemblyEnvironment getServiceAssemblyEnvironment(String saName) { 429 File rootDir = FileUtil.getDirectoryPath(getServiceAssembliesDir(), saName); 430 File versDir = FileVersionUtil.getLatestVersionDirectory(rootDir); 431 File instDir = FileUtil.getDirectoryPath(versDir, "install"); 432 File susDir = FileUtil.getDirectoryPath(versDir, "sus"); 433 File stateFile = FileUtil.getDirectoryPath(rootDir, "state.xml"); 434 ServiceAssemblyEnvironment env = new ServiceAssemblyEnvironment(); 435 env.setRootDir(rootDir); 436 env.setInstallDir(instDir); 437 env.setSusDir(susDir); 438 env.setStateFile(stateFile); 439 return env; 440 } 441 442 449 public File createWorkspaceDirectory(String componentName) throws IOException { 450 File result = FileUtil.getDirectoryPath(getComponentsDir(), componentName); 451 result = FileUtil.getDirectoryPath(result, "workspace"); 452 FileUtil.buildDirectory(result); 453 return result; 454 } 455 456 462 public void unreregister(ComponentMBeanImpl connector) { 463 ComponentEnvironment ce = (ComponentEnvironment) envMap.remove(connector); 464 if (ce != null) { 465 ce.close(); 466 } 467 } 468 469 474 public void removeComponentRootDirectory(String componentName) { 475 File file = getComponentRootDir(componentName); 476 if (file != null) { 477 if (!FileUtil.deleteFile(file)) { 478 log.warn("Failed to remove directory structure for component [version]: " + componentName + " [" + file.getName() + ']'); 479 } 480 else { 481 log.info("Removed directory structure for component [version]: " + componentName + " [" + file.getName() + ']'); 482 } 483 } 484 } 485 486 493 public File createSharedLibraryDirectory(String name) { 494 File result = FileUtil.getDirectoryPath(getSharedLibDir(), name); 495 FileUtil.buildDirectory(result); 496 return result; 497 } 498 499 504 public void removeSharedLibraryDirectory(String name) { 505 File result = FileUtil.getDirectoryPath(getSharedLibDir(), name); 506 FileUtil.deleteFile(result); 507 } 508 509 510 private void buildDirectoryStructure() throws JBIException { 511 if (container.isEmbedded()) { 514 return; 515 } 516 try { 517 jbiRootDir = jbiRootDir.getCanonicalFile(); 518 if (!jbiRootDir.exists()) { 519 if (!jbiRootDir.mkdirs()) { 520 throw new JBIException("Directory could not be created: "+jbiRootDir.getCanonicalFile()); 521 } 522 } else if (!jbiRootDir.isDirectory()) { 523 throw new JBIException("Not a directory: " + jbiRootDir.getCanonicalFile()); 524 } 525 if (installationDir == null){ 526 installationDir = FileUtil.getDirectoryPath(jbiRootDir, "install"); 527 } 528 installationDir = installationDir.getCanonicalFile(); 529 if (deploymentDir == null){ 530 deploymentDir = FileUtil.getDirectoryPath(jbiRootDir, "deploy"); 531 } 532 deploymentDir = deploymentDir.getCanonicalFile(); 533 componentsDir = FileUtil.getDirectoryPath(jbiRootDir, "components").getCanonicalFile(); 534 tmpDir = FileUtil.getDirectoryPath(jbiRootDir, "tmp").getCanonicalFile(); 535 sharedLibDir = FileUtil.getDirectoryPath(jbiRootDir, "sharedlibs").getCanonicalFile(); 536 serviceAssembliesDir = FileUtil.getDirectoryPath(jbiRootDir,"service-assemblies").getCanonicalFile(); 537 FileUtil.buildDirectory(installationDir); 539 FileUtil.buildDirectory(deploymentDir); 540 FileUtil.buildDirectory(componentsDir); 541 FileUtil.buildDirectory(tmpDir); 542 FileUtil.buildDirectory(sharedLibDir); 543 FileUtil.buildDirectory(serviceAssembliesDir); 544 } catch (IOException e) { 545 throw new JBIException(e); 546 } 547 } 548 549 private void scheduleStatsTimer() { 550 if (isDumpStats()) { 551 if (statsTimer == null) { 552 statsTimer = new Timer (true); 553 } 554 if (timerTask != null) { 555 timerTask.cancel(); 556 } 557 timerTask = new TimerTask () { 558 public void run() { 559 doDumpStats(); 560 } 561 }; 562 long interval = statsInterval * 1000; 563 statsTimer.scheduleAtFixedRate(timerTask, interval, interval); 564 } 565 } 566 567 568 569 570 571 577 public MBeanAttributeInfo [] getAttributeInfos() throws JMException { 578 AttributeInfoHelper helper = new AttributeInfoHelper(); 579 helper.addAttribute(getObjectToManage(), "dumpStats", "Periodically dump Component statistics"); 580 helper.addAttribute(getObjectToManage(), "statsInterval", "Interval (secs) before dumping statistics"); 581 return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos()); 582 } 583 584 public File getJbiRootDir() { 585 return jbiRootDir; 586 } 587 588 589 } 590 | Popular Tags |