Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TopoGenerateTopologyHolderMojo |
|
| 16.0;16 |
1 | package com.aurea.maven.plugins.sonic.topology; | |
2 | ||
3 | import java.io.BufferedReader; | |
4 | import java.io.File; | |
5 | import java.io.FileNotFoundException; | |
6 | import java.io.FileOutputStream; | |
7 | import java.io.FileReader; | |
8 | import java.io.IOException; | |
9 | import java.io.ObjectOutputStream; | |
10 | ||
11 | import no.geosoft.cc.util.SmartTokenizer; | |
12 | ||
13 | import org.apache.maven.plugin.MojoExecutionException; | |
14 | import org.apache.maven.plugin.MojoFailureException; | |
15 | ||
16 | import com.aurea.maven.plugins.sonic.topology.utils.Environment; | |
17 | import com.aurea.maven.plugins.sonic.topology.utils.MachineConfig; | |
18 | import com.aurea.maven.plugins.sonic.topology.utils.ParameterHandlingException; | |
19 | import com.aurea.maven.plugins.sonic.topology.utils.ParameterHolder; | |
20 | import com.aurea.maven.plugins.sonic.topology.utils.TopologyHolder; | |
21 | ||
22 | ||
23 | /** | |
24 | * This Mojo is responsible for generating the SDM Model, by collecting the xar | |
25 | * files and analysing them to update the Model.xml file appropriately. | |
26 | * | |
27 | * @goal topo-generate-topology-holder | |
28 | */ | |
29 | 0 | public class TopoGenerateTopologyHolderMojo extends AbstractTopoMojo { |
30 | ||
31 | @Override | |
32 | protected void doExecute() throws MojoExecutionException, | |
33 | MojoFailureException { | |
34 | 0 | getLog().info("Generating Topology Holder"); |
35 | ||
36 | //checking the csv file | |
37 | 0 | if(! getTopologyDefinitionFile().exists() || !getTopologyDefinitionFile().isFile()){ |
38 | 0 | throw new MojoExecutionException("The Topology definition file is not available: " + getTopologyDefinitionFile().getAbsolutePath()); |
39 | } | |
40 | //creating the output directory | |
41 | 0 | getTargetTopologyDir().mkdirs(); |
42 | 0 | TopologyHolder [] topoHolder = null; |
43 | try { | |
44 | 0 | topoHolder = parseTopologyGenFile(); |
45 | 0 | } catch (IOException e) { |
46 | // TODO Auto-generated catch block | |
47 | 0 | throw new MojoExecutionException("IO Exception with Topology File: " + e.getMessage()); |
48 | 0 | } |
49 | ||
50 | try { | |
51 | 0 | saveTopologyHolderInfo(topoHolder); |
52 | 0 | } catch (FileNotFoundException e) { |
53 | 0 | e.printStackTrace(); |
54 | 0 | throw new MojoExecutionException("IO Exception with Topology File: " + e.getMessage()); |
55 | 0 | } catch (IOException e) { |
56 | 0 | e.printStackTrace(); |
57 | 0 | throw new MojoExecutionException("IO Exception with Topology File: " + e.getMessage()); |
58 | 0 | } |
59 | ||
60 | ||
61 | 0 | } |
62 | ||
63 | private void saveTopologyHolderInfo(TopologyHolder[] topoHolder) throws FileNotFoundException, IOException { | |
64 | 0 | for (int i = 0; i < topoHolder.length; i++) { |
65 | //Determine the filename | |
66 | 0 | String fName = topoHolder[i].getTopologyId(); |
67 | 0 | ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(getTargetTopologyDir(),fName + ".topo.ser"))); |
68 | 0 | oos.writeObject(topoHolder[i]); |
69 | 0 | oos.close(); |
70 | } | |
71 | 0 | } |
72 | ||
73 | private TopologyHolder[] parseTopologyGenFile() throws IOException, MojoExecutionException { | |
74 | // open line reader for the topology file | |
75 | 0 | FileReader fr = new FileReader(getTopologyDefinitionFile()); |
76 | 0 | BufferedReader br = new BufferedReader(fr); |
77 | String line; | |
78 | 0 | TopologyHolder[] tpholder = null; |
79 | 0 | while ((line = br.readLine()) != null) { |
80 | 0 | getLog().info("READ-LINE: " + line); |
81 | 0 | if (line.startsWith("HEADER")) { |
82 | 0 | getLog().info("Parsing Header information"); |
83 | 0 | String[] pline = parseTopologyLine(line); |
84 | 0 | tpholder = new TopologyHolder[pline.length - 1]; |
85 | 0 | for (int i = 1; i < pline.length; i++) { |
86 | 0 | getLog().info("Parameter Found:" + pline[i]); |
87 | 0 | tpholder[i - 1] = new TopologyHolder(pline[i]); |
88 | } | |
89 | 0 | line = br.readLine(); |
90 | 0 | if(line == null || (!line.toUpperCase().startsWith("topologyEnvID".toUpperCase()))){ |
91 | 0 | br.close(); |
92 | 0 | throw new MojoExecutionException( |
93 | "Problem in the Topology File ... not topologyEnvID entry found"); | |
94 | } | |
95 | 0 | pline = null; |
96 | 0 | pline = parseTopologyLine(line); |
97 | 0 | for (int i = 1; i < pline.length; i++) { |
98 | 0 | tpholder[i-1].setTopoEnvId(pline[i]); |
99 | } | |
100 | ||
101 | } | |
102 | 0 | while (line.startsWith("BEGIN-MACHINE")) { |
103 | String pline []; | |
104 | 0 | getLog().info("Machines configuration"); |
105 | 0 | MachineConfig [] mc = new MachineConfig[tpholder.length]; |
106 | ||
107 | //init of the MAchine Configuration holder | |
108 | 0 | for (int i = 0; i < mc.length; i++) { |
109 | 0 | mc[i] = new MachineConfig(); |
110 | } | |
111 | ||
112 | 0 | getLog().info("Init machine config done"); |
113 | ||
114 | 0 | while ((line = br.readLine()) != null |
115 | && !line.startsWith("END-MACHINE")) { | |
116 | 0 | pline = parseTopologyLine(line); |
117 | 0 | getLog().info("mc.length: " + mc.length + " ... pline.lenght: " + pline.length); |
118 | 0 | if(pline[0].toUpperCase().equals("Machine-id".toUpperCase())){ |
119 | 0 | for (int i = 1; i < pline.length; i++) { |
120 | 0 | mc[i -1].setMachineId(pline[i]); |
121 | } | |
122 | 0 | getLog().info("Handling Machine-id"); |
123 | } | |
124 | 0 | if(pline[0].toUpperCase().equals("Alternate-id".toUpperCase())){ |
125 | 0 | for (int i = 1; i < pline.length; i++) { |
126 | 0 | mc[i - 1].addAlternateId(pline[i]); |
127 | } | |
128 | 0 | getLog().info("Handling Alternate-id"); |
129 | } | |
130 | 0 | if(pline[0].toUpperCase().equals("LogicalHost".toUpperCase())){ |
131 | 0 | for (int i = 1; i < pline.length; i++) { |
132 | 0 | mc[i - 1].addLogicalHosts(pline[i]); |
133 | } | |
134 | 0 | getLog().info("Handling LogicalHost"); |
135 | } | |
136 | 0 | if(pline[0].toUpperCase().equals("Machine-ContainersDir".toUpperCase())){ |
137 | 0 | for (int i = 1; i < pline.length; i++) { |
138 | 0 | mc[i - 1].setContainerDir(pline[i]); |
139 | } | |
140 | 0 | getLog().info("Machine-ContainersDir"); |
141 | } | |
142 | 0 | if(pline[0].toUpperCase().equals("Machine-RegisterAsService".toUpperCase())){ |
143 | 0 | for (int i = 1; i < pline.length; i++) { |
144 | 0 | mc[i - 1].setRegisterAsService(pline[i]); |
145 | } | |
146 | 0 | getLog().info("Machine-RegisterAsService"); |
147 | } | |
148 | 0 | if(pline[0].toUpperCase().equals("Machine-EncryptPWD".toUpperCase())){ |
149 | 0 | for (int i = 1; i < pline.length; i++) { |
150 | 0 | mc[i - 1].setEncryptPWD(pline[i]); |
151 | } | |
152 | 0 | getLog().info("Handling Machine-EncryptPWD"); |
153 | } | |
154 | 0 | if(pline[0].toUpperCase().equals("Machine-ServiceStartupTimeout".toUpperCase())){ |
155 | 0 | for (int i = 1; i < pline.length; i++) { |
156 | 0 | mc[i - 1].setServiceStartupTime(pline[i]); |
157 | } | |
158 | 0 | getLog().info("Handling Machine-ServiceStartupTimeout"); |
159 | } | |
160 | //read machine information ... machine-id / alternate id's / logical hosts ... | |
161 | ||
162 | } | |
163 | //add final Machine Config | |
164 | 0 | getLog().info("Need to add Machine configurations here"); |
165 | 0 | for (int i = 0; i < mc.length; i++) { |
166 | 0 | tpholder[i].getMachineHolder().addMachineConfig(mc[i]); |
167 | } | |
168 | 0 | getLog().info("Machine configuration is added"); |
169 | 0 | } |
170 | 0 | getLog().info("Machines Are Configured"); |
171 | ||
172 | 0 | if (line.startsWith("BEGIN-ENVIRONMENT")) { |
173 | 0 | getLog().info("Configuring Environment"); |
174 | 0 | Environment [] env = new Environment[tpholder.length]; |
175 | ||
176 | //init of the MAchine Configuration holder | |
177 | 0 | for (int i = 0; i < env.length; i++) { |
178 | 0 | env[i] = new Environment(); |
179 | } | |
180 | 0 | while ((line = br.readLine()) != null |
181 | && !line.startsWith("END-ENVIRONMENT")) { | |
182 | 0 | String [] pline = parseTopologyLine(line); |
183 | ||
184 | 0 | if(pline[0].toUpperCase().equals("ContainersDir".toUpperCase())){ |
185 | 0 | for (int i = 1; i < pline.length; i++) { |
186 | 0 | env[i - 1].setContainerDir(pline[i]); |
187 | } | |
188 | 0 | getLog().info("ContainersDir"); |
189 | } | |
190 | 0 | if(pline[0].toUpperCase().equals("RegisterAsService".toUpperCase())){ |
191 | 0 | for (int i = 1; i < pline.length; i++) { |
192 | 0 | env[i - 1].setRegisterAsService(pline[i]); |
193 | } | |
194 | 0 | getLog().info("RegisterAsService"); |
195 | } | |
196 | 0 | if(pline[0].toUpperCase().equals("EncryptPWD".toUpperCase())){ |
197 | 0 | for (int i = 1; i < pline.length; i++) { |
198 | 0 | env[i - 1].setEncryptPWD(pline[i]); |
199 | } | |
200 | 0 | getLog().info("Handling EncryptPWD"); |
201 | } | |
202 | 0 | if(pline[0].toUpperCase().equals("ServiceStartupTimeout".toUpperCase())){ |
203 | 0 | for (int i = 1; i < pline.length; i++) { |
204 | 0 | env[i - 1].setServiceStartupTime(pline[i]); |
205 | } | |
206 | 0 | getLog().info("Handling Machine-ServiceStartupTimeout"); |
207 | } | |
208 | ||
209 | 0 | } |
210 | 0 | getLog().info("Need to add Environment configurations here"); |
211 | 0 | for (int i = 0; i < env.length; i++) { |
212 | 0 | getLog().info(env[i].toString()); |
213 | 0 | tpholder[i].setEnv(env[i]); |
214 | } | |
215 | 0 | getLog().info("Environment configuration is added"); |
216 | } | |
217 | 0 | if (line.startsWith("BEGIN-PARAMETERS")) { |
218 | 0 | getLog().info("Starting Parameter Configuration"); |
219 | 0 | ParameterHolder [] parm = new ParameterHolder[tpholder.length]; |
220 | ||
221 | //init of the MAchine Configuration holder | |
222 | 0 | for (int i = 0; i < parm.length; i++) { |
223 | 0 | parm[i] = new ParameterHolder(); |
224 | } | |
225 | 0 | while ((line = br.readLine()) != null |
226 | && !line.startsWith("END-PARAMETERS")) { | |
227 | 0 | String [] pline = parseTopologyLine(line); |
228 | 0 | for (int i = 1; i < pline.length; i++) { |
229 | 0 | getLog().warn("pline[0] " + pline[0] + " --- pline[" + i +"] " + pline[i]); |
230 | 0 | if(pline[i].equals("")){ |
231 | 0 | getLog().warn("null token found"); |
232 | } | |
233 | try { | |
234 | 0 | parm[i - 1].addParameter("@" + pline[0] + "@" , pline[i]); |
235 | 0 | } catch (ParameterHandlingException e) { |
236 | 0 | br.close(); |
237 | 0 | throw new MojoExecutionException(e.getMessage()); |
238 | 0 | } |
239 | } | |
240 | 0 | } |
241 | 0 | getLog().info("Need to add Parameter configurations here"); |
242 | 0 | for (int i = 0; i < parm.length; i++) { |
243 | 0 | tpholder[i].setPholder(parm[i]); |
244 | } | |
245 | 0 | getLog().info("Parameter configuration is added"); |
246 | 0 | } |
247 | } | |
248 | ||
249 | //Section to print the parsed content ... validation that can be removed (put in debug later) | |
250 | 0 | for (int i = 0; i < tpholder.length; i++) { |
251 | // getLog().info("Printing stuff"); | |
252 | 0 | getLog().info("Topology entry created for: " + tpholder[i].toString()); |
253 | // getLog().info("Done Printing Stuff"); | |
254 | } | |
255 | ||
256 | 0 | br.close(); |
257 | ||
258 | 0 | return tpholder; |
259 | } | |
260 | ||
261 | ||
262 | private String[] parseTopologyLine(String line) { | |
263 | 0 | getLog().info("Parsing: " + line); |
264 | 0 | SmartTokenizer st = new SmartTokenizer(line, topoFileSeparator); |
265 | 0 | String[] retVal = new String[st.countTokens()]; |
266 | 0 | int index = 0; |
267 | 0 | while (st.hasMoreTokens()) { |
268 | 0 | retVal[index] = st.nextToken(); |
269 | 0 | index++; |
270 | } | |
271 | 0 | return retVal; |
272 | } | |
273 | ||
274 | } |