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
20
21
22
23
24
25
26
27
28
29
30 public class Consumer extends Thread {
31
32
33 Logger logger = Logger.getLogger(getClass());
34
35
36 Operation operation;
37 Configuration config;
38
39
40 SQLAdapter operationQueue;
41
42
43
44
45
46
47
48
49
50
51
52
53
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
66
67
68
69
70
71
72
73
74
75
76
77
78 public void run() {
79
80 boolean doContinue = true;
81
82 try {
83
84
85
86
87 this.operationQueue = new SQLAdapter(this.config.db);
88
89
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
98 logger.info("Operation type: " + this.operation.getType());
99
100
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
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
125 this.operationQueue.updateOperationStatus(this.operation.getId(), "DONE");
126 else
127 if (response.retry)
128
129 this.operationQueue.updateOperationStatus(this.operation.getId(), "RETR");
130 else
131
132 this.operationQueue.updateOperationStatus(this.operation.getId(), "ERRO");
133
134
135 } catch (Exception e) {
136 try {
137
138
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
149 logger.info("Freeing thread resources.......");
150 System.gc();
151 System.runFinalization();
152
153 }
154
155 return;
156 }
157 }