OpenCms Listener 监听器的使用

如果想在OpenCms发布数据的同时do something怎么办呢?你可以使用PublishListener,publish事件监听器,OpenCms有事件的概念,有点类似javascript的listener。下面我就教你怎么写一个完整的Publish监听器。

 

第一步:创建一个ActionClass继承org.opencms.module.I_CmsModuleAction

import org.opencms.configuration.CmsConfigurationManager;
import org.opencms.db.CmsPublishList;
import org.opencms.file.CmsObject;
import org.opencms.main.CmsEvent;
import org.opencms.main.OpenCms;
import org.opencms.module.CmsModule;
import org.opencms.module.I_CmsModuleAction;
import org.opencms.report.I_CmsReport;

public class ActionClass implements I_CmsModuleAction {

	public void initialize(CmsObject cmso, CmsConfigurationManager arg1,
			CmsModule arg2) {
		OpenCms.getPublishManager().addPublishListener(new MyPublishListener(cmso));
	}

	public void moduleUninstall(CmsModule arg0) {
		// TODO Auto-generated method stub

	}

	public void moduleUpdate(CmsModule arg0) {
		// TODO Auto-generated method stub

	}

	public void publishProject(CmsObject arg0, CmsPublishList arg1, int arg2,
			I_CmsReport arg3) {
		// TODO Auto-generated method stub

	}

	public void shutDown(CmsModule arg0) {
		// TODO Auto-generated method stub

	}

	public void cmsEvent(CmsEvent arg0) {
		// TODO Auto-generated method stub

	}

}

 第二步:创建MyPublishListener继承org.opencms.publish.I_CmsPublishEventListener

import org.opencms.db.CmsPublishList;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.types.CmsResourceTypeXmlContent;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.publish.CmsPublishJobBase;
import org.opencms.publish.CmsPublishJobEnqueued;
import org.opencms.publish.CmsPublishJobFinished;
import org.opencms.publish.CmsPublishJobRunning;
import org.opencms.publish.I_CmsPublishEventListener;
import org.opencms.util.CmsUUID;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;

public class MyPublishListener implements I_CmsPublishEventListener{

	private CmsObject cmso;
	
	public MyPublishListener(CmsObject cmso){
		this.cmso = cmso;
	}
	
	public void onAbort(CmsUUID arg0, CmsPublishJobEnqueued arg1) {
		//do something on abort
	}

	public void onEnqueue(CmsPublishJobBase arg0) {
		//do something on file insert into publish queue
	}

	public void onFinish(CmsPublishJobRunning publishJob) {
		//do something on publish finished
	}

	public void onRemove(CmsPublishJobFinished arg0) {
		//do something on publish removed
	}

	public void onStart(CmsPublishJobEnqueued arg0) {
		//do something on publish start
	}

}

 一般自定义代码写在onFinish方法里面,OpenCms每发布一个文件就执行一次onFinish,所以在onfinish里面获得CmsResource就是当前发布的文件。

以下是在onFinish里面获得CmsResource的代码:

CmsPublishList publishList = publishJob.getPublishList();
		List fileList = publishList.getFileList();
		Iterator it = fileList.iterator();
		while(it.hasNext()){
			CmsResource resource = (CmsResource) it.next();
}

第三步,进入view=Administration,进入Module Manage,选择你自己的module,选择Edit Module,在ActionClass里面写入你自己的ActionClass(包名+ClassName),然后再onFinish方法里面加入一些输出代码,重启Tomcat,试着发布一个文件,看看有没有效果?

 

 

你可能感兴趣的:(JavaScript,tomcat,xml,jsp)