Example usage for org.apache.commons.collections ExtendedProperties load

List of usage examples for org.apache.commons.collections ExtendedProperties load

Introduction

In this page you can find the example usage for org.apache.commons.collections ExtendedProperties load.

Prototype

public void load(InputStream input) throws IOException 

Source Link

Document

Load the properties from the given input stream.

Usage

From source file:com.cyclopsgroup.waterview.jelly.JellyScriptsRunner.java

/**
 * Main entry to run a script//from  w  ww  .java 2 s.co  m
 *
 * @param args Script paths
 * @throws Exception Throw it out
 */
public static final void main(String[] args) throws Exception {
    List scripts = new ArrayList();
    for (int i = 0; i < args.length; i++) {
        String path = args[i];
        File file = new File(path);
        if (file.isFile()) {
            scripts.add(file.toURL());
        } else {
            Enumeration enu = JellyScriptsRunner.class.getClassLoader().getResources(path);
            CollectionUtils.addAll(scripts, enu);
        }
    }
    if (scripts.isEmpty()) {
        System.out.println("No script to run, return!");
        return;
    }

    String basedir = new File("").getAbsolutePath();
    Properties initProperties = new Properties(System.getProperties());
    initProperties.setProperty("basedir", basedir);
    initProperties.setProperty("plexus.home", basedir);

    WaterviewPlexusContainer container = new WaterviewPlexusContainer();
    for (Iterator j = initProperties.keySet().iterator(); j.hasNext();) {
        String initPropertyName = (String) j.next();
        container.addContextValue(initPropertyName, initProperties.get(initPropertyName));
    }

    container.addContextValue(Waterview.INIT_PROPERTIES, initProperties);
    container.initialize();
    container.start();

    JellyEngine je = (JellyEngine) container.lookup(JellyEngine.ROLE);
    JellyContext jc = new JellyContext(je.getGlobalContext());
    XMLOutput output = XMLOutput.createXMLOutput(System.out);
    for (Iterator i = scripts.iterator(); i.hasNext();) {
        URL script = (URL) i.next();
        System.out.print("Running script " + script);
        ExtendedProperties ep = new ExtendedProperties();
        ep.putAll(initProperties);
        ep.load(script.openStream());
        for (Iterator j = ep.getKeys("script"); j.hasNext();) {
            String name = (String) j.next();
            if (name.endsWith(".file")) {
                File file = new File(ep.getString(name));
                if (file.exists()) {
                    System.out.println("Runner jelly file " + file);
                    jc.runScript(file, output);
                }
            } else if (name.endsWith(".resource")) {
                Enumeration k = JellyScriptsRunner.class.getClassLoader().getResources(ep.getString(name));
                while (j != null && k.hasMoreElements()) {
                    URL s = (URL) k.nextElement();
                    System.out.println("Running jelly script " + s);
                    jc.runScript(s, output);
                }
            }
        }
        //jc.runScript( script, XMLOutput.createDummyXMLOutput() );
        System.out.println("... Done!");
    }
    container.dispose();
}

From source file:adalid.commons.properties.PropertiesHandler.java

public static ExtendedProperties getResourceAsExtendedProperties(String resource) {
    if (StringUtils.isBlank(resource)) {
        return null;
    }//from  w  ww  . j a v  a2s .c o  m
    ExtendedProperties properties = new ExtendedProperties();
    try (InputStream stream = PropertiesHandler.class.getResourceAsStream(resource)) {
        if (stream == null) {
            logger.error("resource " + resource + " is missing");
        } else {
            properties.load(stream);
            logger.info("resource " + resource + " loaded (" + properties.size() + " properties)");
        }
    } catch (Exception ex) {
        logger.fatal(ex);
    }
    return properties;
}

From source file:adalid.commons.properties.PropertiesHandler.java

public static ExtendedProperties getExtendedProperties(byte[] buffer) {
    ExtendedProperties extendedProperties = new ExtendedProperties();
    if (buffer == null) {
        logger.error("null properties buffer");
    } else {/*from ww w  . ja v a 2  s.  c om*/
        try {
            logger.trace("loading buffer " + Arrays.toString(buffer));
            try (InputStream inStream = new ByteArrayInputStream(buffer)) {
                extendedProperties.load(inStream);
            }
            printExtendedProperties(extendedProperties);
        } catch (Exception ex) {
            logger.fatal(ThrowableUtils.getString(ex), ex);
        }
    }
    return extendedProperties;
}

From source file:adalid.commons.properties.PropertiesHandler.java

public static ExtendedProperties getExtendedProperties(File file, Level badFileLogLevel,
        Level goodFileLogLevel) {
    ExtendedProperties extendedProperties = new ExtendedProperties();
    String filename = file == null ? "" : file.getPath();
    if (file == null) {
        logger.error("null properties file");
    } else if (file.isFile()) {
        try {//from   ww w  .  j  a  v  a 2s  . c  o  m
            logger.trace("loading " + filename);
            try (InputStream inStream = new FileInputStream(filename)) {
                extendedProperties.load(inStream);
            }
            logger.log(goodFileLogLevel,
                    "file " + filename + " loaded (" + extendedProperties.size() + " properties)");
            printExtendedProperties(extendedProperties);
        } catch (Exception ex) {
            logger.fatal(ThrowableUtils.getString(ex), ex);
        }
    } else {
        logger.log(badFileLogLevel, filename + " does not exist or is not a normal file");
    }
    return extendedProperties;
}

From source file:autohit.server.command.CommandLoadProps.java

/**
 * Execute the command.//from  w w  w.ja  v a2 s  .co  m
 * 
 * @throws ServerException
 * @return return the manreadable message for success.
 */
public String execute() throws ServerException {

    String victory = "failed.";
    boolean started = false;

    // Trap all non-critical errors and just log them to the
    // responseChannel
    try {

        // Get the properties and try to save them.
        ExtendedProperties iprops = sc.getInvokerProperties();
        InputStream is = uni.getStream(command);
        iprops.load(is);
        is.close();

        // Ok, it's working
        victory = "saved.";

    } catch (IOException ioe) {
        throw new ServerException("failed.  There was an IO error.  message=" + ioe.getMessage(),
                AutohitErrorCodes.EVENT_COMMAND_FAILED, ioe);

    } catch (UniverseException ue) {

        throw new ServerException("failed.  There was a Universe Exception.  message=" + ue.getMessage(),
                AutohitErrorCodes.EVENT_COMMAND_FAILED, ue);

    } catch (Exception e) {

        throw new ServerException("failed.  There was a general Exception.  message=" + e.getMessage(),
                AutohitErrorCodes.EVENT_COMMAND_FAILED, e);
    }

    // return
    return victory;
}

From source file:com.alkacon.opencms.formgenerator.CmsFieldFactory.java

/**
 * Default constructor.<p>// w ww  .j  a v a 2s . c  o  m
 */
private CmsFieldFactory() {

    super();

    m_registeredFieldTypes = new HashMap<String, String>(20);

    // register all the standard OpenCms field types
    registerFieldType(CmsCheckboxField.getStaticType(), CmsCheckboxField.class.getName());
    registerFieldType(CmsEmailField.getStaticType(), CmsEmailField.class.getName());
    registerFieldType(CmsFileUploadField.getStaticType(), CmsFileUploadField.class.getName());
    registerFieldType(CmsHiddenField.getStaticType(), CmsHiddenField.class.getName());
    registerFieldType(CmsRadioButtonField.getStaticType(), CmsRadioButtonField.class.getName());
    registerFieldType(CmsSelectionField.getStaticType(), CmsSelectionField.class.getName());
    registerFieldType(CmsTextField.getStaticType(), CmsTextField.class.getName());
    registerFieldType(CmsTextareaField.getStaticType(), CmsTextareaField.class.getName());
    registerFieldType(CmsEmptyField.getStaticType(), CmsEmptyField.class.getName());
    registerFieldType(CmsPrivacyField.getStaticType(), CmsPrivacyField.class.getName());
    registerFieldType(CmsDynamicField.getStaticType(), CmsDynamicField.class.getName());
    registerFieldType(CmsTableField.getStaticType(), CmsTableField.class.getName());
    registerFieldType(CmsPasswordField.getStaticType(), CmsPasswordField.class.getName());
    registerFieldType(CmsPagingField.getStaticType(), CmsPagingField.class.getName());
    registerFieldType(CmsDisplayField.getStaticType(), CmsDisplayField.class.getName());
    registerFieldType(CmsHiddenDisplayField.getStaticType(), CmsHiddenDisplayField.class.getName());

    File propertyFile = null;
    try {

        // register all custom field types declared in a property file.
        // since custom fields are optional, the property file doesn't have to exist necessarily in the file system.
        // this file should contain a mapping of field type names to a Java classes separated by a colon ":", e.g.:
        // FIELDS=<fieldtype>:<java class>,...,<fieldtype>:<java class>

        propertyFile = new File(CUSTOM_FORM_FIELD_PROPERTIES);
        if (propertyFile.exists()) {

            ExtendedProperties fieldProperties = new ExtendedProperties();
            fieldProperties.load(new FileInputStream(propertyFile));

            Iterator<String> i = fieldProperties.keySet().iterator();
            while (i.hasNext()) {

                String key = i.next();
                if (!"FIELDS".equalsIgnoreCase(key)) {
                    continue;
                }

                String[] values = fieldProperties.getStringArray(key);
                if ((values == null) || (values.length == 0)) {
                    continue;
                }

                for (int j = 0, n = values.length; j < n; j++) {

                    String field = values[j];
                    int index = field.indexOf(":");
                    if (index == -1) {
                        continue;
                    }

                    String fieldType = field.substring(0, index);
                    String fieldClass = field.substring(index + 1, field.length());
                    registerFieldType(fieldType, fieldClass);
                }
            }
        }
    } catch (IOException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_READING_CUSTOM_FORM_FIELD_PROPERTIES_1,
                    propertyFile == null ? CUSTOM_FORM_FIELD_PROPERTIES : propertyFile.getAbsolutePath()), e);
        }
    }
}

From source file:com.alkacon.opencms.v8.formgenerator.CmsFieldFactory.java

/**
 * Default constructor.<p>//  ww w. j ava  2s  .  c  o m
 */
private CmsFieldFactory() {

    super();

    m_registeredFieldTypes = new HashMap<String, String>(20);

    // register all the standard OpenCms field types
    registerFieldType(CmsCheckboxField.getStaticType(), CmsCheckboxField.class.getName());
    registerFieldType(CmsEmailField.getStaticType(), CmsEmailField.class.getName());
    registerFieldType(CmsFileUploadField.getStaticType(), CmsFileUploadField.class.getName());
    registerFieldType(CmsHiddenField.getStaticType(), CmsHiddenField.class.getName());
    registerFieldType(CmsRadioButtonField.getStaticType(), CmsRadioButtonField.class.getName());
    registerFieldType(CmsSelectionField.getStaticType(), CmsSelectionField.class.getName());
    registerFieldType(CmsTextField.getStaticType(), CmsTextField.class.getName());
    registerFieldType(CmsTextareaField.getStaticType(), CmsTextareaField.class.getName());
    registerFieldType(CmsEmptyField.getStaticType(), CmsEmptyField.class.getName());
    registerFieldType(CmsPrivacyField.getStaticType(), CmsPrivacyField.class.getName());
    registerFieldType(CmsDynamicField.getStaticType(), CmsDynamicField.class.getName());
    registerFieldType(CmsTableField.getStaticType(), CmsTableField.class.getName());
    registerFieldType(CmsPasswordField.getStaticType(), CmsPasswordField.class.getName());
    registerFieldType(CmsPagingField.getStaticType(), CmsPagingField.class.getName());
    registerFieldType(CmsDisplayField.getStaticType(), CmsDisplayField.class.getName());
    registerFieldType(CmsHiddenDisplayField.getStaticType(), CmsHiddenDisplayField.class.getName());
    registerFieldType(CmsParameterField.getStaticType(), CmsParameterField.class.getName());

    File propertyFile = null;
    try {

        // register all custom field types declared in a property file.
        // since custom fields are optional, the property file doesn't have to exist necessarily in the file system.
        // this file should contain a mapping of field type names to a Java classes separated by a colon ":", e.g.:
        // FIELDS=<fieldtype>:<java class>,...,<fieldtype>:<java class>

        propertyFile = new File(CUSTOM_FORM_FIELD_PROPERTIES);
        if (propertyFile.exists()) {

            ExtendedProperties fieldProperties = new ExtendedProperties();
            fieldProperties.load(new FileInputStream(propertyFile));

            @SuppressWarnings("unchecked")
            Iterator<String> i = fieldProperties.keySet().iterator();
            while (i.hasNext()) {

                String key = i.next();
                if (!"FIELDS".equalsIgnoreCase(key)) {
                    continue;
                }

                String[] values = fieldProperties.getStringArray(key);
                if ((values == null) || (values.length == 0)) {
                    continue;
                }

                for (int j = 0, n = values.length; j < n; j++) {

                    String field = values[j];
                    int index = field.indexOf(":");
                    if (index == -1) {
                        continue;
                    }

                    String fieldType = field.substring(0, index);
                    String fieldClass = field.substring(index + 1, field.length());
                    registerFieldType(fieldType, fieldClass);
                }
            }
        }
    } catch (IOException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_READING_CUSTOM_FORM_FIELD_PROPERTIES_1,
                    propertyFile.getAbsolutePath()), e);
        }
    }
}

From source file:com.cyclopsgroup.waterview.velocity.VelocityEngine.java

/**
 * Overwrite or implement method initialize()
 * @see org.apache.avalon.framework.activity.Initializable#initialize()
 *///from w  w  w . j  av a 2  s  .  c  o m
public void initialize() throws Exception {
    ExtendedProperties props = new ExtendedProperties();
    if (initProperties != null) {
        for (Iterator i = initProperties.keySet().iterator(); i.hasNext();) {
            String name = (String) i.next();
            props.addProperty(name, initProperties.getProperty(name));
        }
    }
    props.load(getClass().getResourceAsStream("basevelocity.properties"));

    engine = new org.apache.velocity.app.VelocityEngine();
    engine.setExtendedProperties(props);
    engine.init();
}

From source file:com.cyclopsgroup.tornado.hibernate.impl.DefaultHibernateService.java

/**
 * Override method DefaultHibernateFactory in supper class
 *
 * @see org.apache.avalon.framework.activity.Initializable#initialize()
 *//*from   ww  w . j  av a  2 s .c o  m*/
public void initialize() throws Exception {
    Enumeration enu = getClass().getClassLoader()
            .getResources("META-INF/cyclopsgroup/hibernate-entities.properties");
    ExtendedProperties props = new ExtendedProperties();
    while (enu.hasMoreElements()) {
        URL resource = (URL) enu.nextElement();
        props.load(resource.openStream());
    }

    for (Iterator i = hibernateProperties.keySet().iterator(); i.hasNext();) {
        String name = (String) i.next();

        org.hibernate.cfg.Configuration c = new org.hibernate.cfg.Configuration();
        Properties p = (Properties) hibernateProperties.get(name);
        c.setProperties(p);
        for (Iterator j = props.getKeys(); j.hasNext();) {
            String key = (String) j.next();
            String value = (String) props.getString(key);

            if (StringUtils.equals(value, name)) {
                try {
                    Class entityClass = Class.forName(key);
                    c.addClass(entityClass);
                    entityClasses.put(value, entityClass);
                } catch (Exception e) {
                    getLogger().warn("Entity " + key + " is not loaded", e);
                }
            }
        }
        hibernateConfigurations.put(name, c);
        SessionFactory sessionFactory = c.buildSessionFactory();
        sessionFactories.put(name, sessionFactory);
    }
}

From source file:bboss.org.apache.velocity.texen.ant.TexenTask.java

/**
 * Set the context properties that will be
 * fed into the initial context be the/*from   w  w  w.  java2s  .  c o m*/
 * generating process starts.
 * @param file
 */
public void setContextProperties(String file) {
    String[] sources = StringUtils.split(file, ",");
    contextProperties = new ExtendedProperties();

    // Always try to get the context properties resource
    // from a file first. Templates may be taken from a JAR
    // file but the context properties resource may be a
    // resource in the filesystem. If this fails than attempt
    // to get the context properties resource from the
    // classpath.
    for (int i = 0; i < sources.length; i++) {
        ExtendedProperties source = new ExtendedProperties();

        try {
            // resolve relative path from basedir and leave
            // absolute path untouched.
            File fullPath = project.resolveFile(sources[i]);
            log("Using contextProperties file: " + fullPath);
            source.load(new FileInputStream(fullPath));
        } catch (IOException e) {
            ClassLoader classLoader = this.getClass().getClassLoader();

            try {
                InputStream inputStream = classLoader.getResourceAsStream(sources[i]);

                if (inputStream == null) {
                    throw new BuildException("Context properties file " + sources[i]
                            + " could not be found in the file system or on the classpath!");
                } else {
                    source.load(inputStream);
                }
            } catch (IOException ioe) {
                source = null;
            }
        }

        if (source != null) {
            for (Iterator j = source.getKeys(); j.hasNext();) {
                String name = (String) j.next();
                String value = StringUtils.nullTrim(source.getString(name));
                contextProperties.setProperty(name, value);
            }
        }
    }
}