View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.app.daemon.domain;
18  
19  import java.io.FileOutputStream;
20  import java.util.List;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.gwe.persistence.model.DaemonConfigDesc;
27  import org.gwe.persistence.model.HeadResourceInfo;
28  import org.gwe.persistence.model.JobExecutionInfo;
29  import org.gwe.persistence.model.JobInfo;
30  import org.gwe.persistence.model.JobInfoIdGenerator;
31  import org.gwe.persistence.model.OrderExecutionProfileInfo;
32  import org.gwe.persistence.model.OrderInfo;
33  import org.gwe.utils.IOUtils;
34  import org.gwe.utils.concurrent.BlockingList;
35  import org.springframework.transaction.annotation.Isolation;
36  import org.springframework.transaction.annotation.Propagation;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  /**
40   * @author Marco Ruiz
41   * @since Mar 10, 2008
42   */
43  public class UserDomain extends BaseDomain {
44  
45  	private static Log log = LogFactory.getLog(UserDomain.class);
46  	
47  	private static final Pattern EMAIL_PATTERN = Pattern.compile("(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*");
48  
49  	private BlockingList<OrderInfo> newOrders = new BlockingList<OrderInfo>();
50  
51  	private boolean isEmail(String target) {
52  		Matcher matcher = EMAIL_PATTERN.matcher(target);
53  		if (!matcher.find()) return false;
54  		if (matcher.find()) return false;
55  		return true;
56  	}
57  	
58  	//=====================
59  	// Server API Services
60  	//=====================
61  	@Transactional(propagation=Propagation.REQUIRES_NEW)
62  	public void setupDaemon(HeadResourceInfo daemonInfo) {
63  		headResourceDAO.save(daemonInfo);
64  	}
65  
66  	public void shutdownDaemon(DaemonConfigDesc admin) {
67  		System.exit(0);
68  	}
69  	
70  	// ==============
71  	//  USER PROFILE
72  	// ==============
73  	public void updateConfig(DaemonConfigDesc config) throws Exception {
74  		DaemonConfigDesc persistedConfig = getConfig();
75  		persistedConfig.setKeys(config.getKeys());
76  		
77  		// Update in DB
78  		updateHeadResource(config.getHeadResource());
79  		
80  		// Save serialized user info
81  		HeadResourceInfo daemonInfo = persistedConfig.getHeadResource();
82          IOUtils.serializeObject(config, new FileOutputStream(daemonInfo.getInstallation().getConfigurationFilePath()));
83      }
84  
85  	public void updateHeadResource(HeadResourceInfo daemonInfo) {
86  		HeadResourceInfo persistedDaemon = headResourceDAO.get(daemonInfo.getId());
87  		persistedDaemon.setName(           daemonInfo.getName());
88  		persistedDaemon.setMaxHijackMins(  daemonInfo.getMaxHijackMins());
89  		persistedDaemon.setMaxIdleMins(    daemonInfo.getMaxIdleMins());
90  		persistedDaemon.setQueueSize(      daemonInfo.getQueueSize());
91  		persistedDaemon.setHeartBeatPeriodSecs(daemonInfo.getHeartBeatPeriodSecs());
92  		headResourceDAO.updateAndEvict(persistedDaemon);
93      }
94  
95  	public void updateDefaultExecutionProfile(OrderExecutionProfileInfo executionProfile) {
96  		OrderExecutionProfileInfo persistedExecutionProfile = getConfig().getDefaultExecutionProfile();
97      }
98  
99  	// ====================
100 	//  ORDERS & JOBS INFO
101 	// ====================
102 	@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_UNCOMMITTED, readOnly=true)
103 	public List<OrderInfo> getOrdersDefined() {
104 		List<OrderInfo> orders = orderDAO.getList();
105 		orders.size(); // load orders
106 		return orders;
107 	}
108 
109 	@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_UNCOMMITTED, readOnly=true)
110 	public List<OrderInfo> getOrdersList(boolean includeJobs) {
111 		List<OrderInfo> result = getOrdersDefined();
112 		if (includeJobs)
113 			for (OrderInfo order : result) order.getJobs().size();
114 		return result;
115 	}
116 
117 	@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_UNCOMMITTED, readOnly=true)
118 	public List<OrderInfo> getOrdersByDescription(String description) {
119 		return orderDAO.getByDescription(description);
120 	}
121 
122 	@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_UNCOMMITTED, readOnly=true)
123 	public JobInfo getJobDetails(int orderId, int jobNum) {
124 		OrderInfo order = getOrder(orderId, false);
125 		String jobId = JobInfoIdGenerator.generateId(orderId, order.getTotalJobsCount(), jobNum);
126 		return jobDAO.get(jobId);
127 	}
128 	
129 	@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_UNCOMMITTED, readOnly=true)
130 	public List<JobExecutionInfo> getExecutionDetails(int orderId, int jobNum) {
131 		OrderInfo order = getOrder(orderId, false);
132 		String jobId = JobInfoIdGenerator.generateId(orderId, order.getTotalJobsCount(), jobNum);
133 		return jobExecutionDAO.getBelongingToJob(jobId);
134 	}
135 	
136 	@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_UNCOMMITTED, readOnly=true)
137 	public OrderInfo getOrder(int orderId, boolean includeJobs) {
138 		return orderDAO.getOrder(orderId, includeJobs);
139 	}
140 
141 	// ================
142 	//  ORDERS CONTROL
143 	// ================
144 	@Transactional(propagation=Propagation.REQUIRES_NEW)
145 	public OrderInfo persistOrder(OrderInfo order) {
146 		// Preparation and error checking
147 		if (order == null) return null;
148 
149 		orderDAO.save(order);
150 		return order;
151 	}
152 	
153 	public void queueOrder(OrderInfo order) {
154 		order.generateJobs(getConfig(), getConfig().getDefaultExecutionProfile().clone());
155 		newOrders.add(order);
156 	}
157 
158 	public List<String> previewOrder(OrderInfo order) throws Exception {
159 		return order.getDescriptor().generateCommands(getConfig());
160 	}
161 
162 	@Transactional(propagation=Propagation.REQUIRES_NEW)
163 	public void pauseOrder(int orderId, boolean pause) {
164 		orderDAO.setPause(orderId, pause);
165 		if (!pause) newOrders.forceWakeUp();
166 	}
167 
168 	@Transactional(propagation=Propagation.REQUIRES_NEW)
169 	public void abortOrder(int orderId) {
170 		getOrder(orderId, false).setAborted(true);
171 	}
172 
173 	@Transactional(propagation=Propagation.REQUIRES_NEW)
174 	public void deleteOrder(int orderId) {
175 		orderDAO.markAsDeleted(orderId);
176 	}
177 	
178 	@Transactional(propagation=Propagation.REQUIRES_NEW)
179 	public void swapPriorities(int orderId1, int orderId2) {
180 		orderDAO.swapPriorities(orderId1, orderId2);
181     }
182 
183 	public void publishOrder(int orderId, String host, String path) {
184 //		this.getConfig().getDaemonHostLink()
185 		
186 //		orderDAO.swapPriorities(orderId1, orderId2);
187     }
188 
189 	public void cleanupDisposedAllocations() {
190 		getConfig().getHeadResource().getInstallation().cleanupDisposedAllocFolder();
191     }
192 	
193 	//==============================================
194 	// METHODS TO BE INVOKED BY BACKGROUND SERVICES
195 	//==============================================
196 	@Transactional(propagation=Propagation.NEVER)
197 	public List<OrderInfo> dequeueUnprocessedOrders() {
198 		return newOrders.takeAll();
199 	}
200 
201 	@Transactional(propagation=Propagation.REQUIRES_NEW)
202 	public List<OrderInfo> generateJobs(List<OrderInfo> newOrdersCopy) {
203 		if (newOrdersCopy != null) {
204 		    for (OrderInfo order : newOrdersCopy) 
205 				jobDAO.saveAll(order.getJobs());
206 			
207 		    orderDAO.saveOrUpdateAll(newOrdersCopy);
208 		}
209 		return newOrdersCopy;
210     }
211 }
212