Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.modules.projectapi; |
41 |
|
42 |
import java.lang.reflect.Constructor; |
43 |
import java.util.ArrayList; |
44 |
import java.util.List; |
45 |
import java.util.Map; |
46 |
import org.netbeans.api.project.Project; |
47 |
import org.netbeans.spi.project.LookupMerger; |
48 |
import org.netbeans.spi.project.LookupProvider; |
49 |
import org.netbeans.spi.project.ProjectServiceProvider; |
50 |
import org.openide.util.Exceptions; |
51 |
import org.openide.util.Lookup; |
52 |
import org.openide.util.Lookup.Template; |
53 |
import org.openide.util.lookup.Lookups; |
54 |
import org.openide.util.lookup.ProxyLookup; |
55 |
|
56 |
/** |
57 |
* Factory methods for lazy {@link LookupProvider} registration. |
58 |
*/ |
59 |
public class LazyLookupProviders { |
60 |
|
61 |
private LazyLookupProviders() {} |
62 |
|
63 |
/** |
64 |
* @see ProjectServiceProvider |
65 |
*/ |
66 |
public static LookupProvider forProjectServiceProvider(Map<String,Object> attrs) throws ClassNotFoundException { |
67 |
Class<?> service = Thread.currentThread().getContextClassLoader().loadClass((String) attrs.get("service")); |
68 |
String implName = (String) attrs.get("class"); |
69 |
return new LazyLookupProvider(service, implName, false); |
70 |
} |
71 |
|
72 |
/** |
73 |
* @see org.netbeans.spi.project.LookupMerger.Registration |
74 |
*/ |
75 |
public static LookupProvider forLookupMerger(Map<String,Object> attrs) throws ClassNotFoundException { |
76 |
Class<?> service = Thread.currentThread().getContextClassLoader().loadClass((String) attrs.get("service")); |
77 |
String implName = (String) attrs.get("class"); |
78 |
return new LazyLookupProvider(service, implName, true); |
79 |
} |
80 |
|
81 |
private static class LazyLookupProvider implements LookupProvider { |
82 |
private final Class<?> service; |
83 |
private final String implName; |
84 |
private final boolean merger; |
85 |
LazyLookupProvider(Class<?> service, String implName, boolean merger) { |
86 |
this.service = service; |
87 |
this.implName = implName; |
88 |
this.merger = merger; |
89 |
} |
90 |
public Lookup createAdditionalLookup(final Lookup lkp) { |
91 |
if (merger) { |
92 |
// must be a separate method so that it can capture the type parameter |
93 |
return proxyMergerLookup(service, implName, lkp); |
94 |
} else { |
95 |
return new ProxyLookup() { |
96 |
boolean inited = false; |
97 |
protected @Override void beforeLookup(Template<?> template) { |
98 |
System.err.println("XXX template.type=" + template.getType()); |
99 |
if (!inited && template.getType() == service) { |
100 |
inited = true; |
101 |
try { |
102 |
Class<?> impl = Thread.currentThread().getContextClassLoader().loadClass(implName); |
103 |
CONSTRUCTOR: for (Constructor c : impl.getConstructors()) { |
104 |
Class[] params = c.getParameterTypes(); |
105 |
if (params.length > 2) { |
106 |
continue; |
107 |
} |
108 |
List<Object> values = new ArrayList<Object>(); |
109 |
for (Class param : params) { |
110 |
if (param == Lookup.class) { |
111 |
values.add(lkp); |
112 |
} else if (param == Project.class) { |
113 |
values.add(lkp.lookup(Project.class)); |
114 |
} else { |
115 |
continue CONSTRUCTOR; |
116 |
} |
117 |
} |
118 |
Object instance = c.newInstance(values.toArray()); |
119 |
service.cast(instance); |
120 |
System.err.println("XXX adding " + instance); |
121 |
setLookups(Lookups.singleton(instance)); |
122 |
} |
123 |
} catch (Exception x) { |
124 |
Exceptions.printStackTrace(x); |
125 |
} |
126 |
} |
127 |
} |
128 |
}; |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
private static <T> Lookup proxyMergerLookup(final Class<T> service, final String implName, final Lookup lkp) { |
134 |
return new ProxyLookup() { |
135 |
boolean mergerInited = false; |
136 |
boolean serviceInited = false; |
137 |
protected @Override void beforeLookup(Template<?> template) { |
138 |
System.err.println("XXX (merger) template.type=" + template.getType()); |
139 |
if (template.getType() == service) { |
140 |
System.err.println("XXX service inited"); |
141 |
serviceInited = true; |
142 |
} |
143 |
if (!mergerInited && template.getType() == LookupMerger.class) { |
144 |
mergerInited = true; |
145 |
System.err.println("XXX adding proxy merger"); |
146 |
setLookups(Lookups.fixed(new LookupMerger<T>() { |
147 |
public Class<T> getMergeableClass() { |
148 |
return service; |
149 |
} |
150 |
public T merge(Lookup lookup) { |
151 |
if (!serviceInited) { |
152 |
System.err.println("XXX service not inited yet, not merging anything"); |
153 |
return null; |
154 |
} |
155 |
System.err.println("XXX merging " + service); |
156 |
try { |
157 |
Class<?> impl = Thread.currentThread().getContextClassLoader().loadClass(implName); |
158 |
LookupMerger<?> m = (LookupMerger<?>) impl.newInstance(); |
159 |
if (m.getMergeableClass() != service) { |
160 |
throw new ClassCastException(); |
161 |
} |
162 |
return service.cast(m.merge(lkp)); |
163 |
} catch (Exception x) { |
164 |
Exceptions.printStackTrace(x); |
165 |
return null; |
166 |
} |
167 |
} |
168 |
})); |
169 |
} |
170 |
} |
171 |
}; |
172 |
} |
173 |
|
174 |
} |