Skip to content

Commit fcaa12f

Browse files
committed
Improving control exception
1 parent a42d496 commit fcaa12f

File tree

16 files changed

+264
-49
lines changed

16 files changed

+264
-49
lines changed

jsondiscoverer.examples/src/jsondiscoverer/ExampleJsonComposer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static void exampleCompose() throws FileNotFoundException, IOException {
8383
sourceSetList.add(api2SourceSet);
8484

8585
JsonComposer composer = new JsonComposer(sourceSetList);
86-
EPackage composed = composer.compose(null);
86+
EPackage composed = composer.compose();
8787
ModelHelper.saveEPackage(composed, new File("./exampleData/composer/exampleComposer-composed.ecore"));
8888

8989
}

jsondiscoverer.tests/src/jsondiscoverer/test/TestJsonComposer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void testCompose() throws FileNotFoundException, IOException {
8080
sourceSetList.add(api2SourceSet);
8181

8282
JsonComposer composer = new JsonComposer(sourceSetList);
83-
EPackage composed = composer.compose(null);
83+
EPackage composed = composer.compose();
8484
assertNotNull(composed);
8585

8686
EClass stopPositionInput = (EClass) composed.getEClassifier("StopPositionInput");

jsondiscoverer.web/src/jsondiscoverer/web/JsonAdvancedDiscovererServlet.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,24 @@ public class JsonAdvancedDiscovererServlet extends AbstractJsonDiscoverer {
4848
private static final long serialVersionUID = 23L;
4949

5050
// The ID for this servlet which will be used to access to the working directory
51-
public static final String MULTIDISCOVERER_ID = "IdMultiDiscoverer";
51+
public static final String ADVANCEDDISCOVERER_FOLDER = "SubfolderAdvancedDiscoverer";
52+
53+
/**
54+
* Name of the folder where the temp files will be stored
55+
*/
56+
private String folderName;
5257

53-
// This pattern is used to analyze the params
54-
// The format is sources[JSON_SOURCE_NAME][SOMETHING]([])?
55-
// The important part is the JSON_SOURCE_NAME which provides the name of the parameter
58+
/** This pattern is used to analyze the params
59+
* The format is sources[JSON_SOURCE_NAME][SOMETHING]([])?
60+
* The important part is the JSON_SOURCE_NAME which provides the name of the parameter */
5661
private static String paramsPattern = Pattern.quote("sources[") + "([a-zA-Z]*)"+ Pattern.quote("][") + "[\\$a-zA-Z]*" + Pattern.quote("]") + "(" + Pattern.quote("[]") + ")?";
5762

63+
public JsonAdvancedDiscovererServlet() {
64+
super();
65+
folderName = properties.getProperty(ADVANCEDDISCOVERER_FOLDER);
66+
if(folderName == null) throw new IllegalStateException("ID for Advanced Discoverer not found in properties");
67+
}
68+
5869
/**
5970
* Digest the parameters of the request according to the pattern defined in
6071
* paramsPattern
@@ -109,17 +120,16 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
109120
EPackage finalMetamodel = multiDiscoverer.discover();
110121

111122
// 3. Get the picture
112-
String id = properties.getProperty(MULTIDISCOVERER_ID);
113-
if(id == null) throw new ServletException("ID for composer not found in properties");
123+
if(folderName == null) throw new ServletException("ID for composer not found in properties");
114124

115125
List<EObject> toDraw= new ArrayList<EObject>();
116126
toDraw.add(finalMetamodel);
117-
File resultPath = drawModel(toDraw, id);
127+
File resultPath = drawModel(toDraw, folderName);
118128
String resultImage = encodeToString(resultPath);
119129
resultPath.delete();
120130

121131
// 4. Get the metamodel as string
122-
String resultXMI = encodeToString(finalMetamodel, id);
132+
String resultXMI = encodeToString(finalMetamodel, folderName);
123133

124134
// 4. Write the response
125135
// Building the response

jsondiscoverer.web/src/jsondiscoverer/web/JsonComposerServlet.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
package jsondiscoverer.web;
1414

15-
import java.io.File;
1615
import java.io.IOException;
1716
import java.io.PrintWriter;
1817
import java.io.StringReader;
@@ -46,34 +45,21 @@
4645
@WebServlet("/composer")
4746
public class JsonComposerServlet extends AbstractJsonDiscoverer {
4847
private static final long serialVersionUID = 335L;
49-
// This pattern is used to analyze the params
50-
// The format is sources[JSON_SOURCE_NAME][SOMETHING]([])?([input|output])?
51-
// The important part is the JSON_SOURCE_NAME which provides the name of the parameter
48+
49+
/** This pattern is used to analyze the params
50+
* The format is sources[JSON_SOURCE_NAME][SOMETHING]([])?([input|output])?
51+
* The important part is the JSON_SOURCE_NAME which provides the name of the parameter */
5252
private static String paramsPattern = Pattern.quote("sources[") + "([a-zA-Z]*)"+ Pattern.quote("]") +
5353
Pattern.quote("[") + "[\\$a-zA-Z]*" + Pattern.quote("]") +
5454
"(" + Pattern.quote("[") + "[0-9]*" + Pattern.quote("]") + ")?" +
5555
"(" + Pattern.quote("[") + "[a-zA-Z]*" + Pattern.quote("]") + ")?";
56-
57-
58-
protected EPackage composer(HashMap<String, List<String>> sources) {
59-
List<JsonSourceSet> sourceSets = new ArrayList<JsonSourceSet>();
60-
61-
for(String sourceName : sources.keySet()) {
62-
List<String> sourcesList = sources.get(sourceName);
63-
JsonSource source = new JsonSource(sourceName);
64-
for(String json : sourcesList) {
65-
source.addJsonData(null, new StringReader(json));
66-
}
67-
JsonSourceSet sourceSet = new JsonSourceSet(sourceName+"Set");
68-
sourceSet.addJsonSource(source);
69-
sourceSets.add(sourceSet);
70-
}
71-
72-
JsonComposer composer = new JsonComposer(sourceSets);
73-
EPackage result = composer.compose(new File(workingDir.getAbsoluteFile() + File.separator + "composed.ecore"));
74-
return result;
75-
}
76-
56+
57+
/**
58+
* Digest the received sources according to the pattern (see {@link JsonComposerServlet.paramsPattern}
59+
*
60+
* @param request The HTTP request
61+
* @return A list of {@link JsonSourceSet}
62+
*/
7763
protected List<JsonSourceSet> digestSources(HttpServletRequest request) {
7864
List<JsonSourceSet> result = new ArrayList<JsonSourceSet>();
7965

@@ -127,7 +113,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
127113

128114
// 2. Composition
129115
JsonComposer composer = new JsonComposer(sourceSets);
130-
EPackage finalMetamodel = composer.compose(new File(workingDir.getAbsoluteFile() + File.separator + "composed.ecore"));
116+
EPackage finalMetamodel = composer.compose(null);
131117

132118
// 3. Getting the graph
133119
String gexfString = GexfConverter.convert(finalMetamodel);

jsondiscoverer.web/src/jsondiscoverer/web/JsonSimpleDiscovererServlet.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,18 @@ public class JsonSimpleDiscovererServlet extends AbstractJsonDiscoverer {
4343
private static final long serialVersionUID = 83L;
4444

4545
// The ID for this servlet which will be used to access to the working directory
46-
public static final String DISCOVERER_ID = "IdDiscoverer";
46+
public static final String SIMPLEDISCOVERER_FOLDER = "SubfolderSimpleDiscoverer";
47+
48+
/**
49+
* Name of the folder where the temp files will be stored
50+
*/
51+
private String folderName;
52+
53+
public JsonSimpleDiscovererServlet() {
54+
super();
55+
folderName = properties.getProperty(SIMPLEDISCOVERER_FOLDER);
56+
if(folderName == null) throw new IllegalStateException("ID for Simple Discoverer not found in properties");
57+
}
4758

4859
/*
4960
* Performs a POST call to this servlet. The JSON_CODE parameter is queried to get the JSON code to
@@ -58,9 +69,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
5869

5970
EPackage resultMetamodel = discoverMetamodel(jsonCode);
6071

61-
String id = properties.getProperty(DISCOVERER_ID);
6272
String resultImage = discoverMetamodelBase64(resultMetamodel);
63-
String resultXMI = encodeToString(resultMetamodel, id);
73+
String resultXMI = encodeToString(resultMetamodel, folderName);
6474

6575
// Building the response
6676
response.setContentType("text/x-json;charset=UTF-8");
@@ -101,7 +111,7 @@ private File convertToImage(EPackage ePackage) throws ServletException {
101111
List<EObject> toDraw= new ArrayList<EObject>();
102112
toDraw.add(ePackage);
103113

104-
String id = properties.getProperty(DISCOVERER_ID);
114+
String id = properties.getProperty(SIMPLEDISCOVERER_FOLDER);
105115
if(id == null) throw new ServletException("ID for discoverer not found in properties");
106116

107117
File resultPath = drawModel(toDraw, id);

jsondiscoverer.web/src/jsondiscoverer/web/StatusServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
4747
out.println("injector dir: " + ((injectorFile.isDirectory()) ? "Exists" : "NOT EXISTS"));
4848
}
4949

50-
String discovererId = properties.getProperty(JsonSimpleDiscovererServlet.DISCOVERER_ID);
50+
String discovererId = properties.getProperty(JsonSimpleDiscovererServlet.SIMPLEDISCOVERER_FOLDER);
5151
if(discovererId == null || discovererId.equals("")) {
5252
out.println("discoverer dir: NOT EXISTS (neither the property)");
5353
} else {
5454
File discovererFile = new File(workingDir.getAbsolutePath() + File.separator + discovererId);
5555
out.println("discoverer dir: " + ((discovererFile.isDirectory()) ? "Exists" : "NOT EXISTS"));
5656
}
5757

58-
String multidiscovererId = properties.getProperty(JsonAdvancedDiscovererServlet.MULTIDISCOVERER_ID);
58+
String multidiscovererId = properties.getProperty(JsonAdvancedDiscovererServlet.ADVANCEDDISCOVERER_FOLDER);
5959
if(multidiscovererId == null || multidiscovererId.equals("")) {
6060
out.println("multidiscoverer dir: NOT EXISTS (neither the property)");
6161
} else {

jsondiscoverer/src/jsondiscoverer/AbstractJsonSource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public abstract class AbstractJsonSource {
3333
private EPackage metamodel;
3434

3535
public AbstractJsonSource(String name) {
36+
if(name == null || name.equals(""))
37+
throw new IllegalArgumentException("Name cannot be null or empty");
3638
this.name = name;
3739
}
3840

jsondiscoverer/src/jsondiscoverer/AnnotationHelper.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public class AnnotationHelper {
4444
private AnnotationHelper() {}
4545

4646
public EAnnotation getCoverageAnnotation(EModelElement modelElement) {
47+
if(modelElement == null)
48+
throw new IllegalArgumentException("The modelElement cannot be null");
49+
4750
EAnnotation annotation = modelElement.getEAnnotation(COVERAGE_TAG);
4851
if(annotation == null) {
4952
annotation = EcoreFactory.eINSTANCE.createEAnnotation();
@@ -55,6 +58,9 @@ public EAnnotation getCoverageAnnotation(EModelElement modelElement) {
5558
}
5659

5760
public EAnnotation getFoundInAnnotation(EModelElement modelElement) {
61+
if(modelElement == null)
62+
throw new IllegalArgumentException("The modelElement cannot be null");
63+
5864
EAnnotation annotation = getCoverageAnnotation(modelElement);
5965
EAnnotation namesAnnotation = annotation.getEAnnotation(FOUND_IN_TAG);
6066
if(namesAnnotation == null) {
@@ -66,6 +72,9 @@ public EAnnotation getFoundInAnnotation(EModelElement modelElement) {
6672
}
6773

6874
public EAnnotation getAkaAnnotation(EModelElement modelElement) {
75+
if(modelElement == null)
76+
throw new IllegalArgumentException("The modelElement cannot be null");
77+
6978
EAnnotation annotation = getCoverageAnnotation(modelElement);
7079
EAnnotation akaAnnotation = annotation.getEAnnotation(AKA_TAG);
7180
if(akaAnnotation == null) {
@@ -77,6 +86,9 @@ public EAnnotation getAkaAnnotation(EModelElement modelElement) {
7786
}
7887

7988
public void increaseTotalFound(EModelElement modelElement) {
89+
if(modelElement == null)
90+
throw new IllegalArgumentException("The modelElement cannot be null");
91+
8092
EAnnotation annotation = getCoverageAnnotation(modelElement);
8193
if(annotation != null) {
8294
String currentCounter = annotation.getDetails().get(TOTAL_FOUND_TAG);
@@ -87,6 +99,9 @@ public void increaseTotalFound(EModelElement modelElement) {
8799
}
88100

89101
public void calculateCoverage(EPackage ePackage) {
102+
if(ePackage == null)
103+
throw new IllegalArgumentException("The ePackage cannot be null");
104+
90105
for(EClassifier eClassifier : ePackage.getEClassifiers()) {
91106
if (eClassifier instanceof EClass) {
92107
EClass eClass = (EClass) eClassifier;
@@ -104,6 +119,11 @@ public void calculateCoverage(EPackage ePackage) {
104119
}
105120

106121
public void registerInclusion(EModelElement modelElement, String name) {
122+
if(modelElement == null)
123+
throw new IllegalArgumentException("The modelElement cannot be null");
124+
if(name == null)
125+
throw new IllegalArgumentException("The name cannot be null");
126+
107127
EAnnotation annotation = getFoundInAnnotation(modelElement);
108128
String times = annotation.getDetails().get(name);
109129
if(times == null) {
@@ -114,6 +134,11 @@ public void registerInclusion(EModelElement modelElement, String name) {
114134
}
115135

116136
public void registerName(EModelElement modelElement, String name) {
137+
if(modelElement == null)
138+
throw new IllegalArgumentException("The modelElement cannot be null");
139+
if(name == null)
140+
throw new IllegalArgumentException("The name cannot be null");
141+
117142
EAnnotation annotation = getAkaAnnotation(modelElement);
118143
String times = annotation.getDetails().get(name);
119144
if(times == null) {
@@ -124,16 +149,25 @@ public void registerName(EModelElement modelElement, String name) {
124149
}
125150

126151
public int getTotalFound(EModelElement modelElement) {
152+
if(modelElement == null)
153+
throw new IllegalArgumentException("The modelElement cannot be null");
154+
127155
EAnnotation annotation = getCoverageAnnotation(modelElement);
128156
return (annotation.getDetails().get(TOTAL_FOUND_TAG) != null) ? Integer.valueOf(annotation.getDetails().get(TOTAL_FOUND_TAG)).intValue() : 0;
129157
}
130158

131159
public double getRatioTotalFound(EModelElement modelElement) {
160+
if(modelElement == null)
161+
throw new IllegalArgumentException("The modelElement cannot be null");
162+
132163
EAnnotation annotation = getCoverageAnnotation(modelElement);
133164
return (annotation.getDetails().get(RATIO_TOTAL_FOUND_TAG) != null) ? Double.valueOf(annotation.getDetails().get(RATIO_TOTAL_FOUND_TAG)).doubleValue() : 0.0;
134165
}
135166

136167
public List<String> getAka(EModelElement modelElement) {
168+
if(modelElement == null)
169+
throw new IllegalArgumentException("The modelElement cannot be null");
170+
137171
EAnnotation annotation = getAkaAnnotation(modelElement);
138172
List<String> result = new ArrayList<String>();
139173
Iterator<String> keyIt = annotation.getDetails().keySet().iterator();
@@ -145,11 +179,19 @@ public List<String> getAka(EModelElement modelElement) {
145179
}
146180

147181
public void registerSourceName(EClass modelElement, String name) {
182+
if(modelElement == null)
183+
throw new IllegalArgumentException("The modelElement cannot be null");
184+
if(name == null)
185+
throw new IllegalArgumentException("The name cannot be null");
186+
148187
EAnnotation annotation = getCoverageAnnotation(modelElement);
149188
annotation.getDetails().put(SOURCE_NAME_TAG, name);
150189
}
151190

152191
public String getSourceName(EClass modelElement) {
192+
if(modelElement == null)
193+
throw new IllegalArgumentException("The modelElement cannot be null");
194+
153195
EAnnotation annotation = getCoverageAnnotation(modelElement);
154196
return annotation.getDetails().get(SOURCE_NAME_TAG);
155197
}
@@ -161,6 +203,9 @@ public String getSourceName(EClass modelElement) {
161203
* @return
162204
*/
163205
public EPackage cleanAnnotations(EPackage ePackage) {
206+
if(ePackage == null)
207+
throw new IllegalArgumentException("The ePackage cannot be null");
208+
164209
for(EClassifier eClassifier : ePackage.getEClassifiers()) {
165210
eClassifier.getEAnnotations().clear();
166211
if (eClassifier instanceof EClass) {

jsondiscoverer/src/jsondiscoverer/CoreographyBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class CoreographyBuilder {
2626
private EPackage domain;
2727

2828
public CoreographyBuilder(EPackage ePackage) {
29+
if(ePackage == null)
30+
throw new IllegalArgumentException("The ePackage cannot be null");
31+
2932
this.domain = ePackage;
3033
}
3134

@@ -94,6 +97,11 @@ public String calculate(EClass source, EClass target) {
9497
}
9598

9699
public String discoverMapping(EClass source, EClass target) {
100+
if(source == null)
101+
throw new IllegalArgumentException("A source has to be provided");
102+
if(target == null)
103+
throw new IllegalArgumentException("A target has to be provided");
104+
97105
String result = "";
98106

99107
List<EAttribute> visited = new ArrayList<EAttribute>();
@@ -112,6 +120,11 @@ public String discoverMapping(EClass source, EClass target) {
112120
}
113121

114122
private boolean isSimilar(EAttribute source, EAttribute target) {
123+
if(source == null)
124+
throw new IllegalArgumentException("A source has to be provided");
125+
if(target == null)
126+
throw new IllegalArgumentException("A target has to be provided");
127+
115128
if(source.getName().equals(target.getName())) return true;
116129

117130
String sourceName = source.getName();

0 commit comments

Comments
 (0)