You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by ks...@apache.org on 2002/07/19 05:57:35 UTC

cvs commit: xml-xindice/java/src/org/apache/xindice/util Configuration.java

kstaken     2002/07/18 20:57:34

  Modified:    java/src/org/apache/xindice/server Kernel.java
               java/src/org/apache/xindice/util Configuration.java
  Log:
  Applying Fernando's cleanup patches for Kernel and Configuration.
  Submitted by: Fernando Padilla
  Reviewed by: Kimbro Staken
  
  Revision  Changes    Path
  1.5       +70 -120   xml-xindice/java/src/org/apache/xindice/server/Kernel.java
  
  Index: Kernel.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/server/Kernel.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Kernel.java	11 Jul 2002 23:25:28 -0000	1.4
  +++ Kernel.java	19 Jul 2002 03:57:34 -0000	1.5
  @@ -76,7 +76,7 @@
    * the system services.
    */
   
  -public final class Kernel implements Runnable {
  +public final class Kernel {
      private static final String NAME = "name";
      private static final String CLASS = "class";
      private static final String COMMON = "common";
  @@ -93,11 +93,13 @@
   
      private Configuration config = null;
      private Configuration commonConfig = null;
  -   private List tasks = new ArrayList(); // Of TaskInfo
  -   private Task finalizer = null;
  -   private Thread scheduler = null;
  -   private boolean running = false;
      private String serverName = null;
  +   private boolean running = false;
  +
  +   private Map tasks = new HashMap();
  +   private Timer scheduler = new Timer();
  +   private Task finalizer = null;
  +   private Task stayAlive = null;
   
      /* Kernel API manager hooks */
   
  @@ -120,29 +122,24 @@
      
      public Kernel(final String name, boolean exit) {
   
  -       //System.runFinalizersOnExit(true);
  -
  -      // Read in the configuration
  -      Document tmp = null;
  -      try {
  -         DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  -         FileInputStream fis = new FileInputStream(name);
  -         BufferedInputStream bis = new BufferedInputStream(fis, 4096);
  -         tmp = db.parse(fis);
  -         fis.close();
  -      }
  -      catch ( Exception e ) {
  -         System.err.println("\u0007FATAL ERROR: Reading configuration file '"+name+"'");
  -         org.apache.xindice.Debug.printStackTrace(e);
  -         if ( exit ) {
  -             System.exit(1);    
  -         }
  -         else {
  -             throw new RuntimeException("\u0007FATAL ERROR: Reading configuration file '"+name+"'");
  -         }
  -      }
  -      final Document doc = tmp;
  -      config = new Configuration(doc.getDocumentElement(), false); // Read/Write
  +       // Read in the configuration
  +       try {
  +	   DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  +	   FileInputStream fis = new FileInputStream(name);
  +	   Document doc = db.parse(fis);
  +	   fis.close();
  +	   config = new Configuration(doc.getDocumentElement(), false); // Read/Write
  +       }
  +       catch ( Exception e ) {
  +	   System.err.println("\u0007FATAL ERROR: Reading configuration file '"+name+"'");
  +	   org.apache.xindice.Debug.printStackTrace(e);
  +	   if ( exit ) {
  +	       System.exit(1);
  +	   }
  +	   else {
  +	       throw new RuntimeException("\u0007FATAL ERROR: Reading configuration file '"+name+"'");
  +	   }
  +       }
   
         commonConfig = config.getChild(COMMON);
         Configuration serverConfig = config.getChild(SERVER);
  @@ -166,11 +163,6 @@
             }
         }
   
  -      // Start The Scheduling Daemon
  -      scheduler = new Thread(this, "Task Scheduler");
  -      scheduler.setDaemon(!serverConfig.getBooleanAttribute(KEEPALIVE));
  -      scheduler.start();
  -      
         // Garbage Collect And Finalize Every Specified Number Of Seconds
         long gcinterval = serverConfig.getIntAttribute(GCINTERVAL)*1000;
         if ( gcinterval > 0 ) {
  @@ -183,6 +175,16 @@
            addTask(finalizer, gcinterval);
         }
   
  +      {
  +	  stayAlive = new Task() {
  +		  public void runTask() {
  +		      // do nothing, just keep the Timer Thread Alive
  +		  }
  +	      };
  +	  addTask( stayAlive, 60*1000 );
  +      }
  +
  +
         System.out.println();
         System.out.println("Server Running");
         running = true;
  @@ -193,77 +195,8 @@
         return kernel;
      }
      
  -   /**
  -    * run is used to keep the kernel alive with no services running
  -    * and to run interval-scheduled tasks
  -    */
  -   public void run() {
  -      TaskInfo info;
  -      long wait = 5000;
  -      long time = 0;
  -      long done = 0;
  -      long diff = 0;
  -      int size = 0;
  -      int i = 0;
  -      while ( true ) {
  -         try {
  -            wait = 5000;
  -            size = tasks.size();
  -            for ( i = 0; i < size; i++ ) {
  -               info = (TaskInfo)tasks.get(i);
  -               time = System.currentTimeMillis();
  -               if ( time >= info.lastrun+info.interval ) {
  -                  try {
  -                     info.task.runTask();
  -                  }
  -                  catch ( Exception e ) {
  -                     if ( info.error == false ) {
  -                        info.error = true;
  -                        System.err.println("\u0007TASK ERROR: "+e);
  -                     }
  -                  }
  -                  done = System.currentTimeMillis();
  -                  info.lastrun = time;
  -                  diff = done-time;
  -                  if ( info.interval-diff < wait )
  -                     wait = info.interval-diff;
  -               }
  -               else {
  -                  diff = (info.lastrun+info.interval)-time;
  -                  if ( diff > 0 && diff < wait )
  -                     wait = diff;
  -               }
  -               //Thread.yield();
  -            }
  -            Thread.sleep(wait);
  -         }
  -         catch ( Exception e ) {
  -         }
  -      }
  -   }
   
  -   public synchronized void addTask(Task task, long interval) {
  -      int size = tasks.size();
  -      for ( int i = 0; i < size; i++ ) {
  -         TaskInfo info = (TaskInfo)tasks.get(i);
  -         if ( task == info.task ) {
  -            info.interval = interval;
  -            return;
  -         }
  -      }
  -      tasks.add(new TaskInfo(task, interval));
  -   }
   
  -   public synchronized void removeTask(Task task) {
  -      int size = tasks.size();
  -      for ( int i = 0; i < size; i++ ) {
  -         TaskInfo info = (TaskInfo)tasks.get(i);
  -         if ( task == info.task ) {
  -            tasks.remove(i);
  -            return;
  -         }
  -      }
  -   }
   
      public static void disposeOf(Object obj) {
         if ( obj != null && (obj instanceof Disposable) )
  @@ -316,10 +249,14 @@
         System.gc();
         System.runFinalization();
   
  -      // Shut Down The Kernel
  +      // Shut Down The Kernel Scheduler
  +      removeTask( stayAlive );
  +      scheduler.cancel();
  +
  +
         if ( exit ) {
  -          System.exit(exitCode);    
  -      }      
  +	  System.exit( exitCode );    
  +      }
      }
   
      private Object loadAPI(Configuration config) {
  @@ -516,24 +453,37 @@
         return users.listUsers();
      }
   
  -   /**
  -    * TaskInfo
  -    */
  +   public void addTask(Task task, long interval) {
  +      synchronized ( tasks ) {
  +         if ( tasks.containsKey( task ) ) {
  +            removeTask( task );
  +         }
   
  -   private class TaskInfo {
  -      public Task task;
  -      public long interval = 0;
  -      public long lastrun = 0;
  -      public boolean error = false;
  +         TaskTimerTask ttt = new TaskTimerTask( task );
  +         tasks.put( task, ttt );
  +         scheduler.schedule( ttt, 0, interval );
  +      }
  +   }
   
  -      public TaskInfo(Task task, long interval) {
  -         this.task = task;
  -         this.interval = interval;
  -         this.lastrun = System.currentTimeMillis();
  +   public void removeTask(Task task) {
  +      synchronized ( tasks ) {
  +         TaskTimerTask ttt = (TaskTimerTask) tasks.remove( task );
  +         if ( ttt != null ) {
  +            ttt.cancel();
  +         }
         }
      }
  -   
  -   
   
  +   private class TaskTimerTask extends TimerTask {
  +      private Task task;
  +
  +      public TaskTimerTask( Task task ) {
  +         this.task = task;
  +      }
  +
  +      public void run() {
  +         task.runTask();
  +      }
  +   }
   }
   
  
  
  
  1.3       +62 -70    xml-xindice/java/src/org/apache/xindice/util/Configuration.java
  
  Index: Configuration.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/util/Configuration.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Configuration.java	24 Feb 2002 01:20:27 -0000	1.2
  +++ Configuration.java	19 Jul 2002 03:57:34 -0000	1.3
  @@ -74,8 +74,9 @@
   
   public final class Configuration {
      private static final Configuration[] EmptySet = new Configuration[0];
  -   
  -   public static final Map Modified = Collections.synchronizedMap(new HashMap()); // Temporary HACK
  +
  +    // commented out 2002-07-16: not used
  +    //private static final Map Modified = Collections.synchronizedMap(new HashMap()); // Temporary HACK
   
      private Element config = null;
      private boolean readOnly = true;
  @@ -129,9 +130,12 @@
         return config.getAttributes().getLength() > 0;
      }
   
  -   public void setDirty() {
  -      Modified.put(config.getOwnerDocument(), Boolean.TRUE);
  -   }
  +    /*
  +      // commented out 2002-07-16: not doing anything
  +      public void setDirty() {
  +      //Modified.put(config.getOwnerDocument(), Boolean.TRUE);
  +      }
  +    */
   
      /**
       * getAttribute returns an attribute from the Configuration node.
  @@ -420,7 +424,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public boolean getBooleanValue(String name, boolean defValue) {
  +   public boolean getBooleanValue(boolean defValue) {
         try {
            return "[true][yes][1][y][on]".indexOf("["+getValue().toLowerCase()+"]") != -1;
         }
  @@ -434,8 +438,8 @@
       *
       * @return The value
       */
  -   public boolean getBooleanValue(String name) {
  -      return getBooleanValue(name, false);
  +   public boolean getBooleanValue() {
  +      return getBooleanValue(false);
      }
   
      /**
  @@ -444,7 +448,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public int getIntValue(String name, int defValue) {
  +   public int getIntValue(int defValue) {
         try {
            return Integer.parseInt(getValue());
         }
  @@ -458,8 +462,8 @@
       *
       * @return The value
       */
  -   public int getIntValue(String name) {
  -      return getIntValue(name, 0);
  +   public int getIntValue() {
  +      return getIntValue(0);
      }
   
      /**
  @@ -468,7 +472,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public short getShortValue(String name, short defValue) {
  +   public short getShortValue(short defValue) {
         try {
            return Short.parseShort(getValue());
         }
  @@ -482,8 +486,8 @@
       *
       * @return The value
       */
  -   public short getShortValue(String name) {
  -      return getShortValue(name, (short)0);
  +   public short getShortValue() {
  +      return getShortValue((short)0);
      }
   
      /**
  @@ -492,7 +496,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public long getLongValue(String name, long defValue) {
  +   public long getLongValue(long defValue) {
         try {
            return Long.parseLong(getValue());
         }
  @@ -506,8 +510,8 @@
       *
       * @return The value
       */
  -   public long getLongValue(String name) {
  -      return Long.parseLong(getValue());
  +   public long getLongValue() {
  +      return getLongValue(0);
      }
   
      /**
  @@ -516,7 +520,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public float getFloatValue(String name, float defValue) {
  +   public float getFloatValue(float defValue) {
         try {
            return Float.parseFloat(getValue());
         }
  @@ -530,8 +534,8 @@
       *
       * @return The value
       */
  -   public float getFloatValue(String name) {
  -      return getFloatValue(name, (float)0.0);
  +   public float getFloatValue() {
  +      return getFloatValue((float)0.0);
      }
   
      /**
  @@ -540,7 +544,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public double getDoubleValue(String name, double defValue) {
  +   public double getDoubleValue(double defValue) {
         try {
            return Double.parseDouble(getValue());
         }
  @@ -554,8 +558,8 @@
       *
       * @return The value
       */
  -   public double getDoubleValue(String name) {
  -      return getDoubleValue(name, (double)0.0);
  +   public double getDoubleValue() {
  +      return getDoubleValue((double)0.0);
      }
   
      /**
  @@ -564,7 +568,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public byte getByteValue(String name, byte defValue) {
  +   public byte getByteValue(byte defValue) {
         try {
            return Byte.parseByte(getValue());
         }
  @@ -578,8 +582,8 @@
       *
       * @return The value
       */
  -   public byte getByteValue(String name) {
  -      return getByteValue(name, (byte)0);
  +   public byte getByteValue() {
  +      return getByteValue((byte)0);
      }
   
      /**
  @@ -588,7 +592,7 @@
       * @param defValue The default value
       * @return The value
       */
  -   public char getCharValue(String name, char defValue) {
  +   public char getCharValue(char defValue) {
         try {
            return getValue().charAt(0);
         }
  @@ -602,10 +606,15 @@
       *
       * @return The value
       */
  -   public char getCharValue(String name) {
  -      return getCharValue(name, '\0');
  +   public char getCharValue() {
  +      return getCharValue('\0');
      }
   
  +
  +
  +
  +
  +
      /**
       * hasChildren returns whether or not the Configuration node has child
       * Configuration elements.
  @@ -727,6 +736,8 @@
         }
      }
   
  +
  +
      /**
       * isReadOnly returns whether or not this Configuration node is a
       * read-only node.
  @@ -749,7 +760,7 @@
            throw new ReadOnlyException();
         Element elem = config.getOwnerDocument().createElement(name);
         config.appendChild(elem);
  -      setDirty();
  +      //setDirty();
         return new Configuration(elem, readOnly);
      }
   
  @@ -767,7 +778,7 @@
         Node imported = config.getOwnerDocument().importNode(newConfig.config, true);
         config.appendChild(imported);
         newConfig.config = (Element)imported;
  -      setDirty();
  +      //setDirty();
      }
   
      /**
  @@ -780,7 +791,7 @@
            throw new ReadOnlyException();
         if ( config.getParentNode() != null )
            config.getParentNode().removeChild(config);
  -      setDirty();
  +      //setDirty();
      }
   
      /**
  @@ -793,9 +804,12 @@
         if ( readOnly )
            throw new ReadOnlyException();
         config.removeAttribute(name);
  -      setDirty();
  +      //setDirty();
      }
   
  +
  +
  +
      /**
       * setAttribute sets the attribute value for this Configuration.
       *
  @@ -807,7 +821,7 @@
         if ( readOnly )
            throw new ReadOnlyException();
         config.setAttribute(name, value);
  -      setDirty();
  +      //setDirty();
      }
   
      /**
  @@ -818,10 +832,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, boolean value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
      /**
  @@ -832,10 +843,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, int value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
      /**
  @@ -846,10 +854,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, short value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
      /**
  @@ -860,10 +865,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, long value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
      /**
  @@ -874,10 +876,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, float value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
      /**
  @@ -888,10 +887,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, double value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
      /**
  @@ -902,10 +898,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, byte value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
      /**
  @@ -916,12 +909,12 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setAttribute(String name, char value) throws ReadOnlyException {
  -      if ( readOnly )
  -         throw new ReadOnlyException();
  -      config.setAttribute(name, String.valueOf(value));
  -      setDirty();
  +       setAttribute( name, String.valueOf( value ) );
      }
   
  +
  +
  +
      /**
       * setValue sets the value of the Configuration node.
       *
  @@ -938,7 +931,7 @@
            Text text = config.getOwnerDocument().createTextNode(value);
            config.appendChild(text);
         }
  -      setDirty();
  +      //setDirty();
      }
   
      /**
  @@ -948,8 +941,7 @@
       * @throws ReadOnlyException if the Configuration is read-only
       */
      public void setValue(boolean value) throws ReadOnlyException {
  -      setValue( value ? "true"
  -                      : "false" );
  +       setValue( String.valueOf( value ) );
      }
   
      /**