Search in sources :

Example 36 with ConfigurationException

use of org.jpos.core.ConfigurationException in project jPOS by jpos.

the class PackagerWrapper method setConfiguration.

/**
 * requires <code>inner-packager</code> property
 * @param cfg Configuration object
 * @throws ConfigurationException
 */
public void setConfiguration(Configuration cfg) throws ConfigurationException {
    this.cfg = cfg;
    String packagerName = cfg.get("inner-packager");
    try {
        Class p = Class.forName(packagerName);
        setPackager((ISOPackager) p.newInstance());
        if (standardPackager instanceof Configurable)
            ((Configurable) standardPackager).setConfiguration(cfg);
    } catch (ClassNotFoundException e) {
        throw new ConfigurationException("Invalid inner-packager", e);
    } catch (InstantiationException e) {
        throw new ConfigurationException("Invalid inner-packager", e);
    } catch (IllegalAccessException e) {
        throw new ConfigurationException("Invalid inner-packager", e);
    }
}
Also used : ConfigurationException(org.jpos.core.ConfigurationException) Configurable(org.jpos.core.Configurable)

Example 37 with ConfigurationException

use of org.jpos.core.ConfigurationException in project jPOS by jpos.

the class QFactory method getObject.

/**
 * Creates an object from a definition element.
 * The element may have an attribute called type indicating the type of the object
 * to create, if this attribute is not present java.lang.String is assumed.
 * int, long and boolean are converted to their wrappers.
 * @return The created object.
 * @param childElement Dom Element with the definition of the object.
 * @throws ConfigurationException If an exception is found trying to create the object.
 */
@SuppressWarnings("unchecked")
protected Object getObject(Element childElement) throws ConfigurationException {
    String type = childElement.getAttributeValue("type", "java.lang.String");
    if ("int".equals(type))
        type = "java.lang.Integer";
    else if ("long".equals(type))
        type = "java.lang.Long";
    else if ("boolean".equals(type))
        type = "java.lang.Boolean";
    String value = childElement.getText();
    try {
        Class attributeType = Class.forName(type);
        if (Collection.class.isAssignableFrom(attributeType))
            return getCollection(attributeType, childElement);
        else {
            Class[] parameterTypes = { "".getClass() };
            Object[] parameterValues = { value };
            return attributeType.getConstructor(parameterTypes).newInstance(parameterValues);
        }
    } catch (Exception e1) {
        throw new ConfigurationException(e1);
    }
}
Also used : ConfigurationException(org.jpos.core.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ConfigurationException(org.jpos.core.ConfigurationException) MissingResourceException(java.util.MissingResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 38 with ConfigurationException

use of org.jpos.core.ConfigurationException in project jPOS by jpos.

the class QFactory method getConfiguration.

public Configuration getConfiguration(Element e) throws ConfigurationException {
    String configurationFactoryClazz = e.getAttributeValue("configuration-factory");
    ConfigurationFactory cf = configurationFactoryClazz != null ? (ConfigurationFactory) newInstance(configurationFactoryClazz) : defaultConfigurationFactory;
    Configuration cfg = cf.getConfiguration(e);
    String merge = e.getAttributeValue("merge-configuration");
    if (merge != null) {
        StringTokenizer st = new StringTokenizer(merge, ", ");
        while (st.hasMoreElements()) {
            try {
                Configuration c = QConfig.getConfiguration(st.nextToken());
                for (String k : c.keySet()) {
                    if (cfg.get(k, null) == null) {
                        String[] v = c.getAll(k);
                        switch(v.length) {
                            case 0:
                                break;
                            case 1:
                                cfg.put(k, v[0]);
                                break;
                            default:
                                cfg.put(k, v);
                        }
                    }
                }
            } catch (NameRegistrar.NotFoundException ex) {
                throw new ConfigurationException(ex.getMessage());
            }
        }
    }
    return cfg;
}
Also used : StringTokenizer(java.util.StringTokenizer) NameRegistrar(org.jpos.util.NameRegistrar) Configuration(org.jpos.core.Configuration) ConfigurationException(org.jpos.core.ConfigurationException)

Example 39 with ConfigurationException

use of org.jpos.core.ConfigurationException in project jPOS by jpos.

the class SimpleConfigurationFactory method getConfiguration.

public Configuration getConfiguration(Element e) throws ConfigurationException {
    Properties props = new Properties();
    Iterator iter = e.getChildren("property").iterator();
    while (iter.hasNext()) {
        Element property = (Element) iter.next();
        String name = property.getAttributeValue("name");
        String value = property.getAttributeValue("value");
        String file = property.getAttributeValue("file");
        if (file != null)
            try {
                props.load(new FileInputStream(new File(file)));
            } catch (Exception ex) {
                throw new ConfigurationException(file, ex);
            }
        else if (name != null && value != null) {
            Object obj = props.get(name);
            if (obj instanceof String[]) {
                String[] mobj = (String[]) obj;
                String[] m = new String[mobj.length + 1];
                System.arraycopy(mobj, 0, m, 0, mobj.length);
                m[mobj.length] = value;
                props.put(name, m);
            } else if (obj instanceof String) {
                String[] m = new String[2];
                m[0] = (String) obj;
                m[1] = value;
                props.put(name, m);
            } else
                props.put(name, value);
        }
    }
    return new SimpleConfiguration(props);
}
Also used : ConfigurationException(org.jpos.core.ConfigurationException) Element(org.jdom2.Element) Iterator(java.util.Iterator) SimpleConfiguration(org.jpos.core.SimpleConfiguration) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) ConfigurationException(org.jpos.core.ConfigurationException)

Example 40 with ConfigurationException

use of org.jpos.core.ConfigurationException in project jPOS by jpos.

the class DailyLogListener method setConfiguration.

public void setConfiguration(Configuration cfg) throws ConfigurationException {
    String suffix = cfg.get("suffix", DEF_SUFFIX), prefix = cfg.get("prefix");
    setSuffix(suffix);
    setPrefix(prefix);
    Integer formatObj = COMPRESSION_FORMATS.get(cfg.get("compression-format", "none").toLowerCase());
    int compressionFormat = formatObj == null ? 0 : formatObj;
    setCompressionFormat(compressionFormat);
    setCompressedSuffix(cfg.get("compressed-suffix", DEF_COMPRESSED_SUFFIX[compressionFormat]));
    setCompressionBufferSize(cfg.getInt("compression-buffer-size", DEF_BUFFER_SIZE));
    logName = prefix + suffix;
    maxSize = cfg.getLong("maxsize", DEF_MAXSIZE);
    try {
        openLogFile();
    } catch (IOException e) {
        throw new ConfigurationException("error opening file: " + logName, e);
    }
    sleepTime = cfg.getInt("window", DEF_WIN);
    if (sleepTime <= 0)
        sleepTime = DEF_WIN;
    sleepTime *= 1000;
    DateFormat fmt = new SimpleDateFormat(cfg.get("date-format", DEF_DATE_FMT));
    setDateFmt(fmt);
    setLastDate(fmt.format(new Date()));
    Date time;
    try {
        time = new SimpleDateFormat("HH:mm:ss").parse(cfg.get("first-rotate-time", "00:00:00"));
    } catch (ParseException ex) {
        throw new ConfigurationException("Bad 'first-rotate-time' format " + "expected HH(0-23):mm:ss ", ex);
    }
    String strDate = cfg.get("first-rotate-date", null);
    // calculate the first execution time
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MILLISECOND, 0);
    Calendar calTemp = Calendar.getInstance();
    calTemp.setTime(time);
    cal.set(Calendar.SECOND, calTemp.get(Calendar.SECOND));
    cal.set(Calendar.MINUTE, calTemp.get(Calendar.MINUTE));
    cal.set(Calendar.HOUR_OF_DAY, calTemp.get(Calendar.HOUR_OF_DAY));
    if (strDate != null) {
        Date date;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(strDate);
        } catch (ParseException ex) {
            throw new ConfigurationException("Bad 'first-rotate-date' " + "format, expected (yyyy-MM-dd)", ex);
        }
        calTemp.setTime(date);
        cal.set(calTemp.get(Calendar.YEAR), calTemp.get(Calendar.MONTH), calTemp.get(Calendar.DATE));
    }
    // here cal contains the first execution, let/s calculate the next one
    calTemp.setTime(new Date());
    // if first executiontime already happened
    if (cal.before(calTemp)) {
        // how many windows between cal and now
        long n = (calTemp.getTimeInMillis() - cal.getTimeInMillis()) / sleepTime;
        cal.setTimeInMillis(cal.getTimeInMillis() + sleepTime * (n + 1));
    }
    DefaultTimer.getTimer().scheduleAtFixedRate(rotate = new DailyRotate(), cal.getTime(), sleepTime);
}
Also used : ConfigurationException(org.jpos.core.ConfigurationException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

ConfigurationException (org.jpos.core.ConfigurationException)52 Element (org.jdom2.Element)10 SimpleConfiguration (org.jpos.core.SimpleConfiguration)10 Test (org.junit.Test)9 Configuration (org.jpos.core.Configuration)8 Configurable (org.jpos.core.Configurable)5 SubConfiguration (org.jpos.core.SubConfiguration)5 NameRegistrar (org.jpos.util.NameRegistrar)4 IOException (java.io.IOException)3 Date (java.util.Date)3 Properties (java.util.Properties)3 LogSource (org.jpos.util.LogSource)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 StringTokenizer (java.util.StringTokenizer)2 Timer (java.util.Timer)2 ISOException (org.jpos.iso.ISOException)2 Logger (org.jpos.util.Logger)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1