KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > libraries > LibraryTypeRegistry


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 package org.netbeans.modules.project.libraries;
20
21 import java.io.IOException JavaDoc;
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.SoftReference JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.spi.project.libraries.LibraryTypeProvider;
27 import org.openide.ErrorManager;
28 import org.openide.cookies.InstanceCookie;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileSystem;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.filesystems.Repository;
33 import org.openide.loaders.DataFolder;
34 import org.openide.loaders.FolderInstance;
35
36 public final class LibraryTypeRegistry extends FolderInstance {
37
38     private static final String JavaDoc REGISTRY = "org-netbeans-api-project-libraries/LibraryTypeProviders"; //NOI18N
39
private static FileObject findProvidersFolder() {
40         FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
41         FileObject folder = sfs.findResource(REGISTRY);
42         if (folder == null) {
43             // #50391 - maybe we are turning this module off?
44
try {
45                 folder = FileUtil.createFolder(sfs.getRoot(), REGISTRY);
46             } catch (IOException JavaDoc e) {
47                 // Hmm, what to do?
48
throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("Cannot make folder " + REGISTRY + ": " + e).initCause(e);
49             }
50         }
51         return folder;
52     }
53     
54     private static Reference JavaDoc<LibraryTypeRegistry> instance;
55
56     private LibraryTypeRegistry () {
57         super(DataFolder.findFolder(findProvidersFolder()));
58     }
59
60     public LibraryTypeProvider[] getLibraryTypeProviders () {
61         try {
62             return (LibraryTypeProvider[]) this.instanceCreate ();
63         } catch (IOException JavaDoc ioe) {
64             ErrorManager.getDefault().notify(ioe);
65         }
66         catch (ClassNotFoundException JavaDoc cnf) {
67             ErrorManager.getDefault().notify(cnf);
68         }
69         return new LibraryTypeProvider[0];
70     }
71
72     public LibraryTypeProvider getLibraryTypeProvider (String JavaDoc libraryType) {
73         assert libraryType != null;
74         try {
75             LibraryTypeProvider[] providers = (LibraryTypeProvider[]) this.instanceCreate ();
76             for (int i = 0; i < providers.length; i++) {
77                 if (libraryType.equals(providers[i].getLibraryType()))
78                     return providers[i];
79             }
80         } catch (IOException JavaDoc ioe) {
81             ErrorManager.getDefault().notify (ioe);
82         }
83         catch (ClassNotFoundException JavaDoc cnfe) {
84             ErrorManager.getDefault().notify (cnfe);
85         }
86         return null;
87     }
88
89
90     protected Object JavaDoc createInstance(InstanceCookie[] cookies) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
91         List JavaDoc<LibraryTypeProvider> installers = new ArrayList JavaDoc<LibraryTypeProvider>(cookies.length);
92         for (InstanceCookie cake : cookies) {
93             LibraryTypeProvider o = null;
94             try {
95                 if (cake instanceof InstanceCookie.Of && !(((InstanceCookie.Of)cake).instanceOf(LibraryTypeProvider.class)))
96                     continue;
97                 o = (LibraryTypeProvider) cake.instanceCreate();
98             } catch (IOException JavaDoc ex) {
99             } catch (ClassNotFoundException JavaDoc ex) {
100             }
101             if (o != null)
102                 installers.add(o);
103         }
104         return installers.toArray(new LibraryTypeProvider[installers.size()]);
105     }
106
107
108     public static synchronized LibraryTypeRegistry getDefault () {
109         LibraryTypeRegistry regs = null;
110         if (instance == null || (regs = instance.get()) == null) {
111             regs = new LibraryTypeRegistry();
112             instance = new SoftReference JavaDoc<LibraryTypeRegistry>(regs);
113         }
114         return regs;
115     }
116
117 }
118
Popular Tags