KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > os > unix > UnixHelper


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/ http://developer.berlios.de/projects/izpack/
5  *
6  * Copyright 2006 Marc Eppelmann
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the License
14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15  * or implied. See the License for the specific language governing permissions and limitations under
16  * the License.
17  */

18 package com.izforge.izpack.util.os.unix;
19
20 import com.izforge.izpack.util.FileExecutor;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.StringReader JavaDoc;
28
29 import java.util.ArrayList JavaDoc;
30
31 /**
32  * Helper Methods for unix-systems and derived.
33  *
34  * @author marc.eppelmann@reddot.de
35  * @version $Revision: 1708 $
36  */

37 public class UnixHelper
38 {
39
40     // ~ Static fields/initializers *********************************************************
41

42     /** whichCommand = "/usr/bin/which" or so */
43     public static String JavaDoc whichCommand = FileExecutor.getExecOutput(
44             new String JavaDoc[] { "/usr/bin/env", "which", "which"}, false).trim();
45
46     public final static String JavaDoc VERSION = "$Revision: 1708 $";
47
48     // ~ Methods ****************************************************************************
49

50     /**
51      * get the lines from /etc/passwd as Array
52      *
53      * @return the /etc/passwd as String ArrayList
54      */

55     public static ArrayList JavaDoc getEtcPasswdArray()
56     {
57         ArrayList JavaDoc result = new ArrayList JavaDoc();
58
59         String JavaDoc line = "";
60         BufferedReader JavaDoc reader = null;
61
62         try
63         {
64             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(UnixConstants.etcPasswd));
65
66             while ((line = reader.readLine()) != null)
67             {
68                 result.add(line);
69             }
70         }
71         catch (Exception JavaDoc e)
72         {
73             // ignore - there are maybe no users
74
}
75
76         return result;
77     }
78
79     /**
80      * get the lines from /etc/passwd as Array
81      *
82      * @return the /etc/passwd as String ArrayList
83      */

84     public static ArrayList JavaDoc getYpPasswdArray()
85     {
86         ArrayList JavaDoc result = new ArrayList JavaDoc();
87
88         String JavaDoc line = "";
89         BufferedReader JavaDoc reader = null;
90
91         try
92         {
93             reader = new BufferedReader JavaDoc(new StringReader JavaDoc(FileExecutor.getExecOutput(new String JavaDoc[] {
94                     getYpCatCommand(), "passwd"})));
95
96             while ((line = reader.readLine()) != null)
97             {
98                 result.add(line);
99             }
100         }
101         catch (Exception JavaDoc e)
102         {
103             // ignore - there are maybe no users
104
}
105
106         return result;
107     }
108
109     /**
110      * Test if KDE is installed. This is done by $>/usr/bin/env konqueror --version This assumes
111      * that the konqueror as a main-app of kde is already installed. If this returns with 0 konqeror
112      * and resp. kde means to be installed,
113      *
114      * @return true if kde is installed otherwise false.
115      */

116     public static boolean kdeIsInstalled()
117     {
118         FileExecutor fe = new FileExecutor();
119
120         String JavaDoc[] execOut = new String JavaDoc[2];
121
122         int execResult = fe.executeCommand(
123                 new String JavaDoc[] { "/usr/bin/env", "konqueror", "--version"}, execOut);
124
125         return execResult == 0;
126     }
127
128     /**
129      * Gets the absolute Pathe to the cp (Copy) Command. This is necessary, because the command is
130      * located at /bin on linux but in /usr/bin on Sun Solaris.
131      *
132      * @return /bin/cp on linux /usr/bin/cp on solaris
133      */

134     public static String JavaDoc getWhichCommand()
135     {
136         return whichCommand;
137     }
138
139     /**
140      * Gets the absolute Pathe to the cp (Copy) Command. This is necessary, because the command is
141      * located at /bin on linux but in /usr/bin on Sun Solaris.
142      *
143      * @return /bin/cp on linux /usr/bin/cp on solaris
144      */

145     public static String JavaDoc getCpCommand()
146     {
147         return FileExecutor.getExecOutput(new String JavaDoc[] { getWhichCommand(), "cp"}).trim();
148     }
149
150     /**
151      * Gets the absolute Pathe to the su (SuperUser) Command. This is necessary, because the command
152      * is located at /bin on linux but in /usr/bin on Sun Solaris.
153      *
154      * @return /bin/su on linux /usr/bin/su on solaris
155      */

156     public static String JavaDoc getSuCommand()
157     {
158         return FileExecutor.getExecOutput(new String JavaDoc[] { getWhichCommand(), "su"}).trim();
159     }
160
161     /**
162      * Gets the absolute Pathe to the rm (Remove) Command. This is necessary, because the command is
163      * located at /bin on linux but in /usr/bin on Sun Solaris.
164      *
165      * @return /bin/rm on linux /usr/bin/rm on solaris
166      */

167     public static String JavaDoc getRmCommand()
168     {
169         return FileExecutor.getExecOutput(new String JavaDoc[] { whichCommand, "rm"}).trim();
170     }
171
172     /**
173      * Gets the absolute Pathe to the ypcat (YellowPage/NIS Cat) Command. This is necessary, because
174      * the command is located at /bin on linux but in /usr/bin on Sun Solaris.
175      *
176      * @return /bin/ypcat on linux /usr/bin/ypcat on solaris
177      */

178     public static String JavaDoc getYpCatCommand()
179     {
180         return FileExecutor.getExecOutput(new String JavaDoc[] { whichCommand, "ypcat"}).trim();
181     }
182
183     /**
184      * Gets the absolute Pathe to the ypcat (YellowPage/NIS Cat) Command. This is necessary, because
185      * the command is located at /bin on linux but in /usr/bin on Sun Solaris.
186      *
187      * @param aCommand a Custom Command
188      *
189      * @return /bin/ypcat on linux /usr/bin/ypcat on solaris
190      */

191     public static String JavaDoc getCustomCommand(String JavaDoc aCommand)
192     {
193         return FileExecutor.getExecOutput(new String JavaDoc[] { whichCommand, aCommand}).trim();
194     }
195
196     /**
197      * Standalone Test Main Method call with : > java -cp
198      * ../bin/panels/UserInputPanel.jar:../_dist/IzPack-install-3.9.0-preview1.jar
199      * com.izforge.izpack.util.os.unix.UnixHelper
200      *
201      * @param args commandline args
202      */

203     public static void main(String JavaDoc[] args)
204     {
205         System.out.println("Hallo from " + UnixHelper.class.getName() + VERSION);
206
207         // System.out.println( StringTool.stringArrayListToString(UnixUsers.getUsersAsArrayList())
208
// );
209

210         //System.out.println("Kde is" + (kdeIsInstalled() ? " " : " not ") + "installed");
211

212         System.out.println("WhichCommand: '" + getWhichCommand() + "'");
213         System.out.println("SuCommand: " + getSuCommand());
214         System.out.println("RmCommand: " + getRmCommand());
215         System.out.println("CopyCommand: " + getCpCommand());
216         System.out.println("YpCommand: " + getYpCatCommand());
217
218         System.out.println("CustomCommand: " + getCustomCommand("cat"));
219
220         File JavaDoc tempFile = null;
221
222         try
223         {
224             tempFile = File.createTempFile(UnixHelper.class.getName(), Long.toString(System
225                     .currentTimeMillis())
226                     + ".tmp");
227         }
228         catch (Exception JavaDoc e)
229         {
230             e.printStackTrace();
231         }
232
233         System.out.println("Tempfile: " + tempFile.toString());
234
235         // This does not work :-(
236
/*
237          * FileExecutor.getExecOutput(new String[] { getCustomCommand("echo"), "Hallo", ">",
238          * tempFile.toString()});
239          */

240
241         // so try:
242
try
243         {
244             BufferedWriter JavaDoc w = new BufferedWriter JavaDoc( new FileWriter JavaDoc(tempFile) );
245             w.write("Hallo");
246             w.flush();
247             w.close();
248             if( tempFile.exists() )
249               System.out.println("Wrote: " + tempFile + ">>Hallo");
250             else
251                 System.out.println("Could not Wrote: " + tempFile + "Hallo");
252         }
253         catch (Exception JavaDoc e)
254         {
255             e.printStackTrace();
256         }
257         // tempFile
258

259         String JavaDoc destfilename = "/home/marc.eppelmann" + File.separator + "Desktop" ;
260
261         System.out.println("Copy: " + tempFile.toString() + " to " + destfilename);
262
263         String JavaDoc result = FileExecutor.getExecOutput(new String JavaDoc[] { getSuCommand(), "marc.eppelmann", "-c",
264                 "\"" + getCpCommand() + " " + tempFile.toString() + " " + destfilename + "\""});
265         
266         System.out.println("Wrote: " + tempFile.toString() + " to " + destfilename + " > " + result);
267
268         // getYpPasswdArray();
269
// System.out.println("/bin/bash".endsWith("sh"));
270
}
271 }
272
Popular Tags