PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.pde.api.tools.source_1.0.301.v20110728_r371/org/eclipse/pde/api/tools/internal/model/ApiModelFactory.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 283 lines | 140 code | 16 blank | 127 comment | 27 complexity | 356c8dfe9cbfe7bb70dcffa5c640dec6 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2008, 2011 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.pde.api.tools.internal.model;
  12. import java.io.File;
  13. import java.io.FilenameFilter;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.eclipse.core.resources.IProject;
  17. import org.eclipse.core.resources.ResourcesPlugin;
  18. import org.eclipse.core.runtime.CoreException;
  19. import org.eclipse.core.runtime.IPath;
  20. import org.eclipse.core.runtime.IProgressMonitor;
  21. import org.eclipse.core.runtime.Path;
  22. import org.eclipse.core.runtime.SubMonitor;
  23. import org.eclipse.core.runtime.URIUtil;
  24. import org.eclipse.osgi.service.resolver.BundleDescription;
  25. import org.eclipse.osgi.util.NLS;
  26. import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
  27. import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline;
  28. import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent;
  29. import org.eclipse.pde.api.tools.internal.util.Util;
  30. import org.eclipse.pde.core.plugin.IPluginModelBase;
  31. import org.eclipse.pde.internal.core.target.provisional.IBundleContainer;
  32. import org.eclipse.pde.internal.core.target.provisional.IResolvedBundle;
  33. import org.eclipse.pde.internal.core.target.provisional.ITargetDefinition;
  34. import org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService;
  35. /**
  36. * Utility class for creating new {@link org.eclipse.pde.api.tools.internal.provisional.model.IApiElement}s
  37. * and for performing common tasks on them
  38. *
  39. * @since 1.0.0
  40. */
  41. public class ApiModelFactory {
  42. private static final String CVS_FOLDER_NAME = "CVS"; //$NON-NLS-1$
  43. public static final IApiComponent[] NO_COMPONENTS = new IApiComponent[0];
  44. /**
  45. * {@link FilenameFilter} for CVS files
  46. * @since 1.0.1
  47. */
  48. static class CVSNameFilter implements FilenameFilter {
  49. public boolean accept(File dir, String name) {
  50. return !name.equalsIgnoreCase(CVS_FOLDER_NAME);
  51. }
  52. }
  53. private static CVSNameFilter fgCvsFilter = new CVSNameFilter();
  54. /**
  55. * Next available bundle id
  56. */
  57. private static long fNextId = 0L;
  58. /**
  59. * @return a viable int id for a bundle
  60. */
  61. private static long getBundleID() {
  62. return fNextId++;
  63. }
  64. /**
  65. * Creates and returns a new API component for this baseline at the specified
  66. * location or <code>null</code> if the location specified does not contain
  67. * a valid API component. The component is not added to the baseline.
  68. *
  69. * @param location absolute path in the local file system to the API component
  70. * @return API component or <code>null</code> if the location specified does not contain a valid
  71. * API component
  72. * @exception CoreException if unable to create the component
  73. */
  74. public static IApiComponent newApiComponent(IApiBaseline baseline, String location) throws CoreException {
  75. BundleComponent component = new BundleComponent(baseline, location, getBundleID());
  76. if(component.isValidBundle()) {
  77. return component;
  78. }
  79. return null;
  80. }
  81. /**
  82. * Creates and returns a new API component for this baseline based on the given
  83. * model or <code>null</code> if the given model cannot be resolved or does not contain
  84. * a valid API component. The component is not added to the baseline.
  85. *
  86. * @param baseline
  87. * @param model the given model
  88. * @return API component or <code>null</code> if the given model cannot be resolved or does not contain
  89. * a valid API component
  90. * @exception CoreException if unable to create the component
  91. */
  92. public static IApiComponent newApiComponent(IApiBaseline baseline, IPluginModelBase model) throws CoreException {
  93. BundleDescription bundleDescription = model.getBundleDescription();
  94. if (bundleDescription == null) {
  95. return null;
  96. }
  97. String location = bundleDescription.getLocation();
  98. if (location == null) {
  99. return null;
  100. }
  101. BundleComponent component = null;
  102. if (isBinaryProject(location)) {
  103. component = new BundleComponent(baseline, location, getBundleID());
  104. } else {
  105. component = new ProjectComponent(baseline, location, model, getBundleID());
  106. }
  107. if(component.isValidBundle()) {
  108. return component;
  109. }
  110. return null;
  111. }
  112. /**
  113. * Returns if the specified location is an imported binary project.
  114. * <p>
  115. * We accept projects that are plug-ins even if not API enabled (i.e.
  116. * with API nature), as we still need them to make a complete
  117. * API baseline without resolution errors.
  118. * </p>
  119. * @param location
  120. * @return true if the location is an imported binary project, false otherwise
  121. * @throws CoreException
  122. */
  123. private static boolean isBinaryProject(String location) throws CoreException {
  124. IPath path = new Path(location);
  125. IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.lastSegment());
  126. return project != null && (!project.exists() || Util.isBinaryProject(project));
  127. }
  128. /**
  129. * Creates a new empty {@link IApiBaseline} with the given name. Its execution
  130. * environment will be automatically resolved when components are added
  131. * to it.
  132. * <p>
  133. * Note, a baseline can only automatically resolve an execution environment
  134. * when it is created within an Eclipse SDK. A baseline created in a non-OSGi
  135. * environment must have its execution environment specified at creation
  136. * time.
  137. * </p>
  138. *
  139. * @param name baseline name
  140. * @return a new empty {@link IApiBaseline}
  141. */
  142. public static IApiBaseline newApiBaseline(String name) {
  143. return new ApiBaseline(name);
  144. }
  145. /**
  146. * Creates a new empty API baseline with the specified execution environment.
  147. * <p>
  148. * The execution environment description file describes how an execution
  149. * environment profile is provided by or mapped to a specific JRE. The format for
  150. * this file is described here
  151. * <code>http://wiki.eclipse.org/index.php/Execution_Environment_Descriptions</code>.
  152. * </p>
  153. * @param name baseline name
  154. * @param eeDescription execution environment description file
  155. * @return a new {@link IApiBaseline}
  156. * @throws CoreException if unable to create a new baseline with the specified attributes
  157. */
  158. public static IApiBaseline newApiBaseline(String name, File eeDescription) throws CoreException {
  159. return new ApiBaseline(name, eeDescription);
  160. }
  161. /**
  162. * Creates a new empty {@link IApiBaseline} with the given name. Its execution
  163. * environment will be automatically resolved when components are added
  164. * to it.
  165. * <p>
  166. * Note, a baseline can only automatically resolve an execution environment
  167. * when it is created within an Eclipse SDK. A baseline created in a non-OSGi
  168. * environment must have its execution environment specified at creation
  169. * time.
  170. * </p>
  171. *
  172. * @param name baseline name
  173. * @param location the given baseline's location
  174. * @return a new empty {@link IApiBaseline}
  175. * @throws CoreException if unable to create a new baseline with the specified attributes
  176. */
  177. public static IApiBaseline newApiBaseline(String name, String location) throws CoreException {
  178. return new ApiBaseline(name, null, location);
  179. }
  180. /**
  181. * Creates a new empty API baseline with the specified execution environment.
  182. * <p>
  183. * The execution environment description file describes how an execution
  184. * environment profile is provided by or mapped to a specific JRE. The format for
  185. * this file is described here
  186. * <code>http://wiki.eclipse.org/index.php/Execution_Environment_Descriptions</code>.
  187. * </p>
  188. * @param name baseline name
  189. * @param eeDescription execution environment description file
  190. * @param location the given baseline's location
  191. * @return a new {@link IApiBaseline}
  192. * @throws CoreException if unable to create a new baseline with the specified attributes
  193. */
  194. public static IApiBaseline newApiBaseline(String name, File eeDescription, String location) throws CoreException {
  195. return new ApiBaseline(name, eeDescription, location);
  196. }
  197. /**
  198. * Collects API components for the bundles part of the specified installation and adds them to the baseline. The
  199. * components that were added to the baseline are returned.
  200. *
  201. * @param baseline The baseline to add the components to
  202. * @param installLocation location of an installation that components are collected from
  203. * @param monitor progress monitor or <code>null</code>, the caller is responsible for calling {@link IProgressMonitor#done()}
  204. * @return List of API components that were added to the baseline, possibly empty, never <code>null</code>
  205. * @throws CoreException If problems occur getting components or modifying the baseline
  206. */
  207. public static IApiComponent[] addComponents(IApiBaseline baseline, String installLocation, IProgressMonitor monitor) throws CoreException {
  208. SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.configuring_baseline, 50);
  209. IApiComponent[] result = null;
  210. try {
  211. // Acquire the service
  212. ITargetPlatformService service = null;
  213. ApiPlugin plugin = ApiPlugin.getDefault();
  214. if (plugin != null){
  215. service = (ITargetPlatformService) ApiPlugin.getDefault().acquireService(ITargetPlatformService.class.getName());
  216. Util.updateMonitor(subMonitor, 1);
  217. IBundleContainer container = service.newProfileContainer(installLocation, null);
  218. ITargetDefinition definition = service.newTarget();
  219. subMonitor.subTask(Messages.resolving_target_definition);
  220. container.resolve(definition, subMonitor.newChild(30));
  221. Util.updateMonitor(subMonitor, 1);
  222. IResolvedBundle[] bundles = container.getBundles();
  223. List components = new ArrayList();
  224. if (bundles.length > 0) {
  225. subMonitor.setWorkRemaining(bundles.length);
  226. for (int i = 0; i < bundles.length; i++) {
  227. Util.updateMonitor(subMonitor, 1);
  228. if (!bundles[i].isSourceBundle()) {
  229. IApiComponent component = ApiModelFactory.newApiComponent(baseline, URIUtil.toFile(bundles[i].getBundleInfo().getLocation()).getAbsolutePath());
  230. if (component != null) {
  231. subMonitor.subTask(NLS.bind(Messages.adding_component__0, component.getSymbolicName()));
  232. components.add(component);
  233. }
  234. }
  235. }
  236. }
  237. result = (IApiComponent[])components.toArray(new IApiComponent[components.size()]);
  238. } else {
  239. // The target platform service is unavailable (OSGi isn't running), add components by searching the plug-ins directory
  240. File dir = new File(installLocation);
  241. if(dir.exists()) {
  242. File[] files = dir.listFiles(fgCvsFilter);
  243. if(files == null) {
  244. return NO_COMPONENTS;
  245. }
  246. List components = new ArrayList();
  247. for (int i = 0; i < files.length; i++) {
  248. File bundle = files[i];
  249. IApiComponent component = ApiModelFactory.newApiComponent(baseline, bundle.getAbsolutePath());
  250. if(component != null) {
  251. components.add(component);
  252. }
  253. }
  254. result = (IApiComponent[]) components.toArray(new IApiComponent[components.size()]);
  255. }
  256. }
  257. if(result != null) {
  258. baseline.addApiComponents(result);
  259. return result;
  260. }
  261. return NO_COMPONENTS;
  262. }
  263. finally {
  264. subMonitor.done();
  265. }
  266. }
  267. }