1 17 package org.apache.avalon.excalibur.component.servlet; 18 19 import org.apache.avalon.excalibur.logger.LoggerManager; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.framework.logger.AbstractLogEnabled; 22 import org.apache.avalon.framework.service.ServiceManager; 23 import org.apache.excalibur.instrument.InstrumentManager; 24 25 44 abstract class AbstractReferenceProxyLatch 45 extends AbstractLogEnabled 46 { 47 48 private String m_name; 49 50 51 private int m_waitingProxies; 52 53 54 private boolean m_triggerRequested; 55 56 59 62 public AbstractReferenceProxyLatch() 63 { 64 } 65 66 69 74 public ReferenceProxy createProxy( Object object, String name ) 75 { 76 m_name = name; 77 78 if( getLogger().isDebugEnabled() ) 79 { 80 getLogger().debug( "Creating a proxy named '" + m_name + "' for a " 81 + object.getClass().getName() ); 82 } 83 84 AbstractReferenceProxy proxy; 85 if( object instanceof LoggerManager ) 86 { 87 proxy = new LoggerManagerReferenceProxy( (LoggerManager)object, this, name ); 88 } 89 else if( object instanceof ServiceManager ) 90 { 91 proxy = new ServiceManagerReferenceProxy( (ServiceManager)object, this, name ); 92 } 93 else if( object instanceof ComponentManager ) 94 { 95 proxy = new ComponentManagerReferenceProxy( (ComponentManager)object, this, name ); 96 } 97 else if( object instanceof InstrumentManager ) 98 { 99 proxy = new InstrumentManagerReferenceProxy( (InstrumentManager)object, this, name ); 100 } 101 else 102 { 103 throw new IllegalArgumentException ( "Don't know how to create a proxy for a " 104 + object.getClass().getName() ); 105 } 106 107 m_waitingProxies++; 108 109 return proxy; 110 } 111 112 117 public void requestTrigger() 118 { 119 int waitingProxies; 120 synchronized( this ) 121 { 122 waitingProxies = m_waitingProxies; 123 } 124 125 if( waitingProxies > 0 ) 126 { 127 System.gc(); 129 130 try 132 { 133 Thread.sleep( 1500 ); 134 } 135 catch( InterruptedException e ) 136 { 137 } 138 } 139 140 synchronized( this ) 141 { 142 m_triggerRequested = true; 143 waitingProxies = m_waitingProxies; 144 } 145 146 if( waitingProxies > 0 ) 147 { 148 if( getLogger().isDebugEnabled() ) 149 { 150 getLogger().debug( "Trigger requested. " + waitingProxies 151 + " proxies have not yet been finalized." ); 152 } 153 } 154 else 155 { 156 if( getLogger().isDebugEnabled() ) 157 { 158 getLogger().debug( "Trigger requested. All proxies have been finalized." ); 159 } 160 161 try 162 { 163 triggered(); 164 } 165 catch( Exception e ) 166 { 167 getLogger().error( "Encountered an unexpected error in the trigger callback:", e ); 168 } 169 } 170 } 171 172 177 void notifyFinalized( AbstractReferenceProxy proxy ) 178 { 179 synchronized( this ) 180 { 181 m_waitingProxies--; 182 183 if( m_waitingProxies > 0 ) 185 { 186 if( getLogger().isDebugEnabled() ) 187 { 188 getLogger().debug( "The proxy named '" + proxy.getName() + "' was finalized. " 189 + m_waitingProxies + " proxies remaining." ); 190 } 191 return; 192 } 193 } 194 195 if ( getLogger().isDebugEnabled() ) 197 { 198 getLogger().debug( "The proxy named '" + proxy.getName() + "' was finalized. " 199 + "All proxies have been finalized." ); 200 } 201 202 if ( m_triggerRequested ) 203 { 204 try 205 { 206 triggered(); 207 } 208 catch ( Exception e ) 209 { 210 getLogger().error( "Encountered an unexpected error in the trigger callback:", e ); 211 } 212 } 213 } 214 215 218 public abstract void triggered() throws Exception ; 219 } 220 | Popular Tags |