KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > StandardComponent


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package tutorial;
19
20 import java.io.File JavaDoc;
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.activity.Initializable;
24 import org.apache.avalon.framework.activity.Executable;
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26
27 /**
28  * This is a minimal demonstration component that implements the
29  * <code>BasicService</code> interface and has no dependencies.
30  *
31  * @avalon.component name="standard" lifestyle="singleton"
32  * @avalon.service type="tutorial.StandardService"
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  */

35 public class StandardComponent extends AbstractLogEnabled
36     implements Contextualizable, Initializable, Executable, Disposable, StandardService
37 {
38     private File JavaDoc m_home;
39     private File JavaDoc m_work;
40     private String JavaDoc m_name;
41     private String JavaDoc m_partition;
42     private String JavaDoc m_message;
43     private StandardContext m_context;
44
45     //=======================================================================
46
// Contextualizable
47
//=======================================================================
48

49    /**
50     * Supply of the component context to the component type.
51     *
52     * @param context the context value
53     *
54     * @avalon.context strategy="tutorial.Contextualizable"
55     * @avalon.entry key="urn:avalon:name"
56     * @avalon.entry key="urn:avalon:partition"
57     * @avalon.entry key="urn:avalon:home" type="java.io.File"
58     * @avalon.entry key="urn:avalon:temp" type="java.io.File"
59     */

60     public void contextualize( StandardContext context )
61     {
62         m_context = context;
63         m_home = context.getHomeDirectory();
64         m_work = context.getWorkingDirectory();
65         m_name = context.getName();
66         m_partition = context.getPartitionName();
67     }
68
69     //=======================================================================
70
// Initializable
71
//=======================================================================
72

73     /**
74      * Initialization of the component type by its container.
75      */

76     public void initialize() throws Exception JavaDoc
77     {
78         m_message =
79           " strategy: " + Contextualizable.class.getName()
80           + "\n context: " + m_context.getClass().getName()
81           + "\n home: " + m_home
82           + "\n work: " + m_work
83           + "\n name: " + m_name
84           + "\n partition: " + m_partition;
85     }
86
87     //=======================================================================
88
// Disposable
89
//=======================================================================
90

91     /**
92      * Dispose of the component.
93      */

94     public void dispose()
95     {
96         getLogger().debug( "dispose" );
97     }
98
99     //=======================================================================
100
// Executable
101
//=======================================================================
102

103     /**
104      * Execute the component.
105      */

106     public void execute()
107     {
108         printMessage();
109     }
110
111     //=======================================================================
112
// BasicService
113
//=======================================================================
114

115     /**
116      * Service interface implementation.
117      */

118     public void printMessage()
119     {
120         getLogger().info( "contextualization using a custom strategy\n\n"
121          + m_message + "\n");
122     }
123 }
124
Popular Tags