KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > Web3RfcTransformer


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.transformation;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.excalibur.pool.Poolable;
23 import org.apache.avalon.excalibur.pool.Recyclable;
24 import org.apache.avalon.framework.activity.Disposable;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.parameters.Parameters;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.ServiceSelector;
31 import org.apache.avalon.framework.service.Serviceable;
32 import org.apache.cocoon.components.web3.Web3;
33 import org.apache.cocoon.components.web3.Web3Client;
34 import org.apache.cocoon.components.web3.Web3DataSource;
35 import org.apache.cocoon.components.web3.Web3Streamer;
36 import org.apache.cocoon.environment.SourceResolver;
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.helpers.AttributesImpl JavaDoc;
40
41 import com.sap.mw.jco.IFunctionTemplate;
42 import com.sap.mw.jco.JCO;
43
44 /**
45  * TBD
46  *
47  * @author <a HREF="mailto:michael.gerzabek@at.efp.cc">Michael Gerzabek</a>
48  * @since 2.1
49  * @version CVS $Id: Web3RfcTransformer.java 30932 2004-07-29 17:35:38Z vgritsenko $
50  */

51 public class Web3RfcTransformer extends AbstractTransformer
52 implements Serviceable, Disposable, Configurable, Poolable, Recyclable {
53     
54     /** The service manager instance */
55     protected ServiceManager manager = null;
56     protected Web3DataSource web3source = null;
57     
58     protected Web3Client connection = null;
59     protected JCO.Repository repository = null;
60     protected IFunctionTemplate functionT = null;
61     protected JCO.Function function = null;
62     protected JCO.ParameterList importParameterList = null;
63     protected JCO.ParameterList tablesParameterList = null;
64     protected JCO.Record theRecord = null;
65     protected JCO.Field fillMe = null;
66     
67     protected AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
68     protected int startcount = 0;
69     protected boolean error = false;
70     protected String JavaDoc backend = null;
71     protected String JavaDoc default_backend = null;
72     protected String JavaDoc streamer = null;
73     protected HashMap JavaDoc tags = new HashMap JavaDoc();
74     
75     public void setup(SourceResolver resolver, Map JavaDoc objectModel,
76                     String JavaDoc source, Parameters parameters)
77     throws SAXException JavaDoc {
78         
79         try {
80             backend = parameters.getParameter("system");
81         }
82         catch (Exception JavaDoc x) {
83             if ( null == backend ) {
84                 getLogger().warn("No backend configured! Try to use configuration");
85                 backend = default_backend;
86             }
87         }
88     }
89
90     public void service(ServiceManager manager) {
91         this.manager = manager;
92         initTags();
93     }
94     
95     public void configure(final Configuration configuration)
96         throws ConfigurationException {
97             
98         this.default_backend = configuration.getChild("system").getValue(null);
99     }
100      
101     public void recycle() {
102         this.connection = null;
103         this.repository = null;
104         this.functionT = null;
105         this.function = null;
106         this.importParameterList = null;
107         this.tablesParameterList = null;
108         this.theRecord = null;
109         this.backend = null;
110         this.streamer = null;
111         this.error = false;
112         this.startcount = 0;
113         super.recycle();
114     }
115
116     /** Free all ressources */
117     public void dispose() {
118         this.manager = null;
119         this.attributes = null;
120         this.web3source = null;
121         this.tags = null;
122     }
123     
124     /**
125     * Receive notification of the beginning of a document.
126     */

127     public void startDocument()
128     throws SAXException JavaDoc {
129         
130         if ( null != super.contentHandler ) {
131             super.contentHandler.startDocument();
132         }
133     }
134
135     /**
136     * Receive notification of the end of a document.
137     */

138     public void endDocument()
139     throws SAXException JavaDoc {
140
141         if ( null != super.contentHandler) {
142             super.contentHandler.endDocument();
143         }
144     }
145
146     /**
147     * Receive notification of the beginning of an element.
148     */

149     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
150         throws SAXException JavaDoc {
151             
152         if ( Web3.URI.equals( uri ) && !this.error ) {
153             switch ( Integer.parseInt( (String JavaDoc) this.tags.get( loc ))) {
154                 case INCLUDE_ELEM:
155                     ServiceSelector r3sc = null;
156                     try {
157                         r3sc = (ServiceSelector)
158                             this.manager.lookup ( Web3DataSource.ROLE + "Selector");
159                         this.web3source = (Web3DataSource) r3sc.select( this.backend );
160                         this.connection = this.web3source.getWeb3Client();
161                         this.repository = (JCO.Repository) this.connection.getRepository();
162                         this.functionT = this.repository.getFunctionTemplate(
163                             a.getValue( Web3.INCLUDE_NAME_ATTR ) );
164                         this.streamer = (null == a.getValue( Web3.INCLUDE_CLASS_ATTR )) ?
165                             "default" : a.getValue( Web3.INCLUDE_CLASS_ATTR );
166                         this.function = this.functionT.getFunction();
167                     }
168                     catch (Exception JavaDoc ex) {
169                         String JavaDoc error = "Problems getting client for backend: '"
170                             + this.backend + "'";
171                         getLogger().error (error, ex);
172                         
173                         error = ex.getMessage();
174                         attributes.clear();
175                         super.contentHandler.startElement(uri, loc, raw, a);
176                         super.contentHandler.startElement(uri, Web3.PROCESSING_X_ELEM,
177                             Web3.PROCESSING_X_ELEM, attributes);
178                         super.contentHandler.characters(error.toCharArray(), 0,
179                             error.length());
180                         super.contentHandler.endElement(uri, Web3.PROCESSING_X_ELEM,
181                             Web3.PROCESSING_X_ELEM);
182                         this.error = true;
183                     }
184                     finally {
185                         this.manager.release ( r3sc );
186                     }
187                 break;
188                 case IMPORT_ELEM:
189                     this.importParameterList = this.function.getImportParameterList();
190                     this.theRecord = this.importParameterList;
191                 break;
192                 case FIELD_ELEM:
193                     this.fillMe = this.theRecord.getField(
194                         a.getValue( Web3.FIELD_NAME_ATTR ));
195                 break;
196                 case STRUCTURE_ELEM:
197                     this.theRecord = this.importParameterList.getStructure(
198                         a.getValue( Web3.STRUCTURE_NAME_ATTR ));
199                 break;
200                 case TABLES_ELEM:
201                     this.tablesParameterList = this.function.getTableParameterList();
202                 break;
203                 case TABLE_ELEM:
204                     this.theRecord = this.tablesParameterList.getTable(
205                         a.getValue( Web3.TABLE_NAME_ATTR ));
206                 break;
207                 case ROW_ELEM:
208                     if (null != this.theRecord) {
209                         try {
210                             JCO.Table tmpTable = (JCO.Table) this.theRecord;
211                             tmpTable.appendRow();
212                         }
213                         catch (ClassCastException JavaDoc x) {
214                             getLogger().error("Not a table! " + x.getMessage(), x);
215                         }
216                     }
217                 break;
218                 default:
219                     getLogger().error("Invalid element " + loc);
220             }
221         }
222         else if (super.contentHandler != null) {
223             super.contentHandler.startElement(uri, loc, raw, a);
224         }
225     }
226
227     /**
228     * Receive notification of the end of an element.
229     */

230     public void characters(char c[], int start, int len)
231     throws SAXException JavaDoc {
232         String JavaDoc theValue = new String JavaDoc(c, start, len).trim();
233         if ( null != this.fillMe ) {
234             if ( "".equals( theValue )) {
235                 theValue = null;
236             }
237             try {
238                 this.fillMe.setValue( theValue );
239                 this.fillMe = null;
240                 if( getLogger().isDebugEnabled() ) {
241                     getLogger().debug("set value = " + theValue);
242                 }
243             }
244             catch (JCO.ConversionException x) {
245                 getLogger().error( x.getMessage(), x);
246             }
247         }
248         else {
249             if (super.contentHandler != null) {
250                 super.contentHandler.characters(c, start, len);
251             }
252         }
253     }
254
255     /**
256     * Receive notification of the end of an element.
257     */

258     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
259         throws SAXException JavaDoc
260     {
261         if ( Web3.URI.equals(uri) && !this.error ) {
262             switch ( Integer.parseInt( (String JavaDoc) this.tags.get( loc ))) {
263                 case INCLUDE_ELEM:
264                     Web3Streamer w3s = null;
265                     ServiceSelector streamerSelector = null;
266                     try {
267                         this.connection.execute( this.function );
268                         streamerSelector =
269                             (ServiceSelector)
270                             this.manager.lookup( Web3Streamer.ROLE + "Selector" );
271                         w3s = (Web3Streamer) streamerSelector.select( this.streamer );
272                         w3s.stream( this.function, super.contentHandler );
273                     }
274                     catch (Exception JavaDoc x) {
275                         this.attributes.clear();
276                         super.contentHandler.startElement(uri, Web3.ABAP_EXCEPTION_ELEM,
277                             Web3.ABAP_EXCEPTION_ELEM, this.attributes);
278                         super.contentHandler.characters(x.getMessage ().toCharArray(),
279                             0, x.getMessage ().length());
280                         super.contentHandler.endElement(uri, Web3.ABAP_EXCEPTION_ELEM,
281                             Web3.ABAP_EXCEPTION_ELEM);
282                         getLogger().error(x.getMessage(), x);
283                     }
284                     finally {
285                         this.web3source.releaseWeb3Client( this.connection );
286                         if ( null != streamerSelector ) {
287                             streamerSelector.release( w3s );
288                         }
289                         manager.release( streamerSelector );
290                     }
291                     this.connection = null;
292                     this.repository = null;
293                     this.functionT = null;
294                     this.function = null;
295                     this.importParameterList = null;
296                     this.tablesParameterList = null;
297                     this.theRecord = null;
298                 break;
299                 case STRUCTURE_ELEM:
300                     this.theRecord = this.importParameterList;
301                 break;
302             }
303         }
304         else if (super.contentHandler != null) {
305             super.contentHandler.endElement(uri,loc,raw);
306         }
307     }
308
309     protected final static int INCLUDE_ELEM = 1;
310     protected final static int IMPORT_ELEM = 2;
311     protected final static int EXPORT_ELEM = 3;
312     protected final static int TABLES_ELEM = 4;
313     protected final static int FIELD_ELEM = 5;
314     protected final static int ROW_ELEM = 6;
315     protected final static int STRUCTURE_ELEM = 7;
316     protected final static int TABLE_ELEM = 8;
317     
318     protected void initTags() {
319         
320         this.tags.put( Web3.INCLUDE_ELEM, "1" );
321         this.tags.put( Web3.IMPORT_ELEM, "2" );
322         this.tags.put( Web3.EXPORT_ELEM, "3" );
323         this.tags.put( Web3.TABLES_ELEM, "4" );
324         this.tags.put( Web3.FIELD_ELEM, "5" );
325         this.tags.put( Web3.ROW_ELEM, "6" );
326         this.tags.put( Web3.STRUCTURE_ELEM, "7" );
327         this.tags.put( Web3.TABLE_ELEM, "8" );
328     }
329 }
330
331
Popular Tags