View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.daemon;
18  
19  import org.apache.jetspeed.services.daemonfactory.DaemonFactory;
20  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
21  import org.apache.jetspeed.services.logging.JetspeedLogger;
22  
23  /***
24  @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
25  @version $Id: DaemonThread.java,v 1.20 2004/02/23 02:48:57 jford Exp $
26  */
27  public class DaemonThread extends Thread {
28  
29      private Daemon daemon = null;
30      
31      /***
32       * Static initialization of the logger for this class
33       */
34      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DaemonThread.class.getName());
35      
36      /***
37      */   
38      public DaemonThread( DaemonEntry entry ) {
39          
40          super( "DaemonThread:" + entry.getName() );
41          try {
42              this.setDaemon(true);
43              this.daemon = DaemonFactory.getDaemon( entry );
44              this.setPriority( Thread.MIN_PRIORITY );
45          } catch (DaemonException e) {
46              //really screwed here.
47              logger.error("Error instantiating DaemonThread", e);
48          }
49      }
50      
51  
52      public DaemonThread()
53      {
54          super();
55          this.setDaemon(true);
56      }
57      
58      /***
59      */
60      public Daemon getDaemon() {
61          return this.daemon;
62      }
63      
64      /***
65      */
66      public void run() {
67          //at the very minimum this daemon is processing...
68          
69          DaemonEntry de = this.getDaemon().getDaemonEntry();
70          
71          logger.info( "DaemonThread: started processing daemon " + de.getName() );
72  
73          if ( de.onStartup() ) {
74              this.runDaemon( this.getDaemon() );
75          }
76          
77  
78          while( true ) {
79  
80              //move the seconds to miliseconds
81              try {
82  
83                  synchronized( this ) {
84                      this.wait( de.getInterval() * 1000 );
85                  }
86  
87              } catch (InterruptedException e) {
88                  //this is a normal situation.  
89                  //the DaemonFactory may want to stop this thread form 
90                  //sleeping and call interrupt() on this thread.
91              }
92  
93              this.runDaemon( this.getDaemon() );
94          
95          }
96      
97  
98      }
99  
100     /***
101     */
102     private void runDaemon( Daemon daemon ) {
103  
104         daemon.setStatus( Daemon.STATUS_PROCESSING );
105 
106         logger.info( "DaemonThread -> PROCESSING daemon -> " + daemon.getDaemonEntry().getName() );
107         
108         try {
109             daemon.run();                        
110         } catch ( Throwable t ) {
111             logger.error( "Could not process Daemon: " + daemon.getDaemonEntry().getName(), t );
112         }
113 
114         logger.info( "DaemonThread -> *DONE* PROCESSING daemon -> " + daemon.getDaemonEntry().getName() );        
115 
116         daemon.setStatus( Daemon.STATUS_PROCESSED );        
117 
118     }
119 
120     
121 }