package examples.time;

import weblogic.time.common.*;
import weblogic.common.*;
import weblogic.jndi.*;
import javax.naming.*;
import java.util.*;

/**
 * This application connects to a WebLogic Server, and schedules a recurring trigger
 * that executes on the server. The server dynamically loads an instance of
 * the ServerTimer class, repeatedly calling its <i>schedule</i> method to
 * schedule execution of its <i>trigger</i> method. In this example, the timer 
 * starts out at a 0 second delay, but with each event, the delay is increased
 * by 1 second. When it reaches 10, the trigger is cancelled.
 * <p>
 * Assuming that the server is running on port 7001 of the local machine, 
 * you would run this example using the following command:
 * <br>
 * <pre>  java examples.time.ServerTimer t3://localhost:7001</pre>
 * <br>
 * View the server output logs to see the results of the timer execution.
 *
 * @author                    Copyright (c) 1996-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */
public class ServerTimer implements Schedulable, Triggerable {
  
  private int delay;  // in milliseconds
  
  // A default constructor is needed to dynamically instantiate this
  // class on the server.
  
  public ServerTimer() {
    delay = 0;
  }
  
  // Schedulable Interface  
  // When the schedule delay reaches 10000, cancel the timer by returning 0.  
  // Otherwise, reschedule the timer for the current time (t) + the current
  // delay.
  public long schedule(long t) {
    System.out.println("---------------------------------------");
    if (delay > 10000) {
      System.out.println("Cancelling Timer");
      return 0;
    } else {
      System.out.println("Scheduling Next Callback for "+ delay/1000 + " seconds");
      return t+delay;
    }
  }
  
  // Triggerable Interface  
  // Each time the trigger is called, increase the delay.  
  public void trigger(Schedulable sched) {
    System.out.println("Trigger Called");
    delay += 1000;
  }
  
  
  /**
   * Main allows this example to be called from the command line.
   * String argv are the params for the event registration.
   */
  public static void main(String argv[]) 
       throws Exception 
  {
    
    if (argv.length != 1) {
      System.out.println("Usage:   java examples.time.ServerTimer t3url");
      System.out.println("Example: java examples.time.ServerTimer t3://localhost:7001");
      return;
    }
    
    try {
      T3ServicesDef t3s = getT3Services(argv[0]);
      ScheduledTriggerDef std = null;
      
      // Supplying class names to the Scheduler and Trigger wrappers ensures that the
      // scheduled trigger will be loaded on the server rather than the client.
      
      std = t3s.time().getScheduledTrigger(new Scheduler("examples.time.ServerTimer"), 
                                           new Trigger("examples.time.ServerTimer"));
      std.setDaemon( true );
      std.schedule();
      System.out.println("\nTimer Executing on the WebLogic Server (see Server Output Log)\n");
    } catch (NamingException ne) {
      ne.printStackTrace();
    }
    catch (TimeTriggerException ttx) {
      ttx.printStackTrace();
    }
  } 
  
  /**
   * Obtains a reference to a remote T3ServicesDef object via JNDI lookup
   * 
   * @param wlUrl             URL of a WebLogic server hosting the JNDI tree
   * @exception               java.naming.NamingException
   *                          if the JNDI lookup fails
   * @return                  remote T3ServicesDef object
   */
  private static T3ServicesDef getT3Services(String wlUrl) 
       throws javax.naming.NamingException
  {
    T3ServicesDef t3s;
    Hashtable env = new Hashtable();
    env.put(Context.PROVIDER_URL, wlUrl);
    env.put(Context.INITIAL_CONTEXT_FACTORY,
            weblogic.jndi.WLInitialContextFactory.class.getName());
    Context ctx = new InitialContext(env);
    t3s = (T3ServicesDef) ctx.lookup("weblogic.common.T3Services");
    ctx.close();
    return(t3s);
  }
  
}

