KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > meta > FactoryDescriptor


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.repository.meta;
19
20 import javax.naming.directory.Attributes JavaDoc;
21 import javax.naming.directory.Attribute JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23
24 import org.apache.avalon.repository.Artifact;
25 import org.apache.avalon.repository.RepositoryRuntimeException;
26
27 /**
28  * A RelationalDescriptor represents a set of metadata describing the
29  * structural relationships that an artifact has on other
30  * artifacts.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version $Revision: 1.6 $
34  */

35 public class FactoryDescriptor extends ArtifactDescriptor
36 {
37     //-----------------------------------------------------------
38
// static
39
//-----------------------------------------------------------
40

41     public static final String JavaDoc API_KEY =
42       "avalon.artifact.dependency.api";
43
44     public static final String JavaDoc SPI_KEY =
45       "avalon.artifact.dependency.spi";
46
47     public static final String JavaDoc IMP_KEY =
48       "avalon.artifact.dependency";
49
50     public static final String JavaDoc FACTORY_KEY =
51       "avalon.artifact.factory";
52
53     public static final String JavaDoc EXPORT_KEY =
54       "avalon.artifact.export";
55
56     //-----------------------------------------------------------
57
// immutable state
58
//-----------------------------------------------------------
59

60     private final Artifact[] c_api;
61
62     private final Artifact[] c_spi;
63
64     private final Artifact[] c_imp;
65     
66     private final String JavaDoc m_factory;
67
68     private final String JavaDoc m_interface;
69
70     //-----------------------------------------------------------
71
// constructor
72
//-----------------------------------------------------------
73

74     /**
75      * Creates a new RelationalDescriptor.
76      *
77      * @param attributes the meta data attributes
78      */

79     public FactoryDescriptor( final Attributes JavaDoc attributes )
80       throws MetaException
81     {
82         super( attributes );
83
84         c_api = buildDependents( attributes, API_KEY );
85         c_spi = buildDependents( attributes, SPI_KEY );
86         c_imp = buildDependents( attributes, IMP_KEY );
87
88         m_factory = getFactory( attributes );
89         m_interface = getInterface( attributes );
90     }
91
92     //-----------------------------------------------------------
93
// public
94
//-----------------------------------------------------------
95

96
97    /**
98     * Return the factory classname.
99     * @return the classname
100     */

101     public String JavaDoc getFactory()
102     {
103         return m_factory;
104     }
105
106    /**
107     * Return the factory interface.
108     * @return the interface classname
109     */

110     public String JavaDoc getInterface()
111     {
112         return m_interface;
113     }
114
115    /**
116     * Return the implementation dependencies
117     * @return the artifacts
118     */

119     public Artifact[] getDependencies( String JavaDoc key )
120     {
121         if( key == API_KEY )
122         {
123             return c_api;
124         }
125         else if( key == SPI_KEY )
126         {
127             return c_spi;
128         }
129         else if( key == IMP_KEY )
130         {
131             return c_imp;
132         }
133         else
134         {
135             final String JavaDoc error =
136               "Invalid dependency key: " + key;
137             throw new IllegalArgumentException JavaDoc( error );
138         }
139     }
140
141     public Artifact[] getDependencies()
142     {
143         int j = c_api.length + c_spi.length + c_imp.length;
144         Artifact[] all = new Artifact[ j ];
145         int q = 0;
146         for( int i=0; i<c_api.length; i++ )
147         {
148             all[q] = c_api[i];
149             q++;
150         }
151         for( int i=0; i<c_spi.length; i++ )
152         {
153             all[q] = c_spi[i];
154             q++;
155         }
156         for( int i=0; i<c_imp.length; i++ )
157         {
158             all[q] = c_imp[i];
159             q++;
160         }
161         return all;
162     }
163
164     public String JavaDoc toString()
165     {
166         return "[factory:" + m_factory + "]";
167     }
168
169     //-----------------------------------------------------------
170
// private
171
//-----------------------------------------------------------
172

173     private Artifact[] buildDependents(
174       Attributes JavaDoc attributes, String JavaDoc key )
175     {
176         try
177         {
178             Attribute JavaDoc attribute = attributes.get( key ) ;
179             if( null == attribute ) return new Artifact[0];
180
181             Artifact[] dependencies =
182               new Artifact[ attribute.size() ] ;
183             for ( int i = 0; i < dependencies.length; i++ )
184             {
185                 final String JavaDoc spec = (String JavaDoc) attribute.get( i );
186                 dependencies[i] = Artifact.createArtifact( spec ) ;
187             }
188             return dependencies;
189         }
190         catch ( NamingException JavaDoc e )
191         {
192             throw new RepositoryRuntimeException(
193               "Failed to resolve dependencies for [" + key
194               + "] on the attribute set [" + attributes + "].", e ) ;
195         }
196     }
197
198     private String JavaDoc getFactory( Attributes JavaDoc attributes )
199     {
200         try
201         {
202             return getValue( attributes, FACTORY_KEY );
203         }
204         catch( Throwable JavaDoc e )
205         {
206             return null;
207         }
208     }
209
210     private String JavaDoc getInterface( Attributes JavaDoc attributes )
211     {
212         try
213         {
214             return getValue( attributes, EXPORT_KEY );
215         }
216         catch( Throwable JavaDoc e )
217         {
218             return null;
219         }
220     }
221 }
222
Popular Tags