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:
042: package org.netbeans.modules.gsfpath.platform;
043:
044: import java.io.IOException;
045: import java.lang.ref.*;
046: import java.util.*;
047: import org.netbeans.modules.gsfpath.spi.platform.CustomPlatformInstall;
048: import org.netbeans.modules.gsfpath.spi.platform.GeneralPlatformInstall;
049:
050: import org.openide.cookies.InstanceCookie;
051: import org.openide.filesystems.*;
052: import org.openide.loaders.*;
053: import org.netbeans.modules.gsfpath.spi.platform.PlatformInstall;
054: import org.openide.util.NbCollections;
055:
056: /**
057: * Simple helper class, which keeps track of registered PlatformInstallers.
058: * It caches its [singleton] instance for a while.
059: *
060: * @author Svata Dedic
061: */
062: public class InstallerRegistry {
063: static final String INSTALLER_REGISTRY_FOLDER = "org-netbeans-modules-gsfpath-api/platform/installers"; // NOI18N
064:
065: static Reference<InstallerRegistry> defaultInstance = new WeakReference<InstallerRegistry>(
066: null);
067:
068: private Provider provider;
069: private List<GeneralPlatformInstall> platformInstalls; //Used by unit test
070:
071: InstallerRegistry(FileObject registryResource) {
072: assert registryResource != null;
073: this .provider = new Provider(registryResource);
074: }
075:
076: /**
077: * Used only by unit tests
078: */
079: InstallerRegistry(GeneralPlatformInstall[] platformInstalls) {
080: assert platformInstalls != null;
081: this .platformInstalls = Arrays.asList(platformInstalls);
082: }
083:
084: /**
085: * Returns all registered Java platform installers, in the order as
086: * they are specified by the module layer(s).
087: */
088: public List<PlatformInstall> getInstallers() {
089: return filter(getAllInstallers(), PlatformInstall.class);
090: }
091:
092: public List<CustomPlatformInstall> getCustomInstallers() {
093: return filter(getAllInstallers(), CustomPlatformInstall.class);
094: }
095:
096: public List<GeneralPlatformInstall> getAllInstallers() {
097: if (this .platformInstalls != null) {
098: //In the unit test
099: return platformInstalls;
100: } else {
101: List<GeneralPlatformInstall> list = Collections.emptyList();
102: try {
103: assert this .provider != null;
104: list = NbCollections.checkedListByCopy((List) provider
105: .instanceCreate(),
106: GeneralPlatformInstall.class, true);
107: } catch (IOException ex) {
108: } catch (ClassNotFoundException ex) {
109: }
110: return list;
111: }
112: }
113:
114: /**
115: * Creates/acquires an instance of InstallerRegistry
116: */
117: public static InstallerRegistry getDefault() {
118: InstallerRegistry regs = defaultInstance.get();
119: if (regs != null)
120: return regs;
121: regs = new InstallerRegistry(Repository.getDefault()
122: .getDefaultFileSystem().findResource(
123: INSTALLER_REGISTRY_FOLDER));
124: defaultInstance = new WeakReference<InstallerRegistry>(regs);
125: return regs;
126: }
127:
128: /**
129: * Used only by Unit tests.
130: * Sets the {@link InstallerRegistry#defaultInstance} to the new InstallerRegistry instance which
131: * always returns the given GeneralPlatformInstalls
132: * @return an instance of InstallerRegistry which has to be hold by strong reference during the test
133: */
134: static InstallerRegistry prepareForUnitTest(
135: GeneralPlatformInstall[] platformInstalls) {
136: InstallerRegistry regs = new InstallerRegistry(platformInstalls);
137: defaultInstance = new WeakReference<InstallerRegistry>(regs);
138: return regs;
139: }
140:
141: private static <T> List<T> filter(List<?> list, Class<T> clazz) {
142: List<T> result = new ArrayList<T>(list.size());
143: for (Object item : list) {
144: if (clazz.isInstance(item)) {
145: result.add(clazz.cast(item));
146: }
147: }
148: return result;
149: }
150:
151: private static class Provider extends FolderInstance {
152:
153: Provider(FileObject registryResource) {
154: super (DataFolder.findFolder(registryResource));
155: }
156:
157: protected Object createInstance(InstanceCookie[] cookies)
158: throws java.io.IOException, ClassNotFoundException {
159: List<Object> installers = new ArrayList<Object>(
160: cookies.length);
161: for (int i = 0; i < cookies.length; i++) {
162: InstanceCookie cake = cookies[i];
163: Object o = null;
164: try {
165: if (cake instanceof InstanceCookie.Of
166: && !((((InstanceCookie.Of) cake)
167: .instanceOf(PlatformInstall.class)) || (((InstanceCookie.Of) cake)
168: .instanceOf(CustomPlatformInstall.class))))
169: continue;
170: o = cake.instanceCreate();
171: } catch (IOException ex) {
172: } catch (ClassNotFoundException ex) {
173: }
174: if (o != null)
175: installers.add(o);
176: }
177: return installers;
178: }
179: }
180: }
|