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: */
019: package org.apache.axis2.dataretrieval;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axis2.context.MessageContext;
023: import org.apache.axis2.description.AxisService;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import javax.xml.namespace.QName;
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.Iterator;
031:
032: /**
033: * The Default Axis2 Data Locator implementation
034: */
035:
036: public class AxisDataLocatorImpl implements AxisDataLocator {
037: private static final Log log = LogFactory
038: .getLog(AxisDataLocatorImpl.class);
039:
040: // HashMap to cache Data elements defined in ServiceData.
041: private HashMap dataMap = new HashMap();
042:
043: private AxisService axisService;
044:
045: /**
046: * Constructor
047: *
048: * @throws DataRetrievalException
049: */
050: public AxisDataLocatorImpl(AxisService in_axisService)
051: throws DataRetrievalException {
052: super ();
053: axisService = in_axisService;
054: }
055:
056: /**
057: * Retrieves and returns data based on the specified request.
058: */
059: public Data[] getData(DataRetrievalRequest request,
060: MessageContext msgContext) throws DataRetrievalException {
061: Data[] data = null;
062: String dialect = request.getDialect();
063: String identifier = request.getIdentifier();
064: String key = dialect;
065: ArrayList dataList = new ArrayList();
066: if (identifier != null) {
067: key = key + identifier;
068: if (dataMap.get(key) != null) {
069: dataList.add(dataMap.get(key));
070: }
071: } else {
072: dataList = getDataList(dialect);
073: }
074:
075: AxisDataLocator dataLocator = DataLocatorFactory
076: .createDataLocator(dialect, (ServiceData[]) dataList
077: .toArray(new ServiceData[0]));
078:
079: if (dataLocator != null) {
080: try {
081: data = dataLocator.getData(request, msgContext);
082: } catch (Throwable e) {
083: log.info("getData request failed for dialect, "
084: + dialect, e);
085: throw new DataRetrievalException(e);
086: }
087: } else {
088: String message = "Failed to instantiate Data Locator for dialect, "
089: + dialect;
090: log.info(message);
091: throw new DataRetrievalException(message);
092: }
093: return data;
094: }
095:
096: /*
097: * For AxisService use only!
098: */
099: public void loadServiceData() {
100: DataRetrievalUtil util = DataRetrievalUtil.getInstance();
101:
102: OMElement serviceData = null;
103: String file = "META-INF/" + DRConstants.SERVICE_DATA.FILE_NAME;
104: try {
105: serviceData = util.buildOM(axisService.getClassLoader(),
106: "META-INF/" + DRConstants.SERVICE_DATA.FILE_NAME);
107: } catch (DataRetrievalException e) {
108: // It is not required to define ServiceData for a Service, just log a warning message
109:
110: String message = "Check loading failure for file, " + file;
111: log.debug(message + ".Message = " + e.getMessage());
112: log.debug(message, e);
113: }
114: if (serviceData != null) {
115: cachingServiceData(serviceData);
116: }
117: }
118:
119: /*
120: * caching ServiceData for Axis2 Data Locators
121: */
122: private void cachingServiceData(OMElement e) {
123: Iterator i = e.getChildrenWithName(new QName(
124: DRConstants.SERVICE_DATA.DATA));
125: String saveKey = "";
126: while (i.hasNext()) {
127: ServiceData data = new ServiceData((OMElement) i.next());
128: saveKey = data.getDialect();
129:
130: String identifier = data.getIdentifier();
131: if (identifier != null) {
132: saveKey = saveKey + identifier;
133: }
134: dataMap.put(saveKey, data);
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: }
|