- handle sequence codes
authorAleš Křenek <ljocha@ics.muni.cz>
Thu, 10 Apr 2008 09:02:08 +0000 (09:02 +0000)
committerAleš Křenek <ljocha@ics.muni.cz>
Thu, 10 Apr 2008 09:02:08 +0000 (09:02 +0000)
- formatting

org.glite.lb.client-java/nbproject/private/private.properties
org.glite.lb.client-java/src/org/glite/lb/client_java/Context.java
org.glite.lb.client-java/src/org/glite/lb/client_java/ContextIL.java
org.glite.lb.client-java/src/org/glite/lb/client_java/ContextProxy.java
org.glite.lb.client-java/src/org/glite/lb/client_java/Running.java
org.glite.lb.client-java/src/org/glite/lb/client_java/SeqCode.java
org.glite.lb.client-java/src/org/glite/test/Test.java

index cf42245..6a0a467 100755 (executable)
@@ -2,6 +2,6 @@ do.depend=false
 do.jar=true
 javac.debug=true
 javadoc.preview=true
-jaxws.endorsed.dir=/packages/share/netbeans-6.0.1/java1/modules/ext/jaxws21/api
-project.jobid-api-java=/home/xpiskac/bakalarka/jobid-api-java
-user.properties.file=/home/xpiskac/.netbeans/6.0/build.properties
+jaxws.endorsed.dir=/home/pavel/netbeans-6.0.1/java1/modules/ext/jaxws21/api
+project.jobid-api-java=/home/pavel/Desktop/bakalarka/org.glite.jobid.api-java
+user.properties.file=/home/pavel/.netbeans/6.0/build.properties
index 07328b1..5143bde 100755 (executable)
@@ -4,6 +4,7 @@ import java.net.UnknownHostException;
 import java.util.Calendar;
 import java.util.Random;
 import org.glite.jobid.api_java.Jobid;
+import org.glite.jobid.api_java.CheckedString;
 
 /**
  * Class representing a context for some job
@@ -12,7 +13,7 @@ import org.glite.jobid.api_java.Jobid;
  * @version 15. 3. 2008
  */
 public class Context {
-    
+
     private int id;
     private Sources source;
     private int flag;
@@ -23,13 +24,13 @@ public class Context {
     private Jobid jobid;
     private SeqCode seqCode;
     private String message;
-    
+
     /**
      * Creates new instance of Context class.
      */
     public Context() {
     }
-    
+
     /**
      * Creates new instance of Context class.
      * 
@@ -41,60 +42,54 @@ public class Context {
      * @param prog if null then is used "egd-wms"
      * @param srcInstance if null then it is set as ""
      * @param jobid
-     * @throws java.lang.IllegalArgumentException if source, user or jobid is null 
-     *  or flag < 0 
+     * @throws java.lang.IllegalArgumentException if user or jobid is null 
+     *  or flag < 0 or source <=0 || >= 9 
      * 
      */
     public Context(int id,
-                   Sources source,
-                   int flag,
-                   String host,
-                   String user,
-                   String prog,
-                   String srcInstance,
-                   Jobid jobid) {
+            Sources source,
+            int flag,
+            String host,
+            String user,
+            String prog,
+            String srcInstance,
+            Jobid jobid) {
         if (id < 0) {
             id = new Random().nextInt();
         }
-        
-        if (source == null)
-        {
+
+        if (source == null) {
             throw new IllegalArgumentException("Context source");
         }
-        
-        if (flag < 0 || flag > 8)
-        {
+
+        if (flag < 0) {
             throw new IllegalArgumentException("Context flag");
         }
-        
-        if (host == null || host.equals(""))
-        {
+
+        if (host == null || host.equals("")) {
             try {
                 host = java.net.InetAddress.getLocalHost().getHostName();
             } catch (UnknownHostException ex) {
-               System.err.println(ex);
+                System.err.println(ex);
             }
         }
-        
-        if (user == null)
-        {
+
+        if (user == null) {
             throw new IllegalArgumentException("Context user");
         }
-        
+
         if (prog == null) {
             prog = new String("edg-wms");
         }
-        
-        if (srcInstance == null)
-        {
+
+        if (srcInstance == null) {
             srcInstance = new String("");
         }
-        
-        if (jobid == null)
-        {
+
+        if (jobid == null) {
             throw new IllegalArgumentException("Context jobid");
         }
-        
+
         this.id = id;
         this.source = source;
         this.flag = flag;
@@ -104,7 +99,7 @@ public class Context {
         this.srcInstance = new CheckedString(srcInstance).toString();
         this.jobid = jobid;
     }
-    
+
     /**
      * Converts Sources enum constants to defined string
      * @param sourceEnum Sources enum constant
@@ -126,106 +121,85 @@ public class Context {
             default: throw new IllegalArgumentException("wrong source type");
         }
     }
-    
-    public Sources stringToSources(String source) {
-        if (source.equals("EDG_WLL_SOURCE_NONE")) return Sources.EDG_WLL_SOURCE_NONE;
-        if (source.equals("EDG_WLL_SOURCE_USER_INTERFACE")) return Sources.EDG_WLL_SOURCE_USER_INTERFACE;
-        if (source.equals( "EDG_WLL_SOURCE_NETWORK_SERVER")) return Sources.EDG_WLL_SOURCE_NETWORK_SERVER;
-        if (source.equals("EDG_WLL_SOURCE_WORKLOAD_MANAGER")) return Sources.EDG_WLL_SOURCE_WORKLOAD_MANAGER;
-        if (source.equals("EDG_WLL_SOURCE_BIG_HELPER")) return Sources.EDG_WLL_SOURCE_BIG_HELPER;
-        if (source.equals("EDG_WLL_SOURCE_JOB_SUBMISSION")) return Sources.EDG_WLL_SOURCE_JOB_SUBMISSION;
-        if (source.equals("EDG_WLL_SOURCE_LOG_MONITOR")) return Sources.EDG_WLL_SOURCE_LOG_MONITOR;
-        if (source.equals("EDG_WLL_SOURCE_LRMS")) return Sources.EDG_WLL_SOURCE_LRMS;
-        if (source.equals("EDG_WLL_SOURCE_APPLICATION")) return Sources.EDG_WLL_SOURCE_APPLICATION;
-        if (source.equals("EDG_WLL_SOURCE_LB_SERVER")) return Sources.EDG_WLL_SOURCE_LB_SERVER;
-        throw new IllegalArgumentException("wrong source type");
-        
-    }
-    
+
     /**
      * Creates message prepared to send
      * @param event event for which is message generated
      * @throws IllegalArgumentException if event, source, user or job is null
      * or flag < 0
      */
-    public void log(Event event) { 
+    public void log(Event event) {
         if (event == null) {
             throw new IllegalArgumentException("Context event");
         }
-        
-        if (source == null)
-        {
+
+        if (source == null) {
             throw new IllegalArgumentException("Context source");
         }
-        
-        if (flag < 0 || flag > 8)
-        {
+
+        if (flag < 0) {
             throw new IllegalArgumentException("Context flag");
         }
-        
-        if (host == null || host.equals(""))
-        {
+
+        if (host == null || host.equals("")) {
             try {
                 host = java.net.InetAddress.getLocalHost().getHostName();
             } catch (UnknownHostException ex) {
-               System.err.println(ex);
+                System.err.println(ex);
             }
         }
-        
+
         if (prog == null) {
             prog = new String("edg-wms");
         }
-        
-        if (user == null)
-        {
+
+        if (user == null) {
             throw new IllegalArgumentException("Context user");
         }
-        
-        if (srcInstance == null)
-        {
+
+        if (srcInstance == null) {
             srcInstance = new String("");
         }
-        
-        if (jobid == null)
-        {
+
+        if (jobid == null) {
             throw new IllegalArgumentException("Context jobid");
         }
-        
+
         String output;
         String date = "";
         String tmp;
         date = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
         tmp = String.valueOf(Calendar.getInstance().get(Calendar.MONTH) + 1);
-        date += "00".substring(0, 2 - tmp.length ()) + tmp;
+        date += "00".substring(0, 2 - tmp.length()) + tmp;
         tmp = String.valueOf(Calendar.getInstance().get(Calendar.DATE));
-        date += "00".substring(0, 2 - tmp.length ()) + tmp;
+        date += "00".substring(0, 2 - tmp.length()) + tmp;
         tmp = String.valueOf(Calendar.getInstance().get(Calendar.HOUR));
-        date += "00".substring(0, 2 - tmp.length ()) + tmp;
+        date += "00".substring(0, 2 - tmp.length()) + tmp;
         tmp = String.valueOf(Calendar.getInstance().get(Calendar.MINUTE));
-        date += "00".substring(0, 2 - tmp.length ()) + tmp;
+        date += "00".substring(0, 2 - tmp.length()) + tmp;
         tmp = String.valueOf(Calendar.getInstance().get(Calendar.SECOND));
-        date += "00".substring(0, 2 - tmp.length ()) + tmp;
-       date += ".";
+        date += "00".substring(0, 2 - tmp.length()) + tmp;
+        date += ".";
         tmp = String.valueOf(Calendar.getInstance().get(Calendar.MILLISECOND));
-        String tmp2 = "000".substring(0, 3 - tmp.length ()) + tmp;
-        date += tmp2 + "000000".substring(tmp.length (), 6);
-        
-        seqCode.setPardOfSeqCode(flag, seqCode.getPardOfSeqCode(flag)+1);
-        
-        output = ("DG.LLLID=" + id + 
-               " DG.USER=\"" + user + "\"" + 
-               " DATE=" + date +
-               " HOST=\"" + host + "\"" + 
-               " PROG=" + prog +
-               " LVL=SYSTEM" +
-               " DG.PRIORITY=0" +
-               " DG.SOURCE=\"" + recognizeSource(source) + "\"" + 
-               " DG.SRC_INSTANCE=\"" + srcInstance + "\"" + 
-               " DG.EVNT=\"" + event.getEventType() + "\"" +
-               " DG.JOBID=\"" + jobid + "\"" +
-               " DG.SEQCODE=\"" + seqCode + "\"" +
-               event.ulm());
-        
+        String tmp2 = "000".substring(0, 3 - tmp.length()) + tmp;
+        date += tmp2 + "000000".substring(tmp.length(), 6);
+
+        seqCode.incrementSeqCode(source);
+
+        output = ("DG.LLLID=" + id +
+                " DG.USER=\"" + user + "\"" +
+                " DATE=" + date +
+                " HOST=\"" + host + "\"" +
+                " PROG=" + prog +
+                " LVL=SYSTEM" +
+                " DG.PRIORITY=0" +
+                " DG.SOURCE=\"" + recognizeSource(source) + "\"" +
+                " DG.SRC_INSTANCE=\"" + srcInstance + "\"" +
+                " DG.EVNT=\"" + event.getEventType() + "\"" +
+                " DG.JOBID=\"" + jobid + "\"" +
+                " DG.SEQCODE=\"" + seqCode + "\"" +
+                event.ulm());
+
         this.message = output;
     }
 
@@ -245,11 +219,10 @@ public class Context {
      * @throws java.lang.IllegalArgumentException if flag is lower than 0
      */
     public void setFlag(int flag) {
-        if (flag < 0 || flag > 8)
-        {
+        if (flag < 0) {
             throw new IllegalArgumentException("Context flag");
         }
-        
+
         this.flag = flag;
     }
 
@@ -268,11 +241,10 @@ public class Context {
      * @throws java.lang.IllegalArgumentException if host is null
      */
     public void setHost(String host) {
-        if (host == null)
-        {
+        if (host == null) {
             throw new IllegalArgumentException("Context host");
         }
-        
+
         this.host = host;
     }
 
@@ -284,7 +256,7 @@ public class Context {
     public int getId() {
         return id;
     }
-    
+
     /**
      * Sets message id.
      * 
@@ -292,8 +264,7 @@ public class Context {
      * @throws java.lang.IllegalArgumentException if id is lower than 0
      */
     public void setId(int id) {
-        if (id < 0)
-        {
+        if (id < 0) {
             throw new IllegalArgumentException("Context id");
         }
         this.id = id;
@@ -315,11 +286,10 @@ public class Context {
      * @throws java.lang.IllegalArgumentException if jobid is null
      */
     public void setJobid(Jobid jobid) {
-        if (jobid == null)
-        {
+        if (jobid == null) {
             throw new IllegalArgumentException("Context jobid");
         }
-        
+
         this.jobid = jobid;
     }
 
@@ -347,7 +317,7 @@ public class Context {
         if (prog == null) {
             prog = new String("edg-wms");
         }
-        
+
         this.prog = (new CheckedString(prog)).toString();
     }
 
@@ -369,7 +339,7 @@ public class Context {
         if (seqCode == null) {
             throw new IllegalArgumentException("Context seqCode");
         }
-        
+
         this.seqCode = seqCode;
     }
 
@@ -387,11 +357,10 @@ public class Context {
      * @throws java.lang.IllegalArgumentException if source is null
      */
     public void setSource(Sources source) {
-        if (source == null)
-        {
+        if (source == null) {
             throw new IllegalArgumentException("Context source");
         }
-        
+
         this.source = source;
     }
 
@@ -408,11 +377,10 @@ public class Context {
      * @param srcInstance srcInstance
      */
     public void setSrcInstance(String srcInstance) {
-        if (srcInstance == null)
-        {
+        if (srcInstance == null) {
             srcInstance = new String("");
         }
-        
+
         this.srcInstance = new CheckedString(srcInstance).toString();
     }
 
@@ -430,11 +398,10 @@ public class Context {
      * @throws java.lang.IllegalArgumentException if user is null
      */
     public void setUser(String user) {
-        if (user == null)
-        {
+        if (user == null) {
             throw new IllegalArgumentException("Context user");
         }
-        
+
         this.user = (new CheckedString(user)).toString();
     }
 }
index 79c6370..03eaa9e 100755 (executable)
@@ -9,6 +9,7 @@ import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import org.glite.jobid.api_java.Jobid;
+import org.glite.jobid.api_java.CheckedString;
 
 /**
  * Class which is used to send messages to inter-logger using unix socket.
@@ -25,7 +26,7 @@ public class ContextIL extends Context {
     private int timeout = 3;
     private Boolean useUnixSocket = true;
 
-    //navod http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
+    //tutorial http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
     //native method which is written in C and imported to Java
     native int sendToSocket(String socket_path,
     long filepos,
index 87809a5..c8f16ff 100755 (executable)
@@ -15,8 +15,9 @@ import java.util.ArrayList;
 import javax.net.ssl.SSLSocket;
 import javax.net.ssl.SSLSocketFactory;
 import org.glite.jobid.api_java.Jobid;
+import org.glite.jobid.api_java.CheckedString;
 
-/**
+/** NOT IMPLEMENTED YET! THIS IS ONLY SKELETON
  *
  * @author xpiskac
  */
index c44a04e..1389b60 100755 (executable)
@@ -1,5 +1,7 @@
 package org.glite.lb.client_java;
 
+import org.glite.jobid.api_java.CheckedString;
+
 /**
  * Class which represents event "running".
  * 
index 2ecbff0..ca58fa0 100755 (executable)
@@ -1,40 +1,72 @@
 package org.glite.lb.client_java;
 
 /**
- *
- * @author xpiskac
+ * This class represents sequence code.
+ * 
+ * @author Pavel Piskac (173297@mail.muni.cz)
+ * @version 9. 4. 2008
  */
 public class SeqCode {
     
     private int[] seqCode = {0, 0, 0, 0, 0, 0, 0, 0, 0};
     
+    /**
+     * Empty constructor which creates new instance of SeqCode with all values
+     * equal 0
+     */
     public SeqCode() {
     }
-
-    public int[] getSeqCode() {
-        return seqCode;
+    
+    /**
+     * Constructor which creates new instance of SeqCode with values set by user
+     * in input attribute
+     * 
+     * @param seqCodeString
+     */
+    public SeqCode(String seqCodeString) {
+        getSeqCodeFromString(seqCodeString);
     }
 
-    public void setSeqCode(int[] seqCode) {
-        this.seqCode = seqCode;
+    /**
+     * This method increments one specific part of sequence code given by part attribute
+     * 
+     * @param part part of sequence number which will be increased
+     */
+    public void incrementSeqCode(Sources part) {
+        seqCode[part.ordinal()-1]++;
     }
     
-    public int getPardOfSeqCode(int part) {
-        if (part < 0 || part > 9) {
-            throw new IllegalArgumentException("part");
+    /**
+     * Converts string representation of sequence code to format which is used
+     * in this class.
+     * Insert sequence codes in format:
+     * UI=000000:NS=0000000000:WM=000000:BH=0000000000:JSS=000000:LM=000000:LRMS=000000:APP=000000:LBS=000000
+     * @param seqCodeString
+     */
+    public void getSeqCodeFromString(String seqCodeString) {
+        
+        if (!seqCodeString.matches("UI=\\d{1,}:NS=\\d{1,}:WM=\\d{1,}:BH=\\d{1,}:" +
+                "JSS=\\d{1,}:LM=\\d{1,}:LRMS=\\d{1,}:APP=\\d{1,}:LBS=\\d{1,}")) {
+            throw new IllegalArgumentException("this is not correct sequence code");
         }
         
-        return seqCode[part];
-    }
-    
-    public void setPardOfSeqCode(int part, int value) {
-        if (part < 0 || part > 9) {
-            throw new IllegalArgumentException("part");
+        int currentPosition = 0;
+        int equalsPosition = 0;
+        int colonPosition = 0;
+        for (int i = 0; i <= 8; i++) {
+            equalsPosition = seqCodeString.indexOf('=', currentPosition);
+            if (i == 8) {
+                colonPosition = seqCodeString.length();
+            } else {
+                colonPosition = seqCodeString.indexOf(':', currentPosition);
+            }
+            seqCode[i] = new Integer(seqCodeString.substring(equalsPosition+1, colonPosition));
+            currentPosition = colonPosition + 1;
         }
         
-        seqCode[part] = value;
     }
     
+    @Override
     public String toString() {        
         String tmp = Integer.toString(seqCode[0]);    
         String output = "UI=";
index 23eda6d..0740b01 100755 (executable)
@@ -9,6 +9,7 @@ import org.glite.jobid.api_java.Jobid;
 import org.glite.lb.client_java.ContextIL;
 import org.glite.lb.client_java.Running;
 import org.glite.lb.client_java.SeqCode;
+import org.glite.lb.client_java.Sources;
 
 /**
  *
@@ -19,11 +20,11 @@ public class Test {
     public static void main(String[] args) {
         
         //how Jobid class works
-        /* //unique part is automatically generated
-        Jobid jobid = new Jobid("https://somewhere.cz", 5000);
-        System.out.println("bkserver "+ jobid.getBkserver());
-        System.out.println("port "+ jobid.getPort());
-        System.out.println("unique "+ jobid.getUnique());
+        //unique part is automatically generated
+        Jobid jobid1 = new Jobid("https://somewhere.cz", 5000);
+        System.out.println("bkserver "+ jobid1.getBkserver());
+        System.out.println("port "+ jobid1.getPort());
+        System.out.println("unique "+ jobid1.getUnique());
         System.out.println("-------------------");
         
         //unique part is set by user
@@ -50,7 +51,6 @@ public class Test {
         System.out.println("unique "+ jobid4.getUnique());
         System.out.println("-------------------");
         
-         */ 
         if (args.length == 0) {
             System.out.println("How to use test class:\n" +
                     "you have to set 10 arguments in this order, if the choice is optional \"\" or text has to be set:\n" +
@@ -80,16 +80,19 @@ public class Test {
             /* Create sequence code
              * Example:
              * SeqCode seqCode = new SeqCode();
-             * Then you can set some parts
+             * Insert sequence number in format 
+             * UI=XXXXXX:NS=XXXXXXXXXX:WM=XXXXXX:BH=XXXXXXXXXX:JSS=XXXXXX:LM=XXXXXX:LRMS=XXXXXX:APP=XXXXXX:LBS=XXXXXX
+             * where X is 0-9, or you can just create new instance of SeqCode where all parts are set to 0
              * Example:
-             * seqCode.setPardOfSeqCode(0, 95);
-             * or whole sequence number
-             * Example:
-             * int[] seqCodeArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};
-             * seqCode.setSeqCode(seqCodeArray);
+             * SeqCode seqCode = new SeqCode();
+             * seqCode.getSeqCodeFromString("UI=000001:NS=0000000002:WM=000003:BH=0000000004:" + 
+             * "JSS=000005:LM=000006:LRMS=000007:APP=000008:LBS=000009"); 
+             * seqCode.incrementSeqCode(Sources.EDG_WLL_SOURCE_USER_INTERFACE); 
+             * resulting sequence code will be 
+             * UI=000002:NS=0000000002:WM=000003:BH=0000000004:JSS=000005:LM=000006:LRMS=000007:APP=000008:LBS=000009
              */
             SeqCode seqCode = new SeqCode();
-
+            
             /* Choose type of sending a log messages (at this time is implemented only ContextIL class)
              * You can choose from some constructors (see org.glite.lb.client_java.ContextIL class)
              */
@@ -106,9 +109,11 @@ public class Test {
             ctx.setId(new Random().nextInt(99999999));
 
             /* Source indicates source of the message, it is constant from org.glite.lb.client_java.Sources class
-             * Example: ctx.setSource(ctx.stringToSources("EDG_WLL_SOURCE_LRMS"));
+             * Example: ctx.setSource(Sources.EDG_WLL_SOURCE_LRMS);
+             * In this case we have to use method which converts args[2] to Sources. In real environment it will
+             * not be used.
              */
-            ctx.setSource(ctx.stringToSources(args[2]));
+            ctx.setSource(stringToSources(args[2]));
 
             /* Flag tells which part of the sequence number will be increased. It is a number in range from 0 to 8
              * Example: ctx.setFlag(0);
@@ -175,4 +180,23 @@ public class Test {
             ctx.log(running);
         }
     }
+    
+    /*
+     * This method helps with converting String from input to convert to Source.
+     * In real environment it will not be used.
+     */
+    private static Sources stringToSources(String source) {         
+        if (source.equals("Sources.EDG_WLL_SOURCE_NONE")) return Sources.EDG_WLL_SOURCE_NONE;
+        if (source.equals("Sources.EDG_WLL_SOURCE_USER_INTERFACE")) return Sources.EDG_WLL_SOURCE_USER_INTERFACE;
+        if (source.equals("Sources.EDG_WLL_SOURCE_NETWORK_SERVER")) return Sources.EDG_WLL_SOURCE_NETWORK_SERVER;
+        if (source.equals("Sources.EDG_WLL_SOURCE_WORKLOAD_MANAGER")) return Sources.EDG_WLL_SOURCE_WORKLOAD_MANAGER;
+        if (source.equals("Sources.EDG_WLL_SOURCE_BIG_HELPER")) return Sources.EDG_WLL_SOURCE_BIG_HELPER;
+        if (source.equals("Sources.EDG_WLL_SOURCE_JOB_SUBMISSION")) return Sources.EDG_WLL_SOURCE_JOB_SUBMISSION;
+        if (source.equals("Sources.EDG_WLL_SOURCE_LOG_MONITOR")) return Sources.EDG_WLL_SOURCE_LOG_MONITOR;
+        if (source.equals("Sources.EDG_WLL_SOURCE_LRMS")) return Sources.EDG_WLL_SOURCE_LRMS;
+        if (source.equals("Sources.EDG_WLL_SOURCE_APPLICATION")) return Sources.EDG_WLL_SOURCE_APPLICATION;
+        if (source.equals("Sources.EDG_WLL_SOURCE_LB_SERVER")) return Sources.EDG_WLL_SOURCE_LB_SERVER;
+        throw new IllegalArgumentException("wrong source type");
+
+    }
 }