1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
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 */
19
20 package org.apache.axis2.dataretrieval;
21
22 import org.apache.axiom.om.OMElement;
23 import org.apache.axis2.context.MessageContext;
24 import org.apache.axis2.description.AxisService;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import javax.xml.namespace.QName;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32
33 /**
34 * The Default Axis2 Data Locator implementation
35 */
36
37 public class AxisDataLocatorImpl implements AxisDataLocator {
38 private static final Log log = LogFactory.getLog(AxisDataLocatorImpl.class);
39
40 // HashMap to cache Data elements defined in ServiceData.
41 private HashMap dataMap = new HashMap();
42
43 private AxisService axisService;
44
45
46 /**
47 * Constructor
48 *
49 * @throws DataRetrievalException
50 */
51 public AxisDataLocatorImpl(AxisService in_axisService) throws DataRetrievalException {
52 super();
53 axisService = in_axisService;
54 }
55
56 /**
57 * Retrieves and returns data based on the specified request.
58 */
59 public Data[] getData(DataRetrievalRequest request,
60 MessageContext msgContext) throws DataRetrievalException {
61 Data[] data = null;
62 String dialect = request.getDialect();
63 String identifier = request.getIdentifier();
64 String key = dialect;
65 ArrayList dataList = new ArrayList();
66 if (identifier != null) {
67 key = key + identifier;
68 if (dataMap.get(key) != null) {
69 dataList.add(dataMap.get(key));
70 }
71 } else {
72 dataList = getDataList(dialect);
73 }
74
75
76 AxisDataLocator dataLocator = DataLocatorFactory
77 .createDataLocator(dialect, (ServiceData[]) dataList.toArray(new ServiceData[0]));
78
79 if (dataLocator != null) {
80 try {
81 data = dataLocator.getData(request, msgContext);
82 }
83 catch (Throwable e) {
84 log.info("getData request failed for dialect, " + dialect, e);
85 throw new DataRetrievalException(e);
86 }
87 } else {
88 String message = "Failed to instantiate Data Locator for dialect, " + dialect;
89 log.info(message);
90 throw new DataRetrievalException(message);
91 }
92 return data;
93 }
94
95 /*
96 * For AxisService use only!
97 */
98 public void loadServiceData() {
99 DataRetrievalUtil util = DataRetrievalUtil.getInstance();
100
101 OMElement serviceData = null;
102 String file = "META-INF/" + DRConstants.SERVICE_DATA.FILE_NAME;
103 try {
104 serviceData = util.buildOM(axisService.getClassLoader(),
105 "META-INF/" + DRConstants.SERVICE_DATA.FILE_NAME);
106 } catch (DataRetrievalException e) {
107 // It is not required to define ServiceData for a Service, just log a warning message
108
109 String message = "Check loading failure for file, " + file;
110 log.debug(message + ".Message = " + e.getMessage());
111 log.debug(message, e);
112 }
113 if (serviceData != null) {
114 cachingServiceData(serviceData);
115 }
116 }
117
118 /*
119 * caching ServiceData for Axis2 Data Locators
120 */
121 private void cachingServiceData(OMElement e) {
122 Iterator i = e.getChildrenWithName(new QName(
123 DRConstants.SERVICE_DATA.DATA));
124 String saveKey = "";
125 while (i.hasNext()) {
126 ServiceData data = new ServiceData((OMElement) i.next());
127 saveKey = data.getDialect();
128
129 String identifier = data.getIdentifier();
130 if (identifier != null) {
131 saveKey = saveKey + identifier;
132 }
133 dataMap.put(saveKey, data);
134
135
136 }
137
138 }
139
140 /*
141 * Return ServiceData for specified dialect
142 */
143 private ArrayList getDataList(String dialect) {
144 ArrayList dataList = new ArrayList();
145 Iterator keys = dataMap.keySet().iterator();
146
147 while (keys.hasNext()) {
148 String keyStr = (String) keys.next();
149 // get all Data element that matching the dialect
150 if (keyStr.indexOf(dialect) == 0) {
151 dataList.add(dataMap.get(keyStr));
152 }
153 }
154 return dataList;
155 }
156 }