FELIX-2208 Refactor into Apache Felix domain:
* Rename Java Package
* Use Felix Parent POM (and make dependency versions explicit)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@925017 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole-plugins/memoryusage/pom.xml b/webconsole-plugins/memoryusage/pom.xml
index aa3bbe0..62b3b70 100644
--- a/webconsole-plugins/memoryusage/pom.xml
+++ b/webconsole-plugins/memoryusage/pom.xml
@@ -21,16 +21,17 @@
<modelVersion>4.0.0</modelVersion>
<parent>
- <groupId>org.apache.sling</groupId>
- <artifactId>sling</artifactId>
- <version>8</version>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>felix-parent</artifactId>
+ <version>1.2.0</version>
+ <relativePath>../../../pom/pom.xml</relativePath>
</parent>
- <artifactId>org.apache.sling.extensions.memoryusage</artifactId>
+ <artifactId>org.apache.felix.webconsole.plugins.memoryusage</artifactId>
<packaging>bundle</packaging>
<version>0.1.0-SNAPSHOT</version>
- <name>Apache Sling Memory Usage Tracker</name>
+ <name>Apache Felix Web Console Memory Usage Plugin</name>
<description>
Plugin providing plugins to the Felix Shell and Web Console to
observe the VMs memory use and cause try to get thread dumps etc.
@@ -38,13 +39,13 @@
<scm>
<connection>
- scm:svn:http://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/memoryusage
+ scm:svn:http://svn.apache.org/repos/asf/felix/trunk/webconsole-plugins/memoryusage
</connection>
<developerConnection>
- scm:svn:https://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/memoryusage
+ scm:svn:https://svn.apache.org/repos/asf/felix/trunk/webconsole-plugins/memoryusage
</developerConnection>
<url>
- http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/memoryusage
+ http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/memoryusage
</url>
</scm>
@@ -65,10 +66,10 @@
org.apache.felix.webconsole;resolution:=optional
</Import-Package>
<Private-Package>
- org.apache.sling.extensions.memoryusage.*
+ org.apache.felix.webconsole.plugins.memoryusage.*
</Private-Package>
<Bundle-Activator>
- org.apache.sling.extensions.memoryusage.internal.Activator
+ org.apache.felix.webconsole.plugins.memoryusage.internal.Activator
</Bundle-Activator>
</instructions>
</configuration>
@@ -79,32 +80,43 @@
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
+ <version>4.0.0</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
+ <version>4.0.0</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.shell</artifactId>
<version>1.0.0</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.webconsole</artifactId>
<version>2.0.7-SNAPSHOT</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
+ <version>2.3</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
+ <version>1.5.2</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
+ <version>3.8.1</version>
</dependency>
</dependencies>
</project>
diff --git a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/Activator.java b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java
similarity index 70%
rename from webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/Activator.java
rename to webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java
index 6346cb0..e964971 100644
--- a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/Activator.java
+++ b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.extensions.memoryusage.internal;
+package org.apache.felix.webconsole.plugins.memoryusage.internal;
import java.util.Dictionary;
import java.util.Hashtable;
@@ -25,57 +25,66 @@
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
-public class Activator implements BundleActivator {
+public class Activator implements BundleActivator
+{
private MemoryUsageSupport support;
- public void start(BundleContext bundleContext) {
+ public void start(BundleContext bundleContext)
+ {
support = new MemoryUsageSupport(bundleContext);
// install thread handler shell command
- try {
- register(bundleContext,
- new String[] { "org.apache.felix.shell.Command" },
- new MemoryUsageCommand(support), null);
- } catch (Throwable t) {
+ try
+ {
+ register(bundleContext, new String[]
+ { "org.apache.felix.shell.Command" }, new MemoryUsageCommand(support), null);
+ }
+ catch (Throwable t)
+ {
// shell service might not be available, don't care
}
// install Web Console plugin
- try {
+ try
+ {
MemoryUsagePanel tdp = new MemoryUsagePanel(support);
tdp.activate(bundleContext);
Dictionary<String, Object> properties = new Hashtable<String, Object>();
properties.put("felix.webconsole.label", tdp.getLabel());
- register(bundleContext, new String[] { "javax.servlet.Servlet",
- "org.apache.felix.webconsole.ConfigurationPrinter" }, tdp,
- properties);
- } catch (Throwable t) {
+ register(bundleContext, new String[]
+ { "javax.servlet.Servlet", "org.apache.felix.webconsole.ConfigurationPrinter" }, tdp, properties);
+ }
+ catch (Throwable t)
+ {
// web console might not be available, don't care
}
}
- public void stop(BundleContext bundleContext) {
- if (support != null) {
+ public void stop(BundleContext bundleContext)
+ {
+ if (support != null)
+ {
support.dispose();
support = null;
}
}
- private void register(BundleContext context, String[] serviceNames,
- Object service, Dictionary<String, Object> properties) {
+ private void register(BundleContext context, String[] serviceNames, Object service,
+ Dictionary<String, Object> properties)
+ {
// ensure properties
- if (properties == null) {
+ if (properties == null)
+ {
properties = new Hashtable<String, Object>();
}
// default settings
- properties.put(Constants.SERVICE_DESCRIPTION, "Memory Usage ("
- + serviceNames[0] + ")");
+ properties.put(Constants.SERVICE_DESCRIPTION, "Memory Usage (" + serviceNames[0] + ")");
properties.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
context.registerService(serviceNames, service, properties);
diff --git a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsageCommand.java b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageCommand.java
similarity index 68%
rename from webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsageCommand.java
rename to webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageCommand.java
index f65d66e..22e72bd 100644
--- a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsageCommand.java
+++ b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageCommand.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.extensions.memoryusage.internal;
+package org.apache.felix.webconsole.plugins.memoryusage.internal;
import java.io.File;
import java.io.PrintStream;
@@ -24,7 +24,8 @@
import org.apache.felix.shell.Command;
-public class MemoryUsageCommand implements Command {
+public class MemoryUsageCommand implements Command
+{
private static final String HELP_CMD = "help";
@@ -40,15 +41,15 @@
private final MemoryUsageSupport support;
- public MemoryUsageCommand(final MemoryUsageSupport support) {
+ public MemoryUsageCommand(final MemoryUsageSupport support)
+ {
this.support = support;
}
- public void execute(String commandLine, final PrintStream out,
- final PrintStream err) {
+ public void execute(String commandLine, final PrintStream out, final PrintStream err)
+ {
- final PrintWriterPrintHelper printHelper = new PrintWriterPrintHelper(
- out);
+ final PrintWriterPrintHelper printHelper = new PrintWriterPrintHelper(out);
// Parse the commandLine to get the mem command.
final StringTokenizer st = new StringTokenizer(commandLine);
@@ -56,134 +57,174 @@
st.nextToken();
// Try to get the mem command, default is simple memory info dump.
String command = null;
- try {
+ try
+ {
command = st.nextToken();
- } catch (Exception ex) {
+ }
+ catch (Exception ex)
+ {
// Ignore.
}
// Perform the specified command.
- if (command == null) {
+ if (command == null)
+ {
mem(printHelper);
- } else if (command.equals(HELP_CMD)) {
+ }
+ else if (command.equals(HELP_CMD))
+ {
help(out, st);
- } else if (command.equals(POOLS_CMD)) {
+ }
+ else if (command.equals(POOLS_CMD))
+ {
pools(printHelper);
- } else if (command.equals(DUMP_CMD)) {
+ }
+ else if (command.equals(DUMP_CMD))
+ {
dump(out, st, err);
- } else if (command.equals(LS_CMD)) {
+ }
+ else if (command.equals(LS_CMD))
+ {
ls(printHelper);
- } else if (command.equals(RM_CMD)) {
+ }
+ else if (command.equals(RM_CMD))
+ {
rm(out, err, st);
- } else {
+ }
+ else
+ {
err.println("Unknown command: " + command);
}
printHelper.flush();
}
- public String getName() {
+ public String getName()
+ {
return "mem";
}
- public String getShortDescription() {
+ public String getShortDescription()
+ {
return "Prints Java VM Memory Consumption or writes a heap dump";
}
- public String getUsage() {
+ public String getUsage()
+ {
return "mem help";
}
// ---------- internal
- private void mem(final MemoryUsageSupport.PrintHelper printHelper) {
+ private void mem(final MemoryUsageSupport.PrintHelper printHelper)
+ {
support.printOverallMemory(printHelper);
}
- private void pools(final MemoryUsageSupport.PrintHelper printHelper) {
+ private void pools(final MemoryUsageSupport.PrintHelper printHelper)
+ {
support.printMemoryPools(printHelper);
}
- private void help(final PrintStream out, final StringTokenizer word) {
+ private void help(final PrintStream out, final StringTokenizer word)
+ {
String command = HELP_CMD;
- if (word.hasMoreTokens()) {
+ if (word.hasMoreTokens())
+ {
command = word.nextToken();
}
- if (command.equals(DUMP_CMD)) {
+ if (command.equals(DUMP_CMD))
+ {
out.printf("%s %s [ %s ]%n", getName(), DUMP_CMD, OPT_DUMP_ALL);
out.println("This command requests a heap dump to be created. If the\n"
+ "-a option is not added, only live objects are dumped.");
- } else if (command.equals(LS_CMD)) {
+ }
+ else if (command.equals(LS_CMD))
+ {
out.printf("%s %s%n", getName(), LS_CMD);
- out.println("This command lists all heap dumps created with the\n"
- + DUMP_CMD + " command.");
- } else if (command.equals(RM_CMD)) {
+ out.println("This command lists all heap dumps created with the\n" + DUMP_CMD + " command.");
+ }
+ else if (command.equals(RM_CMD))
+ {
out.printf("%s %s <dump>%n", getName(), RM_CMD);
- out.println("This command removes the indicated heap dump file.\n"
- + "Use the " + LS_CMD
+ out.println("This command removes the indicated heap dump file.\n" + "Use the " + LS_CMD
+ " to list heap dumps which may be removed.");
- } else if (command.equals(POOLS_CMD)) {
+ }
+ else if (command.equals(POOLS_CMD))
+ {
out.printf("%s %s%n", getName(), POOLS_CMD);
out.println("This command shows information about all memory pools.");
- } else {
- out.printf("%s %s [ %s | %s | %s | %s ]%n", getName(), HELP_CMD,
- DUMP_CMD, LS_CMD, RM_CMD, POOLS_CMD);
+ }
+ else
+ {
+ out.printf("%s %s [ %s | %s | %s | %s ]%n", getName(), HELP_CMD, DUMP_CMD, LS_CMD, RM_CMD, POOLS_CMD);
out.printf("%s %s [ %s ]%n", getName(), DUMP_CMD, OPT_DUMP_ALL);
out.printf("%s %s%n", getName(), LS_CMD);
out.printf("%s %s <dump>%n", getName(), RM_CMD);
out.printf("%s %s%n", getName(), POOLS_CMD);
- out.println("Using the "
- + getName()
- + " command without any arguments prints a memory use overview.");
+ out.println("Using the " + getName() + " command without any arguments prints a memory use overview.");
}
}
- private void dump(final PrintStream out, final StringTokenizer words,
- final PrintStream err) {
+ private void dump(final PrintStream out, final StringTokenizer words, final PrintStream err)
+ {
String dumpTarget = null;
boolean all = false;
- while (words.hasMoreTokens()) {
+ while (words.hasMoreTokens())
+ {
String word = words.nextToken();
- if (OPT_DUMP_ALL.equals(word)) {
+ if (OPT_DUMP_ALL.equals(word))
+ {
all = true;
- } else {
+ }
+ else
+ {
dumpTarget = word;
}
}
- try {
+ try
+ {
File dumpFile = support.dumpHeap(dumpTarget, !all);
- out.println("Heap dumped to " + dumpFile + " (" + dumpFile.length()
- + " bytes)");
- } catch (Exception e) {
+ out.println("Heap dumped to " + dumpFile + " (" + dumpFile.length() + " bytes)");
+ }
+ catch (Exception e)
+ {
err.println("Problem dumping heap");
e.printStackTrace(err);
}
}
- private void ls(final MemoryUsageSupport.PrintHelper printHelper) {
+ private void ls(final MemoryUsageSupport.PrintHelper printHelper)
+ {
support.listDumpFiles(printHelper);
}
- private void rm(final PrintStream out, final PrintStream err,
- final StringTokenizer words) {
- if (words.hasMoreTokens()) {
- do {
+ private void rm(final PrintStream out, final PrintStream err, final StringTokenizer words)
+ {
+ if (words.hasMoreTokens())
+ {
+ do
+ {
final String name = words.nextToken();
- if (support.rmDumpFile(name)) {
+ if (support.rmDumpFile(name))
+ {
out.println("rm: " + name + " removed");
out.flush();
- } else {
+ }
+ else
+ {
err.println("rm: " + name + ": No such dump file");
err.flush();
}
- } while (words.hasMoreTokens());
+ }
+ while (words.hasMoreTokens());
}
}
- private static class PrintWriterPrintHelper implements
- MemoryUsageSupport.PrintHelper {
+ private static class PrintWriterPrintHelper implements MemoryUsageSupport.PrintHelper
+ {
private static final String INDENTS = " ";
@@ -191,39 +232,50 @@
private String indent;
- PrintWriterPrintHelper(final PrintStream pw) {
+ PrintWriterPrintHelper(final PrintStream pw)
+ {
this.pw = pw;
this.indent = "";
}
- public void title(String title, int level) {
+ public void title(String title, int level)
+ {
pw.printf("%s%s%n", getIndent(level - 1), title);
indent = getIndent(level);
}
- public void val(String value) {
+ public void val(String value)
+ {
pw.printf("%s%s%n", indent, value);
}
- public void keyVal(final String key, final Object value) {
- if (value == null) {
+ public void keyVal(final String key, final Object value)
+ {
+ if (value == null)
+ {
val(key);
- } else {
+ }
+ else
+ {
pw.printf("%s%s: %s%n", indent, key, value);
}
}
- void flush() {
+ void flush()
+ {
pw.flush();
}
- private static String getIndent(final int level) {
- if (level <= 0) {
+ private static String getIndent(final int level)
+ {
+ if (level <= 0)
+ {
return "";
}
final int indent = 2 * level;
- if (indent > INDENTS.length()) {
+ if (indent > INDENTS.length())
+ {
return INDENTS;
}
return INDENTS.substring(0, indent);
diff --git a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsagePanel.java b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java
similarity index 69%
rename from webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsagePanel.java
rename to webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java
index 60481ab..a673473 100644
--- a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsagePanel.java
+++ b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.extensions.memoryusage.internal;
+package org.apache.felix.webconsole.plugins.memoryusage.internal;
import java.io.File;
import java.io.FileInputStream;
@@ -41,31 +41,34 @@
import org.apache.felix.webconsole.WebConsoleUtil;
@SuppressWarnings("serial")
-public class MemoryUsagePanel extends AbstractWebConsolePlugin implements
- ConfigurationPrinter, AttachmentProvider {
+public class MemoryUsagePanel extends AbstractWebConsolePlugin implements ConfigurationPrinter, AttachmentProvider
+{
private final MemoryUsageSupport support;
- public MemoryUsagePanel(final MemoryUsageSupport support) {
+ public MemoryUsagePanel(final MemoryUsageSupport support)
+ {
this.support = support;
}
// ---------- AbstractWebConsolePlugin
@Override
- public String getLabel() {
+ public String getLabel()
+ {
return "memoryusage";
}
@Override
- public String getTitle() {
+ public String getTitle()
+ {
return "Heap Dumps";
}
@SuppressWarnings("unchecked")
@Override
- protected void renderContent(HttpServletRequest req, HttpServletResponse res)
- throws IOException {
+ protected void renderContent(HttpServletRequest req, HttpServletResponse res) throws IOException
+ {
final PrintWriter pw = res.getWriter();
final StringBuilder statusBuf = new StringBuilder(1024);
@@ -75,14 +78,15 @@
filesBuf.append('[');
final File[] files = support.getDumpFiles();
- if (files != null) {
+ if (files != null)
+ {
long totalSize = 0;
- for (File file : files) {
+ for (File file : files)
+ {
filesBuf.append('{');
filesBuf.append("'name':'").append(file.getName()).append("',");
- filesBuf.append("'date':").append(file.lastModified()).append(
- ",");
+ filesBuf.append("'date':").append(file.lastModified()).append(",");
filesBuf.append("'size':").append(file.length());
filesBuf.append("},");
@@ -93,7 +97,9 @@
statusBuf.append(',');
statusBuf.append(totalSize);
- } else {
+ }
+ else
+ {
statusBuf.append("0,0");
}
@@ -115,21 +121,29 @@
// ---------- Configuration Printer
- public void printConfiguration(PrintWriter pw) {
+ public void printConfiguration(PrintWriter pw)
+ {
support.printMemory(new PrintWriterPrintHelper(pw));
}
// ---------- AttachmentProvider
- public URL[] getAttachments(String mode) {
- if (ConfigurationPrinter.MODE_ZIP.equals(mode)) {
+ public URL[] getAttachments(String mode)
+ {
+ if (ConfigurationPrinter.MODE_ZIP.equals(mode))
+ {
File[] dumpFiles = support.getDumpFiles();
- if (dumpFiles != null && dumpFiles.length > 0) {
+ if (dumpFiles != null && dumpFiles.length > 0)
+ {
URL[] attachs = new URL[dumpFiles.length];
- for (int i = 0; i < dumpFiles.length; i++) {
- try {
+ for (int i = 0; i < dumpFiles.length; i++)
+ {
+ try
+ {
attachs[i] = dumpFiles[i].toURI().toURL();
- } catch (MalformedURLException mue) {
+ }
+ catch (MalformedURLException mue)
+ {
// not expected ...
}
}
@@ -144,11 +158,12 @@
// ---------- GenericServlet
@Override
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
final DumpFile dumpFile = getDumpFile(request);
- if (dumpFile != null) {
+ if (dumpFile != null)
+ {
spool(dumpFile.dumpFile, response, dumpFile.compress);
}
@@ -156,65 +171,88 @@
}
@Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
- if ("DELETE".equals(req.getParameter("X-Request-Method"))) {
+ if ("DELETE".equals(req.getParameter("X-Request-Method")))
+ {
doDelete(req, resp);
- } else {
+ }
+ else
+ {
String command = req.getParameter("command");
- if ("dump".equals(command)) {
- try {
+ if ("dump".equals(command))
+ {
+ try
+ {
File file = support.dumpHeap(null, false);
- resp.setContentType("text/plain");
- resp.setCharacterEncoding("UTF-8");
+ resp.setContentType("text/plain; charset=UTF-8");
resp.getWriter().print("Dumped heap to " + file.getName());
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
// TODO: handle
}
- } else if ("gc".equals(command)) {
+ }
+ else if ("gc".equals(command))
+ {
System.gc();
- } else if ("threshold".equals(command)) {
- try {
+ }
+ else if ("threshold".equals(command))
+ {
+ try
+ {
int threshold = Integer.parseInt(req.getParameter("threshold"));
support.setThreshold(threshold);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
// ignore
}
resp.sendRedirect(req.getRequestURI());
- } else {
+ }
+ else
+ {
super.doPost(req, resp);
}
}
}
@Override
- protected void doDelete(HttpServletRequest request,
- HttpServletResponse response) throws IOException {
+ protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException
+ {
final DumpFile dumpFile = getDumpFile(request);
- if (dumpFile != null) {
+ if (dumpFile != null)
+ {
dumpFile.dumpFile.delete();
response.setStatus(HttpServletResponse.SC_OK);
- } else {
+ }
+ else
+ {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
}
// ---------- internal
- private DumpFile getDumpFile(final HttpServletRequest request) {
+ private DumpFile getDumpFile(final HttpServletRequest request)
+ {
final String pathInfo = request.getPathInfo();
- if (pathInfo != null && !pathInfo.endsWith(getLabel())) {
+ if (pathInfo != null && !pathInfo.endsWith(getLabel()))
+ {
final int lastSlash = pathInfo.lastIndexOf('/');
- if (lastSlash > 0) {
+ if (lastSlash > 0)
+ {
String label = pathInfo.substring(lastSlash + 1);
boolean isZip = false;
- if (label.endsWith(".zip")) {
+ if (label.endsWith(".zip"))
+ {
label = label.substring(0, label.length() - 4);
isZip = true;
}
File dumpFile = support.getDumpFile(label);
- if (dumpFile != null) {
+ if (dumpFile != null)
+ {
return new DumpFile(dumpFile, isZip);
}
}
@@ -223,10 +261,11 @@
return null;
}
- private void spool(final File dumpFile, final HttpServletResponse response,
- boolean compress) throws IOException {
+ private void spool(final File dumpFile, final HttpServletResponse response, boolean compress) throws IOException
+ {
InputStream ins = null;
- try {
+ try
+ {
ins = new FileInputStream(dumpFile);
response.setDateHeader("Last-Modified", dumpFile.lastModified());
@@ -234,7 +273,8 @@
OutputStream out = response.getOutputStream();
- if (compress) {
+ if (compress)
+ {
ZipOutputStream zip = new ZipOutputStream(out);
zip.setLevel(Deflater.BEST_SPEED);
@@ -249,57 +289,68 @@
// zip output with unknown length
response.setContentType("application/zip");
- } else {
+ }
+ else
+ {
- String type = getServletContext().getMimeType(
- dumpFile.getName());
- if (type == null) {
+ String type = getServletContext().getMimeType(dumpFile.getName());
+ if (type == null)
+ {
type = "application/octet-stream";
}
response.setContentType(type);
- response.setHeader("Content-Length",
- String.valueOf(dumpFile.length())); // might be bigger than
+ response.setHeader("Content-Length", String.valueOf(dumpFile.length())); // might be bigger than
// int
}
byte[] buf = new byte[32768];
int rd = 0;
- while ((rd = ins.read(buf)) >= 0) {
+ while ((rd = ins.read(buf)) >= 0)
+ {
out.write(buf, 0, rd);
}
- if (compress) {
+ if (compress)
+ {
out.flush();
((ZipOutputStream) out).closeEntry();
((ZipOutputStream) out).finish();
}
- } finally {
- if (ins != null) {
- try {
+ }
+ finally
+ {
+ if (ins != null)
+ {
+ try
+ {
ins.close();
- } catch (IOException ignore) {
+ }
+ catch (IOException ignore)
+ {
// ignore
}
}
}
}
- private static class DumpFile {
+ private static class DumpFile
+ {
final File dumpFile;
final boolean compress;
- DumpFile(final File dumpFile, final boolean compress) {
+ DumpFile(final File dumpFile, final boolean compress)
+ {
this.dumpFile = dumpFile;
this.compress = compress;
}
}
- private static class PrintWriterPrintHelper implements
- MemoryUsageSupport.PrintHelper {
+ private static class PrintWriterPrintHelper implements MemoryUsageSupport.PrintHelper
+ {
private static final String INDENTS = " ";
@@ -307,60 +358,76 @@
private String indent;
- PrintWriterPrintHelper(final PrintWriter pw) {
+ PrintWriterPrintHelper(final PrintWriter pw)
+ {
this.pw = pw;
this.indent = "";
}
- public void title(String title, int level) {
+ public void title(String title, int level)
+ {
pw.printf("%n%s%s%n", getIndent(level), title);
indent = getIndent(level + 1);
}
- public void val(String value) {
+ public void val(String value)
+ {
pw.printf("%s%s%n", indent, value);
}
- public void keyVal(final String key, final Object value) {
- if (value == null) {
+ public void keyVal(final String key, final Object value)
+ {
+ if (value == null)
+ {
val(key);
- } else {
+ }
+ else
+ {
pw.printf("%s%s: %s%n", indent, key, value);
}
}
- private static String getIndent(final int level) {
+ private static String getIndent(final int level)
+ {
final int indent = 2 * level;
- if (indent > INDENTS.length()) {
+ if (indent > INDENTS.length())
+ {
return INDENTS;
}
return INDENTS.substring(0, indent);
}
}
- private static class JsonPrintHelper implements
- MemoryUsageSupport.PrintHelper {
+ private static class JsonPrintHelper implements MemoryUsageSupport.PrintHelper
+ {
private final StringBuilder buf;
- JsonPrintHelper() {
+ JsonPrintHelper()
+ {
buf = new StringBuilder();
buf.append('{');
}
- String getString() {
+ String getString()
+ {
final String result = buf.append('}').toString();
buf.delete(1, buf.length());
return result;
}
- public void title(String title, int level) {
+ public void title(String title, int level)
+ {
}
- public void keyVal(String key, Object value) {
- if (value == null) {
+ public void keyVal(String key, Object value)
+ {
+ if (value == null)
+ {
val(key);
- } else {
+ }
+ else
+ {
buf.append('\'');
buf.append(key);
buf.append("':'");
@@ -369,7 +436,8 @@
}
}
- public void val(String value) {
+ public void val(String value)
+ {
buf.append("'");
buf.append(value);
buf.append("':'',");
diff --git a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsageSupport.java b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java
similarity index 65%
rename from webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsageSupport.java
rename to webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java
index 1901472..dd6f9be 100644
--- a/webconsole-plugins/memoryusage/src/main/java/org/apache/sling/extensions/memoryusage/internal/MemoryUsageSupport.java
+++ b/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.extensions.memoryusage.internal;
+package org.apache.felix.webconsole.plugins.memoryusage.internal;
import java.io.File;
import java.lang.management.ManagementFactory;
@@ -38,7 +38,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-final class MemoryUsageSupport implements NotificationListener {
+final class MemoryUsageSupport implements NotificationListener
+{
// This is the name of the HotSpot Diagnostic MBean
private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
@@ -47,37 +48,48 @@
private int threshold;
- MemoryUsageSupport(final BundleContext context) {
- final String slingHome = context.getProperty("sling.home");
- if (slingHome != null) {
- dumpLocation = new File(slingHome, "dumps");
- } else {
- dumpLocation = new File("dumps");
+ MemoryUsageSupport(final BundleContext context)
+ {
+ File dumps = context.getDataFile("dumps");
+ if (dumps == null)
+ {
+ dumps = new File("dumps");
}
+
+ dumpLocation = dumps;
dumpLocation.mkdirs();
NotificationEmitter memEmitter = (NotificationEmitter) getMemory();
memEmitter.addNotificationListener(this, null, null);
}
- void dispose() {
+ void dispose()
+ {
NotificationEmitter memEmitter = (NotificationEmitter) getMemory();
- try {
+ try
+ {
memEmitter.removeNotificationListener(this);
- } catch (ListenerNotFoundException lnfes) {
+ }
+ catch (ListenerNotFoundException lnfes)
+ {
// TODO: not expected really ?
}
}
- final void setThreshold(final int percentage) {
- if (percentage < 50 || percentage > 100) {
+ final void setThreshold(final int percentage)
+ {
+ if (percentage < 50 || percentage > 100)
+ {
// wrong value
- } else {
+ }
+ else
+ {
List<MemoryPoolMXBean> pools = getMemoryPools();
- for (MemoryPoolMXBean pool : pools) {
- if (pool.isUsageThresholdSupported()) {
- long threshold = pool.getUsage().getMax() * percentage
- / 100;
+ for (MemoryPoolMXBean pool : pools)
+ {
+ if (pool.isUsageThresholdSupported())
+ {
+ long threshold = pool.getUsage().getMax() * percentage / 100;
pool.setUsageThreshold(threshold);
}
}
@@ -85,11 +97,13 @@
}
}
- final int getThreshold() {
+ final int getThreshold()
+ {
return threshold;
}
- final void printMemory(final PrintHelper pw) {
+ final void printMemory(final PrintHelper pw)
+ {
pw.title("Overall Memory Use", 0);
pw.keyVal("Heap Dump Threshold", getThreshold() + "%");
printOverallMemory(pw);
@@ -101,95 +115,109 @@
listDumpFiles(pw);
}
- final void printOverallMemory(final PrintHelper pw) {
+ final void printOverallMemory(final PrintHelper pw)
+ {
final MemoryMXBean mem = getMemory();
pw.keyVal("Verbose Memory Output", (mem.isVerbose() ? "yes" : "no"));
- pw.keyVal("Pending Finalizable Objects",
- mem.getObjectPendingFinalizationCount());
+ pw.keyVal("Pending Finalizable Objects", mem.getObjectPendingFinalizationCount());
pw.keyVal("Overall Heap Memory Usage", mem.getHeapMemoryUsage());
pw.keyVal("Overall Non-Heap Memory Usage", mem.getNonHeapMemoryUsage());
}
- final void printMemoryPools(final PrintHelper pw) {
+ final void printMemoryPools(final PrintHelper pw)
+ {
final List<MemoryPoolMXBean> pools = getMemoryPools();
- for (MemoryPoolMXBean pool : pools) {
- final String title = String.format("%s (%s, %s)", pool.getName(),
- pool.getType(), (pool.isValid() ? "valid" : "invalid"));
+ for (MemoryPoolMXBean pool : pools)
+ {
+ final String title = String.format("%s (%s, %s)", pool.getName(), pool.getType(), (pool.isValid() ? "valid"
+ : "invalid"));
pw.title(title, 1);
- pw.keyVal("Memory Managers",
- Arrays.asList(pool.getMemoryManagerNames()));
+ pw.keyVal("Memory Managers", Arrays.asList(pool.getMemoryManagerNames()));
pw.keyVal("Peak Usage", pool.getPeakUsage());
pw.keyVal("Usage", pool.getUsage());
- if (pool.isUsageThresholdSupported()) {
- pw.keyVal("Usage Threshold", String.format(
- "%d, %s, #exceeded=%d", pool.getUsageThreshold(),
- pool.isUsageThresholdExceeded()
- ? "exceeded"
- : "not exceeded", pool.getUsageThresholdCount()));
- } else {
+ if (pool.isUsageThresholdSupported())
+ {
+ pw.keyVal("Usage Threshold", String.format("%d, %s, #exceeded=%d", pool.getUsageThreshold(), pool
+ .isUsageThresholdExceeded() ? "exceeded" : "not exceeded", pool.getUsageThresholdCount()));
+ }
+ else
+ {
pw.val("Usage Threshold: not supported");
}
pw.keyVal("Collection Usage", pool.getCollectionUsage());
- if (pool.isCollectionUsageThresholdSupported()) {
- pw.keyVal("Collection Usage Threshold", String.format(
- "%d, %s, #exceeded=%d", pool.getCollectionUsageThreshold(),
- pool.isCollectionUsageThresholdExceeded()
- ? "exceeded"
- : "not exceeded",
- pool.getCollectionUsageThresholdCount()));
- } else {
+ if (pool.isCollectionUsageThresholdSupported())
+ {
+ pw.keyVal("Collection Usage Threshold", String.format("%d, %s, #exceeded=%d", pool
+ .getCollectionUsageThreshold(), pool.isCollectionUsageThresholdExceeded() ? "exceeded"
+ : "not exceeded", pool.getCollectionUsageThresholdCount()));
+ }
+ else
+ {
pw.val("Collection Usage Threshold: not supported");
}
}
}
- final void listDumpFiles(final PrintHelper pw) {
+ final void listDumpFiles(final PrintHelper pw)
+ {
pw.title(dumpLocation.getAbsolutePath(), 1);
File[] dumps = getDumpFiles();
- if (dumps == null || dumps.length == 0) {
+ if (dumps == null || dumps.length == 0)
+ {
pw.keyVal("-- None", null);
- } else {
+ }
+ else
+ {
long totalSize = 0;
- for (File dump : dumps) {
+ for (File dump : dumps)
+ {
// 32167397 2010-02-25 23:30 thefile
- pw.val(String.format("%10d %tF %2$tR %s", dump.length(),
- new Date(dump.lastModified()), dump.getName()));
+ pw
+ .val(String.format("%10d %tF %2$tR %s", dump.length(), new Date(dump.lastModified()), dump
+ .getName()));
totalSize += dump.length();
}
pw.val(String.format("%d files, %d bytes", dumps.length, totalSize));
}
}
- final File getDumpFile(final String name) {
+ final File getDumpFile(final String name)
+ {
// expect a non-empty string without slash
- if (name == null || name.length() == 0 || name.indexOf('/') >= 0) {
+ if (name == null || name.length() == 0 || name.indexOf('/') >= 0)
+ {
return null;
}
File dumpFile = new File(dumpLocation, name);
- if (dumpFile.isFile()) {
+ if (dumpFile.isFile())
+ {
return dumpFile;
}
return null;
}
- final File[] getDumpFiles() {
+ final File[] getDumpFiles()
+ {
return dumpLocation.listFiles();
}
- final boolean rmDumpFile(final String name) {
- if (name == null || name.length() == 0) {
+ final boolean rmDumpFile(final String name)
+ {
+ if (name == null || name.length() == 0)
+ {
return false;
}
final File dumpFile = new File(dumpLocation, name);
- if (!dumpFile.exists()) {
+ if (!dumpFile.exists())
+ {
return false;
}
@@ -205,21 +233,26 @@
* @see http://blogs.sun.com/sundararajan/entry/
* programmatically_dumping_heap_from_java
*/
- final File dumpHeap(String name, final boolean live) throws Exception {
- try {
- if (name == null) {
+ final File dumpHeap(String name, final boolean live) throws Exception
+ {
+ try
+ {
+ if (name == null)
+ {
name = "heap." + System.currentTimeMillis() + ".bin";
}
dumpLocation.mkdirs();
File tmpFile = new File(dumpLocation, name);
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
- server.invoke(new ObjectName(HOTSPOT_BEAN_NAME), "dumpHeap",
- new Object[] { tmpFile.getAbsolutePath(), live }, new String[] {
- String.class.getName(), boolean.class.getName() });
+ server.invoke(new ObjectName(HOTSPOT_BEAN_NAME), "dumpHeap", new Object[]
+ { tmpFile.getAbsolutePath(), live }, new String[]
+ { String.class.getName(), boolean.class.getName() });
return tmpFile;
// } catch (JMException je) {
// } catch (IOException ioe) {
- } finally {
+ }
+ finally
+ {
}
@@ -227,28 +260,36 @@
// return null;
}
- final MemoryMXBean getMemory() {
+ final MemoryMXBean getMemory()
+ {
return ManagementFactory.getMemoryMXBean();
}
- final List<MemoryPoolMXBean> getMemoryPools() {
+ final List<MemoryPoolMXBean> getMemoryPools()
+ {
return ManagementFactory.getMemoryPoolMXBeans();
}
- public void handleNotification(Notification notification, Object handback) {
+ public void handleNotification(Notification notification, Object handback)
+ {
Logger log = LoggerFactory.getLogger(getClass());
String notifType = notification.getType();
- if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
- try {
+ if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED))
+ {
+ try
+ {
File file = dumpHeap(null, true);
log.warn("Heap dumped to " + file);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
log.error("Failed dumping heap", e);
}
}
}
- static interface PrintHelper {
+ static interface PrintHelper
+ {
void title(final String title, final int level);
void val(final String value);