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

scouter.server.plugin.PlugInLoader Maven / Gradle / Ivy

There is a newer version: 2.20.0
Show newest version
/*
 *  Copyright 2015 the original author or authors. 
 *  @https://github.com/scouter-project/scouter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *
 */
package scouter.server.plugin;

import javassist.*;
import scouter.lang.pack.*;
import scouter.server.Configure;
import scouter.server.Logger;
import scouter.util.*;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;

public class PlugInLoader extends Thread {
	private static PlugInLoader instance;
	public synchronized static PlugInLoader getInstance() {
		if (instance == null) {
			instance = new PlugInLoader();
			instance.setDaemon(true);
			instance.setName("PluginLoader");
			instance.start();
		}
		return instance;
	}
	public void run() {
		while (true) {
			ThreadUtil.sleep(5000);
			try {
				File root = new File(Configure.getInstance().plugin_dir);
				reloadIfModified(root);
			} catch (Throwable t) {
				t.printStackTrace();
			}
		}
	}
	private void reloadIfModified(File root) {
		File scriptFile = new File(root, "alert.plug");
		if (scriptFile.canRead() == false) {
			PlugInManager.alerts = null;
		} else {
			if (PlugInManager.alerts == null || PlugInManager.alerts.__lastModified != scriptFile.lastModified()) {
				PlugInManager.alerts = (IAlert) create(scriptFile, "AlertImpl", IAlert.class, AlertPack.class);
			}
		}
		scriptFile = new File(root, "counter.plug");
		if (scriptFile.canRead() == false) {
			PlugInManager.counters = null;
		} else {
			if (PlugInManager.counters == null || PlugInManager.counters.__lastModified != scriptFile.lastModified()) {
				PlugInManager.counters = (ICounter) create(scriptFile, "CounterImpl", ICounter.class, PerfCounterPack.class);
			}
		}
		scriptFile = new File(root, "object.plug");
		if (scriptFile.canRead() == false) {
			PlugInManager.objects = null;
		} else {
			if (PlugInManager.objects == null || PlugInManager.objects.__lastModified != scriptFile.lastModified()) {
				PlugInManager.objects = (IObject) create(scriptFile, "ObjectImpl", IObject.class, ObjectPack.class);
			}
		}
		scriptFile = new File(root, "xlog.plug");
		if (scriptFile.canRead() == false) {
			PlugInManager.xlog = null;
		} else {
			if (PlugInManager.xlog == null || PlugInManager.xlog.__lastModified != scriptFile.lastModified()) {
				PlugInManager.xlog = (IXLog) create(scriptFile, "XLogImpl", IXLog.class, XLogPack.class);
			}
		}
		scriptFile = new File(root, "xlogdb.plug");
		if (scriptFile.canRead() == false) {
			PlugInManager.xlogdb = null;
		} else {
			if (PlugInManager.xlogdb == null || PlugInManager.xlogdb.__lastModified != scriptFile.lastModified()) {
				PlugInManager.xlogdb = (IXLog) create(scriptFile, "XLogDBImpl", IXLog.class, XLogPack.class);
			}
		}
		scriptFile = new File(root, "xlogprofile.plug");
		if (scriptFile.canRead() == false) {
			PlugInManager.xlogProfiles = null;
		} else {
			if (PlugInManager.xlogProfiles == null || PlugInManager.xlogProfiles.__lastModified != scriptFile.lastModified()) {
				PlugInManager.xlogProfiles = (IXLogProfile) create(scriptFile, "XLogProfileImpl", IXLogProfile.class,
						XLogProfilePack.class);
			}
		}
		scriptFile = new File(root, "summary.plug");
		if (scriptFile.canRead() == false) {
			PlugInManager.summary = null;
		} else {
			if (PlugInManager.summary == null || PlugInManager.summary.__lastModified != scriptFile.lastModified()) {
				PlugInManager.summary = (ISummary) create(scriptFile, "SummaryImpl", ISummary.class,
						SummaryPack.class);
			}
		}
	}

	// Do Not retry compiling the file failed to compile before to prevent compiling broken files permanently.
	private LongSet compileErrorFiles = new LongSet();
	private IPlugIn create(File file, String className, Class superClass, Class paramClass) {
		long fileSignature = fileSign(file);
		if (compileErrorFiles.contains(fileSignature))
			return null;
		try {
			String methodName = "process";
			String superName = superClass.getName();
			String signature = "(" + nativeName(paramClass) + ")V";
			String parameter = paramClass.getName();
			String body = new String(FileUtil.readAll(file));
			ClassPool cp = ClassPool.getDefault();
//			String jar = FileUtil.getJarFileName(IAlert.class);
//			if (jar != null) {
//				cp.appendClassPath(jar);
//			}
			//클래스 패스를 자동으로 잡아주도록 수정함, 사용자가 추가한 jar도 자동인식하도록 
			if(this.getClass().getClassLoader() instanceof URLClassLoader){
				URLClassLoader u = (URLClassLoader)this.getClass().getClassLoader();
				URL[] urls = u.getURLs();
				for(int i = 0; urls!=null && i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy