KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > InstallerRegistry


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.modules.java.platform;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.ref.*;
24 import java.util.*;
25 import org.netbeans.spi.java.platform.CustomPlatformInstall;
26 import org.netbeans.spi.java.platform.GeneralPlatformInstall;
27
28 import org.openide.cookies.InstanceCookie;
29 import org.openide.filesystems.*;
30 import org.openide.loaders.*;
31 import org.netbeans.spi.java.platform.PlatformInstall;
32
33 /**
34  * Simple helper class, which keeps track of registered PlatformInstallers.
35  * It caches its [singleton] instance for a while.
36  *
37  * @author Svata Dedic
38  */

39 public class InstallerRegistry {
40     static final String JavaDoc INSTALLER_REGISTRY_FOLDER = "org-netbeans-api-java/platform/installers"; // NOI18N
41

42     static Reference defaultInstance = new WeakReference(null);
43     
44     private Provider provider;
45     private List/*<GeneralPlatformInstall>*/ platformInstalls; //Used by unit test
46

47     InstallerRegistry(FileObject registryResource) {
48         assert registryResource != null;
49         this.provider = new Provider (registryResource);
50     }
51     
52     /**
53      * Used only by unit tests
54      */

55     InstallerRegistry (GeneralPlatformInstall[] platformInstalls) {
56         assert platformInstalls != null;
57         this.platformInstalls = Arrays.asList(platformInstalls);
58     }
59     
60     /**
61      * Returns all registered Java platform installers, in the order as
62      * they are specified by the module layer(s).
63      */

64     public List/*<PlatformInstall>*/ getInstallers () {
65         return filter(getAllInstallers(),PlatformInstall.class);
66     }
67     
68     public List/*<CustomPlatformIntall>*/ getCustomInstallers () {
69         return filter(getAllInstallers(),CustomPlatformInstall.class);
70     }
71     
72     public List/*<GeneralPlatformInstall>*/ getAllInstallers () {
73         if (this.platformInstalls != null) {
74             //In the unit test
75
return platformInstalls;
76         }
77         else {
78             Object JavaDoc o = Collections.EMPTY_LIST;
79             try {
80                 assert this.provider != null;
81                 o = provider.instanceCreate();
82             } catch (IOException JavaDoc ex) {
83             } catch (ClassNotFoundException JavaDoc ex) {
84             }
85             return (List) o;
86         }
87     }
88     
89     
90
91     /**
92      * Creates/acquires an instance of InstallerRegistry
93      */

94     public static InstallerRegistry getDefault() {
95         Object JavaDoc o = defaultInstance.get();
96         if (o != null)
97             return (InstallerRegistry)o;
98         o = new InstallerRegistry(Repository.getDefault().getDefaultFileSystem().findResource(
99             INSTALLER_REGISTRY_FOLDER));
100         defaultInstance = new WeakReference(o);
101         return (InstallerRegistry)o;
102     }
103     
104     
105     /**
106      * Used only by Unit tests.
107      * Sets the {@link InstallerRegistry#defaultInstance} to the new InstallerRegistry instance which
108      * always returns the given GeneralPlatformInstalls
109      * @return an instance of InstallerRegistry which has to be hold by strong reference during the test
110      */

111     static InstallerRegistry prepareForUnitTest (GeneralPlatformInstall[] platformInstalls) {
112         InstallerRegistry regs = new InstallerRegistry (platformInstalls);
113         defaultInstance = new WeakReference(regs);
114         return regs;
115     }
116         
117     
118     private static List/*<T>*/ filter (List list, Class JavaDoc/*<T>*/ clazz) {
119         List result = new ArrayList (list.size());
120         for (Iterator it = list.iterator(); it.hasNext();) {
121             Object JavaDoc item = it.next();
122             if (clazz.isInstance(item)) {
123                 result.add (item);
124             }
125         }
126         return result;
127     }
128     
129     private static class Provider extends FolderInstance {
130         
131         Provider (FileObject registryResource) {
132             super(DataFolder.findFolder(registryResource));
133         }
134         
135         
136         protected Object JavaDoc createInstance(InstanceCookie[] cookies) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
137             List installers = new ArrayList(cookies.length);
138             for (int i = 0; i < cookies.length; i++) {
139                 InstanceCookie cake = cookies[i];
140                 Object JavaDoc o = null;
141                 try {
142                     if (cake instanceof InstanceCookie.Of &&
143                         !((((InstanceCookie.Of)cake).instanceOf(PlatformInstall.class)) ||
144                         (((InstanceCookie.Of)cake).instanceOf(CustomPlatformInstall.class))))
145                         continue;
146                     o = cake.instanceCreate();
147                 } catch (IOException JavaDoc ex) {
148                 } catch (ClassNotFoundException JavaDoc ex) {
149                 }
150                 if (o != null)
151                     installers.add(o);
152             }
153             return installers;
154         }
155     }
156 }
157
Popular Tags