KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > hibernate > support > SetupJobs


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Jan-Frederic Linde [jfl at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.hibernate.support;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.riotfamily.riot.job.Job;
30 import org.riotfamily.riot.job.JobContext;
31 import org.riotfamily.riot.job.context.CommonsLogginJobContext;
32 import org.springframework.beans.factory.InitializingBean;
33 import org.springframework.transaction.PlatformTransactionManager;
34 import org.springframework.transaction.TransactionStatus;
35 import org.springframework.transaction.support.TransactionCallback;
36 import org.springframework.transaction.support.TransactionTemplate;
37 import org.springframework.util.Assert;
38
39 public class SetupJobs implements InitializingBean {
40     
41     private List JavaDoc jobs;
42     
43     private PlatformTransactionManager transactionManager;
44     
45     public void setTransactionManager(PlatformTransactionManager transactionManager) {
46         this.transactionManager = transactionManager;
47     }
48
49     public void setJobs(List JavaDoc jobs) {
50         this.jobs = jobs;
51     }
52     
53     public void afterPropertiesSet() throws Exception JavaDoc {
54         Assert.notNull(jobs, "Jobs must not be null");
55         
56         if (transactionManager != null) {
57             new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
58                 public Object JavaDoc doInTransaction(TransactionStatus status) {
59                     executeJobs();
60                     return null;
61                 }
62             });
63         }
64         else {
65             executeJobs();
66         }
67         
68     }
69     
70     protected void executeJobs() {
71         JobContext context = new CommonsLogginJobContext();
72         Iterator JavaDoc it = jobs.iterator();
73         while (it.hasNext()) {
74             Job job = (Job) it.next();
75             job.execute(context);
76         }
77     }
78
79 }
80
Popular Tags