Example usage for org.springframework.context.support FileSystemXmlApplicationContext FileSystemXmlApplicationContext

List of usage examples for org.springframework.context.support FileSystemXmlApplicationContext FileSystemXmlApplicationContext

Introduction

In this page you can find the example usage for org.springframework.context.support FileSystemXmlApplicationContext FileSystemXmlApplicationContext.

Prototype

public FileSystemXmlApplicationContext(String... configLocations) throws BeansException 

Source Link

Document

Create a new FileSystemXmlApplicationContext, loading the definitions from the given XML files and automatically refreshing the context.

Usage

From source file:org.apache.s4.adapter.Adapter.java

public static void main(String args[]) {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("corehome").hasArg().withDescription("core home").create("c"));

    options.addOption(/*from w  w w . j a  v a2  s .  c om*/
            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);
}

From source file:org.openvpms.tools.archetype.loader.DerivedNodeUpdater.java

/**
 * Main line./*from  w w  w  .j  a va 2 s .  c  o  m*/
 *
 * @param args the command line arguments
 */
public static void main(String[] args) {
    BasicConfigurator.configure();

    // set the root logger level to error
    Logger root = Logger.getRootLogger();
    root.setLevel(Level.ERROR);
    root.removeAllAppenders();
    root.addAppender(new ConsoleAppender(new PatternLayout("%m%n")));

    try {
        JSAP parser = createParser();
        JSAPResult config = parser.parse(args);
        if (!config.success()) {
            displayUsage(parser, config);
        } else {
            String contextPath = config.getString("context");

            ApplicationContext context;
            if (!new File(contextPath).exists()) {
                context = new ClassPathXmlApplicationContext(contextPath);
            } else {
                context = new FileSystemXmlApplicationContext(contextPath);
            }

            IArchetypeService service = (IArchetypeService) context.getBean("archetypeService");

            if (config.getBoolean("verbose")) {
                log.setLevel(Level.INFO);
            }

            DerivedNodeUpdater updater = new DerivedNodeUpdater(service);
            updater.setBatchSize(config.getInt("batchSize"));
            updater.setFailOnError(config.getBoolean("failOnError"));
            String archetype = config.getString("archetype");
            if (StringUtils.isEmpty(archetype)) {
                displayUsage(parser, config);
            } else {
                updater.update(archetype);
            }
        }
    } catch (Throwable throwable) {
        log.error(throwable, throwable);
    }
}

From source file:org.openvpms.tools.data.migration.DetailsMigrator.java

/**
 * Main line.//from   w w w.j av  a 2s.  c  om
 *
 * @param args command line arguments
 */
public static void main(String[] args) {
    try {
        JSAP parser = createParser();
        JSAPResult config = parser.parse(args);
        if (!config.success()) {
            displayUsage(parser);
        } else {
            String contextPath = config.getString("context");

            ApplicationContext context;
            if (!new File(contextPath).exists()) {
                context = new ClassPathXmlApplicationContext(contextPath);
            } else {
                context = new FileSystemXmlApplicationContext(contextPath);
            }
            DataSource source = (DataSource) context.getBean("dataSource");
            DetailsMigrator migrator = new DetailsMigrator(source);
            migrator.export();
        }
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
}

From source file:org.openvpms.report.tools.TemplateLoader.java

/**
 * Main line.//from ww w  .jav a 2 s. co  m
 *
 * @param args command line arguments
 */
public static void main(String[] args) {
    try {
        JSAP parser = createParser();
        JSAPResult config = parser.parse(args);
        if (!config.success()) {
            displayUsage(parser);
        } else {
            String contextPath = config.getString("context");
            String file = config.getString("file");

            ApplicationContext context;
            if (!new File(contextPath).exists()) {
                context = new ClassPathXmlApplicationContext(contextPath);
            } else {
                context = new FileSystemXmlApplicationContext(contextPath);
            }

            if (file != null) {
                IArchetypeService service = (IArchetypeService) context.getBean("archetypeService");
                DocumentHandlers handlers = (DocumentHandlers) context.getBean("documentHandlers");
                TemplateLoader loader = new TemplateLoader(service, handlers);
                loader.load(file);
            } else {
                displayUsage(parser);
            }
        }
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
}

From source file:org.openvpms.tools.archetype.diff.ArchDiff.java

/**
 * Main line.//from w w  w .  j a  va 2 s . c  o m
 *
 * @param args the command line arguments
 */
public static void main(String[] args) {
    BasicConfigurator.configure();

    try {
        JSAP parser = createParser();
        JSAPResult config = parser.parse(args);
        if (!config.success()) {
            displayUsage(parser, config);
        } else {
            boolean recurse = !config.getBoolean("no-recurse");
            boolean verbose = config.getBoolean("verbose");
            String contextPath = config.getString("context");
            String version1 = config.getString("version1");
            String version2 = config.getString("version2");

            if (!StringUtils.isEmpty(version1) && !StringUtils.isEmpty(version2)) {
                ApplicationContext context;
                if (!new File(contextPath).exists()) {
                    context = new ClassPathXmlApplicationContext(contextPath);
                } else {
                    context = new FileSystemXmlApplicationContext(contextPath);
                }
                IArchetypeService service = (IArchetypeService) context.getBean("archetypeService");
                ArchDiff diff = new ArchDiff();
                if (config.getBoolean("list")) {
                    DescriptorLoader loader = getDescriptorLoader(version1, service, recurse);
                    diff.list(loader, verbose);
                } else {
                    DescriptorLoader oldVersion = getDescriptorLoader(version1, service, recurse);
                    DescriptorLoader newVersion = getDescriptorLoader(version2, service, recurse);
                    diff.compare(oldVersion, newVersion, verbose);
                }
            } else {
                displayUsage(parser, config);
            }
        }
    } catch (Throwable throwable) {
        log.error(throwable, throwable);
    }
}

From source file:streaming.swing.JFramePrincipal.java

/**
 * @param args the command line arguments
 */// ww w  .j  a va2 s. co  m
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            JFramePrincipal jf = new FileSystemXmlApplicationContext(
                    "file:/C:\\Users\\admin\\Documents\\NetBeansProjects\\Streaming\\newSpringXMLConfig.xml")
                            .getBean(JFramePrincipal.class);
            jf.setSize(800, 600);
            jf.setVisible(true);

        }
    });
}

From source file:streaming.gui.JFramePrincipale.java

/**
 * @param args the command line arguments
 *//* ww  w.  ja v a 2 s .c om*/
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipale.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipale.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipale.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JFramePrincipale.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {

            ApplicationContext context = new FileSystemXmlApplicationContext(
                    "file:/C:\\Users\\admin\\Documents\\NetBeansProjects\\Streaming\\application-context.xml");
            JFramePrincipale jfp = context.getBean(JFramePrincipale.class);
            jfp.setSize(800, 600);
            jfp.setVisible(true);

        }
    });
}

From source file:streaming.gui.PrincipaleJFrame.java

public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            String file1 = "file:/C:\\Users\\ajc\\Documents\\NetBeansProjects\\Streaming\\application-context.xml";
            String file2 = "file:/C:\\Users\\admin\\Desktop\\Projets\\Streaming\\application-context.xml";

            ApplicationContext context = new FileSystemXmlApplicationContext(file2);
            JFrame jf = context.getBean(PrincipaleJFrame.class);
            jf.setSize(800, 600);/*from w ww .  j  a va  2  s.  c  o  m*/
            jf.setVisible(true);

        }
    });
}

From source file:org.openvpms.archetype.tools.account.AccountBalanceTool.java

/**
 * Main line.//from  w  w  w .  ja v  a2s.co  m
 *
 * @param args command line arguments
 */
public static void main(String[] args) {
    try {
        JSAP parser = createParser();
        JSAPResult config = parser.parse(args);
        if (!config.success()) {
            displayUsage(parser);
        } else {
            String contextPath = config.getString("context");
            String name = config.getString("name");
            long id = config.getLong("id");
            boolean generate = config.getBoolean("generate");
            boolean check = config.getBoolean("check");

            ApplicationContext context;
            if (!new File(contextPath).exists()) {
                context = new ClassPathXmlApplicationContext(contextPath);
            } else {
                context = new FileSystemXmlApplicationContext(contextPath);
            }
            IArchetypeService service = (IArchetypeService) context.getBean("archetypeService");
            AccountBalanceTool tool = new AccountBalanceTool(service);
            if (check) {
                if (id == -1) {
                    tool.check(name);
                } else {
                    tool.check(id);
                }
            } else if (generate) {
                tool.setFailOnError(config.getBoolean("failOnError"));
                if (id == -1) {
                    tool.generate(name);
                } else {
                    tool.generate(id);
                }
            } else {
                displayUsage(parser);
            }
        }
    } catch (Throwable throwable) {
        log.error(throwable, throwable);
    }
}

From source file:org.openvpms.tools.archetype.loader.ArchetypeLoader.java

/**
 * ArchDiff line./*www . j av  a 2 s  .co m*/
 *
 * @param args the command line arguments
 */
public static void main(String[] args) {
    BasicConfigurator.configure();

    try {
        JSAP parser = createParser();
        JSAPResult config = parser.parse(args);
        if (!config.success()) {
            displayUsage(parser, config);
        } else {
            String contextPath = config.getString("context");

            ApplicationContext context;
            if (!new File(contextPath).exists()) {
                context = new ClassPathXmlApplicationContext(contextPath);
            } else {
                context = new FileSystemXmlApplicationContext(contextPath);
            }

            IArchetypeService service = (IArchetypeService) context.getBean("archetypeService");
            ArchetypeLoader loader = new ArchetypeLoader(service);
            String file = config.getString("file");
            String dir = config.getString("dir");
            boolean recurse = config.getBoolean("subdir");
            loader.setOverwrite(config.getBoolean("overwrite"));
            loader.setFailOnError(config.getBoolean("failOnError"));
            loader.setVerbose(config.getBoolean("verbose"));
            boolean clean = config.getBoolean("clean");
            String mappingFile = config.getString("mappingFile");
            int processed = 0;

            PlatformTransactionManager mgr;
            mgr = (PlatformTransactionManager) context.getBean("txnManager");
            TransactionStatus status = mgr.getTransaction(new DefaultTransactionDefinition());
            try {
                if (clean) {
                    loader.clean();
                    ++processed;
                }
                if (mappingFile != null) {
                    loader.loadAssertions(mappingFile);
                    ++processed;
                }
                if (file != null) {
                    loader.loadArchetypes(file);
                    ++processed;
                } else if (dir != null) {
                    loader.loadArchetypes(dir, recurse);
                    ++processed;
                }
                mgr.commit(status);
                if (processed == 0) {
                    displayUsage(parser, config);
                }
            } catch (Throwable throwable) {
                log.error(throwable, throwable);
                log.error("Rolling back changes");
                mgr.rollback(status);
            }
        }
    } catch (Throwable throwable) {
        log.error(throwable, throwable);
    }
}