1 16 17 package tutorial; 18 19 import java.io.File ; 20 21 import org.apache.avalon.composition.model.ContainmentModel; 22 import org.apache.avalon.composition.model.DeploymentModel; 23 import org.apache.avalon.composition.model.ComponentModel; 24 25 import org.apache.avalon.framework.logger.Logger; 26 import org.apache.avalon.framework.context.Context; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.avalon.framework.activity.Executable; 30 import org.apache.avalon.framework.configuration.DefaultConfiguration; 31 32 import org.apache.avalon.meta.info.ReferenceDescriptor; 33 34 40 public class HelloFacility 41 implements Executable 42 { 43 44 48 51 private final Logger m_logger; 52 53 57 60 private ContainmentModel m_model; 61 62 66 75 public HelloFacility( Logger logger, Context context ) 76 throws ContextException 77 { 78 m_logger = logger; 79 m_model = 80 (ContainmentModel) context.get( 81 "urn:composition:containment.model" ); 82 } 83 84 88 102 public void execute() throws Exception 103 { 104 getLogger().info( "looking for a widget" ); 105 106 111 ReferenceDescriptor reference = new ReferenceDescriptor( Widget.class.getName() ); 112 ComponentModel model = (ComponentModel) m_model.getModel( reference ); 113 getLogger().info( "got a widget model: " + model ); 114 115 119 getLogger().info( "commissioning the widget model" ); 120 model.commission(); 121 Widget widget = (Widget) model.resolve(); 122 getLogger().info( "got the widget: " + widget ); 123 124 getLogger().info( "releasing the widget" ); 125 model.release( widget ); 126 127 getLogger().info( "time for a change" ); 128 getLogger().info( "decommissioning the widget model" ); 129 model.decommission(); 130 131 136 getLogger().info( "building alternative configuration" ); 137 DefaultConfiguration message = new DefaultConfiguration( "message" ); 138 message.setValue( "bonjour!" ); 139 DefaultConfiguration config = new DefaultConfiguration( "config" ); 140 config.addChild( message ); 141 model.setConfiguration( config ); 142 143 147 getLogger().info( "recommissioning the widget model" ); 148 model.commission(); 149 widget = (Widget) model.resolve(); 150 getLogger().info( "got the updated widget: " + widget ); 151 model.release( widget ); 152 model.decommission(); 153 154 159 getLogger().info( "lets play with the gizmo" ); 160 reference = new ReferenceDescriptor( Gizmo.class.getName() ); 161 model = (ComponentModel) m_model.getModel( reference ); 162 getLogger().info( "got a gizmo model: " + model ); 163 164 169 getLogger().info( "building alternative context entry" ); 170 model.getContextModel().setEntry( 171 "urn:avalon:home", 172 new File ( System.getProperty( "user.dir" ) ) ); 173 model.commission(); 174 Gizmo gizmo = (Gizmo) model.resolve(); 175 getLogger().info( "got gizmo: " + gizmo ); 176 } 177 178 182 private Logger getLogger() 183 { 184 return m_logger; 185 } 186 } 187 | Popular Tags |