1 package org.codehaus.xfire.loom; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 7 import org.apache.avalon.framework.configuration.Configurable; 8 import org.apache.avalon.framework.configuration.Configuration; 9 import org.apache.avalon.framework.configuration.ConfigurationException; 10 import org.apache.avalon.framework.logger.AbstractLogEnabled; 11 import org.apache.avalon.phoenix.ApplicationEvent; 12 import org.apache.avalon.phoenix.ApplicationListener; 13 import org.apache.avalon.phoenix.BlockEvent; 14 import org.apache.avalon.phoenix.metainfo.BlockInfo; 15 import org.apache.avalon.phoenix.metainfo.ServiceDescriptor; 16 17 /*** 18 * @author <a href="mailto:peter.royal@pobox.com">peter royal</a> 19 */ 20 public class XFireBlockDeployerApplicationListener extends AbstractLogEnabled 21 implements ApplicationListener, Configurable 22 { 23 private ServiceDeployer m_deployer; 24 private Map m_services; 25 26 public void configure( final Configuration configuration ) throws ConfigurationException 27 { 28 final Configuration[] kids = configuration.getChildren( "service" ); 29 30 m_services = new HashMap( kids.length ); 31 32 for( int i = 0; i < kids.length; i++ ) 33 { 34 m_services.put( kids[i].getValue(), null ); 35 } 36 } 37 38 public void applicationFailure( final Exception e ) 39 { 40 } 41 42 public void applicationStarted() 43 { 44 if( null == m_deployer ) 45 { 46 throw new IllegalStateException( "Must provide a " + ServiceDeployer.class.getName() ); 47 } 48 49 final Iterator i = m_services.entrySet().iterator(); 50 51 while( i.hasNext() ) 52 { 53 final Map.Entry entry = (Map.Entry)i.next(); 54 55 if( null == entry.getValue() ) 56 { 57 throw new IllegalStateException( "Unable to deploy configured service: " + entry.getKey() ); 58 } 59 60 if( getLogger().isDebugEnabled() ) getLogger().debug( "Deploying '" + entry.getKey() + "'" ); 61 62 try 63 { 64 m_deployer.deploy( (String)entry.getKey(), entry.getValue() ); 65 } 66 catch( Exception e ) 67 { 68 throw new RuntimeException( "Unable to deploy '" + entry.getKey() + "'", e ); 69 } 70 } 71 } 72 73 public void applicationStarting( final ApplicationEvent event ) throws Exception 74 { 75 } 76 77 public void applicationStopped() 78 { 79 } 80 81 public void applicationStopping() 82 { 83 final Iterator i = m_services.keySet().iterator(); 84 85 while( i.hasNext() ) 86 { 87 final String service = (String)i.next(); 88 89 if( getLogger().isDebugEnabled() ) getLogger().debug( "Undeploying '" + service + "'" ); 90 91 m_deployer.undeploy( service ); 92 } 93 } 94 95 public void blockAdded( final BlockEvent event ) 96 { 97 if( isServiceDeployer( event.getBlockInfo() ) ) 98 { 99 if( null == m_deployer ) 100 { 101 if( getLogger().isDebugEnabled() ) 102 getLogger().debug( "Using '" + event.getName() + "' as ServiceDeployer" ); 103 104 m_deployer = (ServiceDeployer)event.getObject(); 105 } 106 else 107 { 108 throw new RuntimeException( "Duplicate ServiceDeployer found: " + event.getName() ); 109 } 110 } 111 else if( m_services.containsKey( event.getName() ) ) 112 { 113 if( getLogger().isDebugEnabled() ) 114 getLogger().debug( "Marking '" + event.getName() + "' for XFire deploment" ); 115 116 m_services.put( event.getName(), event.getObject() ); 117 } 118 } 119 120 private boolean isServiceDeployer( final BlockInfo blockInfo ) 121 { 122 final ServiceDescriptor[] services = blockInfo.getServices(); 123 124 for( int i = 0; i < services.length; i++ ) 125 { 126 if( ServiceDeployer.class.getName().equals( services[i].getName() ) ) 127 { 128 return true; 129 } 130 } 131 132 return false; 133 } 134 135 public void blockRemoved( final BlockEvent event ) 136 { 137 } 138 }