import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.net.InetAddress; import java.net.UnknownHostException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; public class Util{ private static Map debugTimers = new HashMap(); private static final String QUOTE = "'"; public static void startTimer(String timerId){ debugTimers.put(timerId, (new Date())); } public static String ReplaceText(String sInput, String sTarget, String sReplace) { int iLen = 0; if(sInput==null){ return ""; } String sRtnStr = sInput; do { iLen = sInput.indexOf(sTarget, iLen); if (iLen == -1) break; sRtnStr = sInput.substring(0, iLen) + sReplace + sInput.substring(iLen + sTarget.length()); iLen += sReplace.length(); sInput = sRtnStr; } while (true); return sRtnStr.substring(0, sRtnStr.length()); } public static void stopTimer(String timerId){ debug("\"" + timerId + "\" in " + (new Date().getTime() - ((Date)debugTimers.get(timerId)).getTime()) + " ms."); debugTimers.put(timerId, null); } public static void clickTimer(String timerId){ if(debugTimers.get(timerId) == null) startTimer(timerId); else stopTimer(timerId); } /** * get "a,b,c" from String[] of "a","b","c" */ public static String join(String[] strings){ if(strings == null) return ""; StringBuffer buffer = new StringBuffer(); for(int i = 0; i < strings.length; i ++){ buffer.append(strings[i]); if(i+1 != strings.length) buffer.append(","); } return buffer.toString(); } public static String join(String[] strs, String delim){ return join(strs, delim); } /** * join a collection of objects using each object's toString() method */ public static String join(Collection s, String delimiter) { StringBuffer buffer = new StringBuffer(); Iterator iter = s.iterator(); while (iter.hasNext()) { buffer.append(iter.next()); if (iter.hasNext()) { buffer.append(delimiter); } } return buffer.toString(); } public static String[] arrayList_2_StringArray(ArrayList al){ if (isEmpty(al)) return null; String[] out = new String[al.size()]; for(int i=0; i<al.size(); i++) out[i] = al.get(i).toString(); return out; } public static ArrayList stringArray_2_ArrayList(String[] strs){ ArrayList list = new ArrayList(); for(int i=0; i<strs.length; i++){ list.add(strs[i]); } return list; } public static boolean inArrayString(String[] a, String b){ for(int i =0; i<a.length; i++) if (a[i].equals(b)) return true; return false; } public static String getUTFString(String sInput) { try { return new String(sInput.getBytes("8859-1"), "UTF-8"); }catch (Exception ex) { System.out.println("TEST error " + ex.getMessage()); return sInput; } } public static HashMap getDaemonInitSetting(Connection conn, String paramKey) throws SQLException{ HashMap hm = new HashMap(); Statement stmt = conn.createStatement(); // get daemon's own setting, can override common ERAS setting PreparedStatement ps = conn.prepareStatement( "SELECT SPP_ID, SPP_VALUE FROM SY_SYSPARMPROFILES WHERE upper(SPP_ID) like upper('%"+paramKey+"%')" ); //ps.setString(1, paramKey); ResultSet rs = ps.executeQuery(); while(rs.next()){ String s = rs.getString(2); if(Util.isEmpty(s)){ System.out.println("WARNING: The config '" + rs.getString(1) + "' is empty."); } hm.put(rs.getString(1), s); } stmt.close(); return hm; } public static boolean isEmpty(Object obj){ if(obj instanceof Object[]){ for(int i=0; i < ((Object[])obj).length; i++) if(!isEmpty(((Object[])obj)[i])) return false; return true; } else return obj == null || obj instanceof String && ((String)obj).equals("") || obj instanceof StringBuffer && isEmpty(obj.toString()) || obj instanceof Collection && ((Collection)obj).size() == 0 ; } public static String getStackTrace(Exception e){ StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); return sw.toString(); } public static ArrayList toArrayList(String[] strs){ ArrayList list = new ArrayList(); for(int i=0; i<strs.length; i++){ list.add(strs[i]); } return list; } public static String NVL(String s){ return (s == null) ? "" : s; } public static String NVLR(String s, String r){ if (s == null) return r; else if (s.equals("")) return r; else return s; } public static String NVL2(String s){ return (s.equals("null")) ? "" : s; } public static void debug(Object obj){ try{ throw new Exception(""); } catch(Exception e){ System.out.println( "[" + e.getStackTrace()[1].getClassName() + "." + e.getStackTrace()[1].getMethodName() + "] " + ((obj== null) ? "null" : obj.toString())); } } public static String getHostName(){ try { InetAddress addr = InetAddress.getLocalHost(); // Get IP Address //byte[] ipAddr = addr.getAddress(); // Get hostname String hostname = addr.getHostName(); return hostname; } catch (UnknownHostException e) { return "UnknownHostException"; } } public static boolean setHeldRequstFail(Connection conn, Hashtable param, String key) throws SQLException{ String name = (String)param.get(key+".tablename"); String prefix = (String)param.get(key+".tableprefix"); String status = (String)param.get(key+".exceptStatus"); if (isEmpty(name) || isEmpty(prefix) || isEmpty(status)){ return false; } else{ String sql = "update "+name+" set "+prefix+"_STATUS = 'F' where "+prefix+"_STATUS not in ("+status+") and "+prefix+"_MACHINE_ID = '"+getHostName()+"'"; Statement stmt = conn.createStatement(); PreparedStatement ps = conn.prepareStatement(sql); ps.execute(); stmt.close(); } return true; } public static String decryptPassword(String sConnStr, String sCipher, String sOutputFileName, String sExeFullPath) throws Exception { File oExeFile = null; File oOutputFile = null; String sExePath = ""; FileReader fr = null; BufferedReader br = null; String sPlainText = ""; try { oExeFile = new File(sExeFullPath); sExePath = oExeFile.getParent(); Runtime rt = Runtime.getRuntime(); Process p = rt.exec(new String[] {sExeFullPath, "\"" + sConnStr + "\"", "\"" + sCipher + "\"", "\"" + sExePath + File.separator + sOutputFileName + "\"" }); p.waitFor(); //p.destroy(); oOutputFile = new File(sExePath + File.separator + sOutputFileName); fr = new FileReader(oOutputFile); br = new BufferedReader(fr); sPlainText = br.readLine(); return sPlainText; } catch(Exception ex){ /*handle exception*/ throw ex; } finally { if (br != null) br.close(); if (fr != null) fr.close(); if (oOutputFile != null) oOutputFile.delete(); } } public static String getStrackTrace(Exception e){ String err =""; StackTraceElement st[] = e.getStackTrace(); for (int i=0; i<st.length; i++) err +=st[i].toString()+"\n"; return err; } public static String decrypt(String sCipher, String sOutputFileName, String sExeFullPath) throws Exception { File oExeFile = null; File oOutputFile = null; String sExePath = ""; FileReader fr = null; BufferedReader br = null; String sPlainText = ""; try { oExeFile = new File(sExeFullPath); sExePath = oExeFile.getParent(); Runtime rt = Runtime.getRuntime(); Process p = rt.exec(new String[] {sExeFullPath, "\"" + sCipher + "\"", "\"" + sExePath + File.separator + sOutputFileName + "\"" }); p.waitFor(); //p.destroy(); oOutputFile = new File(sExePath + File.separator + sOutputFileName); fr = new FileReader(oOutputFile); br = new BufferedReader(fr); sPlainText = br.readLine(); return sPlainText; } catch(Exception ex){ /*handle exception*/ throw ex; } finally { if (br != null) br.close(); if (fr != null) fr.close(); if (oOutputFile != null) oOutputFile.delete(); } } }