KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > extensions > pkgmgr > impl > DelegatingExtensionManager


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.impl;
88
89 import java.util.ArrayList JavaDoc;
90 import java.util.Collections JavaDoc;
91
92 import org.codehaus.loom.components.extensions.pkgmgr.ExtensionManager;
93 import org.codehaus.loom.components.extensions.pkgmgr.OptionalPackage;
94 import org.codehaus.loom.extension.Extension;
95
96 /**
97  * A {@link ExtensionManager} that can delegate to multiple different package
98  * repositories.
99  *
100  * @author Peter Donald
101  * @version $Revision: 1.3 $ $Date: 2004/08/17 23:14:32 $
102  */

103 public class DelegatingExtensionManager
104     implements ExtensionManager
105 {
106     /** The list containing the {@link ExtensionManager} objects. */
107     private final ArrayList JavaDoc m_extensionManagers = new ArrayList JavaDoc();
108
109     /**
110      * Default constructor that does not add any repositories.
111      */

112     public DelegatingExtensionManager()
113     {
114     }
115
116     /**
117      * Default constructor that delegates to specified extensionManagers.
118      */

119     public DelegatingExtensionManager(
120         final ExtensionManager[] extensionManagers )
121     {
122         for( int i = 0; i < extensionManagers.length; i++ )
123         {
124             addExtensionManager( extensionManagers[ i ] );
125         }
126     }
127
128     /**
129      * Add a extensionManager to list of repositories delegated to to find
130      * Optional Packages.
131      *
132      * @param extensionManager the extensionManager to add
133      */

134     protected synchronized void addExtensionManager(
135         final ExtensionManager extensionManager )
136     {
137         if( !m_extensionManagers.contains( extensionManager ) )
138         {
139             m_extensionManagers.add( extensionManager );
140         }
141     }
142
143     /**
144      * Add a extensionManager to list of repositories delegated to to find
145      * Optional Packages.
146      *
147      * @param extensionManager the extensionManager to add
148      * @deprecated Use addExtensionManager instead
149      */

150     protected void addPackageRepository(
151         final ExtensionManager extensionManager )
152     {
153         addExtensionManager( extensionManager );
154     }
155
156     /**
157      * Remove a repository from list of repositories delegated to to find
158      * Optional Packages.
159      *
160      * @param repository the repository to remove
161      */

162     protected synchronized void removeExtensionManager(
163         final ExtensionManager repository )
164     {
165         m_extensionManagers.remove( repository );
166     }
167
168     /**
169      * Remove a extensionManager from list of repositories delegated to to find
170      * Optional Packages.
171      *
172      * @param extensionManager the extensionManager to remove
173      * @deprecated Use removeExtensionManager instead.
174      */

175     protected void removePackageRepository(
176         final ExtensionManager extensionManager )
177     {
178         removeExtensionManager( extensionManager );
179     }
180
181     /**
182      * Scan through list of respositories and return all the matching {@link
183      * OptionalPackage} objects that match in any repository.
184      *
185      * @param extension the extension to search for
186      * @return the matching {@link OptionalPackage} objects.
187      */

188     public synchronized OptionalPackage[] getOptionalPackages(
189         final Extension extension )
190     {
191         final ArrayList JavaDoc resultPackages = new ArrayList JavaDoc();
192
193         final int size = m_extensionManagers.size();
194         for( int i = 0; i < size; i++ )
195         {
196             final ExtensionManager repository =
197                 (ExtensionManager)m_extensionManagers.get( i );
198             final OptionalPackage[] packages =
199                 repository.getOptionalPackages( extension );
200             if( null == packages || 0 == packages.length )
201             {
202                 continue;
203             }
204
205             for( int j = 0; j < packages.length; j++ )
206             {
207                 resultPackages.add( packages[ j ] );
208             }
209         }
210
211         final OptionalPackageComparator comparator =
212             new OptionalPackageComparator( extension.getExtensionName() );
213         Collections.sort( resultPackages, comparator );
214         final OptionalPackage[] resultData =
215             new OptionalPackage[ resultPackages.size() ];
216         return (OptionalPackage[])resultPackages.toArray( resultData );
217     }
218 }
219
Popular Tags