View Javadoc

1   /*
2    * $Id: DefaultMuleDeployer.java 19200 2010-08-25 22:27:20Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.launcher;
12  
13  import org.mule.module.reboot.MuleContainerBootstrapUtils;
14  import org.mule.util.FileUtils;
15  import org.mule.util.FilenameUtils;
16  
17  import java.beans.Introspector;
18  import java.io.File;
19  import java.io.IOException;
20  import java.net.URISyntaxException;
21  import java.net.URL;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  public class DefaultMuleDeployer implements MuleDeployer
27  {
28  
29      protected transient final Log logger = LogFactory.getLog(getClass());
30  
31      public void deploy(Application app)
32      {
33          try
34          {
35              app.install();
36              app.init();
37              app.start();
38          }
39          catch (Throwable t)
40          {
41              // TODO logging
42              t.printStackTrace();
43          }
44      }
45  
46      public void undeploy(Application app)
47      {
48          try
49          {
50              app.stop();
51              app.dispose();
52              final File appDir = new File(MuleContainerBootstrapUtils.getMuleAppsDir(), app.getAppName());
53              FileUtils.deleteDirectory(appDir);
54              // remove a marker, harmless, but a tidy app dir is always better :)
55              File marker = new File(MuleContainerBootstrapUtils.getMuleAppsDir(), String.format("%s-anchor.txt", app.getAppName()));
56              marker.delete();
57              Introspector.flushCaches();
58          }
59          catch (Throwable t)
60          {
61              // TODO logging
62              t.printStackTrace();
63          }
64  
65      }
66  
67      public Application installFromAppDir(String packedMuleAppFileName) throws IOException
68      {
69          final File appsDir = MuleContainerBootstrapUtils.getMuleAppsDir();
70          File appFile = new File(appsDir, packedMuleAppFileName);
71          // basic security measure: outside apps dir use installFrom(url) and go through any
72          // restrictions applied to it
73          if (!appFile.getParentFile().equals(appsDir))
74          {
75              throw new SecurityException("installFromAppDir() can only deploy from $MULE_HOME/apps. Use installFrom(url) instead.");
76          }
77          return installFrom(appFile.toURL());
78      }
79  
80      public Application installFrom(URL url) throws IOException
81      {
82          // TODO plug in app-bloodhound/validator here?
83          if (!url.toString().endsWith(".zip"))
84          {
85              throw new IllegalArgumentException("Only Mule application zips are supported: " + url);
86          }
87  
88          final File appsDir = MuleContainerBootstrapUtils.getMuleAppsDir();
89          String appName;
90          try
91          {
92              final String fullPath = url.toURI().toString();
93  
94              if (logger.isInfoEnabled())
95              {
96                  logger.info("Exploding a Mule application archive: " + fullPath);
97              }
98  
99              appName = FilenameUtils.getBaseName(fullPath);
100             File appDir = new File(appsDir, appName);
101             // normalize the full path + protocol to make unzip happy
102             final File source = new File(url.toURI());
103             
104             FileUtils.unzip(source, appDir);
105             if ("file".equals(url.getProtocol()))
106             {
107                 FileUtils.deleteQuietly(source);
108             }
109         }
110         catch (URISyntaxException e)
111         {
112             final IOException ex = new IOException(e.getMessage());
113             ex.fillInStackTrace();
114             throw ex;
115         }
116 
117         // appname is never null by now
118         return new ApplicationWrapper(new DefaultMuleApplication(appName));
119     }
120 }