001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.job.support;
025:
026: import org.riotfamily.riot.dao.RiotDao;
027: import org.riotfamily.riot.job.Job;
028: import org.riotfamily.riot.job.JobContext;
029: import org.riotfamily.riot.job.JobCreationException;
030: import org.riotfamily.riot.job.JobDescription;
031: import org.riotfamily.riot.job.JobInterruptedException;
032: import org.springframework.beans.factory.BeanNameAware;
033: import org.springframework.transaction.PlatformTransactionManager;
034: import org.springframework.transaction.TransactionDefinition;
035: import org.springframework.transaction.TransactionStatus;
036: import org.springframework.transaction.support.DefaultTransactionDefinition;
037:
038: public abstract class AbstractJob implements Job, BeanNameAware {
039:
040: private static final DefaultTransactionDefinition TRANSACTION_DEFINITION = new DefaultTransactionDefinition(
041: TransactionDefinition.PROPAGATION_REQUIRED);
042:
043: private String beanName;
044:
045: private RiotDao dao;
046:
047: private PlatformTransactionManager transactionManager;
048:
049: public void setDao(RiotDao dao) {
050: this .dao = dao;
051: }
052:
053: protected RiotDao getDao() {
054: return this .dao;
055: }
056:
057: public void setTransactionManager(
058: PlatformTransactionManager transactionManager) {
059: this .transactionManager = transactionManager;
060: }
061:
062: public boolean isConcurrent() {
063: return false;
064: }
065:
066: public boolean isRepeatable() {
067: return true;
068: }
069:
070: public void setBeanName(String beanName) {
071: this .beanName = beanName;
072: }
073:
074: public JobDescription setup(final String objectId) {
075: JobDescription jd = null;
076: if (transactionManager != null) {
077: TransactionStatus status = transactionManager
078: .getTransaction(TRANSACTION_DEFINITION);
079: try {
080: jd = setupInternal(objectId);
081: } catch (Exception e) {
082: transactionManager.rollback(status);
083: }
084: transactionManager.commit(status);
085: } else {
086: jd = setupInternal(objectId);
087: }
088: return jd;
089: }
090:
091: protected final JobDescription setupInternal(String objectId) {
092: JobDescription jd = new JobDescription();
093: jd.setName(beanName);
094: try {
095: initDescription(jd, loadObject(objectId));
096: } catch (Exception e) {
097: throw new JobCreationException("Job creation failed.", e);
098: }
099: return jd;
100: }
101:
102: protected void initDescription(JobDescription jd, Object entity)
103: throws Exception {
104: }
105:
106: public final void execute(final JobContext context) {
107: if (transactionManager != null) {
108:
109: TransactionStatus status = transactionManager
110: .getTransaction(TRANSACTION_DEFINITION);
111: try {
112: execute(context, loadObject(context.getObjectId()));
113: } catch (JobInterruptedException e) {
114: } catch (Exception e) {
115: transactionManager.rollback(status);
116: }
117: transactionManager.commit(status);
118: } else {
119: execute(context, loadObject(context.getObjectId()));
120: }
121: }
122:
123: protected Object loadObject(String objectId) {
124: if (objectId != null) {
125: return dao.load(objectId);
126: }
127: return null;
128: }
129:
130: protected abstract void execute(JobContext context, Object entity);
131:
132: public void tearDown(String objectId) {
133: }
134:
135: }
|