View Javadoc

1   /**
2    * 
3    */
4   package net.sf.provisioner.core;
5   
6   
7   import java.util.*;
8   
9   import net.sf.provisioner.adapters.SQLAdapter;
10  import net.sf.provisioner.config.ConfigRequest;
11  import net.sf.provisioner.config.Configuration;
12  import net.sf.provisioner.requests.*;
13  import net.sf.provisioner.responses.Response;
14  
15  import org.apache.log4j.Logger;
16  
17  
18  /**
19   * This class executes all requests pertaining to one operation
20   * and all operations that have interdependencies .
21   * <p>
22   * 
23   * .
24   * <p>
25   * 
26   *             
27   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:27 $
28   * @author Gonzalo Espert
29   */
30  public class Consumer extends Thread {
31  
32  	/** Logger for this class and subclasses */
33  	Logger logger = Logger.getLogger(getClass());
34  	
35  
36  	Operation operation;
37  	Configuration config;
38  	
39  	
40  	SQLAdapter operationQueue;
41  
42  	/**
43       * .
44       * <p>
45       * 
46       * .
47       * <p>
48       * 
49       * 
50       * 
51       * @param 
52       *            
53       * @throws 
54       *            
55       *           
56       */
57  	public Consumer(Operation operation, Configuration config) { 
58  		this.operation = operation; 
59  		this.config = config;
60  		start();
61  	}
62  
63  	/**
64       * .
65       * <p>
66       * 
67       * .
68       * <p>
69       * 
70       * 
71       * 
72       * @param 
73       *            
74       * @throws 
75       *            
76       *           
77       */
78  	public void run() {
79  		
80  		boolean doContinue = true;
81  		
82  		try {
83  			
84  			/**
85  			 * Create access to operations queue
86  			 */
87  			this.operationQueue = new SQLAdapter(this.config.db);
88  			
89  			/* Apply provisioning rules before sending operation's requests */
90  			this.operation.applyRules(this.config.rules.elements(), this.config.services);
91  			
92  			Response response = new Response();
93  			
94  			logger.info("Setting status for the operation (In Process)...");
95  			this.operationQueue.updateOperationStatus(this.operation.getId(), "PROC");
96  			
97  			/* Print operation contents */
98  			logger.info("Operation type: " + this.operation.getType());
99  						
100 			/* Process each of the operation's requests */
101 			Enumeration requests = this.operation.requests.elements(); 
102 			
103 			while (requests.hasMoreElements() && doContinue) {
104 			
105 				ConfigRequest configRequest = (ConfigRequest) requests.nextElement();
106 				Request       request       = RequestFactory.createRequest(
107 						operation, 
108 						configRequest
109 				);
110 				
111 				logger.info("Sending request to " + configRequest.service.ne.name + "...");
112 				logger.info("Target type " + configRequest.service.ne.type + "...");
113 
114 				response = request.sendRequest();
115 				/* If one of the operation's requests fails we do not continue */
116 				doContinue = response.successfull;				
117 			}
118 			
119 			logger.info("Registering request response in operations queue...");
120 			this.operationQueue.registerResponse(response.errorStr, this.operation.getId());
121 			
122 			logger.info("Update operation status in operations queue...");
123 			if (response.successfull) 
124 				/* Processed */
125 				this.operationQueue.updateOperationStatus(this.operation.getId(), "DONE");
126 			else
127 				if (response.retry)
128 					/* "Recoverable error" */
129 					this.operationQueue.updateOperationStatus(this.operation.getId(), "RETR");
130 				else
131 					/* "Irrecoverable error" */
132 					this.operationQueue.updateOperationStatus(this.operation.getId(), "ERRO");
133 			
134 			
135 		} catch (Exception e) {
136 			try {
137 				
138 				/* "Recoverable error" */
139 				this.operationQueue.registerResponse(e.toString(), this.operation.getId());
140 				this.operationQueue.updateOperationStatus(this.operation.getId(), "RETR");
141 				
142 			} catch (Exception ex) { ex.printStackTrace();}
143 			e.printStackTrace();
144 		}
145 		
146 		finally {
147 			
148 			/* Force resource release */
149 			logger.info("Freeing thread resources.......");
150 			System.gc();
151 			System.runFinalization();
152 		
153 		}
154 		
155 		return;
156 	}
157 }