001/* 002 * (C) Copyright 2007-2010 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Florent Guillaume 016 */ 017package org.nuxeo.ecm.core.scheduler; 018 019import javax.security.auth.login.LoginContext; 020import javax.security.auth.login.LoginException; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.core.api.impl.UserPrincipal; 025import org.nuxeo.ecm.core.event.Event; 026import org.nuxeo.ecm.core.event.EventContext; 027import org.nuxeo.ecm.core.event.EventService; 028import org.nuxeo.ecm.core.event.impl.EventContextImpl; 029import org.nuxeo.ecm.core.event.impl.EventImpl; 030import org.nuxeo.runtime.api.Framework; 031import org.nuxeo.runtime.api.login.LoginAs; 032import org.nuxeo.runtime.transaction.TransactionHelper; 033import org.quartz.Job; 034import org.quartz.JobDataMap; 035import org.quartz.JobExecutionContext; 036import org.quartz.JobExecutionException; 037 038/** 039 * A Quartz job whose execution sends a configured event. 040 */ 041public class EventJob implements Job { 042 043 private static final Log log = LogFactory.getLog(EventJob.class); 044 045 /** 046 * Job execution to send the configured event. 047 */ 048 @Override 049 public void execute(JobExecutionContext context) throws JobExecutionException { 050 JobDataMap dataMap = context.getJobDetail().getJobDataMap(); 051 052 // switch to the Nuxeo classloader so that the event listeners 053 // work as usual 054 055 ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 056 ClassLoader nuxeoCL = getClass().getClassLoader(); 057 Thread.currentThread().setContextClassLoader(nuxeoCL); 058 try { 059 execute(dataMap); 060 } catch (LoginException e) { 061 String eventId = dataMap.getString("eventId"); 062 log.error("Error while processing scheduled event id: " + eventId, e); 063 } finally { 064 Thread.currentThread().setContextClassLoader(oldCL); 065 } 066 } 067 068 @SuppressWarnings("unchecked") 069 protected void execute(JobDataMap dataMap) throws LoginException { 070 String eventId = dataMap.getString("eventId"); 071 String eventCategory = dataMap.getString("eventCategory"); 072 String username = dataMap.getString("username"); 073 074 SchedulerService scheduler = Framework.getLocalService(SchedulerService.class); 075 if (scheduler == null || !scheduler.hasApplicationStarted()) { 076 // too early 077 return; 078 } 079 080 EventService eventService = Framework.getService(EventService.class); 081 if (eventService == null) { 082 log.error("Cannot find EventService"); 083 return; 084 } 085 086 LoginContext loginContext = null; 087 try { 088 // login 089 if (username == null) { 090 loginContext = Framework.login(); 091 } else { 092 if (Framework.getLocalService(LoginAs.class) != null) { 093 loginContext = Framework.loginAsUser(username); 094 } else if (!Framework.isTestModeSet()) { 095 log.error("LoginAs service not available"); 096 } 097 } 098 099 // set up event context 100 UserPrincipal principal = new UserPrincipal(username, null, false, false); 101 EventContext eventContext = new EventContextImpl(null, principal); 102 eventContext.setProperty("category", eventCategory); 103 eventContext.setProperties(dataMap); 104 Event event = new EventImpl(eventId, eventContext); 105 106 // start transaction 107 boolean tx = TransactionHelper.startTransaction(); 108 109 // send event 110 log.debug("Sending scheduled event id=" + eventId + ", category=" + eventCategory + ", username=" 111 + username); 112 boolean ok = false; 113 try { 114 eventService.fireEvent(event); 115 ok = true; 116 } finally { 117 if (tx) { 118 if (!ok) { 119 TransactionHelper.setTransactionRollbackOnly(); 120 } 121 TransactionHelper.commitOrRollbackTransaction(); 122 } 123 } 124 } finally { 125 // logout 126 if (loginContext != null) { 127 loginContext.logout(); 128 } 129 } 130 } 131 132}