Util

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();
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringUtil是一个工具类,它是Spring框架提供的一个辅助类,主要用于获取Spring容器中的Bean实例。 在Spring框架中,我们将各个组件或者对象称为Bean,这些Bean都是由Spring容器进行管理和创建的。有时候,我们需要在代码中获取Spring容器中的某个Bean实例,这时就可以使用SpringUtil。 SpringUtil提供了一系列静态方法,可以根据Bean的名称或者类型来获取对应的实例。通过SpringUtil,我们可以在任何地方获取到Spring容器中的Bean实例,而不需要手动去管理Bean的创建和依赖注入。 使用SpringUtil的方式很简单。首先,我们需要在Spring配置文件中将该工具类配置为一个Bean,以便让Spring容器进行管理。然后,在代码中直接调用SpringUtil的静态方法就可以获取到某个Bean的实例了。 除了获取Bean实例外,SpringUtil还提供了其他一些实用的方法,比如获取Bean的名称、判断某个Bean是否存在等等。这些方法都能帮助我们更方便地操作和管理Spring容器中的Bean。 需要注意的是,尽管SpringUtil提供了便捷的访问方式,但在实际使用中,我们应该尽量避免过多地使用该工具类。因为过度依赖SpringUtil可能会导致代码的可维护性和可测试性下降,不利于代码的拓展和重构。 总之,SpringUtil是Spring框架的一个辅助工具类,能够帮助我们更方便地获取和操作Spring容器中的Bean实例,但在使用过程中需要注意适度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值