KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > extension > manager > PackageManager


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.extension.manager;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.avalon.extension.Extension;
24
25 /**
26  * <p>Basic Implementation Of PackageManager Interface used to manage
27  * "Optional Packages" (formerly known as "Standard Extensions").
28  * The "Optional Packages" are stored on file system in a number of
29  * directories.</p>
30  *
31  * <p>TODO: Determine an appropriate interface to this service and
32  * an appropriate mechanism via which to do searching and
33  * expansion of a package set. At that point separate out
34  * implementation and interface for mechanism.</p>
35  *
36  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
37  * @version $Revision: 1.1.1.1 $ $Date: 2004/02/04 17:24:15 $
38  * @see ExtensionManager
39  */

40 public class PackageManager
41 {
42     ///Ordered list of repositories to search in
43
private ExtensionManager m_repository;
44
45     /**
46      * Construct a PackageManager for a repositories.
47      *
48      * @param repository the repository to use in PackageManager
49      */

50     public PackageManager( final ExtensionManager repository )
51     {
52         if( null == repository )
53         {
54             throw new NullPointerException JavaDoc( "repository" );
55         }
56
57         m_repository = repository;
58     }
59
60     /**
61      * Return the {@link OptionalPackage} that provides specified
62      * {@link Extension}. If the specified {@link Extension}
63      * can not be found then <code>null</code> is returned. If there is
64      * multiple implementations that satisfy {@link Extension},
65      * then an {@link OptionalPackage} returned is based on the
66      * following heristic;
67      *
68      * <p>Return the first Optional Package. (This heuristic will
69      * be replaced in time).</p>
70      *
71      * @param extension Description of the extension that needs to be provided by
72      * optional package
73      * @return an array of optional packages that satisfy extension and
74      * the extensions dependencies
75      * @see OptionalPackage
76      * @see Extension
77      */

78     public OptionalPackage getOptionalPackage( final Extension extension )
79     {
80         final OptionalPackage[] packages = m_repository.getOptionalPackages( extension );
81         if( null == packages || 0 == packages.length )
82         {
83             return null;
84         }
85
86         //The best candidate is always returned first so we
87
//can just return it.
88
return packages[ 0 ];
89     }
90
91     /**
92      * Build a list of dependencies based on specified {@link Extension}s.
93      * Each specified {@link Extension} is expected to be a required extension
94      * of another "Optional Package".
95      *
96      * <p>If the required {@link Extension} can not be found locally then
97      * an UnsatisfiedPackageException is thrown. if an {@link OptionalPackage}
98      * is found locally that satisfies specified required {@link Extension}
99      * then it is returned in the array of OptionalPackages. scanDependencies() is then recursively
100      * called on all of the candidates required extensions.</p>
101      *
102      * @param required the array of required Extensions.
103      * @param available the array of Extensions already available to caller.
104      * @return the list of OptionalPackages that satisfy required Extensions
105      * @throws org.apache.avalon.extension.manager.UnsatisfiedExtensionException if extensions could not be satisified
106      * @see #scanDependencies
107      */

108     public OptionalPackage[] scanDependencies( final Extension required,
109                                                final Extension[] available )
110         throws UnsatisfiedExtensionException
111     {
112         return scanDependencies( new Extension[]{required}, available );
113     }
114
115     /**
116      * Build a list of dependencies based on specified {@link Extension}.
117      * The specified {@link Extension} is expected to be a required extension
118      * of another "Optional Package".
119      *
120      * <p>If the required {@link Extension} can not be found locally then
121      * an UnsatisfiedPackageException is thrown. if an {@link OptionalPackage}
122      * is found locally that satisfies specified required {@link Extension}
123      * then it is returned in the array of OptionalPackages. scanDependencies() is then recursively
124      * called on all of the candidates required extensions.</p>
125      *
126      * @param required the array of required Extensions.
127      * @param available the array of Extensions already available to caller.
128      * @return the list of OptionalPackages that satisfy required Extensions
129      * @throws org.apache.avalon.extension.manager.UnsatisfiedExtensionException if extensions could not be satisified
130      * @see #scanDependencies
131      */

132     public OptionalPackage[] scanDependencies( final Extension[] required,
133                                                final Extension[] available )
134         throws UnsatisfiedExtensionException
135     {
136         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
137         final ArrayList JavaDoc unsatisfied = new ArrayList JavaDoc();
138
139         scanDependencies( required, available, dependencies, unsatisfied );
140
141         if( 0 != unsatisfied.size() )
142         {
143             final Extension extension = (Extension)unsatisfied.get( 0 );
144             throw new UnsatisfiedExtensionException( extension );
145         }
146
147         return (OptionalPackage[])dependencies.toArray( new OptionalPackage[ 0 ] );
148     }
149
150     /**
151      * Build a list of dependencies based on specified {@link Extension}s.
152      * Each specified {@link Extension} is expected to be a required extension
153      * of another "Optional Package".
154      *
155      * <p>If the required {@link Extension} can not be found locally then
156      * it is placed in list of unsatisfied Extensions. If a candidate {@link Extension}
157      * is found locally that satisfies specified required {@link Extension}
158      * then it is added to list of dependencies. scanDependencies() is then recursively
159      * called on all of the candidates required extensions.</p>
160      *
161      * @param required the array of required Extensions.
162      * @param available the array of Extensions already available to caller.
163      * @param dependencies the list of dependencies.
164      * @param unsatisfied the list of unsatisfied (ie non-local) dependencies.
165      * @see #scanDependencies
166      */

167     public void scanDependencies( final Extension[] required,
168                                   final Extension[] available,
169                                   final List JavaDoc dependencies,
170                                   final List JavaDoc unsatisfied )
171     {
172         for( int i = 0; i < required.length; i++ )
173         {
174             scanDependencies( required[ i ], available, dependencies, unsatisfied );
175         }
176     }
177
178     /**
179      * Build a list of dependencies based on specified {@link Extension}.
180      * The specified {@link Extension} is expected to be a required extension
181      * of another "Optional Package".
182      *
183      * <p>If the required {@link Extension} can not be found locally then
184      * it is placed in list of unsatisfied Extensions. If a candidate {@link OptionalPackage}
185      * is found locally that satisfies specified required {@link Extension}
186      * then it is added to list of dependencies. scanDependencies() is then recursively
187      * called on all of the candidates required extensions.</p>
188      *
189      * @param required the required Extension.
190      * @param available the array of Extensions already available to caller.
191      * @param dependencies the list of OptionalPackages required to satisfy extension.
192      * @param unsatisfied the list of unsatisfied (ie non-local) dependencies.
193      * @see #scanDependencies
194      */

195     public void scanDependencies( final Extension required,
196                                   final Extension[] available,
197                                   final List JavaDoc dependencies,
198                                   final List JavaDoc unsatisfied )
199     {
200         //Check to see if extension is satisifed by the
201
//list of available extensions passed in
202
for( int i = 0; i < available.length; i++ )
203         {
204             final Extension other = available[ i ];
205             if( other.isCompatibleWith( required ) )
206             {
207                 return;
208             }
209         }
210
211         //Check to see if extension is satisifed by one
212
//of the extensions already found
213
final int size = dependencies.size();
214         for( int i = 0; i < size; i++ )
215         {
216             final OptionalPackage optionalPackage = (OptionalPackage)dependencies.get( i );
217             if( optionalPackage.isCompatible( required ) )
218             {
219                 return;
220             }
221         }
222
223         final OptionalPackage optionalPackage = getOptionalPackage( required );
224         if( null == optionalPackage )
225         {
226             if( !unsatisfied.contains( required ) )
227             {
228                 unsatisfied.add( required );
229             }
230         }
231         else
232         {
233             if( !dependencies.contains( optionalPackage ) )
234             {
235                 dependencies.add( optionalPackage );
236             }
237
238             scanDependencies( optionalPackage.getRequiredExtensions(),
239                               available,
240                               dependencies,
241                               unsatisfied );
242         }
243     }
244 }
245
Popular Tags