KickJava   Java API By Example, From Geeks To Geeks.

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


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

8 package org.apache.avalon.excalibur.extension;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * Basic Implementation Of PackageManager Interface used to manage
17  * "Optional Packages" (formerly known as "Standard Extensions").
18  * The "Optional Packages" are stored on file system in a number of
19  * directories.
20  *
21  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
22  * @version $Revision: 1.7 $ $Date: 2001/12/11 09:53:34 $
23  * @see PackageRepository
24  */

25 public class PackageManager
26 {
27     ///Ordered list of repositories to search in
28
private PackageRepository m_repository;
29
30     /**
31      * Construct a PackageManager for a repositories.
32      *
33      * @param repository the repository to use in PackageManager
34      */

35     public PackageManager( final PackageRepository repository )
36     {
37         if( null == repository )
38         {
39             throw new NullPointerException JavaDoc( "repository property is null" );
40         }
41
42         m_repository = repository;
43     }
44
45     /**
46      * Return the <code>OptionalPackage</code> that provides specified
47      * <code>Extension</code>. If the specified <code>Extension</code>
48      * can not be found then <code>null</code> is returned. If there is
49      * multiple implementations that satisfy <code>Extension</code>,
50      * then an <code>OptionalPackage</code> returned is based on the
51      * following heristic;
52      *
53      * <p>Return the first Optional Package. (This heuristic will
54      * be replaced in time).</p>
55      *
56      * @param extension Description of the extension that needs to be provided by
57      * optional package
58      * @see OptionalPackage
59      * @see Extension
60      */

61     public OptionalPackage getOptionalPackage( final Extension extension )
62     {
63         final OptionalPackage[] packages = m_repository.getOptionalPackages( extension );
64
65         if( null == packages || 0 == packages.length ) return null;
66
67         //TODO: Use heurisitic to find which is best package
68

69         return packages[ 0 ];
70     }
71
72
73     /**
74      * Build a list of dependencies based on specified <code>Extension</code>s.
75      * Each specified <code>Extension</code> is expected to be a required extension
76      * of another "Optional Package".
77      *
78      * <p>If the required <code>Extension</code> can not be found locally then
79      * an UnsatisfiedPackageException is thrown. if an <code>OptionalPackage</code>
80      * is found locally that satisfies specified required <code>Extension</code>
81      * then it is returned in the array of OptionalPackages. scanDependencies() is then recursively
82      * called on all of the candidates required extensions.</p>
83      *
84      * @param required the array of required Extensions.
85      * @param available the array of Extensions already available to caller.
86      * @return the list of OptionalPackages that satisfy required Extensions
87      * @exception UnsatisfiedPackageException if unable to satisfy all extensions
88      * @see #scanDependencies
89      */

90     public OptionalPackage[] scanDependencies( final Extension required,
91                                                final Extension[] available )
92         throws UnsatisfiedExtensionException
93     {
94         final Extension[] extensions = new Extension[] { required };
95         return scanDependencies( required, available );
96     }
97
98     /**
99      * Build a list of dependencies based on specified <code>Extension</code>.
100      * The specified <code>Extension</code> is expected to be a required extension
101      * of another "Optional Package".
102      *
103      * <p>If the required <code>Extension</code> can not be found locally then
104      * an UnsatisfiedPackageException is thrown. if an <code>OptionalPackage</code>
105      * is found locally that satisfies specified required <code>Extension</code>
106      * then it is returned in the array of OptionalPackages. scanDependencies() is then recursively
107      * called on all of the candidates required extensions.</p>
108      *
109      * @param required the array of required Extensions.
110      * @param available the array of Extensions already available to caller.
111      * @return the list of OptionalPackages that satisfy required Extensions
112      * @exception UnsatisfiedPackageException if unable to satisfy all extensions
113      * @see #scanDependencies
114      */

115     public OptionalPackage[] scanDependencies( final Extension[] required,
116                                                final Extension[] available )
117         throws UnsatisfiedExtensionException
118     {
119         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
120         final ArrayList JavaDoc unsatisfied = new ArrayList JavaDoc();
121
122         scanDependencies( required, available, dependencies, unsatisfied );
123
124         if( 0 != unsatisfied.size() )
125         {
126             final Extension extension = (Extension)unsatisfied.get( 0 );
127             throw new UnsatisfiedExtensionException( extension );
128         }
129
130         return (OptionalPackage[])dependencies.toArray( new OptionalPackage[ 0 ] );
131     }
132
133     /**
134      * Build a list of dependencies based on specified <code>Extension</code>s.
135      * Each specified <code>Extension</code> is expected to be a required extension
136      * of another "Optional Package".
137      *
138      * <p>If the required <code>Extension</code> can not be found locally then
139      * it is placed in list of unsatisfied Extensions. If a candidate <code>Extension</code>
140      * is found locally that satisfies specified required <code>Extension</code>
141      * then it is added to list of dependencies. scanDependencies() is then recursively
142      * called on all of the candidates required extensions.</p>
143      *
144      * @param required the array of required Extensions.
145      * @param available the array of Extensions already available to caller.
146      * @param dependencies the list of dependencies.
147      * @param unsatisfied the list of unsatisfied (ie non-local) dependencies.
148      * @see #scanDependencies
149      */

150     public void scanDependencies( final Extension[] required,
151                                   final Extension[] available,
152                                   final List JavaDoc dependencies,
153                                   final List JavaDoc unsatisfied )
154     {
155         for( int i = 0; i < required.length; i++ )
156         {
157             scanDependencies( required[ i ], available, dependencies, unsatisfied );
158         }
159     }
160
161     /**
162      * Build a list of dependencies based on specified <code>Extension</code>.
163      * The specified <code>Extension</code> is expected to be a required extension
164      * of another "Optional Package".
165      *
166      * <p>If the required <code>Extension</code> can not be found locally then
167      * it is placed in list of unsatisfied Extensions. If a candidate <code>OptionalPackage</code>
168      * is found locally that satisfies specified required <code>Extension</code>
169      * then it is added to list of dependencies. scanDependencies() is then recursively
170      * called on all of the candidates required extensions.</p>
171      *
172      * @param required the required Extension.
173      * @param available the array of Extensions already available to caller.
174      * @param dependencies the list of OptionalPackages required to satisfy extension.
175      * @param unsatisfied the list of unsatisfied (ie non-local) dependencies.
176      * @see #scanDependencies
177      */

178     public void scanDependencies( final Extension required,
179                                   final Extension[] available,
180                                   final List JavaDoc dependencies,
181                                   final List JavaDoc unsatisfied )
182     {
183         //Check to see if extension is satisifed by the
184
//list of available extensions passed in
185
for( int i = 0; i < available.length; i++ )
186         {
187             final Extension other = available[ i ];
188             if( other.isCompatibleWith( required ) )
189             {
190                 return;
191             }
192         }
193
194         //Check to see if extension is satisifed by one
195
//of the extensions already found
196
final int size = dependencies.size();
197         for( int i = 0; i < size; i++ )
198         {
199             final OptionalPackage optionalPackage = (OptionalPackage)dependencies.get( i );
200             if( optionalPackage.isCompatible( required ) )
201             {
202                 return;
203             }
204         }
205
206         final OptionalPackage optionalPackage = getOptionalPackage( required );
207         if( null == optionalPackage )
208         {
209             if( !unsatisfied.contains( required ) )
210             {
211                 unsatisfied.add( required );
212             }
213         }
214         else
215         {
216             if( !dependencies.contains( optionalPackage ) )
217             {
218                 dependencies.add( optionalPackage );
219             }
220             
221             scanDependencies( optionalPackage.getRequiredExtensions(),
222                               available,
223                               dependencies,
224                               unsatisfied );
225         }
226     }
227 }
228
Popular Tags