4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
7 * Kangho Kim <kh5325.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
25 package org.tizen.common.util;
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.FileOutputStream;
33 import java.io.FileReader;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.InputStreamReader;
37 import java.net.InetAddress;
38 import java.nio.channels.FileChannel;
40 import org.eclipse.core.runtime.IPath;
41 import org.eclipse.core.runtime.Path;
42 import org.eclipse.core.runtime.Platform;
43 import org.tizen.common.console.ConsoleManager;
45 public abstract class HostUtil{
47 // public static final String USER = System.getenv("USER");
48 // public static final String USER_HOME = System.getenv("HOME");
49 public static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
50 public static final String FILE_SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$
52 private static final String PROC_PATH = "/proc"; //$NON-NLS-1$
53 private static final String SHELL_COMMAND_LINUX= "/bin/sh"; //$NON-NLS-1$
54 private static final String SHELL_COMMAND_WINDOW= "cmd"; //$NON-NLS-1$
56 public static boolean hasProcFile(String pid) {
57 if(pid==null||pid.equals("")) //$NON-NLS-1$
60 String pidFile = PROC_PATH+Path.SEPARATOR+pid;
61 return exists(pidFile);
63 public static boolean isMenuReady(String pid) {
64 if(pid==null||pid.equals("")) //$NON-NLS-1$
67 String pidFile = PROC_PATH+Path.SEPARATOR+pid;
68 return exists(pidFile);
72 public static String getContents(String filePath) {
73 BufferedReader input = null;
74 StringBuilder contents = new StringBuilder();
78 input = new BufferedReader(new FileReader(filePath));
79 while((line=input.readLine())!=null){
80 contents.append(line);
81 contents.append(LINE_SEPARATOR);
84 // e.printStackTrace();
90 } catch (IOException e) {
94 return contents.toString().trim();
97 public static boolean isReachable(String ip) {
99 return InetAddress.getByName(ip).isReachable(100);
100 } catch (Exception e) {
106 public static boolean exists(String path) {
107 if(path==null||path.equals("")) //$NON-NLS-1$
110 IPath ipath = new Path(path);
111 File file = ipath.toFile();
113 if(file!=null&&file.exists())
119 public static boolean execute(String command){
121 Runtime runtime = Runtime.getRuntime();
123 String[] fullCommand = getCommand(command);
127 proc= runtime.exec(fullCommand);
129 } catch (Exception e) {
133 System.out.println("HostUtil execute - exit value : " + i);
141 public static boolean batchExecute(String command)
143 String[] fullCommand = getCommand(command);
145 Runtime run = Runtime.getRuntime();
149 p = run.exec(fullCommand);
150 StreamGobbler gb1 = new StreamGobbler(p.getInputStream());
151 StreamGobbler gb2 = new StreamGobbler(p.getErrorStream());
163 * Don't need for emultor install registration.
164 * using java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)
165 * @param command - array containing the command to call and its arguments.
166 * @param envp - array of strings, each element of which has environment variable settings in format name=value.
167 * @param dir - Emultor path.
168 * @return success true, failed false.
170 public static boolean batchExecute(String command, String[] envp, File dir)
172 String[] fullCommand = getCommand(command);
174 Runtime run = Runtime.getRuntime();
177 StreamGobbler gb1 = null;
178 StreamGobbler gb2 = null;
180 p = run.exec(fullCommand, envp, dir);
181 gb1 = new StreamGobbler(p.getInputStream());
182 gb2 = new StreamGobbler(p.getErrorStream());
189 System.out.println(gb1.getResult());
190 System.out.println(gb2.getResult());
195 public static String returnExecute(String command, String workingDir) {
196 BufferedReader input = null;
197 StringBuilder contents = new StringBuilder();
202 String[] fullCommand = getCommand(command);
205 ProcessBuilder pb = new ProcessBuilder();
206 pb.command(fullCommand);
207 if(workingDir != null) {
208 pb.directory(new File(workingDir));
211 input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
212 while((line=input.readLine())!=null){
213 contents.append(line);
214 contents.append(LINE_SEPARATOR);
216 } catch (Exception e) {
225 } catch (IOException e) {
229 return contents.toString().trim();
234 * Show output messages while executes the command on console view
235 * @param command - Command to execute.
236 * @param viewName - View name showing messages while executes the command.
238 public static void executeWithConsole(String command, String viewName) throws Exception {
239 BufferedReader input = null;
245 String[] fullCommand = getCommand(command);
247 ConsoleManager cm = new ConsoleManager( viewName, true );
251 ProcessBuilder pb = new ProcessBuilder();
252 pb.redirectErrorStream(true);
253 pb.command(fullCommand);
255 input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
256 while((line=input.readLine())!=null){
260 // abnormal termination
261 if (proc.exitValue() != 0)
262 throw new Exception("Failed to execute command: " + command);
269 } catch (IOException e) {
274 public static String returnExecute(String command){
276 return returnExecute(command, null);
279 public static String[] getCommand(String command){
281 if(OSChecker.isWindows()){
282 return new String[]{SHELL_COMMAND_WINDOW,"/c",command}; //$NON-NLS-1$
285 return new String[]{SHELL_COMMAND_LINUX,"-c",command}; //$NON-NLS-1$
290 public static boolean createFile(String path, String contents) {
291 BufferedWriter output = null;
293 File file = new File(path);
295 File folder = file.getParentFile();
299 file.createNewFile();
301 output = new BufferedWriter(new FileWriter(file));
302 output.write(contents);
304 } catch (Exception e) {
311 } catch (IOException e) {
316 public static boolean removeFile(String path) {
317 File file = new File(path);
320 return file.delete();
324 * @deprecated Use {@link FileUtil#copyTo(String, String)} instead.
327 public static boolean copyFile(String src, String des) throws Exception {
328 boolean canExecute = false;
329 File srcFile=new File(src);
331 FileInputStream fis=null;
332 FileOutputStream fos=null;
333 FileChannel fcin=null;
334 FileChannel fcout=null;
336 canExecute = srcFile.canExecute();
338 desFile= new File(des);
339 File paths = new File(desFile.getAbsolutePath().substring(0, desFile.getAbsolutePath().lastIndexOf("/"))); //$NON-NLS-1$
340 if (!paths.exists()) {
345 fis= new FileInputStream(srcFile);
346 fos= new FileOutputStream(desFile);
347 fcin= fis.getChannel();
348 fcout= fos.getChannel();
350 long size= fcin.size();
351 fcin.transferTo(0, size, fcout);
352 desFile.setExecutable(canExecute);
353 } catch (FileNotFoundException e) {
356 } catch (IOException e) {
365 } catch (IOException e) {
374 public static boolean isLinux(){
375 return Platform.getOS().equals(Platform.OS_LINUX);