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.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import java.util.ArrayList;
027:
028: /**
029: * BaseAxisDataLocator implements common code and serves as a base class
030: * for the supported default Axis2 dialect data locators.
031: */
032:
033: public abstract class BaseAxisDataLocator {
034: private static final Log log = LogFactory
035: .getLog(BaseAxisDataLocator.class);
036:
037: protected ServiceData[] dataList = null;
038: private OutputForm outputform = OutputForm.INLINE_FORM;
039:
040: /**
041: * The default Axis2 Data locator getData API
042: * Checks data information configured in ServiceData.xml for the supported
043: * output forms: inline, url, EndpointReference.
044: * <p/>
045: * Note: Subclass that has its implementation of outInlineForm, outputLocationForm,
046: * and outputReferenceForm logic must implement the getData API.
047: */
048:
049: public Data[] getData(DataRetrievalRequest request,
050: MessageContext msgContext) throws DataRetrievalException {
051: log.trace("Default Base DataLocator getData starts");
052:
053: OutputForm outputform = (OutputForm) request.getOutputForm();
054:
055: if (outputform == null) { // not defined, defualt to inline
056: outputform = OutputForm.INLINE_FORM;
057: }
058:
059: Data[] output = null;
060:
061: String outputFormString = outputform.getType();
062:
063: if (outputform == OutputForm.INLINE_FORM) {
064: output = outputInlineForm(msgContext, dataList);
065: } else if (outputform == OutputForm.LOCATION_FORM) {
066: output = outputLocationForm(dataList);
067:
068: } else if (outputform == OutputForm.REFERENCE_FORM) {
069: output = outputReferenceForm(msgContext, dataList);
070:
071: } else {
072: output = outputInlineForm(msgContext, dataList);
073:
074: }
075:
076: if (output == null) {
077: log
078: .info("Null data return! Data Locator does not know how to handle request for dialect= "
079: + (String) request.getDialect()
080: + " in the form of " + outputFormString);
081: }
082:
083: log.trace("Default Base DataLocator getData ends");
084:
085: return output;
086: }
087:
088: /*
089: * WSDL or a policy document
090: */
091: protected Data[] outputInlineForm(MessageContext msgContext,
092: ServiceData[] serviceData) throws DataRetrievalException {
093: OMElement metaElement = null;
094: ArrayList result = new ArrayList();
095: if (serviceData != null) {
096: int size = serviceData.length;
097: for (int i = 0; i < size; i++) {
098: metaElement = serviceData[i].getFileContent(msgContext
099: .getAxisService().getClassLoader());
100: if (metaElement != null) {
101: result.add(new Data(metaElement, serviceData[i]
102: .getIdentifier()));
103: }
104:
105: }
106:
107: }
108: return (Data[]) result.toArray(new Data[0]);
109:
110: }
111:
112: protected Data[] outputLocationForm(ServiceData[] serviceData)
113: throws DataRetrievalException {
114:
115: ArrayList result = new ArrayList();
116: if (serviceData != null) {
117: for (int i = 0; i < serviceData.length; i++) {
118:
119: String urlValue = serviceData[i].getURL();
120: if (urlValue != null) {
121: result.add(new Data(urlValue, serviceData[i]
122: .getIdentifier()));
123: }
124: }
125: }
126: return (Data[]) result.toArray(new Data[0]);
127: }
128:
129: protected Data[] outputReferenceForm(MessageContext msgContext,
130: ServiceData[] serviceData) throws DataRetrievalException {
131: OMElement epr = null;
132: ArrayList result = new ArrayList();
133: if (serviceData != null) {
134: for (int i = 0; i < serviceData.length; i++) {
135:
136: epr = serviceData[i].getEndpointReference();
137: if (epr != null) {
138: result.add(new Data((epr), serviceData[i]
139: .getIdentifier()));
140: }
141: }
142: }
143: return (Data[]) result.toArray(new Data[0]);
144: }
145:
146: protected void setServiceData(ServiceData[] inServiceData) {
147: this .dataList = inServiceData;
148: }
149:
150: protected OutputForm getOutputForm() {
151: return outputform;
152: }
153:
154: }
|