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.util;
16  
17  import com.liferay.portal.kernel.concurrent.ConcurrentHashSet;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  
21  import java.lang.reflect.Field;
22  
23  import java.util.Set;
24  
25  /**
26   * <a href="ReferenceRegistry.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Shuyang Zhou
29   */
30  public class ReferenceRegistry {
31  
32      public static void registerReference(
33          Class<?> clazz, Object object, String fieldName) {
34  
35          try {
36              Field field = clazz.getDeclaredField(fieldName);
37  
38              _referenceEntries.add(new ReferenceEntry(object, field));
39          }
40          catch (Exception e) {
41              _log.error(
42                  "Failed the get field " + fieldName + " for class " + clazz, e);
43          }
44      }
45  
46      public static void registerReference(Class<?> clazz, String fieldName) {
47          registerReference(clazz, null, fieldName);
48      }
49  
50      public static void registerReference(Field field) {
51          _referenceEntries.add(new ReferenceEntry(field));
52      }
53  
54      public static void registerReference(Object object, Field field) {
55          _referenceEntries.add(new ReferenceEntry(object, field));
56      }
57  
58      public static void releaseReferences() {
59          for (ReferenceEntry referenceEntry : _referenceEntries) {
60              try {
61                  referenceEntry.setValue(null);
62              }
63              catch (Exception e) {
64                  _log.error(
65                      "Failed to release reference for " + referenceEntry, e);
66              }
67          }
68  
69          _referenceEntries.clear();
70      }
71  
72      private static Log _log = LogFactoryUtil.getLog(ReferenceRegistry.class);
73  
74      private static Set<ReferenceEntry> _referenceEntries =
75          new ConcurrentHashSet<ReferenceEntry>();
76  
77  }