Possibility to set owner/group and permissions dynamically.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Mon, 18 Mar 2013 14:20:45 +0000 (15:20 +0100)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Mon, 18 Mar 2013 14:20:45 +0000 (15:20 +0100)
org.glite.lb.client-java/src/org/glite/lb/ContextIL.java
org.glite.lb.client-java/src/org/glite/lb/ContextLL.java
org.glite.lb.client-java/src/org/glite/lb/ILFileWriter.java

index 4dde7d2..6199739 100644 (file)
@@ -31,6 +31,8 @@ public class ContextIL extends Context {
     private int repeatWriteToFile = 5;
     private int connAttempts = 3;
     private int timeout = 3;
+    private String owner = null;
+    private String permissions = "g+rw";
 
     //tutorial http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
     //native method which is written in C and imported to Java
@@ -85,7 +87,7 @@ public class ContextIL extends Context {
 
        String file = prefix + "." + getJobid().getUnique();
 
-        Long fileLength = ILFileWriter.write(file, message, repeatWriteToFile);
+        Long fileLength = ILFileWriter.write(file, message, repeatWriteToFile, owner, permissions);
 
        if (socket != null) sendToSocket(socket,fileLength.longValue(),message,message.length(),connAttempts,timeout);
     }
@@ -160,4 +162,20 @@ public class ContextIL extends Context {
         this.timeout = timeout;
     }
 
+    public String getPermissions() {
+        return permissions;
+    }
+
+    public void setPermissions(String permissions) {
+        this.permissions = permissions;
+    }
+
+    public String getOwner() {
+        return owner;
+    }
+
+    public void setOwner(String owner) {
+        this.owner = owner;
+    }
+
 }
index 4ae98fc..3987002 100644 (file)
@@ -32,6 +32,8 @@ public class ContextLL extends Context {
     private int repeatWriteToFile = 5;
     private int timeout = 30000; //in milliseconds
     private LBCredentials cred;
+    private String owner = null;
+    private String permissions = "g+rw";
     private SSLSend sslSend = null;
 
     public ContextLL() {
@@ -97,7 +99,7 @@ public class ContextLL extends Context {
         
         String message = super.createMessage(event);
 
-        ILFileWriter.write(prefix, message, repeatWriteToFile);
+        ILFileWriter.write(prefix, message, repeatWriteToFile, owner, permissions);
         
         
         sslSend.send(cred, address, port, timeout, message);
@@ -174,5 +176,20 @@ public class ContextLL extends Context {
         this.cred = cred;
     }
 
-    
+    public String getPermissions() {
+        return permissions;
+    }
+
+    public void setPermissions(String permissions) {
+        this.permissions = permissions;
+    }
+
+    public String getOwner() {
+        return owner;
+    }
+
+    public void setOwner(String owner) {
+        this.owner = owner;
+    }
+
 }
index e834d26..57e982a 100644 (file)
@@ -43,13 +43,14 @@ public class ILFileWriter {
      * @param message message which will be written
      * @param repeatWriteToFile count of attempts to write to file in case of failure
      */
-    public static Long write(String prefix, String message, int repeatWriteToFile) throws LBException {
+    public static Long write(String prefix, String message, int repeatWriteToFile, String owner, String permissions) throws LBException {
         FileWriter fileWriter = null;
         long fileLength = 0;
         FileLock fileLock = null;
         File file;
-       Runtime run = Runtime.getRuntime();
-       String cmd[] = {"chmod", "g+rw", null};
+        Runtime run = Runtime.getRuntime();
+        String cmd_owner[] = {"chown", owner, null};
+        String cmd_perm[] = {"chmod", permissions, null};
 
         for (int i = 0; i < repeatWriteToFile; i++) {
             try {
@@ -68,9 +69,15 @@ public class ILFileWriter {
                    out.close();
 
                     if (file.exists()) {
-                       cmd[2] = prefix;
-                       run.exec(cmd);
-                        break;
+                       if (owner != null) {
+                               cmd_owner[2] = prefix;
+                               run.exec(cmd_owner);
+                       }
+                       if (permissions != null) {
+                               cmd_perm[2] = prefix;
+                               run.exec(cmd_perm);
+                       }
+                       break;
                     }
                 }
             } catch (Throwable ex) {
@@ -85,4 +92,15 @@ public class ILFileWriter {
 
         return new Long(fileLength);
     }
+
+    /**
+     * Writes message to a file and returns size of this file before writing the
+     * data
+     * @param prefix file path
+     * @param message message which will be written
+     * @param repeatWriteToFile count of attempts to write to file in case of failure
+     */
+    public static Long write(String prefix, String message, int repeatWriteToFile) throws LBException {
+        return ILFileWriter.write(prefix, message, repeatWriteToFile, null, null);
+    }
 }