001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.project.libraries;
042:
043: import java.io.IOException;
044: import java.lang.ref.Reference;
045: import java.lang.ref.SoftReference;
046: import java.util.ArrayList;
047: import java.util.List;
048: import org.netbeans.spi.project.libraries.LibraryTypeProvider;
049: import org.openide.ErrorManager;
050: import org.openide.cookies.InstanceCookie;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileSystem;
053: import org.openide.filesystems.FileUtil;
054: import org.openide.filesystems.Repository;
055: import org.openide.loaders.DataFolder;
056: import org.openide.loaders.FolderInstance;
057:
058: public final class LibraryTypeRegistry extends FolderInstance {
059:
060: private static final String REGISTRY = "org-netbeans-api-project-libraries/LibraryTypeProviders"; //NOI18N
061:
062: private static FileObject findProvidersFolder() {
063: FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
064: FileObject folder = sfs.findResource(REGISTRY);
065: if (folder == null) {
066: // #50391 - maybe we are turning this module off?
067: try {
068: folder = FileUtil.createFolder(sfs.getRoot(), REGISTRY);
069: } catch (IOException e) {
070: // Hmm, what to do?
071: throw (IllegalStateException) new IllegalStateException(
072: "Cannot make folder " + REGISTRY + ": " + e)
073: .initCause(e);
074: }
075: }
076: return folder;
077: }
078:
079: private static Reference<LibraryTypeRegistry> instance;
080:
081: private LibraryTypeRegistry() {
082: super (DataFolder.findFolder(findProvidersFolder()));
083: }
084:
085: public LibraryTypeProvider[] getLibraryTypeProviders() {
086: try {
087: return (LibraryTypeProvider[]) this .instanceCreate();
088: } catch (IOException ioe) {
089: ErrorManager.getDefault().notify(ioe);
090: } catch (ClassNotFoundException cnf) {
091: ErrorManager.getDefault().notify(cnf);
092: }
093: return new LibraryTypeProvider[0];
094: }
095:
096: public LibraryTypeProvider getLibraryTypeProvider(String libraryType) {
097: assert libraryType != null;
098: try {
099: LibraryTypeProvider[] providers = (LibraryTypeProvider[]) this
100: .instanceCreate();
101: for (int i = 0; i < providers.length; i++) {
102: if (libraryType.equals(providers[i].getLibraryType()))
103: return providers[i];
104: }
105: } catch (IOException ioe) {
106: ErrorManager.getDefault().notify(ioe);
107: } catch (ClassNotFoundException cnfe) {
108: ErrorManager.getDefault().notify(cnfe);
109: }
110: return null;
111: }
112:
113: protected Object createInstance(InstanceCookie[] cookies)
114: throws IOException, ClassNotFoundException {
115: List<LibraryTypeProvider> installers = new ArrayList<LibraryTypeProvider>(
116: cookies.length);
117: for (InstanceCookie cake : cookies) {
118: LibraryTypeProvider o = null;
119: try {
120: if (cake instanceof InstanceCookie.Of
121: && !(((InstanceCookie.Of) cake)
122: .instanceOf(LibraryTypeProvider.class)))
123: continue;
124: o = (LibraryTypeProvider) cake.instanceCreate();
125: } catch (IOException ex) {
126: } catch (ClassNotFoundException ex) {
127: }
128: if (o != null)
129: installers.add(o);
130: }
131: return installers.toArray(new LibraryTypeProvider[installers
132: .size()]);
133: }
134:
135: public static synchronized LibraryTypeRegistry getDefault() {
136: LibraryTypeRegistry regs = null;
137: if (instance == null || (regs = instance.get()) == null) {
138: regs = new LibraryTypeRegistry();
139: instance = new SoftReference<LibraryTypeRegistry>(regs);
140: }
141: return regs;
142: }
143:
144: }
|