Skip to content

Commit

Permalink
[Story #196234] [XPath] Export method for cache entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Todor Neykov authored and MrEasy committed Nov 18, 2024
1 parent afe84ff commit 60bb9d0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
if (CACHE_STATISTICS) {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
try {
jXPathStatisticsMBeanImpl = new JXPathStatisticsMBeanImpl();
jXPathStatisticsMBeanImpl = new JXPathStatisticsMBeanImpl(compiled);
mbs.registerMBean(jXPathStatisticsMBeanImpl, ObjectName.getInstance("org.apache.commons.commons-jxpath:Type=JXPathStatisticsMBean"));
} catch (JMException e) {
// best effort only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface JXPathStatisticsMBean {
long getParseCount();

long getAverageParseTimeInMillis();

void exportCacheKeys();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.jxpath.ri.compiler.Expression;
import org.apache.commons.jxpath.util.CacheExportUtil;


public class JXPathStatisticsMBeanImpl extends StandardMBean implements JXPathStatisticsMBean {

private static final AtomicInteger cacheSize = new AtomicInteger();
Expand All @@ -14,9 +20,11 @@ public class JXPathStatisticsMBeanImpl extends StandardMBean implements JXPathSt
private static final AtomicLong limitExceeded = new AtomicLong();
private static final AtomicLong parseTime = new AtomicLong();
private static final AtomicLong parseCount = new AtomicLong();
private final Map<String, Expression> cache;

public JXPathStatisticsMBeanImpl() throws NotCompliantMBeanException {
public JXPathStatisticsMBeanImpl(Map<String, Expression> cache) throws NotCompliantMBeanException {
super(JXPathStatisticsMBean.class);
this.cache = cache;
}

@Override
Expand Down Expand Up @@ -90,4 +98,9 @@ public void incrementLimitExceeded()
limitExceeded.incrementAndGet();
}

@Override
public void exportCacheKeys() {
CacheExportUtil.exportCacheKeys(cache);
}

}
53 changes: 53 additions & 0 deletions src/main/java/org/apache/commons/jxpath/util/CacheExportUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.apache.commons.jxpath.util;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;

import org.apache.commons.jxpath.ri.compiler.Expression;


/**
* Utility class for exporting cache contents to a file.
*
*/
public class CacheExportUtil
{
private static final Logger LOGGER = Logger.getLogger(CacheExportUtil.class.getName());

private static final String FILE_NAME = "xpath-cache-keys.";
private static final String TEMP_DIR = System.getProperty("bisas.temp", ".");


private CacheExportUtil()
{
// utility class
}


public static void exportCacheKeys(Map<String, Expression> cache)
{
if (cache == null) {
return;
}

File file = new File(TEMP_DIR, FILE_NAME + System.currentTimeMillis());

try (PrintWriter out = new PrintWriter(new FileWriter(file))) {
List<String> keysSnapshot = new CopyOnWriteArrayList<>(cache.keySet());
keysSnapshot.forEach(out::println);
out.println("Size: " + keysSnapshot.size());
LOGGER.info("Exported cache keys to " + file.getAbsolutePath());
}
catch (IOException ex) {
throw new RuntimeException("Failed to export cache keys", ex);
}
}

}

0 comments on commit 60bb9d0

Please sign in to comment.