partially working QueryDemo
authorAleš Křenek <ljocha@ics.muni.cz>
Thu, 4 Jun 2009 16:10:25 +0000 (16:10 +0000)
committerAleš Křenek <ljocha@ics.muni.cz>
Thu, 4 Jun 2009 16:10:25 +0000 (16:10 +0000)
org.glite.lb.client-java/Makefile
org.glite.lb.client-java/build.xml
org.glite.lb.client-java/examples/QueryDemo.java [new file with mode: 0644]
org.glite.lb.client-java/examples/SSLClient.java [new file with mode: 0644]
org.glite.lb.client-java/examples/SSLServer.java
org.glite.lb.client-java/examples/SimpleLLTest.java
org.glite.lb.client-java/src/org/glite/lb/ServerConnection.java

index da86271..87790f0 100644 (file)
@@ -1,13 +1,10 @@
 -include Makefile.inc
 
-EXAMPLES := SimpleLLTest.class SSLClient.class SSLServer.class
+EXAMPLES := SimpleLLTest.class SSLClient.class SSLServer.class QueryDemo.class
 # broken: ProducerTestIL.class ProducerTestLL.class
 
 VPATH := examples
 
-certmanagement_jar := $(shell ls ${jglobus_prefix}/lib/cog-certmanagement-*.jar | sort | tail -1)
-jglobus_jar := $(shell ls ${jglobus_prefix}/lib/cog-jglobus-*.jar | sort | tail -1)
-
 all compile:
        JAVA_HOME=${jdk_prefix} \
        ${ant_prefix}/bin/ant -Dno.deps=yes -DstageDir=${stagedir} \
@@ -21,7 +18,7 @@ examples: ${EXAMPLES}
 
 
 ${EXAMPLES}: %.class: %.java
-       javac -cp build/classes/:${stagedir}/share/java/jobid-api-java.jar $<
+       ${jdk_prefix}/bin/javac -cp build/classes/:${stagedir}/share/java/jobid-api-java.jar $<
 
 
 check:
index 84da105..483d39b 100755 (executable)
@@ -11,7 +11,7 @@
                <exec executable="${stageDir}/sbin/glite-lb-at3">
                        <arg line="project/genEventTypes.pl src/org/glite/lb"/>
                </exec>
-               <java fork="true" dir="." classname="org.apache.axis.wsdl.WSDL2Java">
+               <java fork="true" dir="src" classname="org.apache.axis.wsdl.WSDL2Java">
                        <arg value="${stageDir}/interface/LB.wsdl"/>
                        <classpath path="${axis.classpath}"/>
                </java>
diff --git a/org.glite.lb.client-java/examples/QueryDemo.java b/org.glite.lb.client-java/examples/QueryDemo.java
new file mode 100644 (file)
index 0000000..07410ae
--- /dev/null
@@ -0,0 +1,246 @@
+import java.rmi.RemoteException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Iterator;
+import java.util.List;
+import org.glite.jobid.Jobid;
+import org.glite.lb.*;
+import org.glite.wsdl.types.lb.GenericFault;
+import org.glite.wsdl.types.lb.JobStatus;
+import org.glite.wsdl.types.lb.QueryAttr;
+import org.glite.wsdl.types.lb.QueryConditions;
+import org.glite.wsdl.types.lb.QueryOp;
+import org.glite.wsdl.types.lb.QueryRecValue;
+import org.glite.wsdl.types.lb.QueryRecord;
+import org.glite.wsdl.types.lb.StatName;
+import org.glite.wsdl.types.lb.Timeval;
+
+
+
+/**
+ * This is a demonstration class for query API.
+ * It contains all possible methodes that can be called on ServerConnection
+ * and Job objects.
+ * @author Tomas Kramec, 207545@mail.muni.cz
+ */
+public class QueryDemo {
+
+
+    /**
+     * This method serves for formating output information about given job status.
+     * It is only an example of how the data can be presented. It can be changed
+     * by user's needs.
+     *
+     * @param status Job status
+     * @return text representation of the given status
+     */
+    private static String jobStatusToString(JobStatus status) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("State:      "+status.getState()+"\n");
+        sb.append("Job ID:     "+status.getJobId()+"\n");
+        sb.append("Owner:      "+status.getOwner()+"\n");
+        sb.append("Job type:   "+status.getJobtype()+"\n");
+        sb.append("Destination:   "+status.getLocation()+"\n");
+        sb.append("Done code:  "+status.getDoneCode()+"\n");
+        sb.append("User tags:  ");
+        //if there are some user tags write it out.
+        if (status.getUserTags() != null) {
+            for (int i=0;i<status.getUserTags().length;i++) {
+                if (i==0) {
+                    sb.append(status.getUserTags()[i].getTag()+" = "+
+                            status.getUserTags()[i].getValue()+"\n");
+                } else sb.append("            "+status.getUserTags()[i].getTag()+
+                        " = "+ status.getUserTags()[i].getValue()+"\n");
+            }
+        } else sb.append("\n");
+        //Write the time info in a readable form.
+        Calendar calendar = new GregorianCalendar();
+        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
+        calendar.setTimeInMillis(status.getStateEnterTime().getTvSec()*1000);
+
+        sb.append("Enter time: "+ df.format(calendar.getTime())+"\n");
+        calendar.setTimeInMillis(status.getLastUpdateTime().getTvSec()*1000);
+        sb.append("Last update time: "+df.format(calendar.getTime())+"\n");
+        return sb.toString();
+    }
+
+    /**
+     * @param args the command line arguments
+     */
+    public static void main(String[] args) throws RemoteException {
+
+        if (args.length != 7) {
+            System.out.println("To use this class you have to run it with these arguments," +
+                    "in this order: \n" +
+                    " LB server addres in format \"https://somehost:port\"\n"+
+                    " path to the trusted CA's files, default is /etc/grid-security/certificates/\n"+
+                    " -p for proxy based authentication\n" +
+                    "   -c for certificate based authentication\n"+
+                    " proxy certificate or user certificate\n"+
+                    " user key\n"+
+                    " user password\n"+
+                    " jobid for queries in format \"https://somehost:port/uniquePart\"\n"+
+                    "Enter \"\" for empty option.");
+
+        } else {
+         try {
+
+           LBCredentials creds;
+
+            
+            /**
+             * if -p option is choosed set the proxy file for authentication
+             * if -c option is choosed set user certificate, key and password
+             */
+            if (args[2].equals("-p")) {
+               creds = new LBCredentials(args[3],args[1]);
+            } else if (args[2].equals("-c")) {
+               creds = new LBCredentials(args[3],args[4],null,args[1]);
+            } else {
+                System.err.println("Wrong option: "+args[2]+". Only -p and -c is allowed.");
+               return;
+            }
+
+            ServerConnection sc = new ServerConnection(args[0], creds);
+
+            /**
+             * It is also possible to set these attributes of ServerConnection instance:
+             * timeout for server response
+             * limit for job results
+             * limit for event results
+             * flag indicating the way of handling too large results
+             * loggin level for events
+             * LB server addres
+             */
+//            sc.setQueryTimeout(60);
+//            sc.setQueryJobsLimit(100);
+//            sc.setQueryEventsLimit(50);
+//            sc.setQueryResultsFlag(ServerConnection.QueryResultsFlag.LIMITED);
+//            sc.setEventLoggingLevel(Level.LEVEL_DEBUG);
+//            sc.setQueryServerAddress("https://changedHost", 1234);
+
+            //Get the server limit for query results
+            System.out.println("Limit: " + sc.getServerLimit());
+
+            System.out.println();
+            /**
+             * Print all indexed attributes from LB database.
+             * If the attribute is USERTAG then print its name,
+             * if it is TIME then print state name.
+             */
+            System.out.println("Indexed attributes:");
+            List<QueryConditions> idx = sc.getIndexedAttrs();
+            for (int i=0;i<idx.size();i++) {
+                String name = idx.get(i).getAttr().getValue();
+                System.out.print(idx.get(i).getAttr().getValue());
+                if(name.equals("USERTAG"))
+                    System.out.print(" "+ idx.get(0).getTagName());
+                if(name.equals("TIME"))
+                    System.out.print(" "+ idx.get(0).getStatName());
+                System.out.println();
+            }
+
+
+            /**
+             * Print all user's jobs and their states.
+             */
+            System.out.println();
+            System.out.println("-----ALL USER's JOBS and STATES----");
+            //get user jobs
+            List<Jobid> jobs = sc.userJobs();
+            //get their states
+            List<JobStatus> js = sc.userJobStates();
+            Iterator<Jobid> it = jobs.iterator();
+            Iterator<JobStatus> itJs = js.iterator();
+            while (it.hasNext()) {
+                System.out.println(it.next());
+                System.out.println(jobStatusToString(itJs.next()));
+            }
+
+            //Demonstration of Job class
+            System.out.println();
+            System.out.println("----------------JOB----------------");
+
+            //create new Job
+            Job myJob = new Job(args[6], sc);
+            //print job state info
+            System.out.println();
+            System.out.println("Status: " + jobStatusToString(myJob.getStatus(null)));
+
+            //print info about job's events
+            System.out.println();
+            List<Event> events = myJob.getEvents();
+            System.out.println("Found "+events.size()+" events:");
+            for (int i=0;i<events.size();i++) {
+                System.out.println("Event: " + events.get(i).info());
+            }
+
+            /**
+             * This is demonstration of creating and using query conditions.
+             * It prints information about all user's jobs registered in LB
+             * submitted in last 3 days.
+             */
+            if (!jobs.isEmpty()) {
+                System.out.println();
+                System.out.println("-------------CONDITIONS------------");
+
+                //create query record for job ids from the previous query on all user's jobs.
+                List<QueryRecord> recList = new ArrayList<QueryRecord>();
+                for (int i=0;i<jobs.size();i++) {
+//                   if(jobs.get(i).getPort()==sc.getQueryServerPort()) {
+                        QueryRecValue jobIdValue = new QueryRecValue(null, jobs.get(i).toString(), null);
+                        recList.add(new QueryRecord(QueryOp.EQUAL, jobIdValue, null));
+                        System.out.println(jobIdValue.getC());
+//                   } 
+                }
+                QueryRecord[] jobIdRec = new  QueryRecord[]{};
+                jobIdRec = recList.toArray(jobIdRec);
+
+                //crete QueryConditions instance this formula:
+                //(JOBID='jobId1' or JOBID='jobId2 or ...) where jobId1,... are ids of user's jobs
+                QueryConditions condOnJobid = new QueryConditions(QueryAttr.JOBID, null, null, jobIdRec);
+
+                //create query record for time
+                long time = System.currentTimeMillis()/1000;
+                QueryRecValue timeFrom = new QueryRecValue(null, null, new Timeval(time-259200, 0));
+                QueryRecValue timeTo = new QueryRecValue(null, null, new Timeval(System.currentTimeMillis()/1000, 0));
+                QueryRecord[] timeRec = new QueryRecord[]{new QueryRecord(QueryOp.WITHIN, timeFrom, timeTo)};
+                //create QueryConditions instance representing this formula:
+                //(TIME is in <timeFrom, timeTo> interval)
+                QueryConditions condOnTime = new QueryConditions(QueryAttr.TIME, null, StatName.SUBMITTED, timeRec);
+
+                //create QueryConditions list representing this formula:
+                //(JOBID='jobId1' or JOBID='jobId2 or ...) AND (TIME is in <timeFrom, timeTo> interval)
+                //where jobId1,... are ids of user's jobs
+                List<QueryConditions> condList = new ArrayList<QueryConditions>();
+                condList.add(condOnJobid);
+                condList.add(condOnTime);
+
+                //get all jobs matching the given conditions
+                List<Jobid> jobResult = sc.queryJobs(condList);
+                //get all their states
+                List<JobStatus> jobStatesResult = sc.queryJobStates(condList, null);
+
+                //Print information about results
+                Calendar calendar = new GregorianCalendar();
+                DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
+                System.out.println();
+                System.out.print("Jobs registered ");
+                calendar.setTimeInMillis(timeFrom.getT().getTvSec()*1000);
+                System.out.print("from "+ df.format(calendar.getTime())+" ");
+                calendar.setTimeInMillis(timeTo.getT().getTvSec()*1000);
+                System.out.print("to "+ df.format(calendar.getTime())+"\n");
+                Iterator<Jobid> jobsit = jobResult.iterator();
+                Iterator<JobStatus> statusit = jobStatesResult.iterator();
+                while (jobsit.hasNext()) {
+                    System.out.println(jobsit.next());
+                    System.out.println(jobStatusToString(statusit.next()));
+                }
+            }
+          } catch (Exception e) { e.printStackTrace(); }
+        }
+    }
+}
diff --git a/org.glite.lb.client-java/examples/SSLClient.java b/org.glite.lb.client-java/examples/SSLClient.java
new file mode 100644 (file)
index 0000000..f9b21dc
--- /dev/null
@@ -0,0 +1,16 @@
+import org.glite.lb.SSLSend;
+
+public class SSLClient {
+       public static void main(String[] args) {
+
+               SSLSend ssl = new SSLSend();
+
+               try {
+                       ssl.send(args[0],"localhost",9002,100,"baff");
+               }
+               catch (Exception e) {
+                       System.err.println(e);
+               }
+               
+       }
+}
index cae699e..06896ed 100644 (file)
@@ -1,4 +1,5 @@
 import org.glite.lb.SSL;
+import org.glite.lb.LBCredentials;
 import java.io.*;
 import java.net.Socket;
 
@@ -8,7 +9,7 @@ public class SSLServer {
                SSL ssl = new SSL();
 
                try {
-                       ssl.setProxy(args[0]);
+                       ssl.setCredentials(new LBCredentials(args[0],null));
                        Socket sock = ssl.accept(Integer.parseInt(args[1]),100000);
                        System.out.println("accept ok");
                        InputStream in = sock.getInputStream();
index 083fea7..0e1f456 100644 (file)
@@ -11,6 +11,7 @@ public class SimpleLLTest {
 
     public static void main(String[] args) {
 
+     try {
         Jobid jobid = new Jobid("https://skurut68-2.cesnet.cz:9000/paja6_test2");
         SeqCode seqCode = new SeqCode();
         ContextLL ctx = new ContextLL();
@@ -30,5 +31,7 @@ public class SimpleLLTest {
         EventRunning running = new EventRunning();
         running.setNode("node");
         ctx.log(running);
+      }
+      catch (Exception e) { e.printStackTrace(); }
     }
 }
index 2eaadf8..0183755 100644 (file)
@@ -6,7 +6,6 @@ import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.axis.client.Stub;
-import org.apache.log4j.PropertyConfigurator;
 import org.glite.jobid.Jobid;
 import org.glite.lb.Event;
 import org.glite.lb.Level;
@@ -78,7 +77,6 @@ public class ServerConnection {
         if (server == null) throw new IllegalArgumentException("Server cannot be null");
         if (lbCredent == null) throw new IllegalArgumentException("Credentials cannot be null");
 
-        PropertyConfigurator.configure("log4j.properties");
         this.lbCredent = lbCredent;
         setQueryServerAddress(server);
         setQueryTimeout(120);