KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > SysUtils


1 package snow.utils;
2
3 import java.io.*;
4 import java.util.*;
5 import snow.utils.ProcessUtils;
6
7 /** system utilities, normally working on Windows and on Linux.
8 */

9 public final class SysUtils
10 {
11    /** Constructor. */
12    private SysUtils()
13    {
14    }
15
16    /** on windows, single files are "executed" by the system, with the associated file.
17    */

18    public static void openFileExplorer(String JavaDoc dir)
19    {
20
21             if(System.getProperty("os.name", "Windows").toLowerCase().indexOf("windows")>=0)
22             {
23               try
24               {
25                  String JavaDoc wname = dir.replaceAll("/", "\\\\").trim();
26                  if(wname.indexOf(' ')>0)
27                  {
28                     // the /e does not work here !!! :-((
29
ProcessUtils.executeProcessAndGobble(Arrays.asList("explorer.exe" ,"\""+wname+"\""), "open explorer");
30                  }
31                  else
32                  {
33                     // /e to open the explorer with the left tree...
34
ProcessUtils.executeProcessAndGobble(Arrays.asList("explorer.exe" ,"/e,"+wname), "open explorer");
35                  }
36               }
37               catch(Exception JavaDoc e) { e.printStackTrace(); }
38             }
39             else if(System.getProperty("os.name", "linux").toLowerCase().indexOf("linux")>=0)
40             {
41               try
42               {
43                  // spaces ??
44
ProcessUtils.executeProcessAndGobble(Arrays.asList(System.getProperty("os.filebrowser", "konqueror"),dir), "open explorer");
45               }
46               catch(Exception JavaDoc e) { e.printStackTrace(); }
47             }
48    }
49
50    public static void openBrowser(String JavaDoc url) throws Exception JavaDoc
51    {
52        if(is_Windows_OS())
53        {
54            // spaces ??
55
Process JavaDoc p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+url);
56          ProcessUtils.drainProcess(p, false);
57        }
58        else
59        {
60          Process JavaDoc p = Runtime.getRuntime().exec(""+url);
61          ProcessUtils.drainProcess(p, false);
62           // unix ?
63
}
64    }
65
66  /**
67    @param windowsGroup is normally "Users"
68  */

69   public static boolean setReadWriteAttribs(final File f, String JavaDoc windowsGroup) throws Exception JavaDoc
70   {
71      if(is_Windows_OS())
72      {
73         //
74
System.out.println("setting read-write-access for "+f);
75         String JavaDoc reply = null;
76         if(f.isDirectory())
77         {
78           reply = ProcessUtils.readWholeProcessStack(Arrays.asList("cmd", "/c", "CACLS", ""+f.getName(),
79            "/t", "/e", "/g", // recursive edit grant
80
windowsGroup+":f" // Full access !
81
),
82            f.getParentFile(),
83            Arrays.asList("y\r\n"),
84            true // confirmation required: type "yes" or "y" !!
85
);
86           System.out.println("ok ? "+reply);
87         }
88         else
89         {
90           reply = ProcessUtils.readWholeProcessStack(Arrays.asList("cmd", "/c", "CACLS", ""+f.getName(),
91            "/e", "/g", // edit grant
92
windowsGroup+":f"), // Full access !
93

94            f.getParentFile(),
95
96            Arrays.asList("y\r\n"), true // confirmation required: type "yes" or "y" !!
97

98           );
99           System.out.println("set. "+reply);
100         }
101
102         /*reply = reply.trim();
103         if(reply.length()>0)
104         {
105            System.out.println("Can't set the readwrite for all for "+reply);
106            return false;
107         }*/

108      }
109      else
110      {
111         try
112         {
113           if(f.isDirectory())
114           {
115               String JavaDoc reply = ProcessUtils.readWholeProcessStack(Arrays.asList(
116                 "/bin/sh", "-c", "chmod -R 777 "+f.getName()), f.getParentFile());
117               if(reply==null || reply.trim().length()>0)
118               {
119                 System.out.println("Cannot set read write file access for folder "+f+":\n"+reply);
120                 return false;
121               }
122               return true;
123           }
124           else
125           {
126               String JavaDoc reply = ProcessUtils.readWholeProcessStack(Arrays.asList(
127                "/bin/sh", "-c", "chmod 777 "+f.getName()), f.getParentFile());
128               if(reply==null || reply.trim().length()>0)
129               {
130                 System.out.println("Cannot set read write file access for file "+f+":\n"+reply);
131                 return false;
132               }
133               return true;
134           }
135         }
136         catch(Exception JavaDoc ex)
137         {
138            ex.printStackTrace();
139            return false;
140         }
141      }
142      return true;
143   }
144
145   public static boolean is_Windows_OS()
146   {
147     if(System.getProperty("os.name", "windows").toLowerCase().indexOf("win")>=0) return true;
148
149     return false;
150
151   }
152
153   /*
154   public static void main(String[] arguments) throws Exception
155   {SysUtils.openBrowser("http://localhost:7000");
156   }*/

157
158
159 } // SysUtils
Popular Tags