Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mbean for finding uploaded files for #374
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
14
hawtio-web/src/main/java/io/hawt/jmx/UploadManagerMBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |