KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > configuration > merger > ConfigurationMerger


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.merger;
88
89 import java.util.ArrayList JavaDoc;
90 import java.util.HashSet JavaDoc;
91 import java.util.List JavaDoc;
92 import java.util.Set JavaDoc;
93 import org.codehaus.dna.Configuration;
94 import org.codehaus.dna.ConfigurationException;
95 import org.codehaus.dna.impl.DefaultConfiguration;
96
97 /**
98  * The ConfigurationMerger will take a Configuration object and layer it over
99  * another.
100  *
101  * It will use special attributes on the layer's children to control how
102  * children of the layer and base are combined. In order for a child of the
103  * layer to be merged with a child of the base, the following must hold true:
104  * <ol> <li>The child in the <b>layer</b> Configuration has an attribute named
105  * <code>phoenix-configuration:merge</code> and its value is equal to a boolean
106  * <code>TRUE</code> </li> <li>There must be a single child in both the layer
107  * and base with the same getName() <b>OR</b> there exists an attribute named
108  * <code>phoenix-configuration:key-attribute</code> that names an attribute that
109  * exists on both the layer and base that can be used to match multiple children
110  * of the same getName() </li> </ol>
111  *
112  * @author <a HREF="mailto:proyal@apache.org">Peter Royal</a>
113  * @see ConfigurationSplitter
114  */

115 public class ConfigurationMerger
116 {
117     /**
118      * Merge two configurations.
119      *
120      * @param layer Configuration to <i>layer</i> over the base
121      * @param base Configuration <i>layer</i> will be merged with
122      * @return Result of merge
123      * @throws ConfigurationException if unable to merge
124      */

125     public static Configuration merge( final Configuration layer,
126                                        final Configuration base )
127         throws ConfigurationException
128     {
129         final DefaultConfiguration merged =
130             new DefaultConfiguration( base.getName(),
131                                       base.getPath(),
132                                       "Merged [layer: " +
133                                       layer.getLocation()
134                                       + ", base: " + base.getLocation() + "]" );
135
136         copyAttributes( base, merged );
137         copyAttributes( layer, merged );
138
139         mergeChildren( layer, base, merged );
140
141         final String JavaDoc value = getValue( layer, base );
142         if( null != value )
143         {
144             merged.setValue( value );
145         }
146
147         return merged;
148     }
149
150     private static void mergeChildren( final Configuration layer,
151                                        final Configuration base,
152                                        final DefaultConfiguration merged )
153         throws ConfigurationException
154     {
155         final Configuration[] lc = layer.getChildren();
156         final Configuration[] bc = base.getChildren();
157         final Set JavaDoc baseUsed = new HashSet JavaDoc();
158         final List JavaDoc toMergeUsed = new ArrayList JavaDoc();
159
160         for( int i = 0; i < lc.length; i++ )
161         {
162             final Configuration mergeWith =
163                 getMergePartner( lc[ i ], layer, base );
164
165             if( null == mergeWith )
166             {
167                 toMergeUsed.add( lc[ i ] );
168             }
169             else
170             {
171                 toMergeUsed.add( merge( lc[ i ], mergeWith ) );
172                 baseUsed.add( mergeWith );
173             }
174         }
175
176         for( int i = 0; i < bc.length; i++ )
177         {
178             if( !baseUsed.contains( bc[ i ] ) )
179             {
180                 merged.addChild( bc[ i ] );
181             }
182         }
183
184         final int count = toMergeUsed.size();
185         for( int i = 0; i < count; i++ )
186         {
187             merged.addChild( (Configuration)toMergeUsed.get( i ) );
188         }
189     }
190
191     private static Configuration getMergePartner( final Configuration toMerge,
192                                                   final Configuration layer,
193                                                   final Configuration base )
194         throws ConfigurationException
195     {
196         if( toMerge.getAttributeAsBoolean( Constants.MERGE_ATTR, false ) )
197         {
198             final String JavaDoc keyAttribute = toMerge.getAttribute(
199                 Constants.KEY_ATTR, null );
200             final String JavaDoc keyvalue =
201                 keyAttribute == null ?
202                 null :
203                 toMerge.getAttribute( keyAttribute );
204
205             final Configuration[] layerKids = match( layer,
206                                                      toMerge.getName(),
207                                                      keyAttribute,
208                                                      keyvalue );
209
210             final Configuration[] baseKids = match( base,
211                                                     toMerge.getName(),
212                                                     keyAttribute,
213                                                     keyvalue );
214
215             if( layerKids.length == 1 && baseKids.length == 1 )
216             {
217                 return baseKids[ 0 ];
218             }
219             else
220             {
221                 throw new ConfigurationException( "Unable to merge configuration item, "
222                                                   +
223                                                   "multiple matches on child or base [name: "
224                                                   + toMerge.getName() + "]",
225                                                   toMerge.getPath(),
226                                                   toMerge.getLocation() );
227             }
228         }
229
230         return null;
231     }
232
233     private static String JavaDoc getValue( final Configuration layer,
234                                     final Configuration base )
235     {
236         try
237         {
238             return layer.getValue();
239         }
240         catch( ConfigurationException e )
241         {
242             return base.getValue( null );
243         }
244     }
245
246     private static void copyAttributes( final Configuration source,
247                                         final DefaultConfiguration dest )
248         throws ConfigurationException
249     {
250         final String JavaDoc[] names = source.getAttributeNames();
251
252         for( int i = 0; i < names.length; i++ )
253         {
254             if( !names[ i ].startsWith( Constants.MERGE_METADATA_PREFIX ) )
255             {
256                 dest.setAttribute( names[ i ],
257                                    source.getAttribute( names[ i ] ) );
258             }
259         }
260     }
261
262     /**
263      * Return all occurance of a configuration child containing the supplied
264      * attribute name.
265      *
266      * @param config the configuration
267      * @param element the name of child elements to select from the
268      * configuration
269      * @param attribute the attribute name to filter (null will match any
270      * attribute name)
271      * @return an array of configuration instances matching the query
272      */

273     public static Configuration[] match( final Configuration config,
274                                          final String JavaDoc element,
275                                          final String JavaDoc attribute )
276     {
277         return match( config, element, attribute, null );
278     }
279
280     /**
281      * Return occurance of a configuration child containing the supplied
282      * attribute name and value.
283      *
284      * @param config the configuration
285      * @param element the name of child elements to select from the
286      * configuration
287      * @param attribute the attribute name to filter (null will match any
288      * attribute name )
289      * @param value the attribute value to match (null will match any attribute
290      * value)
291      * @return an array of configuration instances matching the query
292      */

293     public static Configuration[] match( final Configuration config,
294                                          final String JavaDoc element,
295                                          final String JavaDoc attribute,
296                                          final String JavaDoc value )
297     {
298         final ArrayList JavaDoc list = new ArrayList JavaDoc();
299         final Configuration[] children = config.getChildren( element );
300
301         for( int i = 0; i < children.length; i++ )
302         {
303             if( null == attribute )
304             {
305                 list.add( children[ i ] );
306             }
307             else
308             {
309                 final String JavaDoc v =
310                     children[ i ].getAttribute( attribute, null );
311
312                 if( v != null )
313                 {
314                     if( (value == null) || v.equals( value ) )
315                     {
316                         // it's a match
317
list.add( children[ i ] );
318                     }
319                 }
320             }
321         }
322
323         return (Configuration[])list.toArray( new Configuration[ list.size() ] );
324     }
325 }
326
Popular Tags