    001    package com.hammurapi.eventbus.local;
    002
    003    import java.io.File;
    004    import java.io.FileWriter;
    005    import java.io.Writer;
    006    import java.util.HashMap;
    007    import java.util.Map;
    008
    009    import org.onemind.jxp.JxpContext;
    010    import org.onemind.jxp.JxpPageSource;
    011    import org.onemind.jxp.JxpProcessor;
    012    import org.onemind.jxp.ResourceStreamPageSource;
    013
    014    import com.hammurapi.eventbus.EventBusException;
    015
    016    /**
    017     * This class generates domain-specific implementations of
    018     * event store and event bus.
    019     * @author Pavel Vlasov
    020     *
    021     */
    022    public class DomainGenerator {
    023
    024            private String packageName;
    025            private String domain;
    026            private String eventType;
    027            private String priorityType;
    028            private String contextType;
    029            private File outputDir;
    030
    031            public String getPackageName() {
    032                    return packageName;
    033            }
    034
    035            public void setPackageName(String packageName) {
    036                    this.packageName = packageName;
    037            }
    038
    039            public String getDomain() {
    040                    return domain;
    041            }
    042
    043            public void setDomain(String domain) {
    044                    this.domain = domain;
    045            }
    046
    047            public String getEventType() {
    048                    return eventType;
    049            }
    050
    051            public void setEventType(String eventType) {
    052                    this.eventType = eventType;
    053            }
    054
    055            public String getPriorityType() {
    056                    return priorityType;
    057            }
    058
    059            public void setPriorityType(String priorityType) {
    060                    this.priorityType = priorityType;
    061            }
    062
    063            public String getContextType() {
    064                    return contextType;
    065            }
    066
    067            public void setContextType(String contextType) {
    068                    this.contextType = contextType;
    069            }
    070
    071            public File getOutputDir() {
    072                    return outputDir;
    073            }
    074
    075            public void setOutputDir(File outputDir) {
    076                    this.outputDir = outputDir;
    077            }
    078
    079            public void generate() {
    080                    Map<String, String> environment = new HashMap<String,String>();
    081                    environment.put("package", packageName);
    082                    environment.put("domain", domain);
    083                    environment.put("E", eventType);
    084                    environment.put("P", priorityType);
    085                    environment.put("C", contextType);
    086
    087                    try {
    088                            String prefix = getClass().getPackage().getName().replace('.', '/');
    089                            JxpPageSource pageSource = new ResourceStreamPageSource("/"+prefix);
    090                            JxpContext context = new JxpContext(pageSource);
    091                            JxpProcessor processor = new JxpProcessor(context);
    092
    093                            File packageDir = new File(outputDir, packageName.replace('.', File.separatorChar));
    094                            packageDir.mkdirs();
    095
    096                            renderEventStore(processor, environment, packageDir);
    097                            renderEventStoreImpl(processor, environment, packageDir);
    098                            renderEventStorePredicate(processor, environment, packageDir);
    099                            renderEventStoreAbstractPredicate(processor, environment, packageDir);
    100                            renderEventStoreExtractor(processor, environment, packageDir);
    101                            renderEventStoreAbstractExtractor(processor, environment, packageDir);
    102                            renderEventBus(processor, environment, packageDir);
    103                            renderIntrospector(processor, environment, packageDir);
    104                            renderDispatchNetworkSnapshot(processor, environment, packageDir);
    105                            renderDispatchContext(processor, environment, packageDir);
    106                            renderDispatchJoinContext(processor, environment, packageDir);
    107
    108                    } catch (Exception e) {
    109                            throw new EventBusException(e);
    110                    }
    111            }
    112
    113            private void renderEventStore(
    114                            JxpProcessor processor,
    115                            Map<String, String> environment,
    116                            File packageDir) throws Exception {
    117                    File outputFile = new File(packageDir, domain+"EventStore.java");
    118                    Writer writer = new FileWriter(outputFile);
    119                    try {
    120                            processor.process("DomainSpecificEventStore.jxp", writer, environment);
    121                    } finally {
    122                            writer.close();
    123                    }
    124            }
    125
    126            private void renderEventStorePredicate(
    127                            JxpProcessor processor,
    128                            Map<String, String> environment,
    129                            File packageDir) throws Exception {
    130                    File outputFile = new File(packageDir, domain+"EventStorePredicate.java");
    131                    Writer writer = new FileWriter(outputFile);
    132                    try {
    133                            processor.process("DomainSpecificEventStorePredicate.jxp", writer, environment);
    134                    } finally {
    135                            writer.close();
    136                    }
    137            }
    138
    139            private void renderEventStoreAbstractPredicate(
    140                            JxpProcessor processor,
    141                            Map<String, String> environment,
    142                            File packageDir) throws Exception {
    143                    File outputFile = new File(packageDir, domain+"EventStoreAbstractPredicate.java");
    144                    Writer writer = new FileWriter(outputFile);
    145                    try {
    146                            processor.process("DomainSpecificEventStoreAbstractPredicate.jxp", writer, environment);
    147                    } finally {
    148                            writer.close();
    149                    }
    150            }
    151
    152            private void renderEventStoreExtractor(
    153                            JxpProcessor processor,
    154                            Map<String, String> environment,
    155                            File packageDir) throws Exception {
    156                    File outputFile = new File(packageDir, domain+"EventStoreExtractor.java");
    157                    Writer writer = new FileWriter(outputFile);
    158                    try {
    159                            processor.process("DomainSpecificEventStoreExtractor.jxp", writer, environment);
    160                    } finally {
    161                            writer.close();
    162                    }
    163            }
    164
    165            private void renderEventStoreAbstractExtractor(
    166                            JxpProcessor processor,
    167                            Map<String, String> environment,
    168                            File packageDir) throws Exception {
    169                    File outputFile = new File(packageDir, domain+"EventStoreAbstractExtractor.java");
    170                    Writer writer = new FileWriter(outputFile);
    171                    try {
    172                            processor.process("DomainSpecificEventStoreAbstractExtractor.jxp", writer, environment);
    173                    } finally {
    174                            writer.close();
    175                    }
    176            }
    177
    178            private void renderEventStoreImpl(
    179                            JxpProcessor processor,
    180                            Map<String, String> environment,
    181                            File packageDir) throws Exception {
    182                    File outputFile = new File(packageDir, domain+"EventStoreImpl.java");
    183                    Writer writer = new FileWriter(outputFile);
    184                    try {
    185                            processor.process("DomainSpecificEventStoreImpl.jxp", writer, environment);
    186                    } finally {
    187                            writer.close();
    188                    }
    189            }
    190
    191            private void renderEventBus(
    192                            JxpProcessor processor,
    193                            Map<String, String> environment,
    194                            File packageDir) throws Exception {
    195                    File outputFile = new File(packageDir, domain+"EventBus.java");
    196                    Writer writer = new FileWriter(outputFile);
    197                    try {
    198                            processor.process("DomainSpecificEventBus.jxp", writer, environment);
    199                    } finally {
    200                            writer.close();
    201                    }
    202            }
    203
    204            private void renderIntrospector(
    205                            JxpProcessor processor,
    206                            Map<String, String> environment,
    207                            File packageDir) throws Exception {
    208                    File outputFile = new File(packageDir, domain+"Introspector.java");
    209                    Writer writer = new FileWriter(outputFile);
    210                    try {
    211                            processor.process("DomainSpecificIntrospector.jxp", writer, environment);
    212                    } finally {
    213                            writer.close();
    214                    }
    215            }
    216
    217            private void renderDispatchNetworkSnapshot(
    218                            JxpProcessor processor,
    219                            Map<String, String> environment,
    220                            File packageDir) throws Exception {
    221                    File outputFile = new File(packageDir, domain+"DispatchNetworkDotSnapshot.java");
    222                    Writer writer = new FileWriter(outputFile);
    223                    try {
    224                            processor.process("DomainSpecificDispatchNetworkDotSnapshot.jxp", writer, environment);
    225                    } finally {
    226                            writer.close();
    227                    }
    228            }
    229
    230            private void renderDispatchContext(
    231                            JxpProcessor processor,
    232                            Map<String, String> environment,
    233                            File packageDir) throws Exception {
    234                    File outputFile = new File(packageDir, domain+"EventDispatchContext.java");
    235                    Writer writer = new FileWriter(outputFile);
    236                    try {
    237                            processor.process("DomainSpecificEventDispatchContext.jxp", writer, environment);
    238                    } finally {
    239                            writer.close();
    240                    }
    241            }
    242
    243            private void renderDispatchJoinContext(
    244                            JxpProcessor processor,
    245                            Map<String, String> environment,
    246                            File packageDir) throws Exception {
    247                    File outputFile = new File(packageDir, domain+"EventDispatchJoinContext.java");
    248                    Writer writer = new FileWriter(outputFile);
    249                    try {
    250                            processor.process("DomainSpecificEventDispatchJoinContext.jxp", writer, environment);
    251                    } finally {
    252                            writer.close();
    253                    }
    254            }
    255
    256            /**
    257             * @param args
    258             */
    259            public static void main(String[] args) {
    260                    System.out.println("Parameters: <output dir> <package name> <domain> <event type> <priority type> <context type>");
    261                    DomainGenerator generator = new DomainGenerator();
    262                    generator.setOutputDir(new File(args[0]));
    263                    generator.setPackageName(args[1]);
    264                    generator.setDomain(args[2]);
    265                    generator.setEventType(args[3]);
    266                    generator.setPriorityType(args[4]);
    267                    generator.setContextType(args[5]);
    268                    generator.generate();
    269            }
    270
    271    }