001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others. All rights reserved.
003: * This program and the accompanying materials are made available under the
004: * terms of the Eclipse Public License v1.0 which accompanies this distribution,
005: * and is available at http://www.eclipse.org/legal/epl-v10.html
006: *
007: * Contributors: IBM - Initial API and implementation
008: ******************************************************************************/package org.eclipse.pde.internal.build.site;
009:
010: import java.io.File;
011: import java.net.MalformedURLException;
012: import java.net.URL;
013: import java.util.*;
014: import org.eclipse.core.runtime.*;
015: import org.eclipse.osgi.util.NLS;
016: import org.eclipse.pde.build.Constants;
017: import org.eclipse.pde.internal.build.*;
018: import org.eclipse.update.core.*;
019: import org.eclipse.update.core.model.InvalidSiteTypeException;
020: import org.eclipse.update.core.model.SiteModel;
021:
022: public class BuildTimeSiteFactory extends BaseSiteFactory implements
023: ISiteFactory, IPDEBuildConstants {
024: // The whole site : things to be compiled and the installedBase
025: private Site site = null;
026:
027: // Indicate if the content of the site changed
028: private boolean urlsChanged = false;
029:
030: // URLs from the the site will be built
031: private String[] sitePaths;
032:
033: // address of the site used as a base
034: private static String installedBaseLocation = null;
035:
036: private boolean reportResolutionErrors;
037:
038: private PDEUIStateWrapper pdeUIState;
039:
040: //Support for filtering the state
041: private List rootFeaturesForFilter;
042: private List rootPluginsForFilter;
043: private boolean filterState;
044:
045: /**
046: * Create a build time site, using the sitePaths, and the installedBaseLocation.
047: * Note that the site object is not recomputed if no change has been done.
048: *
049: * @return ISite
050: * @throws CoreException
051: */
052: public ISite createSite() throws CoreException {
053: if (site != null && urlsChanged == false)
054: return site;
055:
056: urlsChanged = false;
057: site = (Site) createSiteMapModel();
058:
059: // Here we find the features in the URLs
060: Collection featureXMLs = findFeatureXMLs();
061:
062: // If an installed base is provided we need to look at it
063: String installedBaseURL = null;
064: if (installedBaseLocation != null
065: && !installedBaseLocation.equals("")) { //$NON-NLS-1$
066: if (!new File(installedBaseLocation).exists()) {
067: String message = NLS.bind(
068: Messages.error_incorrectDirectoryEntry,
069: installedBaseLocation);
070: throw new CoreException(new Status(IStatus.ERROR,
071: PI_PDEBUILD, EXCEPTION_READ_DIRECTORY, message,
072: null));
073: }
074:
075: installedBaseURL = installedBaseLocation;
076: Collection installedFeatures = Utils.findFiles(new File(
077: installedBaseLocation), DEFAULT_FEATURE_LOCATION,
078: Constants.FEATURE_FILENAME_DESCRIPTOR);
079: if (installedFeatures != null)
080: featureXMLs.addAll(installedFeatures);
081:
082: // extract features from platform.xml
083: File[] featureDirectories = PluginPathFinder
084: .getFeaturePaths(installedBaseURL);
085: for (int i = 0; i < featureDirectories.length; i++) {
086: File featureXML = new File(featureDirectories[i],
087: Constants.FEATURE_FILENAME_DESCRIPTOR);
088: if (featureXML.exists())
089: featureXMLs.add(featureXML);
090: }
091:
092: }
093:
094: URL featureURL;
095: SiteFeatureReferenceModel featureRef;
096:
097: for (Iterator iter = featureXMLs.iterator(); iter.hasNext();) {
098: File featureXML = (File) iter.next();
099: if (featureXML.exists()) {
100: // Here we could not use toURL() on currentFeatureDir, because the URL has a slash after the colons (file:/c:/foo) whereas the plugins don't
101: // have it (file:d:/eclipse/plugins) and this causes problems later to compare URLs... and compute relative paths
102: try {
103: featureURL = new URL(
104: "file:" + featureXML.getAbsolutePath()); //$NON-NLS-1$
105: featureRef = createFeatureReferenceModel();
106: featureRef.setSiteModel(site);
107: featureRef
108: .setURLString(featureURL.toExternalForm());
109: featureRef
110: .setType(BuildTimeFeatureFactory.BUILDTIME_FEATURE_FACTORY_ID);
111: site.addFeatureReferenceModel(featureRef);
112: } catch (MalformedURLException e) {
113: BundleHelper
114: .getDefault()
115: .getLog()
116: .log(
117: new Status(
118: IStatus.WARNING,
119: PI_PDEBUILD,
120: WARNING_MISSING_SOURCE,
121: NLS
122: .bind(
123: Messages.warning_cannotLocateSource,
124: featureXML
125: .getAbsolutePath()),
126: e));
127: }
128: }
129: }
130: ISiteContentProvider contentProvider = new BuildTimeSiteContentProvider(
131: sitePaths, installedBaseURL, pdeUIState);
132: site.setSiteContentProvider(contentProvider);
133: contentProvider.setSite(site);
134: return site;
135: }
136:
137: /**
138: * This method MUST not be called. The given URL is a pointer to the location
139: * of a site.xml file which describes our site, and we don't have this file.
140: */
141: public ISite createSite(URL url) throws CoreException,
142: InvalidSiteTypeException {
143: throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD,
144: EXCEPTION_READ_DIRECTORY,
145: Messages.error_incorrectDirectoryEntry, null));
146: }
147:
148: public SiteModel createSiteMapModel() {
149: BuildTimeSite model = new BuildTimeSite();
150: model.setReportResolutionErrors(reportResolutionErrors);
151: model.setFilter(filterState);
152: model.setRootFeaturesForFilter(rootFeaturesForFilter);
153: model.setRootPluginsForFiler(rootPluginsForFilter);
154: return model;
155: }
156:
157: public static void setInstalledBaseSite(String installedBaseSite) {
158: BuildTimeSiteFactory.installedBaseLocation = installedBaseSite;
159: }
160:
161: public void setSitePaths(String[] urls) {
162: if (sitePaths == null) {
163: sitePaths = urls;
164: urlsChanged = true;
165: return;
166: }
167:
168: //Check if urls are not the same than sitePaths.
169: int i = 0;
170: boolean found = true;
171: while (found && i < sitePaths.length) {
172: found = false;
173: for (int j = 0; j < urls.length; j++) {
174: if (sitePaths[i].equals(urls[j])) {
175: found = true;
176: break;
177: }
178: }
179: i++;
180: }
181: if (!found) {
182: sitePaths = urls;
183: urlsChanged = true;
184: }
185: }
186:
187: /**
188: * Look for the feature.xml files and return a collection of java.io.File objects
189: * which point to their locations. Only look in directories which are direct descendants
190: * of the /features directory. (do not do an infinite depth look-up)
191: */
192: private Collection findFeatureXMLs() {
193: Collection features = new ArrayList();
194: for (int i = 0; i < sitePaths.length; i++) {
195: Collection foundFeatures = Utils.findFiles(new File(
196: sitePaths[i]), DEFAULT_FEATURE_LOCATION,
197: Constants.FEATURE_FILENAME_DESCRIPTOR);
198: if (foundFeatures != null)
199: features.addAll(foundFeatures);
200: }
201: return features;
202: }
203:
204: public void setReportResolutionErrors(boolean value) {
205: reportResolutionErrors = value;
206: }
207:
208: public void setInitialState(PDEUIStateWrapper uiState) {
209: this .pdeUIState = uiState;
210: }
211:
212: public void setFilterState(boolean b) {
213: this .filterState = b;
214: }
215:
216: public void setFilterRoots(List featuresForFilterRoots,
217: List pluginsForFilterRoots) {
218: this.rootFeaturesForFilter = featuresForFilterRoots;
219: this.rootPluginsForFilter = pluginsForFilterRoots;
220: }
221:
222: }
|