1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.fulcrum.yaafi.framework.configuration;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Properties;
25
26 import org.apache.avalon.framework.configuration.Configurable;
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.context.Context;
30 import org.apache.avalon.framework.context.ContextException;
31 import org.apache.avalon.framework.context.Contextualizable;
32 import org.apache.avalon.framework.logger.LogEnabled;
33 import org.apache.avalon.framework.logger.Logger;
34 import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants;
35 import org.apache.fulcrum.yaafi.framework.util.InputStreamLocator;
36
37
38
39
40
41
42
43 public abstract class ComponentConfigurationPropertiesResolverBaseImpl
44 implements ComponentConfigurationPropertiesResolver, LogEnabled, Contextualizable, Configurable
45 {
46
47 private Logger logger;
48
49
50 private Context context;
51
52
53 private Configuration configuration;
54
55
56
57
58
59 public void enableLogging(Logger logger)
60 {
61 this.logger = logger;
62 }
63
64
65
66
67
68 public void contextualize(Context context) throws ContextException
69 {
70 this.context = context;
71 }
72
73
74
75
76
77 public void configure(Configuration configuration) throws ConfigurationException
78 {
79 this.configuration = configuration;
80 }
81
82
83
84
85 protected Logger getLogger()
86 {
87 return logger;
88 }
89
90
91
92
93 protected Context getContext()
94 {
95 return context;
96 }
97
98
99
100
101 protected File getApplicationRootDir()
102 {
103 try
104 {
105 return (File) this.getContext().get(AvalonYaafiConstants.URN_AVALON_HOME);
106 }
107 catch(Exception e)
108 {
109 throw new RuntimeException(e.getMessage());
110 }
111 }
112
113
114
115
116 protected Configuration getConfiguration()
117 {
118 return configuration;
119 }
120
121
122
123
124 protected String getLocation()
125 {
126 return configuration.getChild("location").getValue(COMPONENT_CONFIG_PROPERTIES_VALUE );
127 }
128
129
130
131
132
133
134
135 protected InputStream createInputStream(String location) throws IOException
136 {
137 InputStreamLocator locator = new InputStreamLocator(this.getApplicationRootDir(), this.getLogger());
138 return locator.locate(location);
139 }
140
141
142
143
144
145
146 protected void addAvalonContext(Properties properties) throws ContextException
147 {
148 properties.put(
149 AvalonYaafiConstants.URN_AVALON_NAME,
150 this.getContext().get(AvalonYaafiConstants.URN_AVALON_NAME)
151 );
152
153 properties.put(
154 AvalonYaafiConstants.URN_AVALON_PARTITION,
155 this.getContext().get(AvalonYaafiConstants.URN_AVALON_PARTITION)
156 );
157
158 properties.put(
159 AvalonYaafiConstants.URN_AVALON_HOME,
160 this.getContext().get(AvalonYaafiConstants.URN_AVALON_HOME)
161 );
162
163 properties.put(
164 AvalonYaafiConstants.URN_AVALON_TEMP,
165 this.getContext().get(AvalonYaafiConstants.URN_AVALON_TEMP)
166 );
167 }
168
169
170
171
172
173
174
175
176 protected Properties loadProperties(String fileLocation) throws Exception
177 {
178 Properties result = new Properties();
179 InputStream is = this.createInputStream(fileLocation);
180
181 try
182 {
183 if(is != null)
184 {
185 result.load(is);
186 is.close();
187 is = null;
188 }
189 else
190 {
191 this.getLogger().debug("Unable to load the following optional file :" + fileLocation);
192 }
193
194 return result;
195 }
196 catch ( Exception e )
197 {
198 String msg = "Unable to parse the following file : " + fileLocation;
199 this.getLogger().error( msg , e );
200 throw e;
201 }
202 }
203 }