1 16 17 package org.apache.cocoon.transformation; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 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 ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.helpers.AttributesImpl ; 40 41 import com.sap.mw.jco.IFunctionTemplate; 42 import com.sap.mw.jco.JCO; 43 44 51 public class Web3RfcTransformer extends AbstractTransformer 52 implements Serviceable, Disposable, Configurable, Poolable, Recyclable { 53 54 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 attributes = new AttributesImpl (); 68 protected int startcount = 0; 69 protected boolean error = false; 70 protected String backend = null; 71 protected String default_backend = null; 72 protected String streamer = null; 73 protected HashMap tags = new HashMap (); 74 75 public void setup(SourceResolver resolver, Map objectModel, 76 String source, Parameters parameters) 77 throws SAXException { 78 79 try { 80 backend = parameters.getParameter("system"); 81 } 82 catch (Exception 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 117 public void dispose() { 118 this.manager = null; 119 this.attributes = null; 120 this.web3source = null; 121 this.tags = null; 122 } 123 124 127 public void startDocument() 128 throws SAXException { 129 130 if ( null != super.contentHandler ) { 131 super.contentHandler.startDocument(); 132 } 133 } 134 135 138 public void endDocument() 139 throws SAXException { 140 141 if ( null != super.contentHandler) { 142 super.contentHandler.endDocument(); 143 } 144 } 145 146 149 public void startElement(String uri, String loc, String raw, Attributes a) 150 throws SAXException { 151 152 if ( Web3.URI.equals( uri ) && !this.error ) { 153 switch ( Integer.parseInt( (String ) 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 ex) { 169 String 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 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 230 public void characters(char c[], int start, int len) 231 throws SAXException { 232 String theValue = new String (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 258 public void endElement(String uri, String loc, String raw) 259 throws SAXException 260 { 261 if ( Web3.URI.equals(uri) && !this.error ) { 262 switch ( Integer.parseInt( (String ) 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 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 |