KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > HelloFacility


1 /*
2  * Copyright 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 tutorial;
18
19 import java.io.File JavaDoc;
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 /**
35  * A demonstration of a facility that invoces the runtime creation
36  * and deployment of other components.
37  *
38  * @avalon.component name="hello" lifestyle="singleton"
39  */

40 public class HelloFacility
41   implements Executable
42 {
43
44    //---------------------------------------------------------
45
// immutable state
46
//---------------------------------------------------------
47

48   /**
49    * The logging channel supplied by the container.
50    */

51    private final Logger m_logger;
52
53    //---------------------------------------------------------
54
// mutable state
55
//---------------------------------------------------------
56

57   /**
58    * The containment model (establish via contexualization)
59    */

60    private ContainmentModel m_model;
61
62    //---------------------------------------------------------
63
// constructor
64
//---------------------------------------------------------
65

66   /**
67    * Creation of a new hello facility.
68    *
69    * @param logger a logging channel
70    * @param context the supplied context
71    * @avalon.entry key="urn:composition:containment.model"
72    * type="org.apache.avalon.composition.model.ContainmentModel"
73    * @exception ContextException if a contextualization error occurs
74    */

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    //---------------------------------------------------------
85
// Executable
86
//---------------------------------------------------------
87

88   /**
89    * Request for execution trigger by the container. The implementation
90    * uses the containment model supplied during the contextualization phase
91    * to dynamically respove a reference to a deployment model capable of
92    * supporting the Widget service interface. The implementation uses this
93    * model to instantiate the instance. Subsequent steps in the example
94    * show the decommissining of the widget model, the modification of the
95    * model state (buy updating the models configuration) and the
96    * recommissioning of the model. Finally a new widget instance is
97    * resolved and we can see (via logging messages) that the widget behaviour
98    * has been modified as a result of the modification to the configuration.
99    *
100    * @exception Exception is a runtime error occurs
101    */

102    public void execute() throws Exception JavaDoc
103    {
104        getLogger().info( "looking for a widget" );
105
106        //
107
// create a reference to the widget service and resolve a reference
108
// to a component model using the service reference
109
//
110

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        //
116
// commission the model and resolve a component instance
117
//
118

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        //
132
// create an alternative configuration and apply it to the
133
// widget model
134
//
135

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        //
144
// redeploy the model and create a new instance
145
//
146

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        //
155
// that's enought playing around with configurations - lets
156
// grab a gizmo model and fiddle with its context
157
//
158

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        //
165
// override the standard home context entry with something
166
// we have derived
167
//
168

169        getLogger().info( "building alternative context entry" );
170        model.getContextModel().setEntry(
171          "urn:avalon:home",
172           new File JavaDoc( System.getProperty( "user.dir" ) ) );
173        model.commission();
174        Gizmo gizmo = (Gizmo) model.resolve();
175        getLogger().info( "got gizmo: " + gizmo );
176    }
177
178    //---------------------------------------------------------
179
// private
180
//---------------------------------------------------------
181

182    private Logger getLogger()
183    {
184        return m_logger;
185    }
186 }
187
Popular Tags