1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.log;
16  
17  import java.util.Map;
18  import java.util.concurrent.ConcurrentHashMap;
19  import java.util.concurrent.ConcurrentMap;
20  
21  /**
22   * <a href="LogFactoryUtil.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   * @author Shuyang Zhou
26   */
27  public class LogFactoryUtil {
28  
29      public static Log getLog(Class<?> c) {
30          return getLog(c.getName());
31      }
32  
33      public static Log getLog(String name) {
34  
35          // The following concurrent collection retrieve has the side effect of a
36          // memory fence read. This will invalidate all dirty cache data if there
37          // are any. If the LogWrapper swap happens before this, the new Log will
38          // be visible to the current Thread.
39  
40          LogWrapper logWrapper = _logWrappers.get(name);
41  
42          if (logWrapper == null) {
43              logWrapper = new LogWrapper(_logFactory.getLog(name));
44  
45              LogWrapper previousLogWrapper = _logWrappers.putIfAbsent(
46                  name, logWrapper);
47  
48              if (previousLogWrapper != null) {
49                  logWrapper = previousLogWrapper;
50              }
51          }
52  
53          return logWrapper;
54      }
55  
56      public static void setLogFactory(LogFactory logFactory) {
57          for (Map.Entry<String, LogWrapper> entry : _logWrappers.entrySet()) {
58              String name = entry.getKey();
59  
60              LogWrapper logWrapper = entry.getValue();
61  
62              logWrapper.setLog(logFactory.getLog(name));
63          }
64  
65          // The following volatile write will flush out all cache data. All
66          // previously swapped LogWrappers will be visible for any reads after a
67          // memory fence read according to the happens-before rules.
68  
69          _logFactory = logFactory;
70      }
71  
72      private static volatile LogFactory _logFactory = new Jdk14LogFactoryImpl();
73      private static final ConcurrentMap<String, LogWrapper> _logWrappers =
74          new ConcurrentHashMap<String, LogWrapper>();
75  
76  }