-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.xml
357 lines (245 loc) · 10 KB
/
build.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<project name="policy-build" default="dist" basedir="/home/oracle/GeoIPPolicy/">
<property file="build.properties" />
<property name="configSrc" location="src/config/java" />
<property name="runtimeSrc" location="src/runtime/java" />
<property name="runtimeApp" location="src/runtime/app" />
<property name="uiResources" location="src/ui/resources" />
<property name="metadataFile" location="src/metadata/metadata.json" />
<property name="build" location="build" />
<property name="dist" location="dist" />
<property name="extLibs" location="extLibs" />
<property name="basedir" value="${basedir}"/>
<target name="init">
<mkdir dir="${build}" />
<mkdir dir="${build}/config" />
<mkdir dir="${build}/runtime" />
</target>
<path id="dependencies.jars">
<fileset dir="${policy-sdk}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${extLibs}">
<include name="**/*.jar" />
<include name="**/*.properties" />
</fileset>
</path>
<target name="compile" depends="init">
<javac includeantruntime="false" srcdir="${configSrc}" destdir="${build}/config" source="1.7" target="1.7">
<classpath refid="dependencies.jars" />
</javac>
<javac includeantruntime="false" srcdir="${runtimeSrc}" destdir="${build}/runtime" source="1.7" target="1.7">
<classpath path="${build}/config" />
<classpath refid="dependencies.jars" />
</javac>
</target>
<target name="createJars" depends="compile">
<jar jarfile="${dist}/cache/config.jar" basedir="${build}/config" />
<jar jarfile="${dist}/cache/runtime.jar" basedir="${build}/runtime">
<zipfileset file="${metadataFile}" />
</jar>
</target>
<target name="createEAR" depends="createJars">
<jar jarfile="${dist}/cache/runtime.ear">
<zipfileset dir="${dist}/cache" includes="*.jar" prefix="APP-INF/lib" />
<zipfileset dir="${extLibs}" includes="*.jar" prefix="APP-INF/lib" />
</jar>
<delete file="${dist}/cache/runtime.jar" />
</target>
<target name="createUIResources">
<zip destfile="${dist}/cache/ui.zip" basedir="${uiResources}" />
</target>
<target name="dist" depends="revisionMgmt, createJars, createEAR, createUIResources">
<copy file="${metadataFile}" toDir="${dist}/cache" />
<jar destfile="${jarPrefix}.${policyName}.jar" basedir="${dist}/cache">
<manifest>
<attribute name="Policy-Metadata" value="metadata.json" />
<attribute name="Policy-UI" value="ui.zip" />
<attribute name="Policy-Config" value="config.jar" />
<attribute name="Policy-Runtime" value="runtime.ear" />
</manifest>
</jar>
<antcall target="deploy" />
<antcall target="cleanUp" />
</target>
<target name="revisionMgmt">
<script language="javascript">
var echo = project.createTask("echo");
aRev = project.getProperty("autoRevision");
autoRev = java.lang.Boolean.parseBoolean(aRev);
if (autoRev) {
fileReader = null;
buffReader = null;
fileWriter = null;
buffWriter = null;
try {
// Read the information from the metadata.json
// file to be able to manage the policy revision
try {
metadataFile = project.getProperty("metadataFile");
fileReader = new java.io.FileReader(metadataFile);
buffReader = new java.io.BufferedReader(fileReader);
strBuilder = new java.lang.StringBuilder();
currLine = null;
while ((currLine = buffReader.readLine()) != null) {
strBuilder.append(currLine);
}
} finally {
if (buffReader != null) {
buffReader.close();
}
}
metadata = JSON.parse(strBuilder.toString());
policyType = metadata.type;
version = metadata.version;
// Retrieve information from the policy using the REST
// API. If the policy currently exists under the type
// and version specified, the revision will then be
// automatically incremented. Otherwise the revision
// will be set to '1';
mgmtServerHost = project.getProperty("mgmtServerHost");
mgmtServerPort = project.getProperty("mgmtServerPort");
mgmtServerUser = project.getProperty("mgmtServerUser");
mgmtServerPass = project.getProperty("mgmtServerPass");
endpoint = mgmtServerHost + ":" + mgmtServerPort +
"/apiplatform/administration/v1/policies/" +
policyType + "/versions/" + version;
url = new java.net.URL(endpoint);
urlConn = url.openConnection();
userPass = mgmtServerUser + ":" + mgmtServerPass;
basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes());
urlConn.setRequestProperty("Authorization", basicAuth);
currLine = null;
strBuilder = null;
policyData = null;
buffReader = null;
try {
strBuilder = new java.lang.StringBuilder();
buffReader = new java.io.BufferedReader(
new java.io.InputStreamReader(urlConn.getInputStream()));
while((currLine = buffReader.readLine()) != null) {
strBuilder.append(currLine);
}
policyData = strBuilder.toString();
} catch (err) {
// A 404 error in this context isn't something bad. It just means
// that the policy doesn't exist for the given type/version. Thus,
// it will be ignored and everything else will be printed out.
if (urlConn.getResponseCode() != java.net.HttpURLConnection.HTTP_NOT_FOUND) {
err.printStackTrace();
}
} finally {
if (buffReader != null) {
buffReader.close();
}
}
if (policyData != null) {
policyData = JSON.parse(policyData);
currRevision = policyData.revision;
nextRevision = parseInt(currRevision) + 1;
currRevision = nextRevision.toString();
} else {
currRevision = "1";
}
// Write the changes back to the metadata.json file
// so the build process can use that along with the
// JAR file that is going to be generated...
metadata.revision = currRevision
metadata = JSON.stringify(metadata, null, 3);
try {
fileWriter = new java.io.FileWriter(metadataFile);
buffWriter = new java.io.BufferedWriter(fileWriter);
buffWriter.write(metadata);
} finally {
if (buffWriter != null) {
buffWriter.close();
}
}
echo.setMessage("Current revision has been set to '" + currRevision + "'.");
echo.perform();
} catch (err) {
err.printStackTrace();
}
} else {
echo.setMessage("Automatic revision is currently disabled. " +
"Using the revision manually set in the policy metadata file.");
echo.perform();
}
</script>
</target>
<target name="deploy" description="deploy">
<script language="groovy" classpathref="dependencies.jars">
import javax.xml.bind.DatatypeConverter
def autoDeploy = project.getProperty("autoDeploy")
def jarPrefix = project.getProperty("jarPrefix")
def policyName = project.getProperty("policyName")
def policyFile = project.getProperty("basedir") +
File.separator + jarPrefix + "." + policyName + ".jar"
printf("Policy file:"+policyFile);
if (Boolean.parseBoolean(autoDeploy) == true) {
def mgmtServerHost = project.getProperty("mgmtServerHost")
def mgmtServerPort = project.getProperty("mgmtServerPort")
def endpoint = mgmtServerHost + ":" + mgmtServerPort +
"/apiplatform/administration/v1/policies"
def mgmtServerUser = project.getProperty("mgmtServerUser")
def mgmtServerPass = project.getProperty("mgmtServerPass")
def userPass = mgmtServerUser + ":" + mgmtServerPass
def basicAuth = "Basic " + DatatypeConverter.printBase64Binary(
userPass.getBytes())
def fis = null
def out = null
try {
def deploymentUrl = new URL(endpoint)
def deployConn = (HttpURLConnection) deploymentUrl.openConnection()
fis = new FileInputStream(new File(policyFile))
deployConn.setRequestProperty("Authorization", basicAuth)
deployConn.setRequestProperty("Content-Type", "application/octet-stream")
deployConn.setRequestProperty("Content-Length", String.valueOf(fis.available()))
deployConn.setRequestMethod("POST")
deployConn.setDoOutput(true)
def buffer = new byte[fis.available()]
out = deployConn.getOutputStream()
while (true) {
def r = fis.read(buffer)
if (r == -1) { break }
out.write(buffer, 0, r)
}
def responseCode = deployConn.getResponseCode()
if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
println "The policy has been successfully deployed into the APIP Mgmt Tier."
} else {
println "Something went wrong during the deployment of the policy:"
def responseStream = deployConn.getErrorStream()
if (responseStream != null)
{
buffer = new byte[responseStream.available()]
responseStream.read(buffer)
println new String(buffer)
}
else
{
printf ("no error response")
}
responseStream = deployConn.getInputStream()
buffer = new byte[responseStream.available()]
responseStream.read(buffer)
println new String(buffer)
}
deployConn.disconnect()
} catch (Exception ex) {
ex.printStackTrace()
} finally {
if (fis != null) { fis.close() }
if (out != null) { out.close() }
}
} else {
println "Automatic deployment is currently disabled. " +
"You can manually deploy the '" + policyFile + "' " +
"file in API Platform."
}
</script>
</target>
<target name="cleanUp" description="clean up">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
</project>