View Javadoc

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