View Javadoc

1   package org.rpi.utils;
2   
3   import java.io.BufferedReader;
4   import java.io.InputStreamReader;
5   import java.util.ArrayList;
6   
7   import org.apache.log4j.Logger;
8   import org.openhome.net.device.IDvInvocation;
9   
10  public class Utils {
11  
12  	private static Logger log = Logger.getLogger(Utils.class);
13  
14  	public static String getLogText(IDvInvocation paramIDvInvocation) {
15  		if (!log.isDebugEnabled())
16  			return "";
17  		String sp = " ";
18  		StringBuilder sb = new StringBuilder();
19  		try {
20  			sb.append(sp);
21  			sb.append("Adapter: ");
22  			sb.append(getAdapterIP(paramIDvInvocation.getAdapter()));
23  			sb.append(sp);
24  			sb.append("uriPrefix: ");
25  			sb.append(paramIDvInvocation.getResourceUriPrefix());
26  			sb.append(sp);
27  			sb.append("Version:");
28  			sb.append(paramIDvInvocation.getVersion());
29  			sb.append(sp);
30  		} catch (Exception e) {
31  		}
32  		return sb.toString();
33  	}
34  
35  	private static String getAdapterIP(int ip) {
36  		String ipStr = String.format("%d.%d.%d.%d", (ip >>> 24 & 0xff), (ip >>> 16 & 0xff), (ip >>> 8 & 0xff), (ip & 0xff));
37  		return ipStr;
38  	}
39  
40  	/**
41  	 * 
42  	 * @param source
43  	 * @param target
44  	 * @return
45  	 */
46  	public static boolean containsString(String source, ArrayList<String> target) {
47  		if (source == null || target == null)
48  			return false;
49  		for (String s : target) {
50  			if (containsString(source, s)) {
51  				return true;
52  			}
53  		}
54  		return false;
55  	}
56  
57  	/**
58  	 * 
59  	 * @param source
60  	 * @param target
61  	 * @return
62  	 */
63  	public static boolean containsString(String source, String target) {
64  		if (source.toUpperCase().contains(target.toUpperCase())) {
65  			return true;
66  		}
67  		return false;
68  	}
69  
70  	public static String[] execute(String command) throws Exception {
71  		ArrayList<String> list = new ArrayList<String>();
72  		Process pa = Runtime.getRuntime().exec(command);
73  		pa.waitFor();
74  		BufferedReader reader = new BufferedReader(new InputStreamReader(pa.getInputStream()));
75  		String line;
76  		while ((line = reader.readLine()) != null) {
77  			line = line.trim();
78  			log.debug("Result of " + command + " : " + line);
79  			list.add(line);
80  		}
81  		reader.close();
82  		pa.getInputStream().close();
83  		return list.toArray(new String[list.size()]);
84  	}
85  
86      /**
87       * Checks if the given string is empty. Empty means, it is null, has no content or has not length.
88       *
89       * @param string
90       * @return
91       */
92      public static boolean isEmpty(String string) {
93          if (string == null || string.equals("") || string.length() == 0) {
94              return true;
95          }
96  
97          return false;
98      }
99  
100 }