package org.jfox.ejb.deploy;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import org.huihoo.jfox.system.ServiceSupport;
public class EJBDeployService extends ServiceSupport implements DeployService {
private EJBDeployer deployer = null;
private volatile List directories = new ArrayList();
private Map jars = new HashMap();
private long sleepTime = 10000L;
private static FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if(name.endsWith(".jar")) {
return true;
}
else {
return false;
}
}
};
public void setSleepTime(long time) {
sleepTime = time;
}
public long getSleepTime() {
return sleepTime;
}
public void addDeployDirectory(String url) {
File file = new File(url);
try {
String dirUrl = file.toURL().toString();
if(!file.exists()){
logger.warn(dirUrl + " is not exists.");
}
else if(!file.isDirectory()){
logger.warn("add deploy directory " + dirUrl + " faild, it must be a directory.");
}
else {
logger.info("add deploy directory " + dirUrl + " successfully");
synchronized(directories) {
directories.add(file);
}
}
}
catch(MalformedURLException e){
e.printStackTrace();
logger.warn(e.getMessage(),e);
}
}
public File[] getDeployDirectories() {
return (File[])directories.toArray(new File[0]);
}
protected void doInit() throws Exception {
deployer = new EJBDeployer();
deployer.init();
}
public URL[] getDeployedJars(){
return (URL[])jars.keySet().toArray(new URL[0]);
}
protected void doStart() throws Exception {
Thread deployThread = new Thread(this,name);
deployThread.setPriority(Thread.MIN_PRIORITY);
deployThread.start();
}
protected void doStop() throws Exception {
}
protected void doDestroy() throws Exception {
deployer = null;
directories.clear();
jars.clear();
}
public void run() {
while(isRunning()){
Map newJars = new HashMap();
File[] directoriesNames = this.getDeployDirectories();
for( int i=0; i<directoriesNames.length; i++){
File dir = directoriesNames[i];
if(dir.exists() && dir.isDirectory()){
File[] jarFiles = dir.listFiles(filter);
for(int j=0; j< jarFiles.length; j++){
File jar = jarFiles[j];
try {
newJars.put(jar.toURL(),new Long(jar.lastModified()));
if(mustBeDeploy(jar)){ deployer.deploy(jar.toURL());
}
jars.remove(jar.toURL());
}
catch(Exception e){
logger.warn(e.getMessage(),e);
}
}
}
}
for(Iterator it = jars.keySet().iterator(); it.hasNext();){
URL url = (URL)it.next();
try {
deployer.undeploy(url);
}
catch(Exception e){
logger.warn("undeploy " + url + " failed", e);
}
}
jars = newJars;
try {
Thread.sleep(sleepTime);
}
catch(InterruptedException e){
logger.warn(e.getMessage(),e);
}
}
}
private boolean mustBeDeploy(File jar){
try {
URL jarUrl = jar.toURL();
if(!jars.containsKey(jarUrl)){ return true;
}
else {
long lastModify = ((Long)jars.get(jarUrl)).longValue();
if(jar.lastModified() > lastModify) { return true;
}
else {
return false;
}
}
}
catch(MalformedURLException e){
e.printStackTrace();
logger.warn(e.getMessage(), e);
return false;
}
}
public static void main(String[] args) {
}
}