Search in sources :

Example 1 with EventProducer

use of io.s4.listener.EventProducer in project core by s4.

the class Adapter method main.

public static void main(String[] args) {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("corehome").hasArg().withDescription("core home").create("c"));
    options.addOption(OptionBuilder.withArgName("instanceid").hasArg().withDescription("instance id").create("i"));
    options.addOption(OptionBuilder.withArgName("configtype").hasArg().withDescription("configuration type").create("t"));
    options.addOption(OptionBuilder.withArgName("userconfig").hasArg().withDescription("user-defined legacy data adapter configuration file").create("d"));
    CommandLineParser parser = new GnuParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException pe) {
        System.err.println(pe.getLocalizedMessage());
        System.exit(1);
    }
    int instanceId = -1;
    if (commandLine.hasOption("i")) {
        String instanceIdStr = commandLine.getOptionValue("i");
        try {
            instanceId = Integer.parseInt(instanceIdStr);
        } catch (NumberFormatException nfe) {
            System.err.println("Bad instance id: %s" + instanceIdStr);
            System.exit(1);
        }
    }
    if (commandLine.hasOption("c")) {
        coreHome = commandLine.getOptionValue("c");
    }
    String configType = "typical";
    if (commandLine.hasOption("t")) {
        configType = commandLine.getOptionValue("t");
    }
    String userConfigFilename = null;
    if (commandLine.hasOption("d")) {
        userConfigFilename = commandLine.getOptionValue("d");
    }
    File userConfigFile = new File(userConfigFilename);
    if (!userConfigFile.isFile()) {
        System.err.println("Bad user configuration file: " + userConfigFilename);
        System.exit(1);
    }
    File coreHomeFile = new File(coreHome);
    if (!coreHomeFile.isDirectory()) {
        System.err.println("Bad core home: " + coreHome);
        System.exit(1);
    }
    if (instanceId > -1) {
        System.setProperty("instanceId", "" + instanceId);
    } else {
        System.setProperty("instanceId", "" + S4Util.getPID());
    }
    String configBase = coreHome + File.separatorChar + "conf" + File.separatorChar + configType;
    String configPath = configBase + File.separatorChar + "adapter_conf.xml";
    File configFile = new File(configPath);
    if (!configFile.exists()) {
        System.err.printf("adapter config file %s does not exist\n", configPath);
        System.exit(1);
    }
    // load adapter config xml
    ApplicationContext coreContext;
    coreContext = new FileSystemXmlApplicationContext("file:" + configPath);
    ApplicationContext context = coreContext;
    Adapter adapter = (Adapter) context.getBean("adapter");
    ApplicationContext appContext = new FileSystemXmlApplicationContext(new String[] { "file:" + userConfigFilename }, context);
    Map listenerBeanMap = appContext.getBeansOfType(EventProducer.class);
    if (listenerBeanMap.size() == 0) {
        System.err.println("No user-defined listener beans");
        System.exit(1);
    }
    EventProducer[] eventListeners = new EventProducer[listenerBeanMap.size()];
    int index = 0;
    for (Iterator it = listenerBeanMap.keySet().iterator(); it.hasNext(); index++) {
        String beanName = (String) it.next();
        System.out.println("Adding producer " + beanName);
        eventListeners[index] = (EventProducer) listenerBeanMap.get(beanName);
    }
    adapter.setEventListeners(eventListeners);
}
Also used : Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) EventProducer(io.s4.listener.EventProducer) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) CommandLine(org.apache.commons.cli.CommandLine) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Iterator(java.util.Iterator) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) Map(java.util.Map)

Aggregations

EventProducer (io.s4.listener.EventProducer)1 File (java.io.File)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 GnuParser (org.apache.commons.cli.GnuParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 ApplicationContext (org.springframework.context.ApplicationContext)1 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)1