KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > util > MemoryMonitorFactory


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/util/MemoryMonitorFactory.java#7 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2002-2002 Kana Software, Inc.
7 // Copyright (C) 2002-2007 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 //
11 // jhyde, Dec 23, 2002
12 */

13 package mondrian.util;
14
15 import org.eigenbase.util.property.StringProperty;
16 import mondrian.olap.MondrianProperties;
17 import mondrian.olap.Util;
18
19 /**
20  * The <code>MemoryMonitorFactory</code> is used to get the application's
21  * <code>MemoryMonitor</code>. The <code>MemoryMonitorFactory</code> is
22  * based upon the <code>ObjectFactory.Singleton</code> generic. The
23  * <code>MemoryMonitorFactory</code> implementation has a single, default
24  * <code>MemoryMonitor</code> per JVM instance which can be overridden
25  * using the provided <code>ThreadLocal</code> variable. Normally, this
26  * <code>ThreadLocal</code> override should only be used during JUnit testing.
27  * The JUnit test, set the <code>ThreadLocal</code> variable to the name of its
28  * own implementation of the <code>MemoryMonitor</code> interface and
29  * then calls the <code>ObjectFactory</code> <code>getObject</code> method.
30  * After doing the test, the <code>ThreadLocal</code> variable should
31  * be cleared.
32  * <p>
33  * The <code>ObjectFactory.Singleton</code> permits the use
34  * of <code>System</code> properties to provide a class name to the
35  * factory. This can be used to create a <code>MemoryMonitor</code>
36  * that is not the default one. The property name is the
37  * <code>MemoryMonitor</code> class name, "mondrian.util.MemoryMonitor".
38  *
39  * @author <a>Richard M. Emberson</a>
40  * @since Feb 03 2007
41  * @version $Id: //open/mondrian/src/main/mondrian/util/MemoryMonitorFactory.java#7 $
42  */

43 public final class MemoryMonitorFactory
44         extends ObjectFactory.Singleton<MemoryMonitor> {
45
46     /**
47      * Single instance of the <code>MemoryMonitorFactory</code>.
48      */

49     private static final MemoryMonitorFactory factory;
50     static {
51         factory = new MemoryMonitorFactory();
52     }
53
54     /**
55      * Access the <code>MemoryMonitorFactory</code> instance.
56      *
57      * @return the <code>MemoryMonitor</code>.
58      */

59     public static MemoryMonitor getMemoryMonitor() {
60         return factory.getObject();
61     }
62
63     /**
64      * ThreadLocal used to hold the class name of an <code>MemoryMonitor</code>
65      * implementation. Generally, this should only be used for testing.
66      */

67     private static final ThreadLocal JavaDoc<String JavaDoc> ClassName =
68         new ThreadLocal JavaDoc<String JavaDoc>();
69
70     /**
71      * Get the class name of a <code>MemoryMonitor</code> implementation
72      * or null.
73      *
74      * @return the class name or null.
75      */

76     private static String JavaDoc getThreadLocalClassName() {
77         return ClassName.get();
78     }
79
80     /**
81      * Sets the class name of a <code>MemoryMonitor</code> implementation.
82      * This should be called (obviously) before calling the
83      * <code>MemoryMonitorFactory</code> <code>getMemoryMonitor</code>
84      * method to get the <code>MemoryMonitor</code> implementation.
85      * Generally, this is only used for testing.
86      *
87      * @param className Class name
88      */

89     public static void setThreadLocalClassName(String JavaDoc className) {
90         ClassName.set(className);
91     }
92
93     /**
94      * Clears the class name (regardless of whether a class name was set).
95      * When a class name is set using <code>setThreadLocalClassName</code>,
96      * the setting whould be done in a try-block and a call to this
97      * clear method should be in the finally-clause of that try-block.
98      */

99     public static void clearThreadLocalClassName() {
100         ClassName.set(null);
101         if (factory.testSingleInstance != null) {
102             factory.testSingleInstance.removeAllListener();
103             factory.testSingleInstance = null;
104         }
105         if (factory.singleInstance instanceof MemoryMonitor.Test) {
106             ((MemoryMonitor.Test) factory.singleInstance).resetFromTest();
107         }
108     }
109
110     /**
111      * The constructor for the <code>MemoryMonitorFactory</code>. This passes
112      * the <code>MemoryMonitor</code> class to the <code>ObjectFactory</code>
113      * base class.
114      */

115     private MemoryMonitorFactory() {
116         super(MemoryMonitor.class);
117     }
118
119     /**
120      * Returns whether the use of a <code>MemoryMonitor</code> is enabled.
121      *
122      * @return <code>true</code> if enabled and <code>false</code> otherwise.
123      */

124     protected boolean enabled() {
125         return MondrianProperties.instance().MemoryMonitor.get();
126     }
127
128     /**
129      * Get the class name set in the <code>ThreadLocal</code> or null.
130      *
131      * @return class name or null.
132      */

133     protected String JavaDoc getClassName() {
134         return getThreadLocalClassName();
135     }
136
137     /**
138      * Return the <code>MemoryMonitorFactory</code property name.
139      *
140      * @return <code>MemoryMonitorFactory</code> property name
141      */

142     protected StringProperty getStringProperty() {
143         return MondrianProperties.instance().MemoryMonitorClass;
144     }
145
146     /**
147      * The <code>MemoryMonitorFactory</code>'s implementation of the
148      * <code>ObjectFactory</code>'s abstract method which returns
149      * the default <code>MemoryMonitor</code> instance.
150      * For Java4 or if the <code>MemoryMonitorFactory</code> is not enabled
151      * then this method returns the "faux" <code>MemoryMonitor</code>
152      * implementation, it does nothing. When enabled and for
153      * Java5 and above JVMs, and instance of the
154      * <code>NotificationMemoryMonitor</code> is returned.
155      *
156      * @param parameterTypes not used
157      * @param parameterValues not used
158      * @return <code>MemoryMonitor</code> instance
159      * @throws CreationException if the <code>MemoryMonitor</code> can not be
160      * created.
161      */

162     protected MemoryMonitor getDefault(Class JavaDoc[] parameterTypes,
163                                        Object JavaDoc[] parameterValues)
164             throws CreationException {
165         return (! enabled() || Util.PreJdk15)
166                     // not enabled or Java4 or below
167
? new FauxMemoryMonitor()
168                     // enabled and Java5 or above
169
: new NotificationMemoryMonitor();
170     }
171
172 }
173
174 // End MemoryMonitorFactory.java
175
Popular Tags