public class FileManager
{
// 注册的监听文件列表
private static HashMap<String, FileListener> FMap = new HashMap<String, FileListener>();
// 配置文件路径(从命令行传入)
private String configFile = null;
// 默认配置文件(从JAR包中加载)
private String defaultConfigFile = "FileListener.properties";
private static Logger logger = Logger.getLogger(FileManager.class);
public static void main(String[] args)
{
FileManager fm = new FileManager();
if (args.length > 0)
{
fm.configFile = args[1];
}
if (FMap.size() == 0)
{
fm.regFileListener();
logger.info("Execute Command sucess.");
System.out.println("loading file.......");
}
else
{
logger.info("The program is running.can not run again.");
}
}
/**
* 注册需要监听的文件(从配置文件读取配置信息)
*/
public void regFileListener()
{
Properties prop = loadProperties();
String num = prop.getProperty("ListenerFileNum");
System.out.println(num);
String Interval = prop.getProperty("Interval");
if (("".equals(num)) || (null == num) || ("".equals(Interval))
|| (null == Interval))
{
logger.error("Configuration error,please check it! Inteval:"
+ Interval + ",ListenerFileNum:" + num);
}
else
{
int number = Integer.parseInt(num);
int t = Integer.parseInt(Interval);
for (int i = 0; i < number; i++)
{
String filePath = prop.getProperty("ListenerFile" + (i + 1));
if (filePath != null)
{
if (filePath.endsWith(File.separator))
{
regBatchFile(filePath, t);
}
else
{
regSingleFile(filePath, t);
}
}
else
{
logger.error("Configuration item[LitenerFileNum]" + (i + 1)
+ "[does not exist.ignore]...");
}
}
}
}
public void regBatchFile(String path,int interval)
{
File file=new File(path);
if(file.isDirectory())
{
//file.list()--->得到这个目录下的所有文件名 数组
for(String s:file.list())
{
String filePath=new StringBuffer(path).append(s).toString();
regSingleFile(filePath,interval);
}
}
}
public void regSingleFile(String path,int interval)
{
FileListener fileTask=null;
if(path.endsWith(".properties"))
{
fileTask=new PropertyFileListener(path);
}
else if(path.endsWith(".xml"))
{
fileTask=new XMLFileListener(path);
}
else
{
return;
}
fileTask.loadFile();
Timer timer=new Timer();
timer.schedule(fileTask, new Date(), interval);
String fileName=path.substring(path.lastIndexOf(File.separator)+1, path.length());
FMap.put(fileName, fileTask);
}
{
// 注册的监听文件列表
private static HashMap<String, FileListener> FMap = new HashMap<String, FileListener>();
// 配置文件路径(从命令行传入)
private String configFile = null;
// 默认配置文件(从JAR包中加载)
private String defaultConfigFile = "FileListener.properties";
private static Logger logger = Logger.getLogger(FileManager.class);
public static void main(String[] args)
{
FileManager fm = new FileManager();
if (args.length > 0)
{
fm.configFile = args[1];
}
if (FMap.size() == 0)
{
fm.regFileListener();
logger.info("Execute Command sucess.");
System.out.println("loading file.......");
}
else
{
logger.info("The program is running.can not run again.");
}
}
/**
* 注册需要监听的文件(从配置文件读取配置信息)
*/
public void regFileListener()
{
Properties prop = loadProperties();
String num = prop.getProperty("ListenerFileNum");
System.out.println(num);
String Interval = prop.getProperty("Interval");
if (("".equals(num)) || (null == num) || ("".equals(Interval))
|| (null == Interval))
{
logger.error("Configuration error,please check it! Inteval:"
+ Interval + ",ListenerFileNum:" + num);
}
else
{
int number = Integer.parseInt(num);
int t = Integer.parseInt(Interval);
for (int i = 0; i < number; i++)
{
String filePath = prop.getProperty("ListenerFile" + (i + 1));
if (filePath != null)
{
if (filePath.endsWith(File.separator))
{
regBatchFile(filePath, t);
}
else
{
regSingleFile(filePath, t);
}
}
else
{
logger.error("Configuration item[LitenerFileNum]" + (i + 1)
+ "[does not exist.ignore]...");
}
}
}
}
public void regBatchFile(String path,int interval)
{
File file=new File(path);
if(file.isDirectory())
{
//file.list()--->得到这个目录下的所有文件名 数组
for(String s:file.list())
{
String filePath=new StringBuffer(path).append(s).toString();
regSingleFile(filePath,interval);
}
}
}
public void regSingleFile(String path,int interval)
{
FileListener fileTask=null;
if(path.endsWith(".properties"))
{
fileTask=new PropertyFileListener(path);
}
else if(path.endsWith(".xml"))
{
fileTask=new XMLFileListener(path);
}
else
{
return;
}
fileTask.loadFile();
Timer timer=new Timer();
timer.schedule(fileTask, new Date(), interval);
String fileName=path.substring(path.lastIndexOf(File.separator)+1, path.length());
FMap.put(fileName, fileTask);
}