KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > extensions > pkgmgr > PackageManager


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.extensions.pkgmgr;
88
89 import java.util.HashSet JavaDoc;
90 import java.util.Iterator JavaDoc;
91 import java.util.Set JavaDoc;
92
93 import org.codehaus.loom.extension.Extension;
94
95 /**
96  * Basic Implementation Of PackageManager Interface used to manage "Optional
97  * Packages" (formerly known as "Standard Extensions"). The "Optional Packages"
98  * are stored on file system in a number of directories.
99  *
100  * @author Peter Donald
101  * @version $Revision: 1.3 $ $Date: 2004/08/17 23:14:32 $
102  * @todo Determine an appropriate interface to this service and an appropriate
103  * mechanism via which to do searching and expansion of a package set. At that
104  * point separate out implementation and interface for mechanism.
105  * @see ExtensionManager
106  */

107 public class PackageManager
108 {
109     ///Ordered list of repositories to search in
110
private ExtensionManager m_repository;
111
112     /**
113      * Construct a PackageManager for a repositories.
114      *
115      * @param repository the repository to use in PackageManager
116      */

117     public PackageManager( final ExtensionManager repository )
118     {
119         if( null == repository )
120         {
121             throw new NullPointerException JavaDoc( "repository" );
122         }
123
124         m_repository = repository;
125     }
126
127     /**
128      * Return the {@link OptionalPackage} that provides specified {@link
129      * Extension}. If the specified {@link Extension} can not be found then
130      * <code>null</code> is returned. If there is multiple implementations that
131      * satisfy {@link Extension}, then an {@link OptionalPackage} returned is
132      * based on the following heristic;
133      *
134      * <p>Return the first Optional Package. (This heuristic will be replaced in
135      * time).</p>
136      *
137      * @param extension Description of the extension that needs to be provided
138      * by optional package
139      * @return an array of optional packages that satisfy extension and the
140      * extensions dependencies
141      * @see OptionalPackage
142      * @see Extension
143      */

144     public OptionalPackage getOptionalPackage( final Extension extension )
145     {
146         final OptionalPackage[] packages = m_repository.getOptionalPackages(
147             extension );
148         if( null == packages || 0 == packages.length )
149         {
150             return null;
151         }
152
153         //The best candidate is always returned first so we
154
//can just return it.
155
return packages[ 0 ];
156     }
157
158     /**
159      * Build a list of dependencies based on specified {@link Extension}s. Each
160      * specified {@link Extension} is expected to be a required extension of
161      * another "Optional Package".
162      *
163      * <p>If the required {@link Extension} can not be found locally then an
164      * UnsatisfiedPackageException is thrown. if an {@link OptionalPackage} is
165      * found locally that satisfies specified required {@link Extension} then it
166      * is returned in the array of OptionalPackages. scanDependencies() is then
167      * recursively called on all of the candidates required extensions.</p>
168      *
169      * @param required the array of required Extensions.
170      * @param available the array of Extensions already available to caller.
171      * @return the list of OptionalPackages that satisfy required Extensions
172      * @throws UnsatisfiedExtensionException if extensions could not be
173      * satisified
174      * @see #scanDependencies
175      */

176     public OptionalPackage[] scanDependencies( final Extension required,
177                                                final Extension[] available )
178         throws UnsatisfiedExtensionException
179     {
180         return scanDependencies( new Extension[]{required}, available );
181     }
182
183     /**
184      * Build a list of dependencies based on specified {@link Extension}. The
185      * specified {@link Extension} is expected to be a required extension of
186      * another "Optional Package".
187      *
188      * <p>If the required {@link Extension} can not be found locally then an
189      * UnsatisfiedPackageException is thrown. if an {@link OptionalPackage} is
190      * found locally that satisfies specified required {@link Extension} then it
191      * is returned in the array of OptionalPackages. scanDependencies() is then
192      * recursively called on all of the candidates required extensions.</p>
193      *
194      * @param required the array of required Extensions.
195      * @param available the array of Extensions already available to caller.
196      * @return the list of OptionalPackages that satisfy required Extensions
197      * @throws UnsatisfiedExtensionException if extensions could not be
198      * satisified
199      * @see #scanDependencies
200      */

201     public OptionalPackage[] scanDependencies( final Extension[] required,
202                                                final Extension[] available )
203         throws UnsatisfiedExtensionException
204     {
205         final Set JavaDoc dependencies = new HashSet JavaDoc();
206         final Set JavaDoc unsatisfied = new HashSet JavaDoc();
207
208         scanDependencies( required, available, dependencies, unsatisfied );
209
210         if( 0 != unsatisfied.size() )
211         {
212             final Extension extension =
213                 (Extension)unsatisfied.iterator().next();
214             throw new UnsatisfiedExtensionException( extension );
215         }
216
217         return (OptionalPackage[])dependencies.toArray(
218             new OptionalPackage[ 0 ] );
219     }
220
221     /**
222      * Build a list of dependencies based on specified {@link Extension}s. Each
223      * specified {@link Extension} is expected to be a required extension of
224      * another "Optional Package".
225      *
226      * <p>If the required {@link Extension} can not be found locally then it is
227      * placed in list of unsatisfied Extensions. If a candidate {@link
228      * Extension} is found locally that satisfies specified required {@link
229      * Extension} then it is added to list of dependencies. scanDependencies()
230      * is then recursively called on all of the candidates required
231      * extensions.</p>
232      *
233      * @param required the array of required Extensions.
234      * @param available the array of Extensions already available to caller.
235      * @param dependencies the list of dependencies.
236      * @param unsatisfied the list of unsatisfied (ie non-local) dependencies.
237      * @see #scanDependencies
238      */

239     public void scanDependencies( final Extension[] required,
240                                   final Extension[] available,
241                                   final Set JavaDoc dependencies,
242                                   final Set JavaDoc unsatisfied )
243     {
244         for( int i = 0; i < required.length; i++ )
245         {
246             scanDependencies( required[ i ],
247                               available,
248                               dependencies,
249                               unsatisfied );
250         }
251     }
252
253     /**
254      * Build a list of dependencies based on specified {@link Extension}. The
255      * specified {@link Extension} is expected to be a required extension of
256      * another "Optional Package".
257      *
258      * <p>If the required {@link Extension} can not be found locally then it is
259      * placed in list of unsatisfied Extensions. If a candidate {@link
260      * OptionalPackage} is found locally that satisfies specified required
261      * {@link Extension} then it is added to list of dependencies.
262      * scanDependencies() is then recursively called on all of the candidates
263      * required extensions.</p>
264      *
265      * @param required the required Extension.
266      * @param available the array of Extensions already available to caller.
267      * @param dependencies the list of OptionalPackages required to satisfy
268      * extension.
269      * @param unsatisfied the list of unsatisfied (ie non-local) dependencies.
270      * @see #scanDependencies
271      */

272     public void scanDependencies( final Extension required,
273                                   final Extension[] available,
274                                   final Set JavaDoc dependencies,
275                                   final Set JavaDoc unsatisfied )
276     {
277         //Check to see if extension is satisifed by the
278
//list of available extensions passed in
279
for( int i = 0; i < available.length; i++ )
280         {
281             final Extension other = available[ i ];
282             if( other.isCompatibleWith( required ) )
283             {
284                 return;
285             }
286         }
287
288         //Check to see if extension is satisifed by one
289
//of the extensions already found
290
final Iterator JavaDoc iterator = dependencies.iterator();
291         while( iterator.hasNext() )
292         {
293             final OptionalPackage optionalPackage = (OptionalPackage)iterator.next();
294             if( optionalPackage.isCompatible( required ) )
295             {
296                 return;
297             }
298         }
299
300         final OptionalPackage optionalPackage = getOptionalPackage( required );
301         if( null == optionalPackage )
302         {
303             if( !unsatisfied.contains( required ) )
304             {
305                 unsatisfied.add( required );
306             }
307         }
308         else
309         {
310             if( !dependencies.contains( optionalPackage ) )
311             {
312                 dependencies.add( optionalPackage );
313             }
314
315             scanDependencies( optionalPackage.getRequiredExtensions(),
316                               available,
317                               dependencies,
318                               unsatisfied );
319         }
320     }
321 }
322
Popular Tags