1 10 11 package org.mule.providers.dq; 12 13 import java.io.InputStream ; 14 import java.util.Iterator ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.dom4j.Document; 19 import org.dom4j.Element; 20 import org.dom4j.io.SAXReader; 21 import org.mule.util.IOUtils; 22 23 import com.ibm.as400.access.AS400; 24 import com.ibm.as400.access.AS400Text; 25 import com.ibm.as400.access.CharacterFieldDescription; 26 import com.ibm.as400.access.Record; 27 import com.ibm.as400.access.RecordFormat; 28 29 32 public final class DQMessageUtils 33 { 34 37 private static Log log = LogFactory.getLog(DQMessageUtils.class); 38 39 public static final String RECORD_DESCRIPTOR_KEY = "recordDescriptor"; 40 41 44 private DQMessageUtils() 45 { 46 super(); 47 } 48 49 57 public static synchronized DQMessage getDQMessage(final byte[] data, final RecordFormat format) 58 throws Exception 59 { 60 Record record = format.getNewRecord(data); 61 62 DQMessage message = new DQMessage(); 63 64 String [] names = format.getFieldNames(); 65 String name; 66 for (int i = 0; i < names.length; i++) 67 { 68 name = names[i]; 69 message.addEntry(name, record.getField(name)); 70 } 71 72 return message; 73 } 74 75 82 83 public static synchronized Record getRecord(final DQMessage msg, final RecordFormat format) 84 { 85 Record rec = format.getNewRecord(); 86 87 String name; 88 Object field; 89 String [] tab = format.getFieldNames(); 90 91 for (int i = 0; i < tab.length; i++) 92 { 93 name = tab[i]; 94 field = msg.getEntry(name); 95 if (field == null) 96 { 97 field = ""; 98 } 99 rec.setField(name, field); 100 } 101 102 return rec; 103 } 104 105 113 public static synchronized RecordFormat getRecordFormat(final String recordDescriptor, final AS400 as400) 114 throws Exception 115 { 116 log.debug("Record descriptor :" + recordDescriptor); 117 118 if (recordDescriptor == null) 119 { 120 throw new Exception ("Failed to read record descriptor : recordDescriptor property is not set "); 121 } 122 123 InputStream input = null; 124 RecordFormat recordFormat = null; 125 try 126 { 127 input = IOUtils.getResourceAsStream(recordDescriptor, DQMessageUtils.class); 128 if (input == null) 129 { 130 throw new Exception ("Failed to read record descriptor (" + recordDescriptor 131 + ") cannot be found "); 132 } 133 recordFormat = parse(input, as400); 134 } 135 finally 136 { 137 if (input != null) 138 { 139 input.close(); 140 } 141 } 142 143 return recordFormat; 144 } 145 146 154 private static RecordFormat parse(final InputStream stream, final AS400 as400) throws Exception 155 { 156 SAXReader reader = new SAXReader(); 157 Document document = reader.read(stream); 158 RecordFormat rec = new RecordFormat(); 159 160 for (Iterator i = document.getRootElement().elementIterator(); i.hasNext();) 161 { 162 Element element = (Element)i.next(); 163 String name = element.attributeValue("name"); 164 int length = Integer.decode(element.attributeValue("length")).intValue(); 165 addCharacterField(rec, length, name, as400); 166 } 167 168 return rec; 169 } 170 171 179 private static void addCharacterField(final RecordFormat format, 180 final int length, 181 final String name, 182 final AS400 as400) 183 { 184 format.addFieldDescription(new CharacterFieldDescription(new AS400Text(length, as400), name)); 185 } 186 187 } 188 | Popular Tags |