一、 创建Plug-in Project工程,名为“PluginMgrCenter”,主要实现定时监控给定文件夹下jar包,对未加载的jar包进行加载
1、创建工程
2、工程结构如下图示意:
3、实现Activator类的方法,Activator类的作用,说白了就是该Bundle启动,停止会调用该类覆写的start(),stop()方法。
Activator类的start()方法中,保存osgi框架传进来的BundleContext对象,并启动一个定时任务。
代码如下:
package pluginmgrcenter;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import directwatcher.DirectWatcherTask;
import directwatcher.ScheduleTimerPool;
public class Activator implements BundleActivator
{
private static BundleContext bundleContext;
public static BundleContext getBundleContext()
{
return bundleContext;
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
* )
*/
public void start(BundleContext context) throws Exception
{
bundleContext = context;
try
{
DirectWatcherTask directWatcherTask = new DirectWatcherTask(
bundleContext, "d:\\plugin\\");
ScheduleTimerPool.getInstance().schedule(directWatcherTask, 10000,
10000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception
{
System.out.println("Bye World!");
}
}
4、ScheduleTimerPool.java类定义了一个简单的定时器。用于定时执行DirectWatcherTask.java中的jar包扫描任务。
package directwatcher;
import java.util.Timer;
import java.ut