Be careful with the "Process proc = rt.exec(cmd);" usage, because depending on 
the OS in some estrange conditions it hangs, even to command that in theory 
shouldn't use stdin/err. A work around is to add threads to consume the stdin 
and stderr of the process, like:
final InputStream in = proc.getInputStream();
new Thread(new Runnable() {
        public void run() {
                try {
                while (in.read() != -1);
                                                } catch (IOException e) {
                                                                        // 
logTheError(e);
                                                                }
                                                        }
                                                }).start();
                                                final InputStream err = 
proc.getErrorStream();
                                                new Thread(new Runnable() {
                                                        public void run() {
                                                                try {
                                                                        while 
(err.read() != -1)
                                                                                
;
                                                                } catch 
(IOException e) {
                                                                        // 
logTheError(e);
                                                                }
                                                        }
                                                }).start();

Cumprimentos,
Loureiro, Gil
Document Engineering Manager
Document Services
_________________________________________
Edinfor - a LogicaCMG company
Rua Particular da EDP (à rua cidade Goa nº11), 2685 Sacavém
Portugal
M: +351 93 741 8888
E: [EMAIL PROTECTED]
www.edinfor.logicacmg.com
-----Original Message-----
From: Andy Thomson [mailto:[EMAIL PROTECTED] 
Sent: sexta-feira, 22 de Agosto de 2008 13:23
To: [email protected]
Subject: Re: File/Dir permissions

Niklas,

I had proposed a change a while back to allow for new SITE commands. I 
have added quite a few using it: CHMOD, UTIME, DUAL [provides MD5 & SHA1 
hashes on a file or all files in a directory], and some custom ones 
specific to my needs.

Maybe it can posted/added as an "extra" for people that would want the 
ability to add custom SITE commands?  I would supply the ones above and 
a generic template on how to do it.

One of the key tasks is to detect the OS platform the service is running 
one and call the appropriate OS command shell & command. There was a 
good article about 7-8 years ago on this, not much has changed from the 
JVM perspective [sad].

Here is a snippet from the code:

             String osName = System.getProperty("os.name");
             String[] cmd = new String[3];

             if (osName.equals("Windows 95")) {
                 cmd[0] = "command.com";
                 cmd[1] = "/C";
                 cmd[2] = cmdline;
             } else if (osName.contains("Windows")) {
                 cmd[0] = "cmd.exe";
                 cmd[1] = "/C";
                 cmd[2] = cmdline;
             } else if (osName.equals("Linux")) {
                 cmd[0] = "sh";
                 cmd[1] = "-c";
                 cmd[2] = cmdline;
             }

             Runtime rt = Runtime.getRuntime();

             // run the command
             Process proc = rt.exec(cmd);

The "cmdline" is a string that holds the actual command and it's 
parameters: "/bin/chmod 755 somefile".  All error messages are passed 
back, works same as command line.

The commands could be separate jar files, or integrated with the main 
package, up to the developer.  I personally like keeping them outside 
the main code, just specify the class path in the config file, and let 
the SITE cmd load it.

The SITE cmd uses two maps, the normal one that it uses now, and then a 
user map that has the add-on commands.  It looks in one map then the 
other, taking into account a user may actually over-ride an existing 
SITE command.

Not sure what is the best way to get this code introduced? Maybe just 
post it somewhere and reference it?

Andy


Niklas Gustavsson wrote:
> On Fri, Aug 22, 2008 at 1:31 PM, Loureiro, Gil <[EMAIL PROTECTED]> wrote:
>> I've seen that the SITE CHMOD command is not supported, that in the case of
>> my application is Ok because user should never change permission.
> 
> The reason for it not being supported is that Java does not currently
> allow setting file permissions. There are some third-party libs that
> could be investigated for this, but they will be dependent on the OS
> so I'm a bit reluctant. Also, the upcoming new file API does allow for
> setting file permissions, hopefully coming in Java 7.
> 
>> But, if I create a new dir, from both fs or ftp, the permissions are always
>> rw for the owner, is it possible to set the default permission for a certain
>> user?
> 
> No, not at the moment. Patches welcome of course :-)
> 
> /niklas
> 


This e-mail and any attachment is for authorised use by the intended 
recipient(s) only. It may contain proprietary material, confidential 
information and/or be subject to legal privilege. It should not be copied, 
disclosed to, retained or used by, any other party. If you are not an intended 
recipient then please promptly delete this e-mail and any attachment and all 
copies and inform the sender. Thank you.


Reply via email to