KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > infobuilder > MetaClassBlockInfoReader


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.infobuilder;
9
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.codehaus.loom.components.util.info.ComponentInfo;
15 import org.codehaus.loom.components.util.info.DependencyDescriptor;
16 import org.codehaus.loom.components.util.info.SchemaDescriptor;
17 import org.codehaus.loom.components.util.info.ServiceDescriptor;
18 import org.codehaus.metaclass.Attributes;
19 import org.codehaus.metaclass.model.Attribute;
20
21 /**
22  * A BlockInfoReader is responsible for building
23  * ComponentInfo objects from DNA MetaClass descriptors.
24  *
25  * @author Peter Donald
26  * @version $Revision: 1.4 $ $Date: 2004/12/22 06:57:40 $
27  */

28 public final class MetaClassBlockInfoReader implements BlockInfoReader
29 {
30     public ComponentInfo buildComponentInfo( final Class JavaDoc type )
31         throws Exception JavaDoc
32     {
33         final Attribute attribute =
34           Attributes.getAttribute( type, "dna.component" );
35         if( null == attribute )
36         {
37             final String JavaDoc message =
38                 "Type " +
39                 type.getName() +
40                 " does not specify the " +
41                 "required MetaClass attributes to be a Component";
42             throw new Exception JavaDoc( message );
43         }
44         final ServiceDescriptor[] services = buildServices( type );
45
46         final DependencyDescriptor[] dependencies =
47             buildDependencies( type );
48
49         final SchemaDescriptor schema = buildConfigurationSchema( type );
50
51         return new ComponentInfo( type,
52                                   services,
53                                   dependencies,
54                                   schema );
55     }
56
57     /**
58      * A utility method to build a descriptor for SchemaDescriptor,
59      *
60      * @param type the component type
61      *
62      * @return the a descriptor for the SchemaDescriptor,
63      */

64     private SchemaDescriptor buildConfigurationSchema( final Class JavaDoc type )
65     {
66         final Class JavaDoc[] types =
67           new Class JavaDoc[]{org.apache.avalon.framework.configuration.Configuration.class};
68         try
69         {
70             final Method JavaDoc method = type.getMethod( "configure", types );
71             final Attribute attribute =
72                 Attributes.getAttribute( method, "dna.configuration" );
73             if( null == attribute )
74             {
75                 return null;
76             }
77             final String JavaDoc location = attribute.getParameter( "location" );
78             final String JavaDoc schemaType = attribute.getParameter( "type" );
79             return new SchemaDescriptor( location, schemaType );
80         }
81         catch( NoSuchMethodException JavaDoc e )
82         {
83             return null;
84         }
85     }
86
87     /**
88      * A utility method to build an array of
89      * DependencyDescriptor objects from specified
90      * configuration and classname.
91      *
92      * @param type the component type
93      *
94      * @return the created DependencyDescriptor
95      */

96     private DependencyDescriptor[] buildDependencies( final Class JavaDoc type )
97     {
98         final Method JavaDoc method = getDependencyMethod( type );
99         if( null == method )
100         {
101             return DependencyDescriptor.EMPTY_SET;
102         }
103         final List JavaDoc deps = new ArrayList JavaDoc();
104
105         final Attribute[] attributes =
106             Attributes.getAttributes( method, "dna.dependency" );
107         for( int i = 0; i < attributes.length; i++ )
108         {
109             final Attribute attribute = attributes[i];
110             final String JavaDoc key = attribute.getParameter( "key" );
111             final String JavaDoc depType = attribute.getParameter( "type" );
112             final boolean optional =
113               attribute.getParameter( "optional", "" ).equals( "true" );
114             deps.add( new DependencyDescriptor( key, depType, optional ) );
115         }
116
117         return (DependencyDescriptor[])deps.toArray( DependencyDescriptor.EMPTY_SET );
118     }
119
120     /**
121      * Return the method that dependency information attached to.
122      *
123      * @param type the component type
124      *
125      * @return the method that dependency information attached to.
126      */

127     private Method JavaDoc getDependencyMethod( final Class JavaDoc type )
128     {
129         try
130         {
131             final Class JavaDoc[] types1 =
132               new Class JavaDoc[]{org.apache.avalon.framework.component.ComponentManager.class};
133             return type.getMethod( "compose", types1 );
134         }
135         catch( final NoSuchMethodException JavaDoc nsme )
136         {
137         }
138         try
139         {
140             final Class JavaDoc[] types2 =
141               new Class JavaDoc[]{org.apache.avalon.framework.service.ServiceManager.class};
142             return type.getMethod( "service", types2 );
143         }
144         catch( NoSuchMethodException JavaDoc e )
145         {
146         }
147         return null;
148     }
149
150     /**
151      * A utility method to build an array of
152      * ServiceDescriptor objects from specified
153      * configuraiton.
154      *
155      * @param type the type
156      *
157      * @return the created ServiceDescriptor
158      */

159     private ServiceDescriptor[] buildServices( final Class JavaDoc type )
160     {
161         final List JavaDoc services = new ArrayList JavaDoc();
162
163         final Attribute[] attributes =
164           Attributes.getAttributes( type, "dna.service" );
165         for( int i = 0; i < attributes.length; i++ )
166         {
167             final Attribute attribute = attributes[i];
168             final String JavaDoc serviceType = attribute.getParameter( "type" );
169             services.add( new ServiceDescriptor( serviceType ) );
170         }
171
172         return (ServiceDescriptor[])services.toArray( ServiceDescriptor.EMPTY_SET );
173     }
174 }
175
Popular Tags