Search in sources :

Example 1 with JposServiceInstance

use of jpos.loader.JposServiceInstance in project openpos-framework by JumpMind.

the class EscpServiceInstanceFactory method createInstance.

@SuppressWarnings({ "unchecked", "rawtypes" })
public JposServiceInstance createInstance(String s, JposEntry jposentry) throws JposException {
    if (!jposentry.hasPropertyWithName("serviceClass")) {
        throw new JposException(JposConst.JPOS_E_NOSERVICE, "The JposEntry does not contain the 'serviceClass' property");
    }
    try {
        String s1 = (String) jposentry.getPropertyValue("serviceClass");
        Class class1 = Class.forName(s1);
        Constructor constructor = class1.getConstructor(new Class[0]);
        JposServiceInstance instance = (JposServiceInstance) constructor.newInstance(new Object[0]);
        Iterator i = jposentry.getProps();
        while (i.hasNext()) {
            JposEntry.Prop prop = (JposEntry.Prop) i.next();
            if (!ignoreProperty(prop.getName())) {
                try {
                    Field field = class1.getDeclaredField(prop.getName());
                    Class<?> type = field.getType();
                    Object value = prop.getValue();
                    if (type == int.class) {
                        value = new Integer(prop.getValueAsString());
                    }
                    field.setAccessible(true);
                    field.set(instance, value);
                } catch (NoSuchFieldException e) {
                    logger.warn("No such property: " + prop.getName() + " exists on " + class1.getSimpleName());
                } catch (Exception e) {
                    logger.error("", e);
                }
            }
        }
        if (instance instanceof IOpenposPrinter) {
            configureOpenposPrinter((IOpenposPrinter) instance, jposentry);
        }
        return instance;
    } catch (Exception ex) {
        throw new PrintException(String.format("Failed to create '%s' (%s)", s, ex.toString()), ex);
    }
}
Also used : JposException(jpos.JposException) JposServiceInstance(jpos.loader.JposServiceInstance) Constructor(java.lang.reflect.Constructor) JposException(jpos.JposException) Field(java.lang.reflect.Field) JposEntry(jpos.config.JposEntry) Iterator(java.util.Iterator)

Example 2 with JposServiceInstance

use of jpos.loader.JposServiceInstance in project JavaForFun by gumartinm.

the class JposServiceInstanceFactoryImpl method createInstance.

/**
 * @input_parameters:
 * entry: (for jpos.xml and jpos.properties must be serviceClass)
 * logicalName: device's name (in jpos.xml or jpos.properties file, jpos.xml file by default)
 */
public JposServiceInstance createInstance(String logicalName, JposEntry entry) throws JposException {
    if (!entry.hasPropertyWithName(JposEntry.SERVICE_CLASS_PROP_NAME)) {
        throw new JposException(JposConst.JPOS_E_NOSERVICE, "The JposEntry does not contain the 'serviceClass' property");
    }
    JposServiceInstance serviceInstance = null;
    try {
        String serviceClassName = (String) entry.getPropertyValue(JposEntry.SERVICE_CLASS_PROP_NAME);
        Class<?> serviceClass = Class.forName(serviceClassName);
        Class<?>[] params = new Class<?>[0];
        Constructor<?> ctor = serviceClass.getConstructor(params);
        serviceInstance = (JposServiceInstance) ctor.newInstance((Object[]) params);
    } catch (Exception e) {
        throw new JposException(JposConst.JPOS_E_NOSERVICE, "Could not create " + "the service instance with logicalName= " + logicalName, e);
    }
    return serviceInstance;
}
Also used : JposException(jpos.JposException) JposServiceInstance(jpos.loader.JposServiceInstance) JposException(jpos.JposException)

Example 3 with JposServiceInstance

use of jpos.loader.JposServiceInstance in project openpos-framework by JumpMind.

the class SimulatedJposServiceInstanceFactory method createInstance.

@SuppressWarnings({ "unchecked", "rawtypes" })
public JposServiceInstance createInstance(String s, JposEntry jposentry) throws JposException {
    if (!jposentry.hasPropertyWithName("serviceClass")) {
        throw new JposException(JposConst.JPOS_E_NOSERVICE, "The JposEntry does not contain the 'serviceClass' property");
    }
    try {
        String s1 = (String) jposentry.getPropertyValue("serviceClass");
        Class class1 = Class.forName(s1);
        Constructor constructor = class1.getConstructor(new Class[0]);
        JposServiceInstance instance = (JposServiceInstance) constructor.newInstance(new Object[0]);
        Iterator i = jposentry.getProps();
        while (i.hasNext()) {
            JposEntry.Prop prop = (JposEntry.Prop) i.next();
            if (!ignoreProperty(prop.getName())) {
                try {
                    Field field = class1.getDeclaredField(prop.getName());
                    Class<?> type = field.getType();
                    Object value = prop.getValue();
                    if (type == int.class) {
                        value = new Integer(prop.getValueAsString());
                    }
                    field.set(instance, value);
                } catch (NoSuchFieldException e) {
                    logger.warn("No such property: " + prop.getName() + " exists on " + class1.getSimpleName());
                } catch (Exception e) {
                    logger.error("", e);
                }
            }
        }
        return instance;
    } catch (Exception exception) {
        throw new JposException(JposConst.JPOS_E_NOSERVICE, "Could not create the service instance!", exception);
    }
}
Also used : JposException(jpos.JposException) JposServiceInstance(jpos.loader.JposServiceInstance) Constructor(java.lang.reflect.Constructor) JposException(jpos.JposException) Field(java.lang.reflect.Field) JposEntry(jpos.config.JposEntry) Iterator(java.util.Iterator)

Aggregations

JposException (jpos.JposException)3 JposServiceInstance (jpos.loader.JposServiceInstance)3 Constructor (java.lang.reflect.Constructor)2 Field (java.lang.reflect.Field)2 Iterator (java.util.Iterator)2 JposEntry (jpos.config.JposEntry)2