001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.transformation;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.avalon.excalibur.pool.Poolable;
023: import org.apache.avalon.excalibur.pool.Recyclable;
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.configuration.Configurable;
026: import org.apache.avalon.framework.configuration.Configuration;
027: import org.apache.avalon.framework.configuration.ConfigurationException;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.ServiceSelector;
032: import org.apache.avalon.framework.service.Serviceable;
033: import org.apache.cocoon.components.web3.Web3;
034: import org.apache.cocoon.components.web3.Web3Client;
035: import org.apache.cocoon.components.web3.Web3DataSource;
036: import org.apache.cocoon.components.web3.Web3Streamer;
037: import org.apache.cocoon.environment.SourceResolver;
038: import org.xml.sax.Attributes;
039: import org.xml.sax.SAXException;
040: import org.xml.sax.helpers.AttributesImpl;
041:
042: import com.sap.mw.jco.IFunctionTemplate;
043: import com.sap.mw.jco.JCO;
044:
045: /**
046: * TBD
047: *
048: * @author <a href="mailto:michael.gerzabek@at.efp.cc">Michael Gerzabek</a>
049: * @since 2.1
050: * @version CVS $Id: Web3RfcTransformer.java 524407 2007-03-31 10:59:28Z joerg $
051: */
052: public class Web3RfcTransformer extends AbstractTransformer implements
053: Serviceable, Disposable, Configurable, Poolable, Recyclable {
054:
055: /** The service manager instance */
056: protected ServiceManager manager;
057: protected Web3DataSource web3source;
058:
059: protected Web3Client connection;
060: protected JCO.Repository repository;
061: protected IFunctionTemplate functionT;
062: protected JCO.Function function;
063: protected JCO.ParameterList importParameterList;
064: protected JCO.ParameterList tablesParameterList;
065: protected JCO.Record theRecord;
066: protected JCO.Field fillMe;
067:
068: protected AttributesImpl attributes = new AttributesImpl();
069: protected int startcount = 0;
070: protected boolean error = false;
071: protected String backend;
072: protected String default_backend;
073: protected String streamer;
074: protected HashMap tags = new HashMap();
075:
076: public void configure(final Configuration configuration)
077: throws ConfigurationException {
078: this .default_backend = configuration.getChild("system")
079: .getValue(null);
080: }
081:
082: public void service(ServiceManager manager) throws ServiceException {
083: this .manager = manager;
084: initTags();
085: }
086:
087: public void setup(SourceResolver resolver, Map objectModel,
088: String source, Parameters parameters) throws SAXException {
089: try {
090: this .backend = parameters.getParameter("system");
091: } catch (Exception x) {
092: getLogger().warn(
093: "No backend configured! Try to use configuration");
094: this .backend = this .default_backend;
095: }
096: }
097:
098: public void recycle() {
099: this .connection = null;
100: this .repository = null;
101: this .functionT = null;
102: this .function = null;
103: this .importParameterList = null;
104: this .tablesParameterList = null;
105: this .theRecord = null;
106: this .backend = null;
107: this .streamer = null;
108: this .error = false;
109: this .startcount = 0;
110: super .recycle();
111: }
112:
113: /** Free all ressources */
114: public void dispose() {
115: this .manager = null;
116: this .attributes = null;
117: this .web3source = null;
118: this .tags = null;
119: }
120:
121: /**
122: * Receive notification of the beginning of an element.
123: */
124: public void startElement(String uri, String loc, String raw,
125: Attributes a) throws SAXException {
126: if (Web3.URI.equals(uri) && !this .error) {
127: switch (Integer.parseInt((String) this .tags.get(loc))) {
128: case INCLUDE_ELEM:
129: ServiceSelector r3sc = null;
130: try {
131: r3sc = (ServiceSelector) this .manager
132: .lookup(Web3DataSource.ROLE + "Selector");
133: this .web3source = (Web3DataSource) r3sc
134: .select(this .backend);
135: this .connection = this .web3source.getWeb3Client();
136: this .repository = (JCO.Repository) this .connection
137: .getRepository();
138: this .functionT = this .repository
139: .getFunctionTemplate(a
140: .getValue(Web3.INCLUDE_NAME_ATTR));
141: this .streamer = (null == a
142: .getValue(Web3.INCLUDE_CLASS_ATTR)) ? "default"
143: : a.getValue(Web3.INCLUDE_CLASS_ATTR);
144: this .function = this .functionT.getFunction();
145: } catch (Exception ex) {
146: String error = "Problems getting client for backend: '"
147: + this .backend + "'";
148: getLogger().error(error, ex);
149:
150: error = ex.getMessage();
151: this .attributes.clear();
152: super .startElement(uri, loc, raw, a);
153: super .startElement(uri, Web3.PROCESSING_X_ELEM,
154: Web3.PROCESSING_X_ELEM, this .attributes);
155: super .characters(error.toCharArray(), 0, error
156: .length());
157: super .endElement(uri, Web3.PROCESSING_X_ELEM,
158: Web3.PROCESSING_X_ELEM);
159: this .error = true;
160: } finally {
161: this .manager.release(r3sc);
162: }
163: break;
164: case IMPORT_ELEM:
165: this .importParameterList = this .function
166: .getImportParameterList();
167: this .theRecord = this .importParameterList;
168: break;
169: case FIELD_ELEM:
170: this .fillMe = this .theRecord.getField(a
171: .getValue(Web3.FIELD_NAME_ATTR));
172: break;
173: case STRUCTURE_ELEM:
174: this .theRecord = this .importParameterList
175: .getStructure(a
176: .getValue(Web3.STRUCTURE_NAME_ATTR));
177: break;
178: case TABLES_ELEM:
179: this .tablesParameterList = this .function
180: .getTableParameterList();
181: break;
182: case TABLE_ELEM:
183: this .theRecord = this .tablesParameterList.getTable(a
184: .getValue(Web3.TABLE_NAME_ATTR));
185: break;
186: case ROW_ELEM:
187: if (null != this .theRecord) {
188: try {
189: JCO.Table tmpTable = (JCO.Table) this .theRecord;
190: tmpTable.appendRow();
191: } catch (ClassCastException x) {
192: getLogger().error(
193: "Not a table! " + x.getMessage(), x);
194: }
195: }
196: break;
197: default:
198: getLogger().error("Invalid element " + loc);
199: }
200: } else {
201: super .startElement(uri, loc, raw, a);
202: }
203: }
204:
205: /**
206: * Receive notification of the end of an element.
207: */
208: public void characters(char c[], int start, int len)
209: throws SAXException {
210: String theValue = new String(c, start, len).trim();
211: if (null != this .fillMe) {
212: if ("".equals(theValue)) {
213: theValue = null;
214: }
215: try {
216: this .fillMe.setValue(theValue);
217: this .fillMe = null;
218: if (getLogger().isDebugEnabled()) {
219: getLogger().debug("set value = " + theValue);
220: }
221: } catch (JCO.ConversionException x) {
222: getLogger().error(x.getMessage(), x);
223: }
224: } else {
225: super .characters(c, start, len);
226: }
227: }
228:
229: /**
230: * Receive notification of the end of an element.
231: */
232: public void endElement(String uri, String loc, String raw)
233: throws SAXException {
234: if (Web3.URI.equals(uri) && !this .error) {
235: switch (Integer.parseInt((String) this .tags.get(loc))) {
236: case INCLUDE_ELEM:
237: Web3Streamer w3s = null;
238: ServiceSelector streamerSelector = null;
239: try {
240: this .connection.execute(this .function);
241: streamerSelector = (ServiceSelector) this .manager
242: .lookup(Web3Streamer.ROLE + "Selector");
243: w3s = (Web3Streamer) streamerSelector
244: .select(this .streamer);
245: w3s.stream(this .function, this .contentHandler);
246: } catch (Exception x) {
247: this .attributes.clear();
248: super .startElement(uri, Web3.ABAP_EXCEPTION_ELEM,
249: Web3.ABAP_EXCEPTION_ELEM, this .attributes);
250: super .characters(x.getMessage().toCharArray(), 0, x
251: .getMessage().length());
252: super .endElement(uri, Web3.ABAP_EXCEPTION_ELEM,
253: Web3.ABAP_EXCEPTION_ELEM);
254: getLogger().error(x.getMessage(), x);
255: } finally {
256: this .web3source.releaseWeb3Client(this .connection);
257: if (null != streamerSelector) {
258: streamerSelector.release(w3s);
259: }
260: this .manager.release(streamerSelector);
261: }
262: this .connection = null;
263: this .repository = null;
264: this .functionT = null;
265: this .function = null;
266: this .importParameterList = null;
267: this .tablesParameterList = null;
268: this .theRecord = null;
269: break;
270: case STRUCTURE_ELEM:
271: this .theRecord = this .importParameterList;
272: break;
273: }
274: } else {
275: super .endElement(uri, loc, raw);
276: }
277: }
278:
279: protected final static int INCLUDE_ELEM = 1;
280: protected final static int IMPORT_ELEM = 2;
281: protected final static int EXPORT_ELEM = 3;
282: protected final static int TABLES_ELEM = 4;
283: protected final static int FIELD_ELEM = 5;
284: protected final static int ROW_ELEM = 6;
285: protected final static int STRUCTURE_ELEM = 7;
286: protected final static int TABLE_ELEM = 8;
287:
288: protected void initTags() {
289: this .tags.put(Web3.INCLUDE_ELEM, "1");
290: this .tags.put(Web3.IMPORT_ELEM, "2");
291: this .tags.put(Web3.EXPORT_ELEM, "3");
292: this .tags.put(Web3.TABLES_ELEM, "4");
293: this .tags.put(Web3.FIELD_ELEM, "5");
294: this .tags.put(Web3.ROW_ELEM, "6");
295: this .tags.put(Web3.STRUCTURE_ELEM, "7");
296: this .tags.put(Web3.TABLE_ELEM, "8");
297: }
298:
299: }
|