01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.pos.jpos.factory;
19:
20: import java.util.HashMap;
21: import java.util.Map;
22:
23: import jpos.JposConst;
24: import jpos.JposException;
25: import jpos.config.JposEntry;
26: import jpos.loader.JposServiceInstance;
27: import jpos.loader.JposServiceInstanceFactory;
28:
29: import org.ofbiz.base.util.ObjectType;
30: import org.ofbiz.pos.jpos.service.BaseService;
31:
32: public class OfbizJposServiceFactory extends Object implements
33: JposServiceInstanceFactory {
34:
35: public static final String module = OfbizJposServiceFactory.class
36: .getName();
37: private static Map serviceMap = new HashMap();
38:
39: public JposServiceInstance createInstance(String logicalName,
40: JposEntry entry) throws JposException {
41: // check to see if we have a service class property
42: if (!entry
43: .hasPropertyWithName(JposEntry.SERVICE_CLASS_PROP_NAME)) {
44: throw new JposException(JposConst.JPOS_E_NOSERVICE,
45: "serviceClass property not found!");
46: }
47:
48: String className = (String) entry
49: .getPropertyValue(JposEntry.SERVICE_CLASS_PROP_NAME);
50: BaseService service = (BaseService) serviceMap.get(className);
51:
52: if (service != null) {
53: service.setEntry(entry);
54: } else {
55: try {
56: Object obj = ObjectType.getInstance(className);
57: if (obj == null) {
58: throw new JposException(JposConst.JPOS_E_NOEXIST,
59: "unable to locate serviceClass");
60: }
61:
62: if (!(obj instanceof JposServiceInstance)) {
63: throw new JposException(JposConst.JPOS_E_NOSERVICE,
64: "serviceClass is not an instance of JposServiceInstance");
65: } else if (!(obj instanceof BaseService)) {
66: throw new JposException(JposConst.JPOS_E_NOSERVICE,
67: "serviceClass is not an instance of BaseKybService");
68: } else {
69: service = (BaseService) obj;
70: service.setEntry(entry);
71: serviceMap.put(className, service);
72: }
73: } catch (Exception e) {
74: throw new JposException(JposConst.JPOS_E_NOSERVICE,
75: "Error creating the service instance ["
76: + className + "]", e);
77: }
78: }
79:
80: return service;
81: }
82: }
|