KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > project > libraries > LibraryManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.project.libraries;
21
22
23 import org.netbeans.api.project.libraries.Library;
24 import org.netbeans.spi.project.libraries.LibraryImplementation;
25 import org.netbeans.spi.project.libraries.LibraryProvider;
26 import org.openide.util.Lookup;
27 import org.openide.util.LookupListener;
28 import org.openide.util.LookupEvent;
29
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.*;
35 import org.netbeans.modules.project.libraries.WritableLibraryProvider;
36 import org.netbeans.spi.project.libraries.LibraryFactory;
37 import org.netbeans.spi.project.libraries.support.LibrariesSupport;
38
39 // XXX make getLibraries return Set not array
40

41 /**
42  * LibraryManager provides registry of the installed libraries.
43  * LibraryManager can be used to list all installed libraries or to
44  * query library by its system name.
45  */

46 public final class LibraryManager {
47
48     public static final String JavaDoc PROP_LIBRARIES = "libraries"; //NOI18N
49

50     private static LibraryManager instance;
51
52     private Lookup.Result<LibraryProvider> result;
53     private Collection<LibraryProvider> currentStorages = new ArrayList<LibraryProvider>();
54     private PropertyChangeListener JavaDoc plistener;
55     private PropertyChangeSupport JavaDoc listeners;
56     private Collection<Library> cache;
57
58
59     private LibraryManager () {
60         this.listeners = new PropertyChangeSupport JavaDoc(this);
61     }
62
63     /**
64      * Returns library by its name.
65      * @param name of the library, must not be null
66      * @return library or null if the library is not found
67      */

68     public Library getLibrary(String JavaDoc name) {
69         assert name != null;
70         Library[] libs = this.getLibraries();
71         for (int i = 0; i < libs.length; i++) {
72             if (name.equals(libs[i].getName())) {
73                 return libs[i];
74             }
75         }
76         return null;
77     }
78
79     /**
80      * List all library defined in the IDE.
81      *
82      * @return Library[] library definitions never <code>null</code>
83      */

84     public synchronized Library[] getLibraries() {
85         if (this.cache == null) {
86             if (this.result == null) {
87                 plistener = new PropertyChangeListener JavaDoc() {
88                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
89                         resetCache ();
90                     }
91                 };
92                 result = Lookup.getDefault().lookupResult(LibraryProvider.class);
93                 result.addLookupListener (new LookupListener() {
94                     public void resultChanged(LookupEvent ev) {
95                             resetCache ();
96                     }
97                 });
98             }
99             List<Library> l = new ArrayList<Library>();
100             Collection<? extends LibraryProvider> instances = result.allInstances();
101             Collection<LibraryProvider> added = new HashSet<LibraryProvider>(instances);
102             added.removeAll (currentStorages);
103             Collection<LibraryProvider> removed = new HashSet<LibraryProvider>(currentStorages);
104             removed.removeAll (instances);
105             currentStorages.clear();
106             for (LibraryProvider storage : instances) {
107                 this.currentStorages.add (storage);
108                 for (LibraryImplementation impl : storage.getLibraries()) {
109                     l.add(LibraryFactory.createLibrary(impl));
110                 }
111             }
112             for (LibraryProvider p : removed) {
113                 p.removePropertyChangeListener(this.plistener);
114             }
115             for (LibraryProvider p : added) {
116                 p.addPropertyChangeListener(this.plistener);
117             }
118             this.cache = l;
119         }
120         return this.cache.toArray(new Library[this.cache.size()]);
121     }
122     
123     
124     /**
125      * Installs a new library into the library manager.
126      * <div class="nonnormative">
127      * <p>
128      * A typical usage would be:
129      * </p>
130      * LibraryManager libraryManager = LibraryManager.getDefault();
131      * LibraryImplementation libImpl = LibrariesSupport.getLibraryTypeProvider("j2se").createLibrary();
132      * libImpl.setName("FooLibTest");
133      * libImpl.setContent ("classpath",listOfResources);
134      * libraryManager.addLibrary(LibraryFactory.createLibrary(libImpl));
135      * </div>
136      * @param library to be installed, the library has to be created
137      * with registered {@link org.netbeans.spi.project.libraries.LibraryTypeProvider}.
138      * @throws IOException when the library cannot be stored
139      * @throws IllegalArgumentException if the library is not recognized by any
140      * {@link org.netbeans.spi.project.libraries.LibraryTypeProvider} or the library
141      * of the same name already exists.
142      * @since org.netbeans.modules.project.libraries/1 1.14
143      */

144     public void addLibrary (final Library library) throws IOException JavaDoc, IllegalArgumentException JavaDoc {
145         assert library != null;
146         if (LibrariesSupport.getLibraryTypeProvider(library.getType()) == null) {
147             throw new IllegalArgumentException JavaDoc ("Trying to add a library of unknown type: " + library.getType()); //NOI18N
148
}
149         String JavaDoc newLibraryName = library.getName();
150         if ( newLibraryName == null || getLibrary(newLibraryName)!= null) {
151             throw new IllegalArgumentException JavaDoc ("Library hasn't name or the name is already used: " + newLibraryName); //NOI18N
152
}
153         final Collection<? extends WritableLibraryProvider> providers = Lookup.getDefault().lookupAll(WritableLibraryProvider.class);
154         assert providers.size() == 1;
155         providers.iterator().next().addLibrary(library.getLibraryImplementation());
156     }
157     
158     /**
159      * Removes installed library
160      * @param library to be removed.
161      * @throws IOException when library cannot be deleted.
162      * @throws IllegalArgumentException when library is not installed in a writable
163      * {@link org.netbeans.spi.project.libraries.LibraryProvider}
164      * @since org.netbeans.modules.project.libraries/1 1.14
165      */

166     public void removeLibrary (final Library library) throws IOException JavaDoc, IllegalArgumentException JavaDoc {
167         assert library != null;
168         final Collection<? extends WritableLibraryProvider> providers = Lookup.getDefault().lookupAll(WritableLibraryProvider.class);
169         assert providers.size() == 1;
170         providers.iterator().next().removeLibrary(library.getLibraryImplementation());
171     }
172
173     /**
174      * Adds PropertyChangeListener.
175      * The listener is notified when library is added or removed.
176      * @param listener to be notified
177      */

178     public synchronized void addPropertyChangeListener (PropertyChangeListener JavaDoc listener) {
179         assert listener != null;
180         this.listeners.addPropertyChangeListener (listener);
181     }
182
183     /**
184      * Removes PropertyChangeListener
185      * @param listener
186      */

187     public void removePropertyChangeListener (PropertyChangeListener JavaDoc listener) {
188         assert listener != null;
189         this.listeners.removePropertyChangeListener (listener);
190     }
191
192
193     private synchronized void resetCache () {
194         this.cache = null;
195         this.listeners.firePropertyChange(PROP_LIBRARIES, null, null);
196     }
197
198
199     /**
200      * Get the default instance of the library manager.
201      * @return the singleton instance
202      */

203     public static synchronized LibraryManager getDefault () {
204         if (instance == null) {
205             instance = new LibraryManager();
206         }
207         return instance;
208     }
209
210
211
212 } // end LibraryManager
213

214
Popular Tags