1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package org.apache.synapse.mediators.db; 21 22 import org.apache.synapse.MessageContext; 23 24 import java.sql.PreparedStatement; 25 import java.sql.ResultSet; 26 import java.sql.SQLException; 27 import java.sql.Connection; 28 import java.util.Iterator; 29 30 /** 31 * Simple database table lookup mediator. Designed only for read/lookup 32 */ 33 public class DBLookupMediator extends AbstractDBMediator { 34 35 protected void processStatement(Statement stmnt, MessageContext msgCtx) { 36 37 boolean traceOn = isTraceOn(msgCtx); 38 boolean traceOrDebugOn = isTraceOrDebugOn(traceOn); 39 40 // execute the prepared statement, and extract the first result row and 41 // set as message context properties, any results that have been specified 42 Connection con = null; 43 try { 44 PreparedStatement ps = getPreparedStatement(stmnt, msgCtx); 45 con = ps.getConnection(); 46 ResultSet rs = ps.executeQuery(); 47 48 if (rs.next()) { 49 if (traceOrDebugOn) { 50 traceOrDebug(traceOn, 51 "Processing the first row returned : " + stmnt.getRawStatement()); 52 } 53 54 Iterator propNameIter = stmnt.getResultsMap().keySet().iterator(); 55 while (propNameIter.hasNext()) { 56 57 String propName = (String) propNameIter.next(); 58 String columnStr = (String) stmnt.getResultsMap().get(propName); 59 60 Object obj = null; 61 try { 62 int colNum = Integer.parseInt(columnStr); 63 obj = rs.getObject(colNum); 64 } catch (NumberFormatException ignore) { 65 obj = rs.getObject(columnStr); 66 } 67 68 if (obj != null) { 69 if (traceOrDebugOn) { 70 traceOrDebug(traceOn, "Column : " + columnStr + 71 " returned value : " + obj + 72 " Setting this as the message property : " + propName); 73 } 74 msgCtx.setProperty(propName, obj.toString()); 75 } else { 76 if (traceOrDebugOn) { 77 traceOrDebugWarn(traceOn, "Column : " + columnStr + 78 " returned null Skip setting message property : " + propName); 79 } 80 } 81 } 82 } else { 83 if (traceOrDebugOn) { 84 traceOrDebug(traceOn, "Statement : " 85 + stmnt.getRawStatement() + " returned 0 rows"); 86 } 87 } 88 89 } catch (SQLException e) { 90 handleException("Error executing statement : " + stmnt.getRawStatement() + 91 " against DataSource : " + getDSName(), e, msgCtx); 92 } finally { 93 if (con != null) { 94 try { 95 con.close(); 96 } catch (SQLException ignore) {} 97 } 98 } 99 } 100 101 }