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.util.compression;
8   
9   import org.mule.util.ClassUtils;
10  
11  import java.security.AccessController;
12  import java.security.PrivilegedAction;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  /**
18   * <code>CompressionHelper</code> a static class that provides facilities for
19   * compressing and uncompressing byte arrays
20   */
21  
22  public final class CompressionHelper
23  {
24      /**
25       * logger used by this class
26       */
27      private static Log logger = LogFactory.getLog(CompressionHelper.class);
28  
29      private static CompressionStrategy defaultStrategy;
30  
31      /** Do not instanciate. */
32      private CompressionHelper ()
33      {
34          // no-op
35      }
36  
37      public static synchronized CompressionStrategy getDefaultCompressionStrategy()
38      {
39          if (defaultStrategy == null)
40          {
41              defaultStrategy = (CompressionStrategy) AccessController.doPrivileged(new PrivilegedAction()
42              {
43                  public Object run()
44                  {
45                      try
46                      {
47                          Object o = ClassUtils.loadClass(CompressionStrategy.COMPRESSION_DEFAULT,
48                              CompressionHelper.class).newInstance();
49                          if (logger.isDebugEnabled())
50                          {
51                              logger.debug("Found CompressionStrategy: " + o.getClass().getName());
52                          }
53                          return o;
54                      }
55                      catch (Exception e)
56                      {
57                          // TODO MULE-863: What should we really do?  Document this?
58                          logger.warn("Failed to build compression strategy: " + e.getMessage());
59                      }
60                      return null;
61                  }
62              });
63          }
64          return defaultStrategy;
65      }
66  
67  }