1 19 20 package org.apache.excalibur.instrument.manager.http.server; 21 22 import org.apache.avalon.framework.activity.Startable; 23 24 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable; 25 26 31 abstract class AbstractLogEnabledInstrumentableStartable 32 extends AbstractLogEnabledInstrumentable 33 implements Startable, Runnable 34 { 35 36 private Thread m_runner; 37 38 39 private boolean m_runnerStop = false; 40 41 44 47 public AbstractLogEnabledInstrumentableStartable() 48 { 49 super(); 50 } 51 52 55 60 public void start() 61 throws Exception 62 { 63 getLogger().debug( "Starting..." ); 64 65 m_runner = new Thread ( this, getInstrumentableName() + "_runner" ); 66 m_runner.start(); 67 } 68 69 74 public void stop() 75 throws Exception 76 { 77 getLogger().debug( "Stopping." ); 78 79 Thread runner = m_runner; 80 m_runnerStop = true; 81 if ( runner != null ) 82 { 83 runner.interrupt(); 84 } 85 86 try 88 { 89 stopRunner(); 90 } 91 catch ( Throwable t ) 92 { 93 getLogger().error( "Encountered a problem while stopping the component.", t ); 94 } 95 96 getLogger().debug( "Waiting for runner thread to stop." ); 97 synchronized ( this ) 98 { 99 while ( m_runner != null ) 100 { 101 try 102 { 103 this.wait(); 105 } 106 catch ( InterruptedException e ) 107 { 108 } 110 } 111 } 112 getLogger().debug( "Stopped." ); 113 } 114 115 118 122 public void run() 123 { 124 if ( Thread.currentThread() != m_runner ) 125 { 126 throw new IllegalStateException ( "Private method." ); 127 } 128 129 getLogger().debug( "Runner thread started." ); 130 131 try 132 { 133 try 134 { 135 runner(); 136 } 137 catch ( Throwable t ) 138 { 139 getLogger().warn( 140 "The runner method threw an uncaught exception, runner is terminating,", t ); 141 } 142 } 143 finally 144 { 145 synchronized ( this ) 146 { 147 m_runner = null; 148 149 this.notify(); 151 } 152 153 getLogger().debug( "Runner thread stopped." ); 154 } 155 } 156 157 160 167 protected void stopRunner() 168 throws Exception 169 { 170 } 171 172 178 protected abstract void runner(); 179 180 185 public boolean isStopping() 186 { 187 return m_runnerStop; 188 } 189 } 190 | Popular Tags |