View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.reboot;
8   
9   import java.io.File;
10  import java.io.FileInputStream;
11  import java.io.FileOutputStream;
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.OutputStream;
15  import java.net.URL;
16  import java.security.AccessController;
17  import java.security.PrivilegedAction;
18  
19  public final class MuleContainerBootstrapUtils
20  {
21      private static final String MULE_APPS_FILENAME = "apps";
22      private static final String MULE_LIB_FILENAME = "lib" + File.separator + "mule";
23  
24      public static final String MULE_LOCAL_JAR_FILENAME = "mule-local-install.jar";
25  
26      private MuleContainerBootstrapUtils()
27      {
28          // utility class only
29      }
30  
31      /**
32       * Whether Mule is running embedded or standalone.
33       * @return true if running standalone
34       */
35      public static boolean isStandalone()
36      {
37          // when embedded, mule.home var is not set
38          return getMuleHome() != null;
39      }
40  
41      /**
42       * @return null if running embedded
43       */
44      public static File getMuleHome()
45      {
46          final String muleHome = System.getProperty("mule.home");
47          return muleHome != null ? new File(muleHome) : null;
48      }
49  
50      /**
51       * @return null if running embedded, otherwise the apps dir as a File ref
52       */
53      public static File getMuleAppsDir()
54      {
55          return isStandalone() ? new File(getMuleHome(), MULE_APPS_FILENAME) : null;
56      }
57  
58      /**
59       * @return null if running embedded
60       */
61      public static File getMuleLibDir()
62      {
63          return isStandalone() ? new File(getMuleHome(), MULE_LIB_FILENAME) : null;
64      }
65  
66      public static File getMuleLocalJarFile()
67      {
68          return isStandalone() ? new File(getMuleLibDir(), MULE_LOCAL_JAR_FILENAME) : null;
69      }
70  
71      public static class ProxyInfo
72      {
73          String host;
74          String port;
75          String username;
76          String password;
77  
78          public ProxyInfo(String host, String port)
79          {
80              this(host, port, null, null);
81          }
82  
83          public ProxyInfo(String host, String port, String username, String password)
84          {
85              this.host = host;
86              this.port = port;
87              this.username = username;
88              this.password = password;
89          }
90      }
91  
92      //////////////////////////////////////////////////////////////////////////////////////////
93      // The following methods are intentionally duplicated from org.mule.util so that
94      // mule-module-boot has no external dependencies at system startup.
95      //////////////////////////////////////////////////////////////////////////////////////////
96  
97      /**
98       * @see org.mule.util.ClassUtils#getResource
99       */
100     public static URL getResource(final String resourceName, final Class<?> callingClass)
101     {
102         URL url = AccessController.doPrivileged(new PrivilegedAction<URL>()
103         {
104             public URL run()
105             {
106                 final ClassLoader cl = Thread.currentThread().getContextClassLoader();
107                 return cl != null ? cl.getResource(resourceName) : null;
108             }
109         });
110 
111         if (url == null)
112         {
113             url = AccessController.doPrivileged(new PrivilegedAction<URL>()
114             {
115                 public URL run()
116                 {
117                     return MuleContainerBootstrap.class.getClassLoader().getResource(resourceName);
118                 }
119             });
120         }
121 
122         if (url == null)
123         {
124             url = AccessController.doPrivileged(new PrivilegedAction<URL>()
125             {
126                 public URL run()
127                 {
128                     return callingClass.getClassLoader().getResource(resourceName);
129                 }
130             });
131         }
132 
133         return url;
134     }
135 
136     /**
137      * @see org.mule.util.FileUtils#renameFile
138      */
139     public static boolean renameFile(File srcFile, File destFile) throws IOException
140     {
141         boolean isRenamed = false;
142         if (srcFile != null && destFile != null)
143         {
144             if (!destFile.exists())
145             {
146                 if (srcFile.isFile())
147                 {
148                     isRenamed = srcFile.renameTo(destFile);
149                     if (!isRenamed && srcFile.exists())
150                     {
151                         isRenamed = renameFileHard(srcFile, destFile);
152                     }
153                 }
154             }
155         }
156         return isRenamed;
157     }
158 
159     /**
160      * @see org.mule.util.FileUtils#renameFileHard
161      */
162     public static boolean renameFileHard(File srcFile, File destFile) throws IOException
163     {
164         boolean isRenamed = false;
165         if (srcFile != null && destFile != null)
166         {
167             if (!destFile.exists())
168             {
169                 if (srcFile.isFile())
170                 {
171                     FileInputStream in = null;
172                     FileOutputStream out = null;
173                     try
174                     {
175                         in = new FileInputStream(srcFile);
176                         out = new FileOutputStream(destFile);
177                         out.getChannel().transferFrom(in.getChannel(), 0, srcFile.length());
178                         isRenamed = true;
179                     }
180                     finally
181                     {
182                         if (in != null)
183                         {
184                             in.close();
185                         }
186                         if (out != null)
187                         {
188                             out.close();
189                         }
190                     }
191                     if (isRenamed)
192                     {
193                         srcFile.delete();
194                     }
195                     else
196                     {
197                         destFile.delete();
198                     }
199                 }
200             }
201         }
202         return isRenamed;
203     }
204 
205     /**
206      * @see org.mule.util.IOUtils#copy
207      */
208     public static int copy(InputStream input, OutputStream output) throws IOException
209     {
210         long count = copyLarge(input, output);
211         if (count > Integer.MAX_VALUE)
212         {
213             return -1;
214         }
215         return (int) count;
216     }
217 
218     /**
219      * @see org.mule.util.IOUtils#copyLarge
220      */
221     public static long copyLarge(InputStream input, OutputStream output) throws IOException
222     {
223         byte[] buffer = new byte[1024 * 4];
224         long count = 0;
225         int n = 0;
226         while (-1 != (n = input.read(buffer)))
227         {
228             output.write(buffer, 0, n);
229             count += n;
230         }
231         return count;
232     }
233 }