a2e1b687154de48acc0fed34f4f135a47d47bb6f
[sdk/ide/common-eplugin.git] / org.tizen.common / src / org / tizen / common / util / HostUtil.java
1 /*
2 *  Common
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: 
7 * Kangho Kim <kh5325.kim@samsung.com>
8
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
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
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.
20 *
21 * Contributors:
22 * - S-Core Co., Ltd
23 *
24 */
25 package org.tizen.common.util;
26
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
29 import java.io.File;
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;
39
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;
44
45 public abstract class HostUtil{
46         
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$
51         
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$
55         
56         public static boolean hasProcFile(String pid) {
57                 if(pid==null||pid.equals("")) //$NON-NLS-1$
58                         return false;
59
60                 String pidFile = PROC_PATH+Path.SEPARATOR+pid;
61                 return exists(pidFile);
62         }
63         public static boolean isMenuReady(String pid) {
64                 if(pid==null||pid.equals("")) //$NON-NLS-1$
65                         return false;
66
67                 String pidFile = PROC_PATH+Path.SEPARATOR+pid;
68                 return exists(pidFile);
69         }
70
71         
72         public static String getContents(String filePath) {
73                 BufferedReader input = null;
74                 StringBuilder contents = new StringBuilder();
75                 String line = null;
76                 
77                 try {
78                         input = new BufferedReader(new FileReader(filePath));
79                         while((line=input.readLine())!=null){
80                                 contents.append(line);
81                                 contents.append(LINE_SEPARATOR);
82                         }
83                 }catch(Exception e) {
84 //                      e.printStackTrace();
85                         return null;
86                 }finally{
87                         if(input!=null)
88                                 try {
89                                         input.close();
90                                 } catch (IOException e) {
91                                 }
92                 }
93                 
94                 return contents.toString().trim();
95         }
96         
97         public static boolean isReachable(String ip) {
98                 try {
99                         return InetAddress.getByName(ip).isReachable(100);
100                 } catch (Exception e) {
101                         e.printStackTrace();
102                 }
103                 return false;
104         }
105
106         public static boolean exists(String path) {
107                 if(path==null||path.equals("")) //$NON-NLS-1$
108                         return false;
109                 
110                 IPath ipath = new Path(path);
111                 File file = ipath.toFile();
112                         
113                 if(file!=null&&file.exists())
114                         return true;
115                 else
116                         return false;
117         }
118         
119         public static boolean execute(String command){
120                 Process proc = null;
121                 Runtime runtime = Runtime.getRuntime();
122                 
123                 String[] fullCommand = getCommand(command);
124                 
125                 int i = 0;
126                 try {
127                         proc= runtime.exec(fullCommand);
128                         i = proc.waitFor();
129                 } catch (Exception e) {
130                         e.printStackTrace();
131                         return false;
132                 } finally{
133                         System.out.println("HostUtil execute - exit value : " + i);
134                         if(proc!=null)  
135                                 proc.destroy();
136                 }
137                 
138                 return i==0;
139         }
140         
141         public static boolean batchExecute(String command)      
142         {
143                 String[] fullCommand = getCommand(command);
144
145                 Runtime run = Runtime.getRuntime();
146                   Process p = null;
147                   
148                   try{
149                    p = run.exec(fullCommand);
150                    StreamGobbler gb1 = new StreamGobbler(p.getInputStream());
151                    StreamGobbler gb2 = new StreamGobbler(p.getErrorStream());
152                    gb1.start();
153                    gb2.start();
154                   }catch(Exception e){
155                           e.printStackTrace();
156                           return false;
157                   }finally{
158                   } 
159                 return true;
160         }
161
162         /**
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.
169          */
170         public static boolean batchExecute(String command, String[] envp, File dir)     
171         {
172                 String[] fullCommand = getCommand(command);
173
174                 Runtime run = Runtime.getRuntime();
175                   Process p = null;
176
177                   StreamGobbler gb1 = null;
178                   StreamGobbler gb2 = null;
179                   try{
180                    p = run.exec(fullCommand, envp, dir);
181                    gb1 = new StreamGobbler(p.getInputStream());
182                    gb2 = new StreamGobbler(p.getErrorStream());
183                    gb1.start();
184                    gb2.start();
185                   }catch(Exception e){
186                           e.printStackTrace();
187                           return false;
188                   }finally{
189                           System.out.println(gb1.getResult());
190                           System.out.println(gb2.getResult());
191                   } 
192                 return true;
193         }
194
195         public static String returnExecute(String command, String workingDir) {
196                 BufferedReader input = null;
197                 StringBuilder contents = new StringBuilder();
198                 String line = null;
199                 
200                 Process proc = null;
201                 
202                 String[] fullCommand = getCommand(command);
203                 
204                 try {
205                                 ProcessBuilder pb = new ProcessBuilder();
206                                 pb.command(fullCommand);
207                                 if(workingDir != null) {
208                                         pb.directory(new File(workingDir));
209                                 }
210                                 proc = pb.start();
211                                 input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
212                                 while((line=input.readLine())!=null){
213                                         contents.append(line);
214                                         contents.append(LINE_SEPARATOR);
215                                 }
216                 } catch (Exception e) {
217                         e.printStackTrace();
218                         return null;
219                 } finally{
220                         if(proc!=null)  
221                                 proc.destroy();
222                         if(input!=null)
223                                 try {
224                                         input.close();
225                                 } catch (IOException e) {
226                                 }
227                 }
228                 
229                 return contents.toString().trim();
230                 
231         }
232         
233         /**
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.
237          */
238         public static void executeWithConsole(String command, String viewName) throws Exception {
239                 BufferedReader input = null;
240
241                 String line = null;
242                 
243                 Process proc = null;
244                 
245                 String[] fullCommand = getCommand(command);
246                 
247                 ConsoleManager cm = new ConsoleManager( viewName, true );
248                 cm.clear();
249                 
250                 try {
251                         ProcessBuilder pb = new ProcessBuilder();
252                         pb.redirectErrorStream(true);
253                         pb.command(fullCommand);
254                         proc = pb.start();
255                         input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
256                         while((line=input.readLine())!=null){
257                                 cm.println(line);
258                         }
259                         proc.waitFor();
260                         // abnormal termination
261                         if (proc.exitValue() != 0)
262                                 throw new Exception("Failed to execute command: " + command);
263                 } finally {
264                         if(proc!=null)
265                                 proc.destroy();
266                         if(input!=null)
267                                 try {
268                                         input.close();
269                                 } catch (IOException e) {
270                                 }
271                 }
272         }
273         
274         public static String returnExecute(String command){
275                 
276                 return returnExecute(command, null);
277         }
278         
279         public static String[] getCommand(String command){
280                 
281                 if(OSChecker.isWindows()){
282                         return new String[]{SHELL_COMMAND_WINDOW,"/c",command}; //$NON-NLS-1$
283                 }else
284                 {
285                         return new String[]{SHELL_COMMAND_LINUX,"-c",command}; //$NON-NLS-1$
286                 }
287         }
288         
289         
290         public static boolean createFile(String path, String contents) {
291                 BufferedWriter output = null;
292                 try {
293                         File file = new File(path);
294                         
295                         File folder = file.getParentFile();
296                         if(folder!=null)
297                                 folder.mkdirs();
298                         
299                         file.createNewFile();
300                         
301                         output = new BufferedWriter(new FileWriter(file));
302                         output.write(contents);
303                         return true;
304                 } catch (Exception e) {
305                         e.printStackTrace();
306                         return false;
307                 }finally{
308                         if(output!=null)
309                                 try {
310                                         output.close();
311                                 } catch (IOException e) {
312                                 }
313                 }
314         }
315         
316         public static boolean removeFile(String path) {
317                 File file = new File(path);
318                 if (!file.exists())
319                         return false;
320                 return file.delete();
321         }
322         
323         /**
324         * @deprecated Use {@link FileUtil#copyTo(String, String)} instead.
325         */
326         @Deprecated
327         public static boolean copyFile(String src, String des) throws Exception  {
328                 boolean canExecute = false;
329                 File srcFile=new File(src);
330                 File desFile=null;
331                 FileInputStream fis=null;
332                 FileOutputStream fos=null;
333                 FileChannel fcin=null;
334                 FileChannel fcout=null;
335                 
336                 canExecute = srcFile.canExecute();
337                 
338                 desFile= new File(des);
339                 File paths = new File(desFile.getAbsolutePath().substring(0, desFile.getAbsolutePath().lastIndexOf("/"))); //$NON-NLS-1$
340                 if (!paths.exists()) {
341                         paths.mkdirs();
342                 }
343                 
344                 try {
345                         fis= new FileInputStream(srcFile);
346                         fos= new FileOutputStream(desFile);
347                         fcin= fis.getChannel();
348                         fcout= fos.getChannel();
349                 
350                         long size= fcin.size();
351                         fcin.transferTo(0, size, fcout);
352                         desFile.setExecutable(canExecute);
353                 } catch (FileNotFoundException e) {
354                         e.printStackTrace();
355                         throw e;
356                 } catch (IOException e) {
357                         e.printStackTrace();
358                         throw e;
359                 }finally{
360                         try {
361                                 fcout.close();
362                                 fcin.close();
363                                 fos.close();
364                                 fis.close();
365                         } catch (IOException e) {
366                                 e.printStackTrace();
367                                 throw e;
368                         }
369                 }
370
371                 return true;
372         }
373         
374         public static boolean isLinux(){
375                 return Platform.getOS().equals(Platform.OS_LINUX);
376         }
377 }
378
379