KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > jpos > service > BaseService


1 /*
2  * $Id: BaseService.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.pos.jpos.service;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
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 /**
42  * JPOS BaseService Implementation for Keyboard Wedge Services
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 5462 $
46  * @since 3.2
47  */

48 public class BaseService implements jpos.services.BaseService, jpos.loader.JposServiceInstance {
49
50     public static final String JavaDoc module = BaseService.class.getName();
51     protected static boolean claimed = false;
52
53     protected List JavaDoc eventQueue = new ArrayList JavaDoc();
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 JavaDoc deviceName = null;
61     protected String JavaDoc healthText = null;
62     protected String JavaDoc physicalName = null;
63     protected String JavaDoc physicalDesc = null;
64     protected String JavaDoc serviceDesc = null;
65
66     protected int serviceVer = 1007000;
67     protected int state = JposConst.JPOS_S_CLOSED;
68
69     private EventCallbacks ecb = null;
70
71     // open/close methods
72
public void open(String JavaDoc 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     // field methods
100
public String JavaDoc 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 JavaDoc 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 JavaDoc getPhysicalDeviceDescription() throws JposException {
153         return this.physicalDesc;
154     }
155
156     public String JavaDoc 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         // This method is not used since there is no physical device to check
166
}
167
168     public void directIO(int i, int[] ints, Object JavaDoc o) throws JposException {
169         // This method is not used since there is no physical IO to be performed
170
}
171
172     public void setEntry(JposEntry entry) {
173         this.entry = entry;
174     }
175
176     // JposServiceInstance
177
public void deleteInstance() throws JposException {
178         // TODO: Implement Me!
179
}
180
181     protected void fireEvent(Object JavaDoc 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 JavaDoc queuedList = new ArrayList JavaDoc(eventQueue);
201         this.eventQueue = new ArrayList JavaDoc();
202         Iterator JavaDoc i = queuedList.iterator();
203
204         while (i.hasNext()) {
205             Object JavaDoc obj = i.next();
206             i.remove();
207             this.fireEvent(obj);
208         }
209     }
210 }
211
Popular Tags