View Javadoc

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