View Javadoc

1   /*
2    * $Id: CompressionHelper.java 8077 2007-08-27 20:15:25Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 = (CompressionStrategy) AccessController.doPrivileged(new PrivilegedAction()
46              {
47                  public Object run()
48                  {
49                      try
50                      {
51                          Object o = ClassUtils.loadClass(CompressionStrategy.COMPRESSION_DEFAULT,
52                              CompressionHelper.class).newInstance();
53                          if (logger.isDebugEnabled())
54                          {
55                              logger.debug("Found CompressionStrategy: " + o.getClass().getName());
56                          }
57                          return o;
58                      }
59                      catch (Exception e)
60                      {
61                          // TODO MULE-863: What should we really do?  Document this?
62                          logger.warn("Failed to build compression strategy: " + e.getMessage());
63                      }
64                      return null;
65                  }
66              });
67          }
68          return defaultStrategy;
69      }
70  
71  }