1 25 package org.ofbiz.pos.jpos.service; 26 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.Iterator ; 30 31 import jpos.services.EventCallbacks; 32 import jpos.JposException; 33 import jpos.JposConst; 34 import jpos.events.DataEvent; 35 import jpos.events.ErrorEvent; 36 import jpos.events.DirectIOEvent; 37 import jpos.events.OutputCompleteEvent; 38 import jpos.events.StatusUpdateEvent; 39 import jpos.config.JposEntry; 40 41 48 public class BaseService implements jpos.services.BaseService, jpos.loader.JposServiceInstance { 49 50 public static final String module = BaseService.class.getName(); 51 protected static boolean claimed = false; 52 53 protected List eventQueue = new ArrayList (); 54 protected JposEntry entry = null; 55 56 protected boolean freezeEvents = false; 57 protected boolean deviceEnabled = false; 58 protected boolean eventsEnabled = true; 59 60 protected String deviceName = null; 61 protected String healthText = null; 62 protected String physicalName = null; 63 protected String physicalDesc = null; 64 protected String serviceDesc = null; 65 66 protected int serviceVer = 1007000; 67 protected int state = JposConst.JPOS_S_CLOSED; 68 69 private EventCallbacks ecb = null; 70 71 public void open(String deviceName, EventCallbacks ecb) throws JposException { 73 this.deviceName = deviceName; 74 this.ecb = ecb; 75 this.healthText = "OK"; 76 this.state = JposConst.JPOS_S_IDLE; 77 this.serviceDesc = entry.getProp(JposEntry.DEVICE_CATEGORY_PROP_NAME).getValueAsString(); 78 this.physicalDesc = entry.getProp(JposEntry.PRODUCT_DESCRIPTION_PROP_NAME).getValueAsString(); 79 this.physicalName = entry.getProp(JposEntry.PRODUCT_NAME_PROP_NAME).getValueAsString(); 80 } 81 82 public void claim(int i) throws JposException { 83 BaseService.claimed = true; 84 } 85 86 public void release() throws JposException { 87 BaseService.claimed = false; 88 } 89 90 public void close() throws JposException { 91 BaseService.claimed = false; 92 this.freezeEvents = false; 93 this.deviceEnabled = false; 94 this.ecb = null; 95 this.healthText = "CLOSED"; 96 this.state = JposConst.JPOS_S_CLOSED; 97 } 98 99 public String getCheckHealthText() throws JposException { 101 return this.healthText; 102 } 103 104 public boolean getClaimed() throws JposException { 105 return BaseService.claimed; 106 } 107 108 public int getDataCount() throws JposException { 109 return this.eventQueue.size(); 110 } 111 112 public boolean getDataEventEnabled() throws JposException { 113 return this.eventsEnabled; 114 } 115 116 public void setDataEventEnabled(boolean b) throws JposException { 117 boolean fireEvents = false; 118 if (!this.eventsEnabled && b) { 119 fireEvents = true; 120 } 121 this.eventsEnabled = b; 122 123 if (fireEvents) { 124 this.fireQueuedEvents(); 125 } 126 } 127 128 public boolean getDeviceEnabled() throws JposException { 129 return this.deviceEnabled; 130 } 131 132 public void setDeviceEnabled(boolean b) throws JposException { 133 this.deviceEnabled = b; 134 } 135 136 public String getDeviceServiceDescription() throws JposException { 137 return this.serviceDesc; 138 } 139 140 public int getDeviceServiceVersion() throws JposException { 141 return this.serviceVer; 142 } 143 144 public boolean getFreezeEvents() throws JposException { 145 return this.freezeEvents; 146 } 147 148 public void setFreezeEvents(boolean b) throws JposException { 149 this.freezeEvents = b; 150 } 151 152 public String getPhysicalDeviceDescription() throws JposException { 153 return this.physicalDesc; 154 } 155 156 public String getPhysicalDeviceName() throws JposException { 157 return this.physicalName; 158 } 159 160 public int getState() throws JposException { 161 return this.state; 162 } 163 164 public void checkHealth(int i) throws JposException { 165 } 167 168 public void directIO(int i, int[] ints, Object o) throws JposException { 169 } 171 172 public void setEntry(JposEntry entry) { 173 this.entry = entry; 174 } 175 176 public void deleteInstance() throws JposException { 178 } 180 181 protected void fireEvent(Object ev) { 182 if (this.eventsEnabled && this.ecb != null) { 183 if (ev instanceof DataEvent) { 184 this.ecb.fireDataEvent((DataEvent) ev); 185 } else if (ev instanceof DirectIOEvent) { 186 this.ecb.fireDirectIOEvent((DirectIOEvent) ev); 187 } else if (ev instanceof DirectIOEvent) { 188 this.ecb.fireErrorEvent((ErrorEvent) ev); 189 } else if (ev instanceof DirectIOEvent) { 190 this.ecb.fireOutputCompleteEvent((OutputCompleteEvent) ev); 191 } else if (ev instanceof DirectIOEvent) { 192 this.ecb.fireStatusUpdateEvent((StatusUpdateEvent) ev); 193 } 194 } else { 195 this.eventQueue.add(ev); 196 } 197 } 198 199 private void fireQueuedEvents() { 200 List queuedList = new ArrayList (eventQueue); 201 this.eventQueue = new ArrayList (); 202 Iterator i = queuedList.iterator(); 203 204 while (i.hasNext()) { 205 Object obj = i.next(); 206 i.remove(); 207 this.fireEvent(obj); 208 } 209 } 210 } 211 | Popular Tags |