KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > configuration > FileSystemPersistentConfigurationInterceptor


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.configuration;
88
89 import java.io.File JavaDoc;
90 import java.io.FileOutputStream JavaDoc;
91 import java.io.IOException JavaDoc;
92 import java.util.HashMap JavaDoc;
93 import java.util.Map JavaDoc;
94 import javax.xml.transform.stream.StreamResult JavaDoc;
95
96 import org.codehaus.loom.components.configuration.merger.ConfigurationMerger;
97 import org.codehaus.loom.components.util.ExtensionFileFilter;
98 import org.codehaus.loom.interfaces.ConfigurationInterceptor;
99 import org.codehaus.spice.salt.i18n.ResourceManager;
100 import org.codehaus.spice.salt.i18n.Resources;
101 import org.codehaus.spice.salt.io.FileUtil;
102 import org.codehaus.dna.AbstractLogEnabled;
103 import org.codehaus.dna.Active;
104 import org.codehaus.dna.Configurable;
105 import org.codehaus.dna.Configuration;
106 import org.codehaus.dna.ConfigurationException;
107 import org.codehaus.dna.impl.ConfigurationUtil;
108 import org.xml.sax.InputSource JavaDoc;
109
110 /**
111  * <p> A ConfigurationInterceptor that will store partial configurations on
112  * disk. </p><p> When a Configuration is retrieved from the repository, the
113  * configuration from disk is <i>merged</i> with the configuration from the SAR.
114  * This merge is accompilished via {@link ConfigurationMerger#merge}. </p>
115  *
116  * @author Peter Royal
117  * @see ConfigurationMerger
118  */

119 public class FileSystemPersistentConfigurationInterceptor
120     extends AbstractLogEnabled
121     implements ConfigurationInterceptor, Configurable, Active
122 {
123     private static final Resources REZ =
124         ResourceManager.getPackageResources(
125             FileSystemPersistentConfigurationInterceptor.class );
126
127     private final Map JavaDoc m_persistedConfigurations = new HashMap JavaDoc();
128
129     private File JavaDoc m_storageDirectory;
130     private String JavaDoc m_debugPath;
131
132     public void configure( final Configuration configuration )
133         throws ConfigurationException
134     {
135         final String JavaDoc path =
136             configuration.getChild( "storage-directory" ).getValue();
137         m_storageDirectory = new File JavaDoc( FileUtil.normalize( path ) );
138
139         try
140         {
141             FileUtil.forceMkdir( m_storageDirectory );
142         }
143         catch( IOException JavaDoc e )
144         {
145             final String JavaDoc message = REZ.format( "config.error.dir.invalid",
146                                                m_storageDirectory );
147
148             throw new ConfigurationException( message, e );
149         }
150
151         m_debugPath =
152         configuration.getChild( "debug-output-path" ).getValue( null );
153     }
154
155     public Configuration processConfiguration( final String JavaDoc application,
156                                                final String JavaDoc block,
157                                                final Configuration configuration )
158         throws ConfigurationException
159     {
160         final Configuration processedConfiguration =
161             doProcessConfiguration( application, block, configuration );
162
163         if( null != m_debugPath )
164         {
165             writeDebugConfiguration( application,
166                                      block,
167                                      processedConfiguration );
168         }
169
170         return processedConfiguration;
171     }
172
173     private Configuration doProcessConfiguration( final String JavaDoc application,
174                                                   final String JavaDoc block,
175                                                   final Configuration configuration )
176         throws ConfigurationException
177     {
178         final Configuration persistedConfiguration =
179             (Configuration)m_persistedConfigurations.get(
180                 genKey( application, block ) );
181
182         if( null != persistedConfiguration )
183         {
184             return ConfigurationMerger.merge( persistedConfiguration,
185                                               configuration );
186         }
187         else
188         {
189             return configuration;
190         }
191     }
192
193     public void initialize()
194         throws Exception JavaDoc
195     {
196         loadConfigurations();
197
198         if( null != m_debugPath )
199         {
200             FileUtil.forceMkdir( new File JavaDoc( m_debugPath ) );
201         }
202     }
203
204     public void dispose()
205         throws Exception JavaDoc
206     {
207     }
208
209     private void loadConfigurations()
210         throws Exception JavaDoc
211     {
212         final File JavaDoc[] apps = m_storageDirectory.listFiles(
213             new ConfigurationDirectoryFilter() );
214         for( int i = 0; i < apps.length; i++ )
215         {
216             loadConfigurations( apps[ i ] );
217         }
218     }
219
220     private void loadConfigurations( final File JavaDoc appPath )
221         throws Exception JavaDoc
222     {
223         final String JavaDoc app = appPath.getName();
224         final File JavaDoc[] blocks = appPath.listFiles(
225             new ExtensionFileFilter( ".xml" ) );
226
227         for( int i = 0; i < blocks.length; i++ )
228         {
229             final String JavaDoc block =
230                 blocks[ i ].getName().substring( 0,
231                                                  blocks[ i ].getName().indexOf(
232                                                      ".xml" ) );
233
234             final InputSource JavaDoc input = new InputSource JavaDoc(
235                 blocks[ i ].getAbsolutePath() );
236             final Configuration configuration =
237                 ConfigurationUtil.buildFromXML( input );
238             m_persistedConfigurations.put( genKey( app, block ),
239                                            configuration );
240
241             if( getLogger().isDebugEnabled() )
242             {
243                 getLogger().debug( "Loaded persistent configuration [app: " +
244                                    app
245                                    + ", block: " + block + "]" );
246             }
247         }
248     }
249
250     private String JavaDoc genKey( final String JavaDoc app, final String JavaDoc block )
251     {
252         return app + '-' + block;
253     }
254
255     private void writeDebugConfiguration( final String JavaDoc application,
256                                           final String JavaDoc block,
257                                           final Configuration configuration )
258     {
259         try
260         {
261             final File JavaDoc temp = File.createTempFile(
262                 application + "-" + block + "-",
263                 ".xml",
264                 new File JavaDoc( m_debugPath ) );
265             final StreamResult JavaDoc result =
266                 new StreamResult JavaDoc( new FileOutputStream JavaDoc( temp ) );
267             ConfigurationUtil.serializeToResult( result, configuration );
268
269             if( getLogger().isDebugEnabled() )
270             {
271                 final String JavaDoc message = "Configuration written at: " + temp;
272                 getLogger().debug( message );
273             }
274         }
275         catch( final Exception JavaDoc e )
276         {
277             final String JavaDoc message = "Unable to write debug output";
278             getLogger().error( message, e );
279         }
280     }
281 }
282
Popular Tags