View Javadoc

1   /*
2   * $Id: MuleDeploy.java 21720 2011-04-22 19:50:04Z mike.schilling $
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  package org.mule.tools.anttasks;
11  
12  import org.apache.tools.ant.BuildException;
13  import org.apache.tools.ant.Task;
14  import org.apache.tools.ant.util.FileUtils;
15  
16  import java.io.File;
17  import java.io.IOException;
18  import java.text.MessageFormat;
19  
20  /**
21  * Ant task to deploy a Mule application to the MULE_HOME/apps directory.
22  *
23  * XML format
24  *
25  * <taskdef name="muleDeploy" classname="org.mule.tools.anttasks.MuleDeploy"/>
26  *
27  * <muleDeploy applicationFile="file" muleHome="dir"/>
28  *
29  * If muleHome isn't specified, the value of the Ant property dir.mule.home is used.
30  */
31  public class MuleDeploy extends Task
32  {
33      /** The Ant property containing the default Mule home directory */
34      private static final String MULE_HOME_PROPERTY = "dir.mule.home";
35  
36      /** The application file to install */
37      private File applicationFile;
38  
39      /** The home directory of the mule installation to install the application into */
40      private File muleHome;
41  
42      /**
43  * Specify the Mule application file to install
44  */
45      public void setApplicationFile(File applicationFile)
46      {
47          this.applicationFile = applicationFile;
48      }
49  
50      /**
51  * install the application file
52  */
53      @Override
54      public void execute() throws BuildException
55      {
56          FileChecker checker = new FileChecker(getLocation());
57          checker.checkFile(applicationFile, "application file", false, false);
58          File home = getMuleHome();
59          checker.checkFile(home, "mule home directory", true, false);
60          File appsDir = new File(home, "apps");
61          checker.checkFile(appsDir, "mule apps directory", true, true);
62          try
63          {
64              File destFile = new File(appsDir, applicationFile.getName());
65              FileUtils.getFileUtils().copyFile(applicationFile, destFile, null, true);
66          }
67          catch (IOException ex)
68          {
69              throw new BuildException(
70                  MessageFormat.format("Problem copying Mule application file {0} to {1}", applicationFile, appsDir),
71                  ex, getLocation());
72          }
73      }
74  
75      /**
76  * @return the Mule home directory
77  */
78      public File getMuleHome()
79      {
80          if (muleHome != null)
81          {
82              return muleHome;
83          }
84          String home = getProject().getProperty(MULE_HOME_PROPERTY);
85          return home == null ? null : new File(home);
86      }
87  
88      /**
89  * Specify the Mule home directory
90  */
91      public void setMuleHome(File muleHome)
92      {
93          this.muleHome = muleHome;
94      }
95  }
96