1 57 58 package com.myeis.j2c.formathandler; 59 60 import java.beans.BeanInfo ; 61 import java.beans.Introspector ; 62 import java.beans.PropertyDescriptor ; 63 import java.io.ByteArrayInputStream ; 64 import java.io.IOException ; 65 import java.io.InputStream ; 66 import java.io.InputStreamReader ; 67 import java.lang.reflect.Method ; 68 import java.util.Hashtable ; 69 70 import javax.wsdl.Binding; 71 import javax.wsdl.Definition; 72 import javax.xml.namespace.QName ; 73 89 90 import org.apache.wsif.WSIFException; 91 import org.apache.wsif.providers.jca.toolplugin.FormatHandlerGenerator; 92 import org.apache.wsif.util.WSIFUtils; 93 94 public class MyEISFormatHandlerGenerator implements FormatHandlerGenerator { 95 96 99 public Hashtable generate( 100 String generationPackage, 101 String beanClass, 102 javax.wsdl.Definition aDefinition, 103 javax.wsdl.Binding aBinding, 104 String anEncoding, 105 String aStyle, 106 javax.xml.namespace.QName xsdQname, 107 int elementType, 108 Object generationContext) throws WSIFException 109 { 110 111 if (!anEncoding.equals("myeis")) { 112 System.err.println("Encoding: " + anEncoding); 113 return null; 114 } 115 116 ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader(); 117 String definitionPackageName = WSIFUtils.getPackageNameFromNamespaceURI(aDefinition.getTargetNamespace()); 118 definitionPackageName = definitionPackageName.replace('.','/'); 119 String metaDataFile = "com/myeis/services/MyEISFormatHandlerGenMetaData.eis"; 120 InputStream metaDataStream = threadClassLoader.getResourceAsStream(metaDataFile); 121 String metaDatafield = null; 122 123 if (metaDataStream != null) { 124 try { 125 InputStreamReader reader = new InputStreamReader (metaDataStream); 127 char buffer[] = new char[1024]; 128 StringBuffer data = new StringBuffer (); 129 int length; 130 length = reader.read(buffer); 131 while (length > 0) { 132 data.append(buffer, 0, length); 133 length = reader.read(buffer); 134 } 135 metaDatafield = data.toString(); 136 } catch (IOException e) { 137 throw new WSIFException("Error occurred reading metadata needed for format handler generator!",e); 139 } 140 141 } 142 143 String beanClassName = beanClass.substring(beanClass.lastIndexOf(".") + 1, beanClass.length()); 144 String beanClassField = "field" + beanClassName; 145 String formatHandlerClassName = beanClassName + "FormatHandler"; 146 147 StringBuffer buffer = new StringBuffer (); 148 149 buffer.append("package " + generationPackage + ";\n"); 150 buffer.append(importsGen); 151 buffer.append("public class " + beanClassName + "FormatHandler implements WSIFFormatHandler_JCA {\n"); 152 153 buffer.append("private " + beanClassName + " " + beanClassField + " = null;\n"); 154 155 if(metaDatafield!=null && metaDatafield.length()>0){ 156 buffer.append("private static String META_DATA_A = \""+ metaDatafield+"\";\n"); 157 } 158 159 160 buffer.append("public void read(InputStream inputStream) throws IOException {\n"); 161 buffer.append(" try {\n"); 162 buffer.append(" ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);\n"); 163 buffer.append(" Object obj = (Object)objectInputStream.readObject();\n"); 164 buffer.append(" if(obj instanceof "+beanClassName+"){\n"); 165 buffer.append(" this."+ beanClassField +" = ("+ beanClassName + ")obj;\n"); 166 buffer.append(" }else if (obj instanceof com.myeis.services.internal."+beanClassName+"DataObject){\n"); 167 buffer.append(" com.myeis.services.internal."+ beanClassName + "DataObject a"+ beanClassName + " = (com.myeis.services.internal."+ beanClassName + "DataObject)obj;\n"); 168 buffer.append(" this."+ beanClassField +" = new "+ beanClassName +"();\n"); 169 170 try { 171 Class bean = 172 Class.forName("com.myeis.services.internal." + beanClassName+"DataObject"); 173 BeanInfo bInfo = Introspector.getBeanInfo(bean); 174 if (bInfo != null) { 175 PropertyDescriptor properties[] = 176 bInfo.getPropertyDescriptors(); 177 for (int i = 0; i < properties.length; i++) { 178 Method writer = properties[i].getWriteMethod(); 179 Method reader = properties[i].getReadMethod(); 180 if (reader != null && writer != null) { 181 String setter = writer.getName(); 182 String getter = reader.getName(); 183 buffer.append( 184 " this." 185 + beanClassField 186 + "." 187 + setter 188 + "(a" 189 + beanClassName 190 + "." 191 + getter 192 + "());\n"); 193 } 194 } 195 } 196 } catch (Exception e) { 197 201 buffer.append(" String ex ="+e+";\n"); 202 } 203 204 buffer.append(" }\n"); 205 buffer.append(" } catch (ClassNotFoundException e) {\n"); 206 buffer.append(" e.printStackTrace();\n"); 207 buffer.append(" throw new IOException(\"class not found !\");\n"); 208 buffer.append(" } \n"); 209 buffer.append("}\n\n"); 210 buffer.append("public void write(OutputStream outputStream) throws IOException {\n"); 211 212 buffer.append(" ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);\n"); 213 buffer.append(" objectOutputStream.writeObject(this." + beanClassField + ");\n"); 214 buffer.append(" objectOutputStream.flush();\n"); 215 buffer.append("}\n\n"); 216 217 buffer.append("public void setObjectPart(Object objectPart) {\n"); 218 buffer.append(" this." + beanClassField + " = (" + beanClassName + ")objectPart;\n"); 219 buffer.append("}\n\n"); 220 buffer.append("public Object getObjectPart() {\n"); 221 buffer.append(" return this." + beanClassField + ";\n"); 222 buffer.append("}\n\n"); 223 224 buffer.append("public Object getObjectPart(Class sourceClass) {\n"); 225 buffer.append(" try{\n"); 226 buffer.append(" if (sourceClass.isAssignableFrom(Class.forName(\"javax.xml.transform.sax.SAXSource\"))){\n"); 227 buffer.append(" SAXSource saxSource = new SAXSource(new InputSource());\n"); 228 buffer.append(" XMLReader reader = new ContentReader();\n"); 229 buffer.append(" saxSource.setXMLReader(reader);\n"); 230 buffer.append(" return saxSource;\n"); 231 buffer.append(" }\n"); 232 buffer.append(" }\n"); 233 buffer.append(" catch (Throwable e) {\n"); 234 buffer.append(" }\n"); 235 236 buffer.append(" return null;\n"); 237 buffer.append("}\n\n"); 238 239 buffer.append(defaultMethodsGen); 240 241 writeContentReader(buffer, beanClassField); 242 buffer.append("}"); 244 ByteArrayInputStream inputStream = new ByteArrayInputStream (buffer.toString().getBytes()); 245 Hashtable returnTable = new Hashtable (); 246 returnTable.put(formatHandlerClassName, inputStream); 247 return returnTable; 248 249 } 250 251 private void writeContentReader(StringBuffer buffer, String beanClassField) { 252 buffer.append("public class ContentReader implements org.xml.sax.XMLReader {\n"); 253 buffer.append(" private ContentHandler fieldContentHandler = null;\n"); 254 255 buffer.append(" public org.xml.sax.ContentHandler getContentHandler() {return null;}\n"); 256 buffer.append(" public org.xml.sax.DTDHandler getDTDHandler() {return null;}\n"); 257 buffer.append(" public org.xml.sax.EntityResolver getEntityResolver() {return null;}\n"); 258 buffer.append(" public org.xml.sax.ErrorHandler getErrorHandler() {return null;}\n"); 259 buffer.append(" public boolean getFeature(String name) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {return false;}\n"); 260 buffer.append(" public Object getProperty(String name) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {return null;}\n"); 261 buffer.append(" public void setDTDHandler(org.xml.sax.DTDHandler handler) {}\n"); 262 buffer.append(" public void setEntityResolver(org.xml.sax.EntityResolver resolver) {}\n"); 263 buffer.append(" public void setErrorHandler(org.xml.sax.ErrorHandler handler) {}\n"); 264 buffer.append(" public void setFeature(String name, boolean value) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {}\n"); 265 buffer.append(" public void setProperty(String name, Object value) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {}\n"); 266 buffer.append(" public void parse(String systemId) throws java.io.IOException, org.xml.sax.SAXException {}\n"); 267 buffer.append(" public void parse(org.xml.sax.InputSource input) throws java.io.IOException, org.xml.sax.SAXException {\n"); 268 buffer.append(" try {\n"); 269 buffer.append(" Attributes attributes = new AttributesImpl();\n"); 270 buffer.append(" this.fieldContentHandler.startDocument();\n"); 271 272 if (beanClassField.equals("fieldCustomer")) { 273 274 buffer.append(" this.fieldContentHandler.startElement(\"\", \"number\", \"number\", attributes);\n"); 275 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getNumber().toCharArray(), 0, " + beanClassField + ".getNumber().length());\n"); 276 buffer.append(" this.fieldContentHandler.endElement(\"\", \"number\", \"number\");\n"); 277 buffer.append(" this.fieldContentHandler.startElement(\"\", \"firstName\", \"firstName\", attributes);\n"); 278 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getFirstName().toCharArray(), 0, " + beanClassField + ".getFirstName().length());\n"); 279 buffer.append(" this.fieldContentHandler.endElement(\"\", \"firstName\", \"firstName\");\n"); 280 buffer.append(" this.fieldContentHandler.startElement(\"\", \"lastName\", \"lastName\", attributes);\n"); 281 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getLastName().toCharArray(), 0, " + beanClassField + ".getLastName().length());\n"); 282 buffer.append(" this.fieldContentHandler.endElement(\"\", \"lastName\", \"lastName\");\n"); 283 } else if (beanClassField.equals("fieldPurchaseOrder")) { 284 buffer.append(" this.fieldContentHandler.startElement(\"\", \"orderNumber\", \"orderNumber\", attributes);\n"); 285 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getOrderNumber().toCharArray(), 0, " + beanClassField + ".getOrderNumber().length());\n"); 286 buffer.append(" this.fieldContentHandler.endElement(\"\", \"orderNumber\", \"orderNumber\");\n"); 287 288 buffer.append(" this.fieldContentHandler.startElement(\"\", \"description\", \"description\", attributes);\n"); 289 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getDescription().toCharArray(), 0, " + beanClassField + ".getDescription().length());\n"); 290 buffer.append(" this.fieldContentHandler.endElement(\"\", \"description\", \"description\");\n"); 291 292 buffer.append(" this.fieldContentHandler.startElement(\"\", \"partNumber\", \"partNumber\", attributes);\n"); 293 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getPartNumber().toCharArray(), 0, " + beanClassField + ".getPartNumber().length());\n"); 294 buffer.append(" this.fieldContentHandler.endElement(\"\", \"partNumber\", \"partNumber\");\n"); 295 296 } else if (beanClassField.equals("fieldAddress")) { 297 buffer.append(" this.fieldContentHandler.startElement(\"\", \"customerNumber\", \"customerNumber\", attributes);\n"); 298 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getCustomerNumber().toCharArray(), 0, " + beanClassField + ".getCustomerNumber().length());\n"); 299 buffer.append(" this.fieldContentHandler.endElement(\"\", \"customerNumber\", \"customerNumber\");\n"); 300 301 buffer.append(" this.fieldContentHandler.startElement(\"\", \"city\", \"city\", attributes);\n"); 302 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getCity().toCharArray(), 0, " + beanClassField + ".getCity().length());\n"); 303 buffer.append(" this.fieldContentHandler.endElement(\"\", \"city\", \"city\");\n"); 304 305 buffer.append(" this.fieldContentHandler.startElement(\"\", \"state\", \"state\", attributes);\n"); 306 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getState().toCharArray(), 0, " + beanClassField + ".getState().length());\n"); 307 buffer.append(" this.fieldContentHandler.endElement(\"\", \"state\", \"state\");\n"); 308 309 buffer.append(" this.fieldContentHandler.startElement(\"\", \"street\", \"street\", attributes);\n"); 310 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getStreet().toCharArray(), 0, " + beanClassField + ".getStreet().length());\n"); 311 buffer.append(" this.fieldContentHandler.endElement(\"\", \"street\", \"street\");\n"); 312 313 buffer.append(" this.fieldContentHandler.startElement(\"\", \"zipCode\", \"zipCode\", attributes);\n"); 314 buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getZipCode().toCharArray(), 0, " + beanClassField + ".getZipCode().length());\n"); 315 buffer.append(" this.fieldContentHandler.endElement(\"\", \"zipCode\", \"zipCode\");\n"); 316 317 } 318 319 buffer.append(" this.fieldContentHandler.endDocument();\n"); 320 buffer.append(" } catch (Exception e) {\n"); 321 buffer.append(" e.printStackTrace();\n"); 322 buffer.append(" } \n"); 323 buffer.append(" }\n"); 324 buffer.append(" public void setContentHandler(org.xml.sax.ContentHandler handler) {\n"); 325 buffer.append(" this.fieldContentHandler = handler;\n"); 326 buffer.append(" }\n"); 327 buffer.append("} \n"); 328 329 } 330 331 private static final String importsGen = "import java.io.*;\n" + "import com.myeis.services.*;\n" + "import org.apache.wsif.providers.jca.WSIFFormatHandler_JCA;\n" + 332 "import javax.xml.transform.Transformer;\n" + "import javax.xml.transform.TransformerFactory;\n" + "import javax.xml.transform.sax.SAXSource;\n" + 333 "import org.xml.sax.XMLReader;\n" + 334 "import org.xml.sax.Attributes;\n" + "import org.xml.sax.ContentHandler;\n" + "import org.xml.sax.InputSource;\n" + "import org.xml.sax.helpers.AttributesImpl;\n\n\n\n"; 335 336 private static final String defaultMethodsGen = 337 "public Object getElement(String elementName) {return null;}\n" 338 + "public void setElement(String elementName, Object element) {}\n" 339 + "public Object getElement(String name, int index) {return null;}\n" 340 + "public void setElement(String name, int index, Object value) {}\n" 341 + "public javax.xml.namespace.QName getPartQName(){\n" 342 + " return null;\n" 343 + " }\n" 344 + " \n" 345 + " public void setPartQName(javax.xml.namespace.QName qname){\n" 346 + " }\n" 347 + "public void setInteractionSpec(javax.resource.cci.InteractionSpec arg0) {\n" 348 + " return;\n" 349 + " }\n"; 350 353 public Object createGenerationContext() { 354 return null; 355 } 356 } | Popular Tags |