001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Jeff Mesnil.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.controller.cache.parsing;
021:
022: import javax.management.NotCompliantMBeanException;
023:
024: import org.continuent.sequoia.common.i18n.Translate;
025: import org.continuent.sequoia.common.jmx.mbeans.ParsingCacheMBean;
026: import org.continuent.sequoia.controller.jmx.AbstractStandardMBean;
027:
028: /**
029: * RequestFactoryControlMBean implemementation. Used to manage RequestFactory
030: *
031: * @see org.continuent.sequoia.controller.requests.RequestFactory
032: * @see org.continuent.sequoia.common.jmx.mbeans.ParsingCacheMBean
033: */
034: public class ParsingCacheControl extends AbstractStandardMBean
035: implements ParsingCacheMBean {
036: private ParsingCache managedCache;
037: private static final int ENTRIES_PER_DUMP = 100;
038: private int numberOfCacheEntries = 0;
039: private int currentDumpIndex = 0;
040:
041: /**
042: * Creates a new <code>ParsingCacheControl</code> object
043: *
044: * @param cache the managed cache
045: * @throws NotCompliantMBeanException if this mbean is not compliant
046: */
047: public ParsingCacheControl(ParsingCache cache)
048: throws NotCompliantMBeanException {
049: super (ParsingCacheMBean.class);
050: this .managedCache = cache;
051: }
052:
053: /**
054: * @see org.continuent.sequoia.controller.jmx.AbstractStandardMBean#getAssociatedString()
055: */
056: public String getAssociatedString() {
057: return "requestfactory"; //$NON-NLS-1$
058: }
059:
060: /**
061: * @see ParsingCacheMBean#dumpCacheConfig()
062: */
063: public String dumpCacheConfig() {
064: return managedCache.dumpCacheConfig();
065: }
066:
067: /**
068: * @see org.continuent.sequoia.common.jmx.mbeans.ParsingCacheMBean#dumpNextCacheEntries()
069: */
070: public String dumpNextCacheEntries() {
071: StringBuffer ret = new StringBuffer();
072: if (currentDumpIndex > numberOfCacheEntries) {
073: // last dump reached the end of the list, return null and reset counters
074: currentDumpIndex = 0;
075: return null;
076: }
077: if (currentDumpIndex == 0) {
078: // This is a new dump => get the cache size and print a description
079: numberOfCacheEntries = managedCache
080: .getNumberOfCacheEntries();
081: ret.append(Translate.get("cache.entries")); //$NON-NLS-1$
082: }
083: if (numberOfCacheEntries == 0) // no entry, stop dump
084: {
085: currentDumpIndex = 0;
086: return null;
087: }
088: try {
089: ret.append(managedCache.dumpCacheEntries(currentDumpIndex,
090: ENTRIES_PER_DUMP));
091: currentDumpIndex += ENTRIES_PER_DUMP;
092: } catch (OutOfMemoryError e) {
093: // stop dump
094: currentDumpIndex = 0;
095: return null;
096: }
097: return ret.toString();
098: }
099:
100: /**
101: * @see ParsingCacheMBean#resetDump()
102: */
103: public void resetDump() {
104: currentDumpIndex = 0;
105: numberOfCacheEntries = 0;
106: }
107:
108: /**
109: * @see ParsingCacheMBean#dumpCurrentlyParsedEntries()
110: */
111: public String dumpCurrentlyParsedEntries() {
112: // Assume that the number of currently parsed entries is small enough to fit
113: // into a string (this is actually realistic...)
114: return managedCache.dumpCurrentlyParsedEntries();
115: }
116: }
|