All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ff.prac.jobs.JobMap Maven / Gradle / Ivy

There is a newer version: 0.0.4
Show newest version
/*
 * Copyright (C) 2015-2018 XinZhenfeng 
 * 
 * This file is part of prac
 * 
 * prac is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */

package ff.prac.jobs;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import ff.prac.jobs.annotation.Alias;

public final class JobMap {

	private HashMap jobmap = new HashMap();
	
	public JobMap() {
		
	}
	
	public void put(Class cls) throws DuplicateAliasException {
		this.putOrNot(cls);
	}
	
	private synchronized void putOrNot(Class cls) throws DuplicateAliasException {
		Alias jobano = cls.getAnnotation(Alias.class);
		if(jobano != null 
		&& jobano.value() != null 
		&& jobano.value().trim().length() > 0
		) {
			String alias = jobano.value().trim();
			String clsname = cls.getName();
			if(jobmap.containsKey(alias) && !clsname.equals(jobmap.get(alias))) {
				throw new DuplicateAliasException(alias, jobmap.get(alias), clsname);
			} else if(!jobmap.containsKey(alias)) {
				jobmap.put(alias, clsname);
			}			
		}
	}

	public String getClassName(String alias) {
		return jobmap.get(alias);
	}

	public synchronized void lookupJobs(String path, boolean recursive, String ... pkgs) throws DuplicateAliasException, IOException {
		
		if(path==null || path.trim().length()==0) {
			return;
		}

		if(path.toLowerCase().endsWith(".jar")) {
			ZipFile zfile = new ZipFile(path);
			this.lookupInJar(zfile, recursive, pkgs);
		} else {
			if(pkgs==null || pkgs.length==0) {
				this.lookupInDir(path, recursive, null);
			} else {
				String dir = (path.endsWith("/") || path.endsWith("\\")) ? path.substring(0, path.length()-1) : path;
				for(int i=0; i en = zfile.entries();
		while(en.hasMoreElements()) {
			ZipEntry ze = en.nextElement();
			if ( ! ze.isDirectory() ) {
				String name = ze.getName();
				if(name.toLowerCase().endsWith(".class")) {
					String clsname = name.substring(0, name.length()-6).replace('/', '.').replace('\\', '.');
					int idx = clsname.lastIndexOf('.');
					String clspkg = idx==-1 ? "" : clsname.substring(0, idx);
					for(int i=0; i cls = Class.forName(clsname);
								this.putOrNot(cls);
							} catch (ClassNotFoundException e) {
								continue;
							}
						}
					}
				}
			}
		}
	}
	
	private void lookupInDir(String dir, boolean recursive, String pkg) throws DuplicateAliasException {

		File fdir = new File(dir);
		if ( ! fdir.isDirectory()) {
			return;
		}
		
		File[] list = fdir.listFiles();
		
		for(int i=0; i cls = Class.forName(clsname);
					this.putOrNot(cls);
				} catch (ClassNotFoundException e) {
					continue;
				}
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy