Source Code Cross Referenced for UnixHelper.java in  » Installer » IzPack » com » izforge » izpack » util » os » unix » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Installer » IzPack » com.izforge.izpack.util.os.unix 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003:         *
004:         * http://izpack.org/ http://izpack.codehaus.org/
005:         *
006:         * Copyright 2006 Marc Eppelmann
007:         *
008:         * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
009:         * in compliance with the License. You may obtain a copy of the License at
010:         *
011:         * http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software distributed under the License
014:         * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
015:         * or implied. See the License for the specific language governing permissions and limitations under
016:         * the License.
017:         */
018:        package com.izforge.izpack.util.os.unix;
019:
020:        import com.izforge.izpack.util.FileExecutor;
021:
022:        import java.io.BufferedReader;
023:        import java.io.BufferedWriter;
024:        import java.io.File;
025:        import java.io.FileReader;
026:        import java.io.FileWriter;
027:        import java.io.StringReader;
028:
029:        import java.util.ArrayList;
030:
031:        /**
032:         * Helper Methods for unix-systems and derived.
033:         * 
034:         * @author marc.eppelmann@reddot.de
035:         * @version $Revision: 2056 $
036:         */
037:        public class UnixHelper {
038:
039:            // ~ Static fields/initializers *********************************************************
040:
041:            /** whichCommand = "/usr/bin/which" or /bin/which */
042:            public static String whichCommand = FileExecutor.getExecOutput(
043:                    new String[] { "/usr/bin/env", "which", "which" }, false)
044:                    .trim();
045:
046:            public final static String VERSION = "$Revision: 2056 $";
047:
048:            // ~ Methods ****************************************************************************
049:
050:            /**
051:             * Get the lines from /etc/passwd as Array
052:             * 
053:             * @return the /etc/passwd as String ArrayList
054:             */
055:            public static ArrayList<String> getEtcPasswdArray() {
056:                ArrayList<String> result = new ArrayList<String>();
057:
058:                String line = "";
059:                BufferedReader reader = null;
060:
061:                try {
062:                    reader = new BufferedReader(new FileReader(
063:                            UnixConstants.etcPasswd));
064:
065:                    while ((line = reader.readLine()) != null) {
066:                        result.add(line);
067:                    }
068:                } catch (Exception e) {
069:                    // ignore - there are maybe no users
070:                } finally {
071:                    try {
072:                        if (reader != null)
073:                            reader.close();
074:                    } catch (Exception ignore) {
075:                        // ignore
076:                    }
077:                }
078:
079:                return result;
080:            }
081:
082:            /**
083:             * Get the YelloyPages (NIS) Users lines from <code>ypcat passwd</code> as Array. Ypcat
084:             * passwd's output has the same format as the the local /etc/passwd. Because there can be
085:             * thousands of yp-users and this query is net-based, this is a candidate for a token-based
086:             * optimization.
087:             * 
088:             * @return the /etc/passwd as String ArrayList
089:             */
090:            public static ArrayList<String> getYpPasswdArray() {
091:                ArrayList<String> result = new ArrayList<String>();
092:
093:                String line = "";
094:                BufferedReader reader = null;
095:                String ypcommand = getYpCatCommand();
096:
097:                if (ypcommand != null && ypcommand.length() > 0) {
098:
099:                    try {
100:                        reader = new BufferedReader(new StringReader(
101:                                FileExecutor.getExecOutput(new String[] {
102:                                        ypcommand, "passwd" })));
103:                        while ((line = reader.readLine()) != null) {
104:                            result.add(line);
105:                        }
106:                    } catch (Exception e) {
107:                        // ignore - there are maybe no users
108:                    } finally {
109:                        try {
110:                            if (reader != null)
111:                                reader.close();
112:
113:                        } catch (Exception i) {
114:                            // ignore
115:                        }
116:                    }
117:                }
118:                return result;
119:            }
120:
121:            /**
122:             * Test if KDE is installed. This is done by <code> $>/usr/bin/env kwin --version</code> This
123:             * assumes that kwin, the window Manager, as part of the kde-base package is already installed.
124:             * If this returns with 0 kwin resp. kde means to be installed,
125:             * 
126:             * @return true if kde is installed otherwise false.
127:             */
128:            public static boolean kdeIsInstalled() {
129:                FileExecutor fe = new FileExecutor();
130:
131:                String[] execOut = new String[2];
132:
133:                int execResult = fe.executeCommand(new String[] {
134:                        "/usr/bin/env", "kwin", "--version" }, execOut);
135:
136:                return execResult == 0;
137:            }
138:
139:            /**
140:             * Gets the absolute path of the which 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/which on linux /usr/bin/which on solaris
144:             */
145:            public static String getWhichCommand() {
146:                return whichCommand;
147:            }
148:
149:            /**
150:             * Gets the absolute path of the cp (Copy) command. This is necessary, because the command is
151:             * located at /bin on linux but in /usr/bin on Sun Solaris.
152:             * 
153:             * @return /bin/cp on linux /usr/bin/cp on solaris
154:             */
155:            public static String getCpCommand() {
156:                return FileExecutor.getExecOutput(
157:                        new String[] { getWhichCommand(), "cp" }).trim();
158:            }
159:
160:            /**
161:             * Gets the absolute path to the su (SuperUser) command. This is necessary, because the command
162:             * is located at /bin on linux but in /usr/bin on Sun Solaris.
163:             * 
164:             * @return /bin/su on linux /usr/bin/su on solaris
165:             */
166:            public static String getSuCommand() {
167:                return FileExecutor.getExecOutput(
168:                        new String[] { getWhichCommand(), "su" }).trim();
169:            }
170:
171:            /**
172:             * Gets the absolute Pathe to the rm (Remove) Command. This is necessary, because the command is
173:             * located at /bin on linux but in /usr/bin on Sun Solaris.
174:             * 
175:             * @return /bin/rm on linux /usr/bin/rm on solaris
176:             */
177:            public static String getRmCommand() {
178:                return FileExecutor.getExecOutput(
179:                        new String[] { whichCommand, "rm" }).trim();
180:            }
181:
182:            /**
183:             * Gets the absolute Pathe to the ypcat (YellowPage/NIS Cat) Command. This is necessary, because
184:             * the command is located at /bin on linux but in /usr/bin on Sun Solaris.
185:             * 
186:             * @return /bin/ypcat on linux /usr/bin/ypcat on solaris
187:             */
188:            public static String getYpCatCommand() {
189:                return FileExecutor.getExecOutput(
190:                        new String[] { whichCommand, "ypcat" }).trim();
191:            }
192:
193:            /**
194:             * Gets the absolute Pathe to the given custom command. This is necessary, because the command
195:             * may be located at /bin on linux but in /usr/bin on Sun Solaris. Which can locate it in your
196:             * $PATH for you.
197:             * 
198:             * @param aCommand a Custom Command
199:             * 
200:             * @return /bin/aCommand on linux /usr/bin/aCommand on solaris
201:             */
202:            public static String getCustomCommand(String aCommand) {
203:                return FileExecutor.getExecOutput(
204:                        new String[] { whichCommand, aCommand }).trim();
205:            }
206:
207:            /**
208:             * Standalone Test Main Method call with : &gt; java -cp ../_build
209:             * com.izforge.izpack.util.os.unix.UnixHelper
210:             * 
211:             * @param args commandline args
212:             */
213:            public static void main(String[] args) {
214:                System.out.println("Hallo from " + UnixHelper.class.getName()
215:                        + VERSION);
216:
217:                // System.out.println( StringTool.stringArrayListToString(UnixUsers.getUsersAsArrayList())
218:                // );
219:
220:                // System.out.println("Kde is" + (kdeIsInstalled() ? " " : " not ") + "installed");
221:
222:                System.out.println("WhichCommand: '" + getWhichCommand() + "'");
223:                System.out.println("SuCommand: " + getSuCommand());
224:                System.out.println("RmCommand: " + getRmCommand());
225:                System.out.println("CopyCommand: " + getCpCommand());
226:                System.out.println("YpCommand: " + getYpCatCommand());
227:
228:                System.out.println("CustomCommand: " + getCustomCommand("cat"));
229:
230:                File tempFile = null;
231:
232:                try {
233:                    tempFile = File.createTempFile(UnixHelper.class.getName(),
234:                            Long.toString(System.currentTimeMillis()) + ".tmp");
235:                } catch (Exception e) {
236:                    e.printStackTrace();
237:                }
238:
239:                System.out.println("Tempfile: " + tempFile.toString());
240:                System.out.println("TempfileName: " + tempFile.getName());
241:
242:                // This does not work :-(
243:                /*
244:                 * FileExecutor.getExecOutput(new String[] { getCustomCommand("echo"), "Hallo", ">",
245:                 * tempFile.toString()});
246:                 */
247:                String username = System.getProperty("user.name");
248:
249:                // System.out.println("Your Name: " + username);
250:                // so try:
251:                if ("root".equals(username)) {
252:                    try {
253:                        BufferedWriter w = new BufferedWriter(new FileWriter(
254:                                tempFile));
255:                        w.write("Hallo");
256:                        w.flush();
257:                        w.close();
258:                        if (tempFile.exists())
259:                            System.out
260:                                    .println("Wrote: " + tempFile + ">>Hallo");
261:                        else
262:                            System.out.println("Could not Wrote: " + tempFile
263:                                    + "Hallo");
264:                    } catch (Exception e) {
265:                        e.printStackTrace();
266:                    }
267:
268:                    String destfilename = "/home/marc.eppelmann"
269:                            + File.separator + "Desktop" + File.separator
270:                            + tempFile.getName();
271:
272:                    System.out.println("Copy: " + tempFile.toString() + " to "
273:                            + destfilename);
274:
275:                    ShellScript s = new ShellScript("bash");
276:
277:                    s.append(getSuCommand() + " " + "marc.eppelmann" + " "
278:                            + "-c" + " " + "\"" + getCpCommand() + " "
279:                            + tempFile.toString() + " " + destfilename + "\"");
280:
281:                    String shLocation = "/tmp/x.21.21";
282:                    try {
283:                        shLocation = File.createTempFile(
284:                                UnixHelper.class.getName(),
285:                                Long.toString(System.currentTimeMillis())
286:                                        + ".sh").toString();
287:                    } catch (Exception x) {
288:                        x.printStackTrace();
289:                    }
290:                    s.write(shLocation);
291:                    s.exec();
292:
293:                    // File deleteMe = new File( shLocation ); deleteMe.delete();
294:
295:                } else {
296:                    System.out
297:                            .println("Your name: '"
298:                                    + username
299:                                    + "' is not 'root'. But this is a test for the user root.");
300:                }
301:            }
302:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.