Skip to content

Commit 8807070

Browse files
committed
CodeGen refactoring and documentation/example
1 parent bb52a84 commit 8807070

31 files changed

+859
-538
lines changed

NEWVERSION.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
When adding a new version of the ARI:
2-
3-
- Copy the files from "Asterisk/rest-api/api-docs" to the right folder under "codegen_data"
4-
- Add the ARI version in run.java under the code generator
5-
- Create folders like "generated.ARI_1_2_3", "generated.ARI_1_2_3.actions" and "generated.ARI_1_2_3.models"
6-
or the code generator will fail
7-
- Run the code generator
8-
9-
In the main source tree:
10-
11-
- create the version to be used in AriVersion.java
12-
13-
14-
15-
(Obsolete)
16-
17-
- in ARI.java, edit the build() function to get you the correct objects
18-
19-
1+
## ARI Versions
202

3+
In the `codegen` folder there is a bash script: `getApis.sh`
4+
that will get and recurse the Asterisk source generating the data required for the APIs
215

6+
Commit the changes as the Gradle build script always builds from those files
227

238
## Deployment
249

2510
When a release is ready:
2611

2712

13+
export BINTRAY_USER=<user>
2814
export BINTRAY_KEY={bintray.txt}
2915

3016
./gradlew clean test jar

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'ch.loway.oss.ari4java'
8-
version = '0.7.0'
8+
version = '0.8.0'
99

1010
sourceSets {
1111
main {

codegen/src/main/java/ch/loway/oss/ari4java/codegen/DefMapper.java

Lines changed: 86 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ public class DefMapper {
4848
/**
4949
* Loads definitions from a module.
5050
*
51-
* @param f The source .json file
52-
* @param apiVersion The version of the API we are working with
53-
* @param modelHasEvents whether this file generates WS events
51+
* @param f The source .json file
52+
* @param apiVersion The version of the API we are working with
5453
* @throws IOException
5554
*/
56-
public void parseJsonDefinition(File f, String apiVersion, boolean modelHasEvents) throws IOException {
55+
public void parseJsonDefinition(File f, String apiVersion) throws IOException {
5756

5857
if (!vers.contains(apiVersion)) {
5958
vers.add(apiVersion);
@@ -66,7 +65,7 @@ public void parseJsonDefinition(File f, String apiVersion, boolean modelHasEvent
6665
mymodels.addAll(lModels);
6766
myAPIs.add(api1);
6867

69-
if (modelHasEvents) {
68+
if ("events.json".equals(f.getName())) {
7069

7170
Model typeMessage = null;
7271
List<Model> otherModels = new ArrayList<Model>();
@@ -105,27 +104,39 @@ public void parseJsonDefinition(File f, String apiVersion, boolean modelHasEvent
105104

106105
// Now generate the interface
107106
for (Model m : mymodels) {
108-
JavaInterface j = interfaces.get(m.getInterfaceName());
109-
if (j == null) {
110-
j = new JavaInterface();
111-
j.pkgName = "ch.loway.oss.ari4java.generated";
112-
j.className = m.getInterfaceName();
113-
j.parent = m.extendsModel;
114-
interfaces.put(m.getInterfaceName(), j);
107+
JavaInterface jim = interfaces.get(m.getInterfaceName());
108+
if (jim == null) {
109+
jim = new JavaInterface();
110+
jim.pkgName = "ch.loway.oss.ari4java.generated.models";
111+
jim.className = m.getInterfaceName();
112+
jim.parent = m.extendsModel;
113+
interfaces.put(m.getInterfaceName(), jim);
115114
}
116-
117-
m.registerInterfaces(j, apiVersion);
115+
m.registerInterfaces(jim, apiVersion);
118116
}
119117

120-
JavaInterface j = interfaces.get(api1.getInterfaceName());
121-
if (j == null) {
122-
j = new JavaInterface();
123-
j.pkgName = "ch.loway.oss.ari4java.generated";
124-
j.className = api1.getInterfaceName();
125-
interfaces.put(api1.getInterfaceName(), j);
118+
JavaInterface jia = interfaces.get(api1.getInterfaceName());
119+
if (jia == null) {
120+
jia = new JavaInterface();
121+
jia.pkgName = "ch.loway.oss.ari4java.generated.actions";
122+
jia.className = api1.getInterfaceName();
123+
interfaces.put(api1.getInterfaceName(), jia);
124+
}
125+
api1.registerInterfaces(jia, apiVersion);
126+
127+
for (Action a : api1.actions) {
128+
for (Operation o : a.operations) {
129+
JavaInterface jio = interfaces.get(o.getInterfaceName());
130+
if (jio == null) {
131+
jio = new JavaInterface();
132+
jio.pkgName = "ch.loway.oss.ari4java.generated.actions.requests";
133+
jio.className = o.getInterfaceName();
134+
interfaces.put(o.getInterfaceName(), jio);
135+
}
136+
o.registerInterfaces(jio, apiVersion);
137+
}
126138
}
127139

128-
api1.registerInterfaces(j, apiVersion);
129140
}
130141

131142
/**
@@ -147,11 +158,14 @@ public void generateAllClasses() throws IOException {
147158
* @throws IOException
148159
*/
149160
public AriBuilderInterface generateInterfaces() throws IOException {
150-
//
161+
// System.out.println("generateInterfaces");
162+
151163
AriBuilderInterface abi = new AriBuilderInterface();
152164
for (String ifName : interfaces.keySet()) {
153165
JavaInterface ji = interfaces.get(ifName);
154-
abi.knownInterfaces.add(ifName);
166+
if (ifName.startsWith("Action")) {
167+
abi.knownInterfaces.add(ifName);
168+
}
155169
saveToDisk(ji);
156170
}
157171

@@ -167,6 +181,7 @@ public AriBuilderInterface generateInterfaces() throws IOException {
167181
* @throws IOException
168182
*/
169183
public void generateModels() throws IOException {
184+
// System.out.println("generateModels");
170185
for (Model m : mymodels) {
171186
String minIf = m.getInterfaceName();
172187
JavaInterface ji = interfaces.get(minIf);
@@ -182,12 +197,17 @@ public void generateModels() throws IOException {
182197
* @throws IOException
183198
*/
184199
public void generateApis() throws IOException {
200+
// System.out.println("generateApis");
185201
for (Apis api : myAPIs) {
186202
String minIf = api.getInterfaceName();
187203
JavaInterface ji = interfaces.get(minIf);
188-
189204
api.setMinimalInterface(ji);
190205
saveToDisk(api);
206+
for (Action a : api.actions) {
207+
for (Operation o : a.operations) {
208+
saveToDisk(o);
209+
}
210+
}
191211
}
192212
}
193213

@@ -197,6 +217,7 @@ public void generateApis() throws IOException {
197217
* @throws IOException
198218
*/
199219
public void generateProperties(AriBuilderInterface abi) throws IOException {
220+
// System.out.println("generateProperties");
200221

201222
Map<String, Set<Model>> mM = new HashMap<String, Set<Model>>();
202223
Map<String, Set<Apis>> mA = new HashMap<String, Set<Apis>>();
@@ -287,33 +308,36 @@ private void builderEnum(StringBuilder sbVerEnum, String ver) {
287308
* @throws IOException
288309
*/
289310
public void generateImplementationClasses(AriBuilderInterface abi) throws IOException {
311+
// System.out.println("generateImplementationClasses");
290312

291-
Map<String, ClassTranslator> mTranslators = new HashMap<String, ClassTranslator>();
313+
List<ClassTranslator> lTranslators = new ArrayList<ClassTranslator>();
292314

293315
for (Apis api : myAPIs) {
294316
String ver = api.apiVersion;
295-
ClassTranslator ct = getClassTranslator(mTranslators, ver);
317+
ClassTranslator ct = getClassTranslator(lTranslators, ver);
296318
ct.setClass(api.className, api.className + "_impl_" + ver);
297319
}
298320

299321
for (Model mod : mymodels) {
300322
String ver = mod.apiVersion;
301-
ClassTranslator ct = getClassTranslator(mTranslators, ver);
323+
ClassTranslator ct = getClassTranslator(lTranslators, ver);
302324
ct.setClass(mod.className, mod.className + "_impl_" + ver);
303325
}
304326

305-
for (ClassTranslator ct : mTranslators.values()) {
327+
for (ClassTranslator ct : lTranslators) {
306328
saveToDisk(ct);
307329
}
308330
}
309331

310-
private ClassTranslator getClassTranslator(Map<String, ClassTranslator> mTranslators, String apiVer) {
311-
if (!mTranslators.containsKey(apiVer)) {
312-
ClassTranslator ct = new ClassTranslator();
313-
ct.apiVersion = apiVer;
314-
mTranslators.put(apiVer, ct);
332+
private ClassTranslator getClassTranslator(List<ClassTranslator> lTranslators, String apiVer) {
333+
int idx = lTranslators.indexOf(new ClassTranslator(apiVer));
334+
if (idx != -1) {
335+
return lTranslators.get(idx);
315336
}
316-
return mTranslators.get(apiVer);
337+
ClassTranslator ct = new ClassTranslator(apiVer);
338+
ct.apiVersion = apiVer;
339+
lTranslators.add(ct);
340+
return ct;
317341
}
318342

319343
/**
@@ -388,10 +412,12 @@ private Apis loadApis(JsonNode apis, File f, String apiVersion) throws IOExcepti
388412
action.path = txt(apiEntry.get("path"));
389413
action.description = txt(apiEntry.get("description"));
390414
action.javaFile = f.getName();
415+
action.api = api;
391416

392417
for (JsonNode operation : apiEntry.get("operations")) {
393418
Operation op = new Operation();
394419
action.operations.add(op);
420+
op.action = action;
395421
op.method = txt(operation.get("httpMethod"));
396422
if ("websocket".equalsIgnoreCase(txt(operation.get("upgrade")))) {
397423
op.wsUpgrade = true;
@@ -400,6 +426,7 @@ private Apis loadApis(JsonNode apis, File f, String apiVersion) throws IOExcepti
400426
op.responseInterface = remapAbstractType(txt(operation.get("responseClass")));
401427
op.responseConcreteClass = remapConcreteType(txt(operation.get("responseClass")), apiVersion);
402428
op.description = txt(operation.get("summary")) + "\n" + txt(operation.get("notes"));
429+
op.apiVersion = apiVersion;
403430

404431
JsonNode parameters = operation.get("parameters");
405432
if (parameters != null) {
@@ -409,9 +436,7 @@ private Apis loadApis(JsonNode apis, File f, String apiVersion) throws IOExcepti
409436
p.name = txt(parameter.get("name"));
410437
p.required = txt(parameter.get("required")).equalsIgnoreCase("true");
411438
p.type = Operation.ParamType.build(txt(parameter.get("paramType")));
412-
413-
op.parms.add(p);
414-
439+
op.params.add(p);
415440
}
416441
}
417442

@@ -425,10 +450,8 @@ private Apis loadApis(JsonNode apis, File f, String apiVersion) throws IOExcepti
425450
}
426451
}
427452

428-
429453
}
430454

431-
//System.out.println( action.toString() );
432455
api.actions.add(action);
433456
}
434457

@@ -439,17 +462,18 @@ private Apis loadApis(JsonNode apis, File f, String apiVersion) throws IOExcepti
439462
}
440463

441464
public void saveToDisk(String pkgName, String className, String classText) throws IOException {
442-
443465
String fName = outputFolder
444466
+ pkgName.replace(".", "/")
445467
+ "/"
446468
+ className + ".java";
447469

448-
FileWriter outFile = new FileWriter(fName);
470+
File f = new File(fName);
471+
f.getParentFile().mkdirs();
472+
// System.out.println("Saving: " + f.getAbsolutePath());
473+
FileWriter outFile = new FileWriter(f);
449474
PrintWriter out = new PrintWriter(outFile);
450475
out.println(classText);
451476
out.close();
452-
453477
}
454478

455479
public void saveToDisk(Model model) throws IOException {
@@ -460,28 +484,34 @@ public void saveToDisk(Apis api) throws IOException {
460484
saveToDisk(api.getActionsPackage(), api.getImplName(), api.toString());
461485
}
462486

487+
public void saveToDisk(Operation operation) throws IOException {
488+
saveToDisk(operation.getPackage(), operation.getImplName(), operation.toString());
489+
}
490+
463491
public void saveToDisk(JavaInterface ji) throws IOException {
464-
saveToDisk("ch.loway.oss.ari4java.generated", ji.className, ji.toString());
492+
saveToDisk(ji.pkgName, ji.className, ji.toString());
465493
}
466494

467495
public void saveToDisk(ClassTranslator ct) throws IOException {
468496
saveToDisk(ct.getBaseApiPackage(), ct.getImplName(), ct.toString());
469497
}
470498

471-
public void clean(String apiVersion) throws IOException {
472-
String base = outputFolder + "ch/loway/oss/ari4java/generated";
473-
cleanPath(base + "/" + apiVersion + "/actions");
474-
cleanPath(base + "/" + apiVersion + "/models");
499+
public void clean() {
500+
deleteFolder(new File(outputFolder));
475501
}
476502

477-
private void cleanPath(String path) throws IOException {
478-
File p = new File(path);
479-
p.mkdirs();
480-
for (File f : p.listFiles()) {
481-
if (f.isFile()) {
482-
f.delete();
503+
private void deleteFolder(File folder) {
504+
File[] files = folder.listFiles();
505+
if(files != null) {
506+
for(File f: files) {
507+
if(f.isDirectory()) {
508+
deleteFolder(f);
509+
} else {
510+
f.delete();
511+
}
483512
}
484513
}
514+
folder.delete();
485515
}
486516

487517
private String txt(JsonNode n) {
@@ -542,11 +572,10 @@ private void writeAriBuilder(String apiVersion, AriBuilderInterface abi, Collect
542572
StringBuilder sb = new StringBuilder();
543573
JavaGen.importClasses(sb, "ch.loway.oss.ari4java.generated." + apiVersion,
544574
Arrays.asList(new String[]{
545-
"ch.loway.oss.ari4java.generated." + apiVersion + ".models.*",
575+
"ch.loway.oss.ari4java.ARI",
576+
"ch.loway.oss.ari4java.generated.AriBuilder",
577+
"ch.loway.oss.ari4java.generated.actions.*",
546578
"ch.loway.oss.ari4java.generated." + apiVersion + ".actions.*",
547-
"ch.loway.oss.ari4java.generated.Module",
548-
"ch.loway.oss.ari4java.generated.*",
549-
"ch.loway.oss.ari4java.ARI"
550579
}));
551580

552581
sb.append("public class ").append(thisClass).append(" implements AriBuilder {\n\n");
@@ -557,12 +586,6 @@ private void writeAriBuilder(String apiVersion, AriBuilderInterface abi, Collect
557586
sb.append(AriBuilderInterface.getMethod(ifc, apiVersion));
558587
}
559588

560-
for (Model m : models) {
561-
String ifc = m.getInterfaceName();
562-
ifToImplement.remove(ifc);
563-
sb.append(AriBuilderInterface.getMethod(ifc, apiVersion));
564-
}
565-
566589
// do we have any unimplemented interface?
567590
for (String ifc : ifToImplement) {
568591
sb.append(AriBuilderInterface.getUnimplemented(ifc));

codegen/src/main/java/ch/loway/oss/ari4java/codegen/VERSION.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @author lenz
77
*/
88
public class VERSION {
9-
public static final String VER = "0.3";
9+
public static final String VER = "0.4";
1010
}
1111

codegen/src/main/java/ch/loway/oss/ari4java/codegen/genJava/JavaGen.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ public static void importClasses(StringBuilder sb, String myPackage, List<String
3434
public static void addBanner(StringBuilder sb, String multiLineBanner) {
3535

3636
String[] rows = multiLineBanner.split("\n");
37-
sb.append("/**********************************************************\n");
37+
sb.append("/**\n");
3838
for (String row : rows) {
39-
sb.append(" * ").append(row).append("\n");
39+
if (!row.isEmpty()) {
40+
row = row.replaceAll("<br /><br />", "\n * ");
41+
row = row.replaceAll("<br />", "\n * ");
42+
sb.append(" * ").append(row).append("\n");
43+
}
4044
}
41-
sb.append(" *********************************************************/\n");
45+
sb.append(" */\n");
4246

4347
}
4448

4549
public static void addBanner(StringBuilder sb, String multilineBanner, String sinceVersion) {
46-
multilineBanner += "\n\n@since " + sinceVersion;
50+
multilineBanner += "\n@since " + sinceVersion;
4751
addBanner(sb, multilineBanner);
4852
}
4953

0 commit comments

Comments
 (0)