KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > ComponentMetaDataConverter


1 /*
2  * Copyright (C) The Loom Group. All rights reserved.
3  *
4  * This software is published under the terms of the Loom
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.loom.components.util;
9
10 import java.io.File JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import org.apache.avalon.phoenix.metadata.BlockListenerMetaData;
13 import org.apache.avalon.phoenix.metadata.BlockMetaData;
14 import org.apache.avalon.phoenix.metadata.DependencyMetaData;
15 import org.apache.avalon.phoenix.metadata.SarMetaData;
16 import org.apache.avalon.phoenix.metainfo.BlockInfo;
17 import org.codehaus.loom.components.util.info.ComponentInfo;
18 import org.codehaus.loom.components.util.metadata.ComponentTemplate;
19 import org.codehaus.loom.components.util.profile.ComponentProfile;
20 import org.codehaus.loom.components.util.profile.PartitionProfile;
21 import org.codehaus.loom.interfaces.ContainerConstants;
22
23 /**
24  * Convert a ComponentTemplate into a BlockMetaData.
25  *
26  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
27  * @version $Revision: 1.2 $ $Date: 2004/05/01 12:48:33 $
28  */

29 public class ComponentMetaDataConverter
30 {
31     private ComponentMetaDataConverter()
32     {
33     }
34
35     public static SarMetaData toSarMetaData( final PartitionProfile partition,
36                                              final File JavaDoc homeDirectory )
37     {
38         final PartitionProfile blockPartition =
39             partition.getPartition( ContainerConstants.BLOCK_PARTITION );
40         final PartitionProfile listenerPartition =
41             partition.getPartition( ContainerConstants.LISTENER_PARTITION );
42         final BlockListenerMetaData[] listeners =
43             toBlockListeners( listenerPartition.getMetaData().getComponents() );
44         final BlockMetaData[] blocks =
45             toBlocks( blockPartition.getComponents() );
46         return new SarMetaData( partition.getMetaData().getName(),
47                                 homeDirectory,
48                                 blocks,
49                                 listeners );
50     }
51
52     /**
53      * Convert a set of {@link org.codehaus.loom.components.util.profile.ComponentProfile}
54      * object into a set of BlockMetaData objects.
55      *
56      * @param components the {@link org.codehaus.loom.components.util.profile.ComponentProfile}
57      * objects
58      * @return the BlockMetaData objects
59      */

60     private static BlockMetaData[] toBlocks(
61         final ComponentProfile[] components )
62     {
63         final ArrayList JavaDoc listenerSet = new ArrayList JavaDoc();
64         for( int i = 0; i < components.length; i++ )
65         {
66             final ComponentProfile component = components[ i ];
67             final BlockMetaData block =
68                 toBlockMetaData( component.getTemplate(),
69                                  component.getInfo() );
70             listenerSet.add( block );
71         }
72         return (BlockMetaData[])listenerSet.toArray(
73             new BlockMetaData[ listenerSet.size() ] );
74     }
75
76     /**
77      * Convert a ComponentTemplate object into a {@link BlockListenerMetaData}
78      * object.
79      *
80      * @param listeners the ComponentTemplate object
81      * @return the {@link BlockListenerMetaData} object
82      */

83     public static BlockListenerMetaData[] toBlockListeners(
84         final ComponentTemplate[] listeners )
85     {
86         final ArrayList JavaDoc listenerSet = new ArrayList JavaDoc();
87         for( int i = 0; i < listeners.length; i++ )
88         {
89             final BlockListenerMetaData listener =
90                 new BlockListenerMetaData( listeners[ i ].getName(),
91                                            listeners[ i ].getImplementationKey() );
92             listenerSet.add( listener );
93         }
94         return (BlockListenerMetaData[])listenerSet.toArray(
95             new BlockListenerMetaData[ listenerSet.size() ] );
96     }
97
98     /**
99      * Convert a ComponentTemplate object into a BlockMetaData object.
100      *
101      * @param component the ComponentTemplate object
102      * @return the BlockMetaData object
103      */

104     public static BlockMetaData toBlockMetaData(
105         final ComponentTemplate component,
106         final ComponentInfo info )
107     {
108         final boolean disableProxy = component.isDisableProxy();
109         final DependencyMetaData[] dependencies =
110             toPhoenixDependencys( component.getDependencies() );
111
112         final BlockInfo blockInfo = ComponentInfoConverter.toBlockInfo( info );
113         return new BlockMetaData( component.getName(),
114                                   dependencies,
115                                   disableProxy,
116                                   blockInfo );
117     }
118
119     /**
120      * Convert ContainerKit dependencys to Phoenix dependencys.
121      *
122      * @param dependencies the ContainerKit dependencys
123      * @return the Phoenix dependencys
124      */

125     private static DependencyMetaData[] toPhoenixDependencys(
126         final org.codehaus.loom.components.util.metadata.DependencyDirective[] dependencies )
127     {
128         final ArrayList JavaDoc depends = new ArrayList JavaDoc();
129         for( int i = 0; i < dependencies.length; i++ )
130         {
131             final DependencyMetaData dependency =
132                 new DependencyMetaData( dependencies[ i ].getProviderName(),
133                                         dependencies[ i ].getKey() );
134             depends.add( dependency );
135         }
136         return (DependencyMetaData[])depends.toArray(
137             new DependencyMetaData[ depends.size() ] );
138     }
139 }
140
Popular Tags