Search in sources :

Example 11 with JposException

use of jpos.JposException 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 12 with JposException

use of jpos.JposException 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)12 JposServiceInstance (jpos.loader.JposServiceInstance)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Constructor (java.lang.reflect.Constructor)2 Field (java.lang.reflect.Field)2 Iterator (java.util.Iterator)2 JposEntry (jpos.config.JposEntry)2 ImprimeTicketEnvio (ar.com.comit.print.javapos.ImprimeTicketEnvio)1 DialogActionListener (ar.com.ergio.print.fiscal.view.AInfoFiscalPrinter.DialogActionListener)1 BaseKeyBoardDriver (de.javapos.example.hardware.BaseKeyBoardDriver)1 JposEventQueueImpl (de.javapos.example.queue.JposEventQueueImpl)1 Dimension (java.awt.Dimension)1 PointerInfo (java.awt.PointerInfo)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileChannel (java.nio.channels.FileChannel)1