44 import org.symbian.tools.tmw.core.projects.ITMWProject; |
44 import org.symbian.tools.tmw.core.projects.ITMWProject; |
45 import org.symbian.tools.tmw.core.runtimes.IMobileWebRuntime; |
45 import org.symbian.tools.tmw.core.runtimes.IMobileWebRuntime; |
46 |
46 |
47 @SuppressWarnings("restriction") |
47 @SuppressWarnings("restriction") |
48 public class RuntimeClasspathManager { |
48 public class RuntimeClasspathManager { |
|
49 private static final class Version implements IVersion { |
|
50 private final String version; |
|
51 |
|
52 public Version(String version) { |
|
53 this.version = version; |
|
54 } |
|
55 |
|
56 public int compareTo(Object o) { |
|
57 return version.compareTo(((IVersion) o).getVersionString()); |
|
58 } |
|
59 |
|
60 @Override |
|
61 public boolean equals(Object obj) { |
|
62 if (this == obj) { |
|
63 return true; |
|
64 } |
|
65 if (!(obj instanceof IVersion)) { |
|
66 return false; |
|
67 } |
|
68 IVersion other = (IVersion) obj; |
|
69 if (version == null) { |
|
70 return false; |
|
71 } else if (!version.equals(other.toString())) { |
|
72 return false; |
|
73 } |
|
74 return true; |
|
75 } |
|
76 |
|
77 public String getVersionString() { |
|
78 return version; |
|
79 } |
|
80 |
|
81 @Override |
|
82 public int hashCode() { |
|
83 final int prime = 31; |
|
84 int result = 1; |
|
85 result = prime * result + ((version == null) ? 0 : version.hashCode()); |
|
86 return result; |
|
87 } |
|
88 } |
|
89 |
|
90 private static final class VersionableObject extends Versionable<Version> { |
|
91 @Override |
|
92 public String createVersionNotFoundErrMsg(String verstr) { |
|
93 return "Version not found"; |
|
94 } |
|
95 |
|
96 @Override |
|
97 public String getPluginId() { |
|
98 return TMWCore.PLUGIN_ID; |
|
99 } |
|
100 } |
|
101 |
49 private static final class VersionedEntry<T> { |
102 private static final class VersionedEntry<T> { |
|
103 protected final T entry; |
50 protected final IVersionExpr versionExpression; |
104 protected final IVersionExpr versionExpression; |
51 protected final T entry; |
|
52 |
105 |
53 public VersionedEntry(String versionExpression, T entry) { |
106 public VersionedEntry(String versionExpression, T entry) { |
54 if (versionExpression == null) { |
107 if (versionExpression == null) { |
55 this.versionExpression = null; |
108 this.versionExpression = null; |
56 } else { |
109 } else { |
75 } |
128 } |
76 return versionExpression.check(new Version(version)); |
129 return versionExpression.check(new Version(version)); |
77 } |
130 } |
78 } |
131 } |
79 |
132 |
80 private static final class VersionableObject extends Versionable<Version> { |
|
81 @Override |
|
82 public String getPluginId() { |
|
83 return TMWCore.PLUGIN_ID; |
|
84 } |
|
85 |
|
86 @Override |
|
87 public String createVersionNotFoundErrMsg(String verstr) { |
|
88 return "Version not found"; |
|
89 } |
|
90 } |
|
91 |
|
92 private static final class Version implements IVersion { |
|
93 private final String version; |
|
94 |
|
95 public Version(String version) { |
|
96 this.version = version; |
|
97 } |
|
98 |
|
99 public int compareTo(Object o) { |
|
100 return version.compareTo(((IVersion) o).getVersionString()); |
|
101 } |
|
102 |
|
103 public String getVersionString() { |
|
104 return version; |
|
105 } |
|
106 } |
|
107 |
|
108 private final Map<String, Collection<VersionedEntry<Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>>>>> providers = new HashMap<String, Collection<VersionedEntry<Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>>>>>(); |
133 private final Map<String, Collection<VersionedEntry<Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>>>>> providers = new HashMap<String, Collection<VersionedEntry<Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>>>>>(); |
109 private final boolean ready = false; |
134 private boolean ready = false; |
110 |
135 |
111 public IIncludePathEntry[] getProjectClasspathEntries(IFacetedProject project) { |
136 public Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>> collectFaceletEntries( |
112 collectProviders(); |
137 IConfigurationElement[] elements) { |
113 final ITMWProject p = TMWCore.create(project.getProject()); |
138 final Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>> faceletsToProviders = new HashMap<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>>(); |
114 if (p != null) { |
139 for (IConfigurationElement element : elements) { |
115 final IMobileWebRuntime runtime = p.getTargetRuntime(); |
140 if ("facet-include-path".equals(element.getName())) { |
116 final Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>> facetToEntry; |
141 final IFacetIncludePathProvider[] providers = collectProviders(element.getChildren()); |
117 if (runtime != null) { |
142 final VersionedEntry<IFacetIncludePathProvider[]> versionedEntry = new VersionedEntry<IFacetIncludePathProvider[]>( |
118 facetToEntry = join(getMatchingEntries(runtime.getId(), runtime.getVersion(), providers)); |
143 element.getAttribute("version"), providers); |
119 } else { |
144 final String id = element.getAttribute("facelet-id"); |
120 facetToEntry = join(getMatchingEntries(null, null, providers)); |
145 Collection<VersionedEntry<IFacetIncludePathProvider[]>> provs = faceletsToProviders.get(id); |
121 } |
146 if (provs == null) { |
122 final Collection<IFacetIncludePathProvider[]> entries = getMatchingEntries(null, null, facetToEntry); |
147 provs = new LinkedList<RuntimeClasspathManager.VersionedEntry<IFacetIncludePathProvider[]>>(); |
123 final Set<IProjectFacetVersion> facets = project.getProjectFacets(); |
148 faceletsToProviders.put(id, provs); |
124 for (IProjectFacetVersion facet : facets) { |
149 } |
125 entries.addAll(getMatchingEntries(facet.getProjectFacet().getId(), facet.getVersionString(), |
150 provs.add(versionedEntry); |
126 facetToEntry)); |
151 } |
127 } |
152 } |
128 final Collection<IIncludePathEntry> pathEntries = new HashSet<IIncludePathEntry>(); |
153 return faceletsToProviders; |
129 for (IFacetIncludePathProvider[] providers : entries) { |
|
130 for (IFacetIncludePathProvider provider : providers) { |
|
131 pathEntries.addAll(Arrays.asList(provider.getEntries(p))); |
|
132 } |
|
133 } |
|
134 return pathEntries.toArray(new IIncludePathEntry[pathEntries.size()]); |
|
135 } |
|
136 return new IIncludePathEntry[0]; |
|
137 } |
154 } |
138 |
155 |
139 @SuppressWarnings({ "rawtypes", "unchecked" }) |
156 @SuppressWarnings({ "rawtypes", "unchecked" }) |
140 private synchronized void collectProviders() { |
157 private synchronized void collectProviders() { |
141 if (!ready) { |
158 if (!ready) { |
177 } |
195 } |
178 } |
196 } |
179 return providers.toArray(new IFacetIncludePathProvider[providers.size()]); |
197 return providers.toArray(new IFacetIncludePathProvider[providers.size()]); |
180 } |
198 } |
181 |
199 |
182 public Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>> collectFaceletEntries( |
200 private <T> Collection<T> getMatchingEntries(String id, String version, |
183 IConfigurationElement[] elements) { |
201 Map<String, Collection<VersionedEntry<T>>> map) { |
184 final Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>> faceletsToProviders = new HashMap<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>>(); |
202 final Collection<VersionedEntry<T>> entries = new LinkedList<VersionedEntry<T>>(); |
185 for (IConfigurationElement element : elements) { |
203 Collection<VersionedEntry<T>> versionedEntries = map.get(null); |
186 if ("facet-include-path".equals(element.getName())) { |
204 if (versionedEntries != null) { |
187 final IFacetIncludePathProvider[] providers = collectProviders(element.getChildren()); |
205 entries.addAll(versionedEntries); |
188 final VersionedEntry<IFacetIncludePathProvider[]> versionedEntry = new VersionedEntry<IFacetIncludePathProvider[]>( |
206 } |
189 element.getAttribute("version"), providers); |
207 if (id != null) { |
190 final String id = element.getAttribute("facelet-id"); |
208 versionedEntries = map.get(id); |
191 Collection<VersionedEntry<IFacetIncludePathProvider[]>> provs = faceletsToProviders.get(id); |
209 if (versionedEntries != null) { |
192 if (provs == null) { |
210 entries.addAll(versionedEntries); |
193 provs = new LinkedList<RuntimeClasspathManager.VersionedEntry<IFacetIncludePathProvider[]>>(); |
211 } |
194 faceletsToProviders.put(id, provs); |
212 } |
195 } |
213 final Collection<T> res = new LinkedList<T>(); |
196 provs.add(versionedEntry); |
214 for (VersionedEntry<T> versionedEntry : entries) { |
197 } |
215 if (versionedEntry.matches(version)) { |
198 } |
216 res.add(versionedEntry.entry); |
199 return faceletsToProviders; |
217 } |
|
218 } |
|
219 return res; |
|
220 } |
|
221 |
|
222 public IIncludePathEntry[] getProjectClasspathEntries(IFacetedProject project) { |
|
223 collectProviders(); |
|
224 final ITMWProject p = TMWCore.create(project.getProject()); |
|
225 if (p != null) { |
|
226 final IMobileWebRuntime runtime = p.getTargetRuntime(); |
|
227 final Map<String, Collection<VersionedEntry<IFacetIncludePathProvider[]>>> facetToEntry; |
|
228 if (runtime != null) { |
|
229 facetToEntry = join(getMatchingEntries(runtime.getId(), runtime.getVersion(), providers)); |
|
230 } else { |
|
231 facetToEntry = join(getMatchingEntries(null, null, providers)); |
|
232 } |
|
233 final Collection<IFacetIncludePathProvider[]> entries = getMatchingEntries(null, null, facetToEntry); |
|
234 final Set<IProjectFacetVersion> facets = project.getProjectFacets(); |
|
235 for (IProjectFacetVersion facet : facets) { |
|
236 entries.addAll(getMatchingEntries(facet.getProjectFacet().getId(), facet.getVersionString(), |
|
237 facetToEntry)); |
|
238 } |
|
239 final Collection<IIncludePathEntry> pathEntries = new HashSet<IIncludePathEntry>(); |
|
240 for (IFacetIncludePathProvider[] providers : entries) { |
|
241 for (IFacetIncludePathProvider provider : providers) { |
|
242 pathEntries.addAll(Arrays.asList(provider.getEntries(p))); |
|
243 } |
|
244 } |
|
245 return pathEntries.toArray(new IIncludePathEntry[pathEntries.size()]); |
|
246 } |
|
247 return new IIncludePathEntry[0]; |
200 } |
248 } |
201 |
249 |
202 private <T, V> Map<T, Collection<V>> join(Collection<Map<T, Collection<V>>> maps) { |
250 private <T, V> Map<T, Collection<V>> join(Collection<Map<T, Collection<V>>> maps) { |
203 final Map<T, Collection<V>> res = new HashMap<T, Collection<V>>(); |
251 final Map<T, Collection<V>> res = new HashMap<T, Collection<V>>(); |
204 for (Map<T, Collection<V>> map : maps) { |
252 for (Map<T, Collection<V>> map : maps) { |