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
* @version 15. 3. 2008
*/
public class Context {
-
+
private int id;
private Sources source;
private int flag;
private Jobid jobid;
private SeqCode seqCode;
private String message;
-
+
/**
* Creates new instance of Context class.
*/
public Context() {
}
-
+
/**
* Creates new instance of Context class.
*
* @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;
this.srcInstance = new CheckedString(srcInstance).toString();
this.jobid = jobid;
}
-
+
/**
* Converts Sources enum constants to defined string
* @param sourceEnum Sources enum constant
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;
}
* @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;
}
* @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;
}
public int getId() {
return id;
}
-
+
/**
* Sets message id.
*
* @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;
* @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;
}
if (prog == null) {
prog = new String("edg-wms");
}
-
+
this.prog = (new CheckedString(prog)).toString();
}
if (seqCode == null) {
throw new IllegalArgumentException("Context seqCode");
}
-
+
this.seqCode = seqCode;
}
* @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;
}
* @param srcInstance srcInstance
*/
public void setSrcInstance(String srcInstance) {
- if (srcInstance == null)
- {
+ if (srcInstance == null) {
srcInstance = new String("");
}
-
+
this.srcInstance = new CheckedString(srcInstance).toString();
}
* @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();
}
}
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=";
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;
/**
*
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
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" +
/* 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)
*/
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);
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");
+
+ }
}