自动注册工厂实例

网络监控项目,初始化时需要对多个任务类注册到类工厂,方便工厂反射,但根据classloader规则,无法初始化与入口类无关的类,因此需要遍历类名,强制加载:

public class TaskRunnableFactory {
	
	public static abstract class TaskRunnable {
		protected RunStat runStat = RunStat.WAIT_FOR_RUN;
		protected Task task;
		protected Context context;
		
		protected void setTask(Context cont, Task task)
		{
			context = cont;
			this.task = task;
		}
		
		protected static HashMap<TaskType, String> prefixMap= new HashMap<TaskType, String>();
		
		public RunStat getRunStat() {
			return runStat;
		}
		
		public abstract void run();
		public abstract int result();
		public abstract String report();
	}
	
	static private HashMap<TaskType, Class<?>> runMap;
	
	public TaskRunnableFactory(Context cont)
	{
		try {
			String path = cont.getPackageResourcePath();
			DexFile df = new DexFile(path);
			for (Enumeration<String> iter = df.entries(); iter.hasMoreElements();) {
				String s = iter.nextElement();
				if (s.startsWith("jd.net.check.runnable.") && !s.contains("$")) {
					Class.forName(s);
				}
			}
		} catch (Exception e) {

		}
	}
	
	static public void registType(TaskType type, Class<?> runClass, String cmd){
		registType(type, runClass);
		TaskRunnable.prefixMap.put(type, cmd);
	}
	
	static public void registType(TaskType type, Class<?> runClass){
		if(runMap==null) runMap = new HashMap<TaskType, Class<?>>();
		runMap.put(type, runClass);
	}	

	public TaskRunnable createRunnalbe(Context cont, Task task) {
		try {
			Class<?> runnableClass = runMap.get(task.getType());
			if (runnableClass == null)
				return null;
			TaskRunnable run = (TaskRunnable) runnableClass.newInstance();
			run.setTask(cont, task);
			return run;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

public class CmdRunnable extends TaskRunnable {
	static {
		TaskRunnableFactory.registType(TaskType.CMD, CmdRunnable.class);
		TaskRunnableFactory.registType(TaskType.GET_GETPROP, CmdRunnable.class, "getprop");
		TaskRunnableFactory.registType(TaskType.GET_NETSTAT, CmdRunnable.class, "netstat");
		TaskRunnableFactory.registType(TaskType.GET_ROUTE, CmdRunnable.class, "cat /proc/net/route");
		TaskRunnableFactory.registType(TaskType.RUN_PS, CmdRunnable.class, "ps");
		TaskRunnableFactory.registType(TaskType.RUN_IFCONFIG, CmdRunnable.class, "ifconfig");
		TaskRunnableFactory.registType(TaskType.RUN_PING, CmdRunnable.class, "ping");
	}

	private ExecRes res = new ExecRes();
	
	@Override
	public void run() {
		runStat = RunStat.RUNNING;
		String cmd = String.format("%s %s", prefixMap.get(task.getType()), task.getCmd());
		SysUtils.exec(cmd, res);
		runStat = (res.ret<0)?RunStat.ERROR:RunStat.DONE;
	}

	@Override
	public int result() {
		return res.ret;
	}

	@Override
	public String report() {
		return res.output;
	}
}


C++ TcpSocket自动工厂类的实现可以参考以下代码: ```cpp #include <iostream> #include <map> #include <functional> #include <memory> #include <string> #include <stdexcept> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> #include <unistd.h> class TcpSocket { public: TcpSocket(int fd) : fd_(fd) { } int fd() const { return fd_; } ~TcpSocket() { if (fd_ != -1) { close(fd_); } } private: int fd_; }; class TcpServer { public: TcpServer() : fd_(socket(AF_INET, SOCK_STREAM, 0)) { if (fd_ == -1) { throw std::runtime_error("socket error"); } } void bind(const std::string& host, int port) { sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(host.c_str()); addr.sin_port = htons(port); if (::bind(fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) { throw std::runtime_error("bind error"); } } void listen() { if (::listen(fd_, 128) == -1) { throw std::runtime_error("listen error"); } } std::unique_ptr<TcpSocket> accept() { int client_fd = ::accept(fd_, nullptr, nullptr); if (client_fd == -1) { throw std::runtime_error("accept error"); } return std::make_unique<TcpSocket>(client_fd); } ~TcpServer() { if (fd_ != -1) { close(fd_); } } private: int fd_; }; class TcpSocketFactory { public: TcpSocketFactory() { register_creator("tcp", []() { return std::make_unique<TcpSocket>(); }); } void register_creator(const std::string& protocol, std::function<std::unique_ptr<TcpSocket>()> creator) { creators_[protocol] = std::move(creator); } std::unique_ptr<TcpSocket> create(const std::string& url) { size_t pos = url.find("://"); if (pos == std::string::npos) { throw std::runtime_error("invalid url"); } std::string protocol = url.substr(0, pos); auto it = creators_.find(protocol); if (it == creators_.end()) { throw std::runtime_error("unsupported protocol"); } return it->second(); } private: std::map<std::string, std::function<std::unique_ptr<TcpSocket>()>> creators_; }; int main() { TcpSocketFactory factory; // 注册TcpSocket的创建函数 factory.register_creator("tcp", []() { return std::make_unique<TcpSocket>(); }); // 使用TcpSocketFactory创建TcpServer TcpServer server; server.bind("127.0.0.1", 12345); server.listen(); while (true) { std::unique_ptr<TcpSocket> client = server.accept(); std::string url = "tcp://127.0.0.1:12345"; // 假设这是客户端连接的url std::unique_ptr<TcpSocket> socket = factory.create(url); std::cout << "client connected" << std::endl; } return 0; } ``` 上述代码中,`TcpSocketFactory` 实现了一个自动工厂类,可以根据不同的协议创建不同的 `TcpSocket` 对象。在 `TcpSocketFactory` 中,我们使用 `std::map` 存储了不同协议对应的创建函数,然后在 `create` 方法中根据传入的 url 解析协议,调用对应的创建函数创建 `TcpSocket` 对象。 在 `main` 函数中,我们首先需要实例化一个 `TcpSocketFactory` 对象,并注册 `TcpSocket` 的创建函数。然后,我们创建了一个 `TcpServer` 对象,并调用 `accept` 方法接受客户端连接。在连接建立后,我们可以使用 `TcpSocketFactory` 创建新的 `TcpSocket` 对象来与客户端通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值