Skip to content

Commit

Permalink
Fix problem with long names of event classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Sep 9, 2016
1 parent 9418e9d commit a59a878
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Expand Up @@ -30,8 +30,6 @@
import org.apache.avro.Schema; import org.apache.avro.Schema;
import org.apache.avro.Schema.Field; import org.apache.avro.Schema.Field;
import org.apache.avro.Schema.Type; import org.apache.avro.Schema.Type;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.kaaproject.kaa.avro.avrogen.GenerationContext; import org.kaaproject.kaa.avro.avrogen.GenerationContext;
Expand Down Expand Up @@ -61,6 +59,9 @@ public abstract class Compiler {
protected String namespacePrefix; protected String namespacePrefix;


protected final Map<Schema, GenerationContext> schemaGenerationQueue; protected final Map<Schema, GenerationContext> schemaGenerationQueue;
// list of schemas that should be skipped during generation
protected Set<Schema> generatedSchemas = new HashSet<>();



private void initVelocityEngine() { private void initVelocityEngine() {
engine = new VelocityEngine(); engine = new VelocityEngine();
Expand Down Expand Up @@ -98,6 +99,12 @@ public Compiler(List<Schema> schemas, String sourceName, OutputStream hdrS, Outp
prepareTemplates(false); prepareTemplates(false);
} }



public Compiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS, Set<Schema> generatedSchemas) throws KaaGeneratorException {
this(schemas, sourceName, hdrS, srcS);
this.generatedSchemas = new HashSet<>(generatedSchemas);
}

public Compiler(String schemaPath, String outputPath, String sourceName) throws KaaGeneratorException { public Compiler(String schemaPath, String outputPath, String sourceName) throws KaaGeneratorException {
this(sourceName); this(sourceName);
try { try {
Expand Down Expand Up @@ -172,10 +179,11 @@ private void writeToFile(StringWriter hdrWriter, StringWriter srcWriter) throws
} }




public void generate() throws KaaGeneratorException { public Set<Schema> generate() throws KaaGeneratorException {
try { try {
LOG.debug("Processing schemas: [" + join(schemas, ", ") + "]"); LOG.debug("Processing schemas: [" + join(schemas, ", ") + "]");
for (Schema schema : schemas) { for (Schema schema : schemas) {

if (schema.getType() == Type.UNION) { if (schema.getType() == Type.UNION) {
for (Schema s : schema.getTypes()) { for (Schema s : schema.getTypes()) {
addAllSchemasToQueue(s, null); addAllSchemasToQueue(s, null);
Expand All @@ -185,9 +193,11 @@ public void generate() throws KaaGeneratorException {
} }
} }



doGenerate(); doGenerate();


LOG.debug("Sources were successfully generated"); LOG.debug("Sources were successfully generated");
return schemaGenerationQueue.keySet();
} catch (Exception e) { } catch (Exception e) {
LOG.error("Failed to generate C sources: ", e); LOG.error("Failed to generate C sources: ", e);
throw new KaaGeneratorException("Failed to generate sources: " + e.toString()); throw new KaaGeneratorException("Failed to generate sources: " + e.toString());
Expand Down Expand Up @@ -280,5 +290,4 @@ public void setNamespacePrefix(String namespacePrefix) {
this.namespacePrefix = namespacePrefix; this.namespacePrefix = namespacePrefix;
} }



} }
Expand Up @@ -24,6 +24,7 @@
import java.lang.*; import java.lang.*;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;


public class ObjectiveCCompiler extends Compiler { public class ObjectiveCCompiler extends Compiler {


Expand All @@ -42,6 +43,10 @@ public ObjectiveCCompiler(List<Schema> schemas, String sourceName, OutputStream
super(schemas, sourceName, hdrS, srcS); super(schemas, sourceName, hdrS, srcS);
} }


public ObjectiveCCompiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS, Set<Schema> generatedSchemas) throws KaaGeneratorException {
super(schemas, sourceName, hdrS, srcS, generatedSchemas);
}

@Override @Override
protected String headerTemplateGen() { protected String headerTemplateGen() {
return "src/main/resources/ObjC/headerObjC.tmpl.gen"; return "src/main/resources/ObjC/headerObjC.tmpl.gen";
Expand Down Expand Up @@ -70,13 +75,15 @@ protected String getSourceExtension() {
@Override @Override
protected void doGenerate() { protected void doGenerate() {
for (Map.Entry<Schema, GenerationContext> cursor : schemaGenerationQueue.entrySet()) { for (Map.Entry<Schema, GenerationContext> cursor : schemaGenerationQueue.entrySet()) {
switch (cursor.getKey().getType()) { if (!generatedSchemas.contains(cursor.getKey())) { // process only not generated schemas
case RECORD: switch (cursor.getKey().getType()) {
processRecord(cursor.getKey(), "ObjC/recordObjC.h.vm", "ObjC/recordObjC.m.vm"); case RECORD:
break; processRecord(cursor.getKey(), "ObjC/recordObjC.h.vm", "ObjC/recordObjC.m.vm");
case ENUM: break;
processEnum(cursor.getKey(), "ObjC/enumObjC.h.vm"); case ENUM:
break; processEnum(cursor.getKey(), "ObjC/enumObjC.h.vm");
break;
}
} }
} }
} }
Expand Down
Expand Up @@ -33,8 +33,11 @@
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;


import static org.kaaproject.kaa.server.control.service.sdk.SchemaUtil.getChildSchemas;

public class ObjCEventClassesGenerator { public class ObjCEventClassesGenerator {


private static final Logger LOG = LoggerFactory.getLogger(ObjCEventClassesGenerator.class); private static final Logger LOG = LoggerFactory.getLogger(ObjCEventClassesGenerator.class);
Expand Down Expand Up @@ -75,7 +78,7 @@ public class ObjCEventClassesGenerator {
private static final String EVENT_FAMILY_FACTORY_IMPORTS_VAR = "\\{event_family_factory_imports\\}"; private static final String EVENT_FAMILY_FACTORY_IMPORTS_VAR = "\\{event_family_factory_imports\\}";
private static final String EVENT_FAMILY_FACTORY_PROPERTIES_VAR = "\\{event_family_factory_properties\\}"; private static final String EVENT_FAMILY_FACTORY_PROPERTIES_VAR = "\\{event_family_factory_properties\\}";
private static final String EVENT_FAMILY_FACTORY_METHODS_VAR = "\\{event_family_factory_methods\\}"; private static final String EVENT_FAMILY_FACTORY_METHODS_VAR = "\\{event_family_factory_methods\\}";
public static final String EVENT_CLASS = "EventClass"; public static final String KAA_EVENT_PREFIX = "KAAEvent";


private static String eventFamilyHeader; private static String eventFamilyHeader;
private static String eventFamilySource; private static String eventFamilySource;
Expand Down Expand Up @@ -121,6 +124,7 @@ public static List<TarEntryData> generateEventSources(List<EventFamilyMetadata>
StringBuilder eventGenSourceBuilder = new StringBuilder(); StringBuilder eventGenSourceBuilder = new StringBuilder();


LOG.debug("Received {} event families", eventFamilies.size()); LOG.debug("Received {} event families", eventFamilies.size());
HashSet<Schema> generatedSchemas = new HashSet<>();
for (EventFamilyMetadata efm : eventFamilies) { for (EventFamilyMetadata efm : eventFamilies) {


LOG.debug("Generating schemas for event family {}", efm.getEcfName()); LOG.debug("Generating schemas for event family {}", efm.getEcfName());
Expand All @@ -132,9 +136,9 @@ public static List<TarEntryData> generateEventSources(List<EventFamilyMetadata>
OutputStream srcStream = new ByteArrayOutputStream() OutputStream srcStream = new ByteArrayOutputStream()
) { ) {


Compiler compiler = new ObjectiveCCompiler(eventCtlSchemas, EVENT_GEN, hdrStream, srcStream); Compiler compiler = new ObjectiveCCompiler(eventCtlSchemas, EVENT_GEN, hdrStream, srcStream, generatedSchemas);
compiler.setNamespacePrefix(efm.getEcfClassName()); compiler.setNamespacePrefix(KAA_EVENT_PREFIX);
compiler.generate(); generatedSchemas.addAll(compiler.generate());


eventGenHeaderBuilder.append(hdrStream.toString()).append("\n"); eventGenHeaderBuilder.append(hdrStream.toString()).append("\n");
eventGenSourceBuilder.append(srcStream.toString()).append("\n"); eventGenSourceBuilder.append(srcStream.toString()).append("\n");
Expand Down Expand Up @@ -168,7 +172,7 @@ public static List<TarEntryData> generateEventSources(List<EventFamilyMetadata>
String eventFamilyListenerMethods = ""; String eventFamilyListenerMethods = "";


for (ApplicationEventMapDto eventMap : efm.getEventMaps()) { for (ApplicationEventMapDto eventMap : efm.getEventMaps()) {
String eventClassName = efm.getEcfClassName() + eventFqnToClassName(eventMap.getFqn()); String eventClassName = eventFqnToClassName(eventMap.getFqn());
if (eventMap.getAction() == ApplicationEventAction.SINK || if (eventMap.getAction() == ApplicationEventAction.SINK ||
eventMap.getAction() == ApplicationEventAction.BOTH) { eventMap.getAction() == ApplicationEventAction.BOTH) {
addSupportedEventClassFqns += eventFamilyAddSupportedFqn addSupportedEventClassFqns += eventFamilyAddSupportedFqn
Expand Down Expand Up @@ -240,6 +244,6 @@ private static String eventFqnToClassName(String fqn) {
if (fqn == null || fqn.isEmpty()) { if (fqn == null || fqn.isEmpty()) {
throw new RuntimeException("Failed to get class name from fqn: " + fqn); throw new RuntimeException("Failed to get class name from fqn: " + fqn);
} }
return fqn.substring(fqn.lastIndexOf('.') + 1); return KAA_EVENT_PREFIX + fqn.substring(fqn.lastIndexOf('.') + 1);
} }
} }

0 comments on commit a59a878

Please sign in to comment.