package com.me;
public class Check {
private static final String path = "D://WorkSpace//ec2";
/**
* @param args
* @throws WrongPathException
*/
public static void main(String[] args) throws WrongPathException {
CheckBundle cb = new CheckBundle();
cb.checkAll(path);
}
}
-----------------------------------------------------------------------------------------
package com.me;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CheckBundle {
String[] str_need = {"service","templates","metadata","packages"};
String[] str_needfile = {"csd","ttd","ntd","gz","zip"};
private Map<String, Boolean> mp_dir = new HashMap<String, Boolean>();
private Map<String, Boolean> mp_file = new HashMap<String, Boolean>();
private List<String> lt_soft = new ArrayList<String>();
private List<String> lt_meta = new ArrayList<String>();
public CheckBundle() {
// TODO Auto-generated constructor stub
for(String st : str_need){
mp_dir.put(st, false);
}
for(String str : str_needfile){
mp_file.put(str, false);
}
}
public void checkAll(String path) throws WrongPathException
{
check(path);
checkResult();
}
public void check(String path) throws WrongPathException {
File file = new File(path);
String[] files = file.list();
for (String fileOrdir : files) {
File file_t = new File(path+"//"+fileOrdir);
if (file_t.isDirectory()) {
checkDirIsNeeded(path,fileOrdir);
check(path+"//"+fileOrdir);
} else {
checkFileIsNeeded(path,fileOrdir);
}
}
}
public void checkDirIsNeeded(String path,String dir) throws WrongPathException
{
if(mp_dir.containsKey(dir))
{
if(dir.equals("metadata") && !path.endsWith("service"))
{
throw new WrongPathException(dir);
}
if(dir.equals("packages") && !path.endsWith("service"))
{
throw new WrongPathException(dir);
}
mp_dir.remove(dir);
mp_dir.put(dir, true);
}
}
public void checkFileIsNeeded(String path,String fileName) throws WrongPathException
{
for(String str : str_needfile)
{
if(fileName.endsWith(str))
{
if(fileName.endsWith("csd") && !path.endsWith("service"))
{
throw new WrongPathException(fileName);
}
if(fileName.endsWith("ttd") && !path.endsWith("templates"))
{
throw new WrongPathException(fileName);
}
if(fileName.endsWith("ntd") && !path.endsWith("templates"))
{
throw new WrongPathException(fileName);
}
if(fileName.endsWith("gz") && !path.endsWith("packages"))
{
throw new WrongPathException(fileName);
}else if(fileName.endsWith("gz") && path.endsWith("packages")){
String[] ff = fileName.split("-");
fileName = ff[0];
lt_soft.add(fileName);
}
if(fileName.endsWith("zip") && !path.endsWith("metadata"))
{
throw new WrongPathException(fileName);
}else if(fileName.endsWith("zip") && path.endsWith("metadata")){
String[] ff = fileName.split("-");
fileName = ff[0];
lt_meta.add(fileName);
}
mp_file.remove(str);
mp_file.put(str, true);
}
}
}
public void checkResult()
{
java.util.Iterator<String> it_dir = mp_dir.keySet().iterator();
while(it_dir.hasNext())
{
String ss = it_dir.next();
boolean bol = mp_dir.get(ss);
if(!bol)
{
System.out.println("缺少必要的目录:"+ss);
}
}
Set<String> set_file = mp_file.keySet();
Iterator<String> it_file = set_file.iterator();
while(it_file.hasNext())
{
String ss = it_file.next();
boolean bol = mp_file.get(ss);
if(!bol)
{
System.out.println("缺少必要的文件:"+ss);
}
}
int n = lt_soft.size();
for(int i=0; i< n; i++)
{
String str_soft = lt_soft.get(i);
// System.out.println("111"+str_soft);
// System.out.println("222"+lt_meta.get(0));
String str_meta = str_soft+"_meta";
if(!lt_meta.contains(str_meta))
{
System.out.println("软件:"+str_soft+" 缺少对应的元数据包:"+str_meta);
}
}
}
}
--------------------------------------------------------------
package com.me;
public class WrongPathException extends Exception {
/**
*
*/
private static final long serialVersionUID = 2468393428172451669L;
public WrongPathException(String message) {
System.out.println(message+" 文件或文件夹处于不正确的位置!");
}
}