⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 managerutil.java

📁 实现了从Google
💻 JAVA
字号:
package com.ct.hotweb.web;

import java.util.*;
import javax.servlet.*;

import org.apache.commons.logging.*;

import com.ct.hotweb.bean.*;
import com.ct.hotweb.dao.*;
import com.ct.hotweb.util.*;

/**
 * 后台管理支持类
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public class ManagerUtil {
	private static Log log = LogFactory.getLog(ManagerUtil.class);
	private static final String TASK_NAME_PREFIX = "/gen/websit/";

	public static List getPath(String path) {
		ArrayList list = new ArrayList();
		if (path == null || (path = path.trim()).equals("")) {
			return list;
		}

		try {
			StringTokenizer st =
				new StringTokenizer(path, ConstParameter.SEARCH_KEY_SEPARATOR);
			CategoryDAO catDao = new CategoryDAO();
			while (st.hasMoreTokens()) {
				String str = st.nextToken().trim();
				Category cat = catDao.getCategory(str);
				list.add(cat);
			}
		} catch (Exception e) {
			log.error(
				"get path error. path str="
					+ path
					+ "\tdetail:"
					+ e.getMessage());
		}
		return list;
	}

	public static List getSubCategory(Category currentCat) {
		List list = null;
		try {
			CategoryDAO catDao = new CategoryDAO();
			String id = (currentCat == null) ? "" : "" + currentCat.getId();
			list = catDao.getSubCategory(id);
		} catch (Exception e) {
			log.error(
				"get sub category error. cat id="
					+ currentCat.getId()
					+ "\tdetail:"
					+ e.getMessage());
		}
		return list;
	}

	public static List getSubSearchKey(Category currentCat) {
		List list = null;
		if (currentCat == null || currentCat.getId() == 0) {
			return new ArrayList();
		}
		try {
			CategoryDAO catDao = new CategoryDAO();
			list = catDao.getSubSearchKey("" + currentCat.getId());
		} catch (Exception e) {
			log.error(
				"get sub category error. cat id="
					+ currentCat.getId()
					+ "\tdetail:"
					+ e.getMessage());
		}
		return list;
	}

	public static void addSubSearchKey(
		Category cat,
		String keyNames,
		String separator) {
		if (keyNames == null || (keyNames = keyNames.trim()).equals("")) {
			return;
		}

		try {
			StringTokenizer st = new StringTokenizer(keyNames, separator);

			SearchKeyDAO keyDAO = new SearchKeyDAO();
			while (st.hasMoreTokens()) {
				String str = st.nextToken().trim();
				if (str.equals(""))
					continue;
				keyDAO.createSearchKey(cat, str, "");
			}
		} catch (Exception e) {
			log.error(
				"get path error. path cat id="
					+ cat.getId()
					+ "\tdetail:"
					+ e.getMessage());
		}

	}

	public static void addSubCategory(
		Category cat,
		String catNames,
		String separator) {
		if (catNames == null || (catNames = catNames.trim()).equals("")) {
			return;
		}

		try {
			StringTokenizer st = new StringTokenizer(catNames, separator);
			CategoryDAO catDao = new CategoryDAO();
			while (st.hasMoreTokens()) {
				String str = st.nextToken().trim();
				if (str.equals(""))
					continue;
				catDao.createSubCategory(cat, str, "");
			}
		} catch (Exception e) {
			log.error(
				"get path error. path cat id="
					+ cat.getId()
					+ "\tdetail:"
					+ e.getMessage());
		}

	}

	public static Category getCategory(String id) {
		try {
			CategoryDAO catDAO = new CategoryDAO();
			return catDAO.getCategory(id);
		} catch (Exception e) {
			log.error("get category error: " + e.getMessage());
		}
		return null;
	}

	public static void deleteCategory(String id) {

		try {
			if (id == null)
				return;
			CategoryDAO catDAO = new CategoryDAO();
			Category cat = catDAO.getCategory(id);
			if (cat != null)
				catDAO.deleteCategory(cat);

		} catch (Exception e) {
			log.error("delete category error." + e.getMessage());
		}
	}

	public static void generateCategory(String id, ServletContext application, String strCount)
		throws HotWebException {
		String path = "";
		try {
			if (id == null)
				id = "";
			CategoryDAO catDAO = new CategoryDAO();
			SearchKeyDAO keyDAO = new SearchKeyDAO();
			Category cat = catDAO.getCategory(id);

			if (ManagerUtil
				.existCorrelativeTask(
					application,
					ManagerUtil.getAttributeTaskName(
						application,
						cat,
						false,
						-1)))
				throw new HotWebException("已经存在相关的抓取任务,请等待.");
			int threadCount = 10;
			int dummy;
			if ((dummy = Integer.parseInt(strCount)) > 0) threadCount = dummy;
			
			List cats = catDAO.getAllInnerCategory(cat);
			List keys = catDAO.getAllInnerSearchKey(cat);

			//更新任务表
			catDAO.refreshCategoryError(cat);
			catDAO.refreshSearchKeyError(cat);

			List pCats = Utils.averageList(cats, threadCount);
			List pKeys = Utils.averageList(keys, threadCount);

			for (int i = 0; i < threadCount; i++) {
				SearchKeyStack stack = new SearchKeyStack();
				stack.setObjects(new ArrayList((List) pKeys.get(i)));
				application.setAttribute(
					ManagerUtil.getAttributeTaskName(
						application,
						cat,
						false,
						i),
					stack);
				new GenerateThread(null, (List) pCats.get(i), stack).start();
				Thread.sleep(1860);
			}
		} catch (HotWebException hwe) {
			log.info(hwe.getMessage());
			throw hwe;
		} catch (Exception e) {
			log.error("generate error." + e.getMessage());
		} finally {
		}

	}
	
	public static void addSearchKeyFromWWW(String catId) {
		Category cat = getCategory(catId);
		if (cat == null) return;
		ResourceManager.getGenManager().workSearchKey(cat);
	}
	
	public static void addSubCategoryFromWWW(String catId) {
		Category cat = getCategory(catId);
		if (cat == null) return;
		ResourceManager.getGenManager().workCategory(cat);		
	}

	public static void regenerateErrors(ServletContext application, String strCount) {
		try {
			CategoryDAO catDAO = new CategoryDAO();
			SearchKeyDAO keyDAO = new SearchKeyDAO();
			Enumeration e = application.getAttributeNames();

			if (ManagerUtil
				.existCorrelativeTask(
					application,
					ManagerUtil.getAttributeTaskName(
						application,
						null,
						true,
						-1)))
				throw new HotWebException("已经存在相关的抓取任务,请等待.");
			List categorys = catDAO.getAllGenErrorCategory();
			List keys = keyDAO.getAllGenErrorSearchKey();

			//开启15线程
			int threadCount = 10;
			int dummy;
			if ((dummy = Integer.parseInt(strCount)) > 0) threadCount = dummy;
			 
			List pCats = Utils.averageList(categorys, threadCount);
			List pKeys = Utils.averageList(keys, threadCount);

			for (int i = 0; i < threadCount; i++) {
				SearchKeyStack stack = new SearchKeyStack();
				stack.setObjects(new ArrayList((List) pKeys.get(i)));
				application.setAttribute(
					ManagerUtil.getAttributeTaskName(
						application,
						null,
						true,
						i),
					stack);
				new GenerateThread(null, (List) pCats.get(i), stack).start();
				Thread.sleep(1860);
			}
		} catch (HotWebException hwe) {
			log.info(hwe.getMessage());
		} catch (Exception e) {
			log.error("generate error." + e.getMessage());
		}
	}

	public static void generateMap(ServletContext application) {
		if (application.getAttribute("genmap") != null) {
			return;
		} else {
			application.setAttribute("genmap", new Object());
			ResourceManager.getGenManager().generateMap();
			application.setAttribute("genmap", null);
		}
	}

	public static int getErrorCountOfCategory() throws Exception {
		return new CategoryDAO().getCountGenErrorSearchKey();
	}

	public static int getErrorCountOfSearchKey() throws Exception {
		return new SearchKeyDAO().getCountGenErrorSearchKey();
	}

	public static void deleteSearchKey(String id) throws Exception {
		new SearchKeyDAO().deleteSearchKey(id);
	}

	public static void deleteError() throws Exception {
		new SearchKeyDAO().clearGenSearchKeyError();
		new CategoryDAO().clearGenCategoryError();
	}

	public static String getAttributeTaskName(
		ServletContext application,
		Category cat,
		boolean error,
		int index) {
		String name = "";
		if (!error)
			name = TASK_NAME_PREFIX + (cat == null ? "" : cat.getPath());
		else
			name = TASK_NAME_PREFIX + "-1/";
		if (index < 0)
			return name;
		else
			return name + "subtask" + index + "/";
	}

	//获取所有任务信息
	public static List getAllTaskInfo(ServletContext application) {
		List names = getAllTaskName(application);
		List infos = new ArrayList();
		for (int i = 0; i < names.size(); i++) {
			String name = (String) names.get(i);

			SearchKeyStack stack =
				(SearchKeyStack) application.getAttribute(name);
			if (stack == null)
				continue;
			List info = new ArrayList();
			int startSize = stack.getStartSize();
			int current = stack.getSize();
			int count = (int) startSize - current;
			int per = count * 100 / startSize;
			long begin = stack.getStartTime();
			long now = System.currentTimeMillis();

			//名称
			info.add(name);
			//开始时间
			info.add(Utils.dateFormat(new Date(stack.getStartTime())));
			//关键字总数
			info.add("" + startSize);
			//已处理关键字
			info.add("" + count);
			//完成百分比
			info.add("" + per);
			//预计完成时间
			info.add(
				""
					+ (Utils
						.dateFormat(
							new Date(
								begin
									+ (now - begin)
										* 100
										/ ((per == 0) ? 1 : per)))));
			infos.add(info);
		}
		return infos;
	}

	//获取所有任务名
	public static List getAllTaskName(ServletContext application) {
		Enumeration e = application.getAttributeNames();
		List list = new ArrayList();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			if (name.startsWith(TASK_NAME_PREFIX)) {

				SearchKeyStack stack =
					(SearchKeyStack) application.getAttribute(name);
				if (stack.isEmpty() || stack.isKilled())
					application.removeAttribute(name);
				else
					list.add(name);
			}
		}
		return list;
	}

	//是否存在相关任务
	public static boolean existCorrelativeTask(
		ServletContext application,
		String taskName) {
		boolean result = false;
		List names = getAllTaskName(application);
		for (int i = 0; i < names.size(); i++) {
			String name = (String) names.get(i);
			if (name.startsWith(taskName) || taskName.startsWith(name))
				return true;
		}
		return result;
	}

	//取消任务
	public static void cancelTask(ServletContext application, String name) {
		SearchKeyStack stack = (SearchKeyStack) application.getAttribute(name);
		if (stack == null)
			return;
		stack.setKilled(true);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -