001package net.gdface.codegen.thrift;
002
003import java.util.List;
004import java.util.Map;
005
006import org.apache.commons.cli.Options;
007import org.apache.velocity.VelocityContext;
008
009import com.google.common.collect.ImmutableMap;
010import com.google.common.collect.Lists;
011
012import net.gdface.cli.Context;
013import net.gdface.codegen.AbstractSchema;
014import net.gdface.codegen.generator.CodeWriter;
015import net.gdface.codegen.generator.Generator;
016import net.gdface.codegen.generator.GeneratorConfiguration;
017
018public class ThriftServiceDecoratorGenerator extends Generator implements ThriftConstants {
019        private final ThriftServiceDecoratorOptions options = ThriftServiceDecoratorOptions.getInstance();
020        private final ThriftServiceDecoratorConfiguration config = ThriftServiceDecoratorConfiguration.INSTANCE;
021        private ThriftSchema thriftSchema;
022        public ThriftServiceDecoratorGenerator() {
023        }
024
025        @Override
026        protected Context createEngineContext() {
027                thriftSchema = new ThriftSchema(config.getInterfaceClasses());
028                Context context = super.createEngineContext();
029                context.setProperty(SCHEMA_INFO, thriftSchema);
030                context.setProperty(CXX_HELPER, new CxxHelper());               
031                return context;
032        }
033        
034        @Override
035        protected GeneratorConfiguration getGeneratorConfiguration() {
036                return config;
037        }
038        @Override
039        protected Options getOptions() {
040                return options.getOptions();
041        }
042        @Override
043        protected Map<String, List<? extends AbstractSchema>> generateTask() {
044                Map<String,List<? extends AbstractSchema>> m = 
045                                ImmutableMap.<String,List<? extends AbstractSchema>>of(
046                                DIR_SCHEMA,Lists.<AbstractSchema>newArrayList(),
047                                DIR_PERSERVICE,thriftSchema.getThriftServiceDecorators(),
048                                DIR_PERSTRUCT,thriftSchema.getAllStructDecorators()
049                                );
050                return m;
051        }
052
053        @Override
054        protected void beforeGenerateFile(VelocityContext context, String templateName) {
055                AbstractSchema sourceinfo = (AbstractSchema)context.get(SOURCE_INFO);
056                if(null !=sourceinfo){
057                        sourceinfo.backupImportedList();
058                }
059        }
060
061        @Override
062        protected void afterGenerateFile(VelocityContext context, String templateName) {
063                AbstractSchema sourceinfo = (AbstractSchema)context.get(SOURCE_INFO);
064                if(null !=sourceinfo){
065                        sourceinfo.restoreImportedList();
066                }
067        }
068
069        public static void main(String[] args) {
070                new ThriftServiceDecoratorGenerator().parseCommandLine(args).initEngine().multiGenerate();
071        }
072
073        @Override
074        protected CodeWriter getCodeWriter(){
075                return config.getCodeWriter();
076        }
077}