Sources class cleanup
authorAleš Křenek <ljocha@ics.muni.cz>
Fri, 18 Sep 2009 12:32:43 +0000 (12:32 +0000)
committerAleš Křenek <ljocha@ics.muni.cz>
Fri, 18 Sep 2009 12:32:43 +0000 (12:32 +0000)
org.glite.lb.client-java/examples/CreamTest.java
org.glite.lb.client-java/examples/ProducerTestIL.java
org.glite.lb.client-java/examples/ProducerTestLL.java
org.glite.lb.client-java/project/genEventTypes.pl
org.glite.lb.client-java/src/org/glite/lb/Context.java
org.glite.lb.client-java/src/org/glite/lb/ContextIL.java
org.glite.lb.client-java/src/org/glite/lb/EventConvertor.java
org.glite.lb.client-java/src/org/glite/lb/ILFileWriter.java
org.glite.lb.client-java/src/org/glite/lb/SeqCode.java
org.glite.lb.client-java/src/org/glite/lb/Sources.java

index 32b4631..a61f34e 100644 (file)
@@ -26,7 +26,7 @@ public static void main(String[] args)
 
        ContextDirect   ctxd = new ContextDirect(srvpart[0],srvport);
        ctxd.setCredentials(cred);
-       ctxd.setSource(Sources.EDG_WLL_SOURCE_CREAM_CORE);
+       ctxd.setSource(new Sources(Sources.CREAM_CORE));
        ctxd.setJobid(job);
        ctxd.setSeqCode(new SeqCode(SeqCode.CREAM,"no_seqcodes_with_cream"));
 
@@ -40,9 +40,9 @@ public static void main(String[] args)
        System.out.println("JOBID="+job);
 
        ContextIL       ctx = new ContextIL(prefix,socket,lib);
-       ctx.setSource(Sources.EDG_WLL_SOURCE_CREAM_CORE);
+       ctx.setSource(new Sources(Sources.CREAM_CORE));
        ctx.setJobid(job);
-       ctx.setSeqCode(new SeqCode(SeqCode.CREAM,"no_seqcodes_with_cream"));
+       ctx.setSeqCode(new SeqCode(SeqCode.CREAM,"no_seqcodes_with_cream_cheat_duplicate"));
        ctx.setUser(ctxd.getUser());
 
 /* 2nd registration with JDL, via IL */
@@ -51,7 +51,19 @@ public static void main(String[] args)
 
        Event e = new EventCREAMStart();
        ctx.log(e);
+
+       EventCREAMStore store = new EventCREAMStore();
+       store.setResult(EventCREAMStore.Result.RESULT_START);
+       store.setCommand(EventCREAMStore.Command.COMMAND_CMDSTART);
+       ctx.log(store);
+
+       EventCREAMCall call = new EventCREAMCall();
+       call.setCallee(new Sources(Sources.LRMS));
+       call.setDestid("fake_Torque_ID");
+       call.setResult(EventCREAMCall.Result.RESULT_OK);
+       call.setCommand(EventCREAMCall.Command.COMMAND_CMDSTART);
        
+       ctx.log(call);
 
    } catch (Exception e)
    {
index 5d8e23a..63dc6ff 100644 (file)
@@ -43,7 +43,7 @@ public class ProducerTestIL {
              * 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); 
+             * seqCode.incrementSeqCode(Sources.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
              */
@@ -67,7 +67,7 @@ public class ProducerTestIL {
 
             /* Source indicates source of the message, it is constant from org.glite.lb.client_java.Sources class
              * and determines which part of sequence number will be increased.
-             * Example: ctx.setSource(Sources.EDG_WLL_SOURCE_LRMS);
+             * Example: ctx.setSource(Sources.LRMS);
              * In this case we have to use method which converts args[2] to Sources. In real environment it will
              * not be used.
              */
index 81e1219..3673094 100644 (file)
@@ -69,7 +69,7 @@ public class ProducerTestLL {
 
             /* Source indicates source of the message, it is constant from org.glite.lb.client_java.Sources class
              * and determines which part of sequence number will be increased.
-             * Example: ctx.setSource(Sources.EDG_WLL_SOURCE_LRMS);
+             * Example: ctx.setSource(Sources.LRMS);
              * In this case we have to use method which converts args[2] to Sources. In real environment it will
              * not be used.
              */
index 822743b..f36d57e 100644 (file)
@@ -12,7 +12,6 @@
                print E
 qq{
 package org.glite.lb;
-import org.glite.jobid.CheckedString;
 import org.glite.jobid.Jobid;
 
 public class Event$uc extends Event \{
@@ -61,7 +60,7 @@ qq{   \}
                                        my $cu = uc $_->{name};
                                        print E
 qq{
-                       case ${fnuu}_${cu}: out = "$cu";
+                       case ${fnuu}_${cu}: out = "$cu"; break;
 };
                                }
                                print E
index a41c15d..900e526 100644 (file)
@@ -15,7 +15,7 @@ import org.glite.jobid.Jobid;
  */
 public abstract class Context {
 
-    private int source;
+    private Sources source;
     private int flag;
     private String host;
     private String user;
@@ -45,7 +45,7 @@ public abstract class Context {
      *  or flag < 0 or source <=0 || >= 9 
      * 
      */
-    public Context(int source,
+    public Context(Sources src,
             int flag,
             String host,
             String user,
@@ -53,7 +53,7 @@ public abstract class Context {
             String srcInstance,
             Jobid jobid) {
 
-        if (source <= -1 || source > Sources.EDG_WLL_SOURCE_LB_SERVER) {
+        if (src == null) {
             throw new IllegalArgumentException("Context source");
         }
 
@@ -85,7 +85,7 @@ public abstract class Context {
             throw new IllegalArgumentException("Context jobid");
         }
 
-        this.source = source;
+        this.source = src;
         this.flag = flag;
         this.host = host;
         this.user = user;
@@ -95,30 +95,6 @@ public abstract class Context {
     }
 
     /**
-     * Converts Sources enum constants to defined string
-     * @param sourceEnum Sources enum constant
-     * @return String representation of Sources enum constants
-     * @throws IllegalArgumentException if wrong source type is set
-     */
-    private String recognizeSource(int sourceEnum) {
-        switch (sourceEnum) {
-            case Sources.EDG_WLL_SOURCE_NONE: return "Undefined";
-            case Sources.EDG_WLL_SOURCE_USER_INTERFACE: return "UserInterface";
-            case Sources.EDG_WLL_SOURCE_NETWORK_SERVER: return "NetworkServer";
-            case Sources.EDG_WLL_SOURCE_WORKLOAD_MANAGER: return "WorkloadManager";
-            case Sources.EDG_WLL_SOURCE_BIG_HELPER: return "BigHelper";
-            case Sources.EDG_WLL_SOURCE_JOB_SUBMISSION: return "JobController";
-            case Sources.EDG_WLL_SOURCE_LOG_MONITOR: return "LogMonitor";
-            case Sources.EDG_WLL_SOURCE_LRMS: return "LRMS";
-            case Sources.EDG_WLL_SOURCE_APPLICATION: return "Application";
-            case Sources.EDG_WLL_SOURCE_LB_SERVER: return "LBServer";
-            case Sources.EDG_WLL_SOURCE_CREAM_CORE: return "CreamCore";
-            case Sources.EDG_WLL_SOURCE_BLAH: return "BLAH";
-            default: throw new IllegalArgumentException("wrong source type");
-        }
-    }
-
-    /**
      * Abstract method which will serve as method for sending messages with events.
      * @param event event for which will be created and send message
      */
@@ -160,8 +136,6 @@ public abstract class Context {
             throw new IllegalArgumentException("Context seqCode");
         }
 
-       Sources.check(source);
-
         if (flag < 0) {
             throw new IllegalArgumentException("Context flag");
         }
@@ -199,7 +173,7 @@ public abstract class Context {
                 " PROG=" + Escape.ulm(prog) +
                 " LVL=SYSTEM" +
                 " DG.PRIORITY=0" +
-                " DG.SOURCE=\"" + recognizeSource(source) + "\"" +
+                " DG.SOURCE=\"" + source + "\"" +
                 " DG.SRC_INSTANCE=\"" + Escape.ulm(srcInstance) + "\"" +
                 " DG.EVNT=\"" + event.getEventType() + "\"" +
                 " DG.JOBID=\"" + jobid + "\"" +
@@ -323,7 +297,7 @@ public abstract class Context {
      * Gets source which represents which part of sequence code will be changed
      * @return source
      */
-    public int getSource() {
+    public Sources getSource() {
         return source;
     }
 
@@ -332,9 +306,8 @@ public abstract class Context {
      * @param source source
      * @throws java.lang.IllegalArgumentException if source is null
      */
-    public void setSource(int source) {
-       Sources.check(source);
-        this.source = source;
+    public void setSource(Sources src) {
+        this.source = src;
     }
 
     /**
index dcdf40b..2fbc1d1 100644 (file)
@@ -28,6 +28,7 @@ public class ContextIL extends Context {
      * Creates new instance of ContextIL.
      */
     public ContextIL() {
+       this.prefix = "/var/glite/log/dglogd.log";
     }
 
     public ContextIL(String prefix) {
@@ -54,7 +55,7 @@ public class ContextIL extends Context {
      * @param event event
      * @throws java.lang.IllegalArgumentException if event, prefix or path
      */
-    public void log(Event event) {
+    public void log(Event event) throws LBException {
         if (event == null) {
             throw new IllegalArgumentException("ContextIL event");
         }
index 76080ea..3fb5e35 100644 (file)
@@ -113,17 +113,17 @@ public class EventConvertor {
     private Sources getSourceFromString(String from) {
         if (from == null) throw new IllegalArgumentException("from cannot be null");
 
-        Sources source = new Sources(Sources.EDG_WLL_SOURCE_NONE);
-
-        if (from.equals(EventSource._UserInterface)) source = new Sources(Sources.EDG_WLL_SOURCE_USER_INTERFACE);
-        else if (from.equals(EventSource._NetworkServer)) source = new Sources(Sources.EDG_WLL_SOURCE_NETWORK_SERVER);
-        else if (from.equals(EventSource._WorkloadManager)) source = new Sources(Sources.EDG_WLL_SOURCE_WORKLOAD_MANAGER);
-        else if (from.equals(EventSource._BigHelper)) source = new Sources(Sources.EDG_WLL_SOURCE_BIG_HELPER);
-        else if (from.equals(EventSource._JobSubmission)) source = new Sources(Sources.EDG_WLL_SOURCE_JOB_SUBMISSION);
-        else if (from.equals(EventSource._LogMonitor)) source = new Sources(Sources.EDG_WLL_SOURCE_LOG_MONITOR);
-        else if (from.equals(EventSource._LRMS)) source = new Sources(Sources.EDG_WLL_SOURCE_LRMS);
-        else if (from.equals(EventSource._Application)) source = new Sources(Sources.EDG_WLL_SOURCE_APPLICATION);
-        else if (from.equals(EventSource._LBServer)) source = new Sources(Sources.EDG_WLL_SOURCE_LB_SERVER);
+        Sources source = new Sources(Sources.NONE);
+
+        if (from.equals(EventSource._UserInterface)) source = new Sources(Sources.USER_INTERFACE);
+        else if (from.equals(EventSource._NetworkServer)) source = new Sources(Sources.NETWORK_SERVER);
+        else if (from.equals(EventSource._WorkloadManager)) source = new Sources(Sources.WORKLOAD_MANAGER);
+        else if (from.equals(EventSource._BigHelper)) source = new Sources(Sources.BIG_HELPER);
+        else if (from.equals(EventSource._JobSubmission)) source = new Sources(Sources.JOB_SUBMISSION);
+        else if (from.equals(EventSource._LogMonitor)) source = new Sources(Sources.LOG_MONITOR);
+        else if (from.equals(EventSource._LRMS)) source = new Sources(Sources.LRMS);
+        else if (from.equals(EventSource._Application)) source = new Sources(Sources.APPLICATION);
+        else if (from.equals(EventSource._LBServer)) source = new Sources(Sources.LB_SERVER);
 
         return source;
     }
@@ -177,7 +177,7 @@ public class EventConvertor {
         org.glite.wsdl.types.lb.EventAccepted wsdlEvent = event.getAccepted();
         EventAccepted ev = new EventAccepted();
 
-        Sources source = new Sources(Sources.EDG_WLL_SOURCE_NONE);
+        Sources source = new Sources(Sources.NONE);
         if (wsdlEvent.getFrom() != null) {
             source = getSourceFromString(wsdlEvent.getFrom().getValue());
         }
@@ -773,7 +773,7 @@ public class EventConvertor {
 
         if (wsdlEvent.getFrom() != null) {
             ev.setFrom(getSourceFromString(wsdlEvent.getFrom().getValue()));
-        } else ev.setFrom(new Sources(Sources.EDG_WLL_SOURCE_NONE));
+        } else ev.setFrom(new Sources(Sources.NONE));
         if (wsdlEvent.getFromHost() != null) ev.setFromHost(wsdlEvent.getFromHost());
         if (wsdlEvent.getFromInstance() != null) ev.setFromInstance(wsdlEvent.getFromInstance());
         if (wsdlEvent.getReason() != null) ev.setReason(wsdlEvent.getReason());
@@ -898,7 +898,7 @@ public class EventConvertor {
 
         if (wsdlEvent.getDestination() != null) {
             ev.setDestination(getSourceFromString(wsdlEvent.getDestination().getValue()));
-        } else ev.setDestination(new Sources(Sources.EDG_WLL_SOURCE_NONE));
+        } else ev.setDestination(new Sources(Sources.NONE));
 
         if (wsdlEvent.getJob() != null) ev.setJob(wsdlEvent.getJob());
         if (wsdlEvent.getReason() != null) ev.setReason(wsdlEvent.getReason());
index 026b41b..574af35 100644 (file)
@@ -4,8 +4,8 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 
@@ -26,62 +26,42 @@ 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) {
+    public static Long write(String prefix, String message, int repeatWriteToFile) throws LBException {
         FileWriter fileWriter = null;
-        Long fileLength = null;
-        RandomAccessFile raf = null;
+        long fileLength = 0;
         FileLock fileLock = null;
         File file;
 
         for (int i = 0; i < repeatWriteToFile; i++) {
             try {
                 file = new File(prefix);
-                raf = new RandomAccessFile(file, "rw");
-                FileChannel fileChannel = raf.getChannel();
+               FileOutputStream out = new FileOutputStream(file,true);
+                FileChannel fileChannel = out.getChannel();
 
                 fileLock = fileChannel.tryLock();
                 if (fileLock != null) {
                     if (!file.exists()) {
                         continue;
                     }
-                    fileLength = new Long(raf.length());
-                    fileWriter = new FileWriter(file, true);
-                    //true means append data at the end of file
-
-                    BufferedWriter bufferedFileWriter = new BufferedWriter(fileWriter);
-
-                    bufferedFileWriter.write(message);
-                    bufferedFileWriter.flush();
+                    fileLength = file.length();
+                   out.write(message.getBytes());
+                   fileLock.release();
+                   out.close();
 
                     if (file.exists()) {
                         break;
                     }
                 }
-            } catch (FileNotFoundException ex) {
-                System.err.println(ex);
-            } catch (IOException ex) {
-                System.err.println(ex);
-            } catch (Exception ex) {
-                System.err.println(ex);
-            } finally {
+            } catch (Throwable ex) {
                 if (fileLock != null) {
                     try {
                         fileLock.release();
-                    } catch (IOException ex) {
-                        System.err.println(ex);
-                    }
-                }
-
-                try {
-                    raf.close();
-                } catch (IOException ex) {
-                    System.err.println(ex);
-                } catch (NullPointerException ex) {
-                    System.err.println(ex);
+                    } catch (IOException ex2) { }
                 }
+               throw new LBException(ex);
             }
         }
 
-        return fileLength;
+        return new Long(fileLength);
     }
 }
index 079435f..5199333 100644 (file)
@@ -16,6 +16,7 @@ public class SeqCode {
     
     private int[] seqCode = {0, 0, 0, 0, 0, 0, 0, 0, 0};
     private int type = 0;
+    private String cream_code;
     
     /**
      * Empty constructor which creates new instance of SeqCode with all values
@@ -39,11 +40,12 @@ public class SeqCode {
      * 
      * @param part part of sequence number which will be increased
      */
-    public void incrementSeqCode(int part) {
+    public void incrementSeqCode(Sources src) {
+       int part = src.source;
        switch (type) {
                case NORMAL:
                case DUPLICATE:
-                       if (part <= -1 || part >= Sources.EDG_WLL_SOURCE_LB_SERVER)
+                       if (src == null)
                                throw new IllegalArgumentException("SeqCode part");
                                seqCode[part-1]++;
                        break;
@@ -81,7 +83,9 @@ public class SeqCode {
                                currentPosition = colonPosition + 1;
                        }
                        break;
-               case CREAM: break;
+               case CREAM: 
+                       cream_code = seqCodeString;
+                       break;
                default: throw new IllegalArgumentException("unsupported seqcode type " + type);
        }
        this.type = type;
@@ -128,7 +132,7 @@ public class SeqCode {
                        output += "0000000000".substring(0, 6 - tmp.length ()) + tmp;
                        return output;
                case CREAM: 
-                       return "no_seqcodes_with_cream";
+                       return cream_code;
                default:
                        throw new IllegalArgumentException("unitialized seqcode");
        }
index bcac17d..6096a0c 100644 (file)
@@ -8,20 +8,34 @@ package org.glite.lb;
  * @author Pavel Piskac (173297@mail.muni.cz)
  */
 public class Sources {
-    public static final int EDG_WLL_SOURCE_NONE = 0;            /* uninitialized value */
-    public static final int EDG_WLL_SOURCE_USER_INTERFACE = 1;
-    public static final int EDG_WLL_SOURCE_NETWORK_SERVER = 2;
-    public static final int EDG_WLL_SOURCE_WORKLOAD_MANAGER = 3;
-    public static final int EDG_WLL_SOURCE_BIG_HELPER = 4;
-    public static final int EDG_WLL_SOURCE_JOB_SUBMISSION = 5;
-    public static final int EDG_WLL_SOURCE_LOG_MONITOR = 6;
-    public static final int EDG_WLL_SOURCE_LRMS = 7;
-    public static final int EDG_WLL_SOURCE_APPLICATION = 8;
-    public static final int EDG_WLL_SOURCE_LB_SERVER = 9;
-    public static final int EDG_WLL_SOURCE_CREAM_CORE = 10;
-    public static final int EDG_WLL_SOURCE_BLAH = 11;
-    
+    public static final int NONE = 0;            /* uninitialized value */
+    public static final int USER_INTERFACE = 1;
+    public static final int NETWORK_SERVER = 2;
+    public static final int WORKLOAD_MANAGER = 3;
+    public static final int BIG_HELPER = 4;
+    public static final int JOB_SUBMISSION = 5;
+    public static final int LOG_MONITOR = 6;
+    public static final int LRMS = 7;
+    public static final int APPLICATION = 8;
+    public static final int LB_SERVER = 9;
+    public static final int CREAM_CORE = 10;
+    public static final int BLAH = 11;
+
     public int source;
+    private String[] names = {
+       "None",
+       "UserInterface",
+       "NetworkServer",
+       "WorkloadManager",
+       "BigHelper",
+       "JobSubmission",
+       "LogMonitor",
+       "LRMS",
+       "Application",
+       "LBServer",
+       "CREAMCore",
+       "BLAH",
+    };
     
     public Sources() {
        this.source = 0;
@@ -36,4 +50,8 @@ public class Sources {
        check(source);
        this.source = source;
     }
+
+    public String toString() {
+       return names[source];
+    }
 }