001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.pos.jpos.service;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Iterator;
023:
024: import jpos.services.EventCallbacks;
025: import jpos.JposException;
026: import jpos.JposConst;
027: import jpos.events.DataEvent;
028: import jpos.events.ErrorEvent;
029: import jpos.events.DirectIOEvent;
030: import jpos.events.OutputCompleteEvent;
031: import jpos.events.StatusUpdateEvent;
032: import jpos.config.JposEntry;
033:
034: public class BaseService implements jpos.services.BaseService,
035: jpos.loader.JposServiceInstance {
036:
037: public static final String module = BaseService.class.getName();
038: protected static boolean claimed = false;
039:
040: protected List eventQueue = new ArrayList();
041: protected JposEntry entry = null;
042:
043: protected boolean freezeEvents = false;
044: protected boolean deviceEnabled = false;
045: protected boolean eventsEnabled = true;
046:
047: protected String deviceName = null;
048: protected String healthText = null;
049: protected String physicalName = null;
050: protected String physicalDesc = null;
051: protected String serviceDesc = null;
052:
053: protected int serviceVer = 1007000;
054: protected int state = JposConst.JPOS_S_CLOSED;
055:
056: private EventCallbacks ecb = null;
057:
058: // open/close methods
059: public void open(String deviceName, EventCallbacks ecb)
060: throws JposException {
061: this .deviceName = deviceName;
062: this .ecb = ecb;
063: this .healthText = "OK";
064: this .state = JposConst.JPOS_S_IDLE;
065: this .serviceDesc = entry.getProp(
066: JposEntry.DEVICE_CATEGORY_PROP_NAME).getValueAsString();
067: this .physicalDesc = entry.getProp(
068: JposEntry.PRODUCT_DESCRIPTION_PROP_NAME)
069: .getValueAsString();
070: this .physicalName = entry.getProp(
071: JposEntry.PRODUCT_NAME_PROP_NAME).getValueAsString();
072: }
073:
074: public void claim(int i) throws JposException {
075: BaseService.claimed = true;
076: }
077:
078: public void release() throws JposException {
079: BaseService.claimed = false;
080: }
081:
082: public void close() throws JposException {
083: BaseService.claimed = false;
084: this .freezeEvents = false;
085: this .deviceEnabled = false;
086: this .ecb = null;
087: this .healthText = "CLOSED";
088: this .state = JposConst.JPOS_S_CLOSED;
089: }
090:
091: // field methods
092: public String getCheckHealthText() throws JposException {
093: return this .healthText;
094: }
095:
096: public boolean getClaimed() throws JposException {
097: return BaseService.claimed;
098: }
099:
100: public int getDataCount() throws JposException {
101: return this .eventQueue.size();
102: }
103:
104: public boolean getDataEventEnabled() throws JposException {
105: return this .eventsEnabled;
106: }
107:
108: public void setDataEventEnabled(boolean b) throws JposException {
109: boolean fireEvents = false;
110: if (!this .eventsEnabled && b) {
111: fireEvents = true;
112: }
113: this .eventsEnabled = b;
114:
115: if (fireEvents) {
116: this .fireQueuedEvents();
117: }
118: }
119:
120: public boolean getDeviceEnabled() throws JposException {
121: return this .deviceEnabled;
122: }
123:
124: public void setDeviceEnabled(boolean b) throws JposException {
125: this .deviceEnabled = b;
126: }
127:
128: public String getDeviceServiceDescription() throws JposException {
129: return this .serviceDesc;
130: }
131:
132: public int getDeviceServiceVersion() throws JposException {
133: return this .serviceVer;
134: }
135:
136: public boolean getFreezeEvents() throws JposException {
137: return this .freezeEvents;
138: }
139:
140: public void setFreezeEvents(boolean b) throws JposException {
141: this .freezeEvents = b;
142: }
143:
144: public String getPhysicalDeviceDescription() throws JposException {
145: return this .physicalDesc;
146: }
147:
148: public String getPhysicalDeviceName() throws JposException {
149: return this .physicalName;
150: }
151:
152: public int getState() throws JposException {
153: return this .state;
154: }
155:
156: public void checkHealth(int i) throws JposException {
157: // This method is not used since there is no physical device to check
158: }
159:
160: public void directIO(int i, int[] ints, Object o)
161: throws JposException {
162: // This method is not used since there is no physical IO to be performed
163: }
164:
165: public void setEntry(JposEntry entry) {
166: this .entry = entry;
167: }
168:
169: // JposServiceInstance
170: public void deleteInstance() throws JposException {
171: // TODO: Implement Me!
172: }
173:
174: protected void fireEvent(Object ev) {
175: if (this .eventsEnabled && this .ecb != null) {
176: if (ev instanceof DataEvent) {
177: this .ecb.fireDataEvent((DataEvent) ev);
178: } else if (ev instanceof DirectIOEvent) {
179: this .ecb.fireDirectIOEvent((DirectIOEvent) ev);
180: } else if (ev instanceof DirectIOEvent) {
181: this .ecb.fireErrorEvent((ErrorEvent) ev);
182: } else if (ev instanceof DirectIOEvent) {
183: this .ecb
184: .fireOutputCompleteEvent((OutputCompleteEvent) ev);
185: } else if (ev instanceof DirectIOEvent) {
186: this .ecb.fireStatusUpdateEvent((StatusUpdateEvent) ev);
187: }
188: } else {
189: this .eventQueue.add(ev);
190: }
191: }
192:
193: private void fireQueuedEvents() {
194: List queuedList = new ArrayList(eventQueue);
195: this .eventQueue = new ArrayList();
196: Iterator i = queuedList.iterator();
197:
198: while (i.hasNext()) {
199: Object obj = i.next();
200: i.remove();
201: this.fireEvent(obj);
202: }
203: }
204: }
|