Skip to content

Commit

Permalink
Add mbean for finding uploaded files for #374
Browse files Browse the repository at this point in the history
  • Loading branch information
gashcrumb committed Jun 24, 2013
1 parent da9855b commit 6ff68a5
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hawtio-web/src/main/java/io/hawt/HawtioContextListener.java
Expand Up @@ -2,6 +2,7 @@

import io.hawt.jmx.JmxTreeWatcher;
import io.hawt.jmx.PluginRegistry;
import io.hawt.jmx.UploadManager;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
Expand All @@ -13,11 +14,13 @@ public class HawtioContextListener implements ServletContextListener {

private JmxTreeWatcher treeWatcher = new JmxTreeWatcher();
private PluginRegistry registry = new PluginRegistry();
private UploadManager uploadManager = new UploadManager();

public void contextInitialized(ServletContextEvent servletContextEvent) {
try {
treeWatcher.init();
registry.init();
uploadManager.init();

} catch (Exception e) {
throw createServletException(e);
Expand All @@ -28,6 +31,7 @@ public void contextDestroyed(ServletContextEvent servletContextEvent) {
try {
treeWatcher.destroy();
registry.destroy();
uploadManager.destroy();
} catch (Exception e) {
throw createServletException(e);
}
Expand Down
44 changes: 44 additions & 0 deletions hawtio-web/src/main/java/io/hawt/jmx/FileDTO.java
@@ -0,0 +1,44 @@
package io.hawt.jmx;

import java.io.File;

/**
* @author Stan Lewis
*/
public class FileDTO {

private String absolutePath;
private String fileName;
private long length;


public FileDTO(File file) {
this.absolutePath = file.getAbsolutePath();
this.fileName = file.getName();
this.length = file.length();
}

public String getAbsolutePath() {
return absolutePath;
}

public void setAbsolutePath(String absolutePath) {
this.absolutePath = absolutePath;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public long getLength() {
return length;
}

public void setLength(long length) {
this.length = length;
}
}
80 changes: 80 additions & 0 deletions hawtio-web/src/main/java/io/hawt/jmx/UploadManager.java
@@ -0,0 +1,80 @@
package io.hawt.jmx;

import io.hawt.web.UploadServlet;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;

/**
* @author Stan Lewis
*/
public class UploadManager implements UploadManagerMBean {

private ObjectName objectName;
private MBeanServer mBeanServer;


public void init() throws Exception {
if (objectName == null) {
objectName = getObjectName();
}

if (mBeanServer == null) {
mBeanServer = ManagementFactory.getPlatformMBeanServer();
}

if (mBeanServer != null) {
try {
mBeanServer.registerMBean(this, objectName);
} catch(InstanceAlreadyExistsException iaee) {
// Try to remove and re-register
mBeanServer.unregisterMBean(objectName);
mBeanServer.registerMBean(this, objectName);
}
}
}

public void destroy() throws Exception {
if (mBeanServer != null) {
if (objectName != null) {
mBeanServer.unregisterMBean(objectName);
}
}
}

protected ObjectName getObjectName() throws Exception {
return new ObjectName("io.hawt.jmx:type=UploadManager");
}

@Override
public String getUploadDirectory() {
return UploadServlet.UPLOAD_DIRECTORY;
}

@Override
public List<FileDTO> list(String parent) {
String uploadDir = UploadServlet.UPLOAD_DIRECTORY;

if (parent != null && !parent.equals("")) {
uploadDir = uploadDir + File.separator + parent;
}

File dir = new File(uploadDir);
if (!dir.exists()) {
return null;
}

List<FileDTO> rc = new ArrayList<FileDTO>();

for (File file : dir.listFiles()) {
rc.add(new FileDTO(file));
}
return rc;

}
}
14 changes: 14 additions & 0 deletions hawtio-web/src/main/java/io/hawt/jmx/UploadManagerMBean.java
@@ -0,0 +1,14 @@
package io.hawt.jmx;

import java.util.List;

/**
* @author Stan Lewis
*/
public interface UploadManagerMBean {

String getUploadDirectory();

List<FileDTO> list(String parent);

}

0 comments on commit 6ff68a5

Please sign in to comment.