1 | /******************************************************************************* |
2 | * Copyright (c) 2008, 2009 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 | |
13 | import java.io.File; |
14 | |
15 | import org.eclipse.core.resources.IProject; |
16 | import org.eclipse.core.resources.ResourcesPlugin; |
17 | import org.eclipse.core.runtime.CoreException; |
18 | import org.eclipse.core.runtime.IPath; |
19 | import org.eclipse.core.runtime.Path; |
20 | import org.eclipse.osgi.service.resolver.BundleDescription; |
21 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
22 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
23 | import org.eclipse.pde.api.tools.internal.util.Util; |
24 | import org.eclipse.pde.core.plugin.IPluginModelBase; |
25 | import org.eclipse.pde.core.plugin.PluginRegistry; |
26 | |
27 | /** |
28 | * Utility class for creating new {@link org.eclipse.pde.api.tools.internal.provisional.model.IApiElement}s |
29 | * and for performing common tasks on them |
30 | * |
31 | * @since 1.0.0 |
32 | */ |
33 | public class ApiModelFactory { |
34 | |
35 | /** |
36 | * Next available bundle id |
37 | */ |
38 | private static long fNextId = 0L; |
39 | |
40 | /** |
41 | * @return a viable int id for a bundle |
42 | */ |
43 | private static long getBundleID() { |
44 | return fNextId++; |
45 | } |
46 | |
47 | /** |
48 | * Creates and returns a new API component for this profile at the specified |
49 | * location or <code>null</code> if the location specified does not contain |
50 | * a valid API component. The component is not added to the profile. |
51 | * |
52 | * @param location absolute path in the local file system to the API component |
53 | * @return API component or <code>null</code> if the location specified does not contain a valid |
54 | * API component |
55 | * @exception CoreException if unable to create the component |
56 | */ |
57 | public static IApiComponent newApiComponent(IApiBaseline profile, String location) throws CoreException { |
58 | BundleApiComponent component = new BundleApiComponent(profile, location); |
59 | if(component.isValidBundle()) { |
60 | component.init(getBundleID()); |
61 | return component; |
62 | } |
63 | return null; |
64 | } |
65 | |
66 | /** |
67 | * Creates and returns a new API component for this profile based on the given |
68 | * model or <code>null</code> if the given model cannot be resolved or does not contain |
69 | * a valid API component. The component is not added to the profile. |
70 | * |
71 | * @param model the given model |
72 | * @return API component or <code>null</code> if the given model cannot be resolved or does not contain |
73 | * a valid API component |
74 | * @exception CoreException if unable to create the component |
75 | */ |
76 | public static IApiComponent newApiComponent(IApiBaseline profile, IPluginModelBase model) throws CoreException { |
77 | BundleDescription bundleDescription = model.getBundleDescription(); |
78 | if (bundleDescription == null) { |
79 | return null; |
80 | } |
81 | String location = bundleDescription.getLocation(); |
82 | if (location == null) { |
83 | return null; |
84 | } |
85 | BundleApiComponent component = null; |
86 | IPluginModelBase model2 = getProjectModel(location); |
87 | if (model2 != null && model == model2) { |
88 | if (isBinaryProject(location)) { |
89 | component = new BundleApiComponent(profile, location); |
90 | } else { |
91 | component = new PluginProjectApiComponent(profile, location, model); |
92 | } |
93 | } else { |
94 | component = new BundleApiComponent(profile, location); |
95 | } |
96 | if(component.isValidBundle()) { |
97 | component.init(getBundleID()); |
98 | return component; |
99 | } |
100 | return null; |
101 | } |
102 | |
103 | /** |
104 | * Returns the plug-in model associated with the project at the specified location |
105 | * or <code>null</code> if none (i.e. if its an external model). |
106 | * |
107 | * @param project location |
108 | * @return plug-in model or <code>null</code> if none |
109 | */ |
110 | private static IPluginModelBase getProjectModel(String location) { |
111 | String projectName = (new Path(location)).lastSegment(); |
112 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); |
113 | if (project != null) { |
114 | return PluginRegistry.findModel(project); |
115 | } |
116 | return null; |
117 | } |
118 | |
119 | /** |
120 | * Returns if the specified location is an imported binary project. |
121 | * <p> |
122 | * We accept projects that are plug-ins even if not API enabled (i.e. |
123 | * with API nature), as we still need them to make a complete |
124 | * API profile without resolution errors. |
125 | * </p> |
126 | * @param location |
127 | * @return true if the location is an imported binary project, false otherwise |
128 | * @throws CoreException |
129 | */ |
130 | private static boolean isBinaryProject(String location) throws CoreException { |
131 | IPath path = new Path(location); |
132 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.lastSegment()); |
133 | return project != null && Util.isBinaryProject(project); |
134 | } |
135 | |
136 | /** |
137 | * Creates a new empty {@link IApiBaseline} with the given name. Its execution |
138 | * environment will be automatically resolved when components are added |
139 | * to it. |
140 | * <p> |
141 | * Note, a baseline can only automatically resolve an execution environment |
142 | * when it is created within an Eclipse SDK. A baseline created in a non-OSGi |
143 | * environment must have its execution environment specified at creation |
144 | * time. |
145 | * </p> |
146 | * |
147 | * @param name baseline name |
148 | * @return a new empty {@link IApiBaseline} |
149 | */ |
150 | public static IApiBaseline newApiBaseline(String name) { |
151 | return new ApiBaseline(name); |
152 | } |
153 | |
154 | /** |
155 | * Creates a new empty API baseline with the specified execution environment. |
156 | * <p> |
157 | * The execution environment description file describes how an execution |
158 | * environment profile is provided by or mapped to a specific JRE. The format for |
159 | * this file is described here |
160 | * <code>http://wiki.eclipse.org/index.php/Execution_Environment_Descriptions</code>. |
161 | * </p> |
162 | * @param name baseline name |
163 | * @param eeDescription execution environment description file |
164 | * @return a new {@link IApiBaseline} |
165 | * @throws CoreException if unable to create a new baseline with the specified attributes |
166 | */ |
167 | public static IApiBaseline newApiBaseline(String name, File eeDescription) throws CoreException { |
168 | return new ApiBaseline(name, eeDescription); |
169 | } |
170 | |
171 | /** |
172 | * Creates a new empty {@link IApiBaseline} with the given name. Its execution |
173 | * environment will be automatically resolved when components are added |
174 | * to it. |
175 | * <p> |
176 | * Note, a baseline can only automatically resolve an execution environment |
177 | * when it is created within an Eclipse SDK. A baseline created in a non-OSGi |
178 | * environment must have its execution environment specified at creation |
179 | * time. |
180 | * </p> |
181 | * |
182 | * @param name baseline name |
183 | * @param location the given baseline's location |
184 | * @return a new empty {@link IApiBaseline} |
185 | * @throws CoreException if unable to create a new baseline with the specified attributes |
186 | */ |
187 | public static IApiBaseline newApiBaseline(String name, String location) throws CoreException { |
188 | return new ApiBaseline(name, null, location); |
189 | } |
190 | |
191 | /** |
192 | * Creates a new empty API baseline with the specified execution environment. |
193 | * <p> |
194 | * The execution environment description file describes how an execution |
195 | * environment profile is provided by or mapped to a specific JRE. The format for |
196 | * this file is described here |
197 | * <code>http://wiki.eclipse.org/index.php/Execution_Environment_Descriptions</code>. |
198 | * </p> |
199 | * @param name baseline name |
200 | * @param eeDescription execution environment description file |
201 | * @param location the given baseline's location |
202 | * @return a new {@link IApiBaseline} |
203 | * @throws CoreException if unable to create a new baseline with the specified attributes |
204 | */ |
205 | public static IApiBaseline newApiBaseline(String name, File eeDescription, String location) throws CoreException { |
206 | return new ApiBaseline(name, eeDescription, location); |
207 | } |
208 | } |