| 1 | package com.hammurapi.eventbus.local; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.FileWriter; |
| 5 | import java.io.Writer; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Map; |
| 8 | |
| 9 | import org.onemind.jxp.JxpContext; |
| 10 | import org.onemind.jxp.JxpPageSource; |
| 11 | import org.onemind.jxp.JxpProcessor; |
| 12 | import org.onemind.jxp.ResourceStreamPageSource; |
| 13 | |
| 14 | import com.hammurapi.eventbus.EventBusException; |
| 15 | |
| 16 | /** |
| 17 | * This class generates domain-specific implementations of |
| 18 | * event store and event bus. |
| 19 | * @author Pavel Vlasov |
| 20 | * |
| 21 | */ |
| 22 | public class DomainGenerator { |
| 23 | |
| 24 | private String packageName; |
| 25 | private String domain; |
| 26 | private String eventType; |
| 27 | private String priorityType; |
| 28 | private String contextType; |
| 29 | private File outputDir; |
| 30 | |
| 31 | public String getPackageName() { |
| 32 | return packageName; |
| 33 | } |
| 34 | |
| 35 | public void setPackageName(String packageName) { |
| 36 | this.packageName = packageName; |
| 37 | } |
| 38 | |
| 39 | public String getDomain() { |
| 40 | return domain; |
| 41 | } |
| 42 | |
| 43 | public void setDomain(String domain) { |
| 44 | this.domain = domain; |
| 45 | } |
| 46 | |
| 47 | public String getEventType() { |
| 48 | return eventType; |
| 49 | } |
| 50 | |
| 51 | public void setEventType(String eventType) { |
| 52 | this.eventType = eventType; |
| 53 | } |
| 54 | |
| 55 | public String getPriorityType() { |
| 56 | return priorityType; |
| 57 | } |
| 58 | |
| 59 | public void setPriorityType(String priorityType) { |
| 60 | this.priorityType = priorityType; |
| 61 | } |
| 62 | |
| 63 | public String getContextType() { |
| 64 | return contextType; |
| 65 | } |
| 66 | |
| 67 | public void setContextType(String contextType) { |
| 68 | this.contextType = contextType; |
| 69 | } |
| 70 | |
| 71 | public File getOutputDir() { |
| 72 | return outputDir; |
| 73 | } |
| 74 | |
| 75 | public void setOutputDir(File outputDir) { |
| 76 | this.outputDir = outputDir; |
| 77 | } |
| 78 | |
| 79 | public void generate() { |
| 80 | Map<String, String> environment = new HashMap<String,String>(); |
| 81 | environment.put("package", packageName); |
| 82 | environment.put("domain", domain); |
| 83 | environment.put("E", eventType); |
| 84 | environment.put("P", priorityType); |
| 85 | environment.put("C", contextType); |
| 86 | |
| 87 | try { |
| 88 | String prefix = getClass().getPackage().getName().replace('.', '/'); |
| 89 | JxpPageSource pageSource = new ResourceStreamPageSource("/"+prefix); |
| 90 | JxpContext context = new JxpContext(pageSource); |
| 91 | JxpProcessor processor = new JxpProcessor(context); |
| 92 | |
| 93 | File packageDir = new File(outputDir, packageName.replace('.', File.separatorChar)); |
| 94 | packageDir.mkdirs(); |
| 95 | |
| 96 | renderEventStore(processor, environment, packageDir); |
| 97 | renderEventStoreImpl(processor, environment, packageDir); |
| 98 | renderEventStorePredicate(processor, environment, packageDir); |
| 99 | 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 | } |