KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > site > BuildTimeSiteFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors: IBM - Initial API and implementation
8  ******************************************************************************/

9 package org.eclipse.pde.internal.build.site;
10
11 import java.io.File JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.pde.build.Constants;
18 import org.eclipse.pde.internal.build.*;
19 import org.eclipse.update.core.*;
20 import org.eclipse.update.core.model.InvalidSiteTypeException;
21 import org.eclipse.update.core.model.SiteModel;
22
23 public class BuildTimeSiteFactory extends BaseSiteFactory implements ISiteFactory, IPDEBuildConstants {
24     // The whole site : things to be compiled and the installedBase
25
private Site site = null;
26
27     // Indicate if the content of the site changed
28
private boolean urlsChanged = false;
29
30     // URLs from the the site will be built
31
private String JavaDoc[] sitePaths;
32
33     // address of the site used as a base
34
private static String JavaDoc installedBaseLocation = null;
35
36     private boolean reportResolutionErrors;
37
38     private PDEUIStateWrapper pdeUIState;
39
40     //Support for filtering the state
41
private List rootFeaturesForFilter;
42     private List rootPluginsForFilter;
43     private boolean filterState;
44
45     /**
46      * Create a build time site, using the sitePaths, and the installedBaseLocation.
47      * Note that the site object is not recomputed if no change has been done.
48      *
49      * @return ISite
50      * @throws CoreException
51      */

52     public ISite createSite() throws CoreException {
53         if (site != null && urlsChanged == false)
54             return site;
55
56         urlsChanged = false;
57         site = (Site) createSiteMapModel();
58
59         // Here we find the features in the URLs
60
Collection featureXMLs = findFeatureXMLs();
61
62         // If an installed base is provided we need to look at it
63
String JavaDoc installedBaseURL = null;
64         if (installedBaseLocation != null && !installedBaseLocation.equals("")) { //$NON-NLS-1$
65
if (!new File JavaDoc(installedBaseLocation).exists()) {
66                 String JavaDoc message = NLS.bind(Messages.error_incorrectDirectoryEntry, installedBaseLocation);
67                 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READ_DIRECTORY, message, null));
68             }
69
70             installedBaseURL = installedBaseLocation;
71             Collection installedFeatures = Utils.findFiles(new File JavaDoc(installedBaseLocation), DEFAULT_FEATURE_LOCATION, Constants.FEATURE_FILENAME_DESCRIPTOR);
72             if (installedFeatures != null)
73                 featureXMLs.addAll(installedFeatures);
74     
75             // extract features from platform.xml
76
File JavaDoc[] featureDirectories = PluginPathFinder.getFeaturePaths(installedBaseURL);
77             for (int i = 0; i < featureDirectories.length; i++) {
78                 File JavaDoc featureXML = new File JavaDoc(featureDirectories[i],Constants.FEATURE_FILENAME_DESCRIPTOR);
79                 if (featureXML.exists())
80                     featureXMLs.add(featureXML);
81             }
82             
83         }
84
85         URL JavaDoc featureURL;
86         SiteFeatureReferenceModel featureRef;
87
88         for (Iterator iter = featureXMLs.iterator(); iter.hasNext();) {
89             File JavaDoc featureXML = (File JavaDoc) iter.next();
90             if (featureXML.exists()) {
91                 // 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
92
// have it (file:d:/eclipse/plugins) and this causes problems later to compare URLs... and compute relative paths
93
try {
94                     featureURL = new URL JavaDoc("file:" + featureXML.getAbsolutePath()); //$NON-NLS-1$
95
featureRef = createFeatureReferenceModel();
96                     featureRef.setSiteModel(site);
97                     featureRef.setURLString(featureURL.toExternalForm());
98                     featureRef.setType(BuildTimeFeatureFactory.BUILDTIME_FEATURE_FACTORY_ID);
99                     site.addFeatureReferenceModel(featureRef);
100                 } catch (MalformedURLException JavaDoc e) {
101                     BundleHelper.getDefault().getLog().log(new Status(IStatus.WARNING, PI_PDEBUILD, WARNING_MISSING_SOURCE, NLS.bind(Messages.warning_cannotLocateSource, featureXML.getAbsolutePath()), e));
102                 }
103             }
104         }
105         ISiteContentProvider contentProvider = new BuildTimeSiteContentProvider(sitePaths, installedBaseURL, pdeUIState);
106         site.setSiteContentProvider(contentProvider);
107         contentProvider.setSite(site);
108         return site;
109     }
110
111     /**
112      * This method MUST not be called. The given URL is a pointer to the location
113      * of a site.xml file which describes our site, and we don't have this file.
114      */

115     public ISite createSite(URL JavaDoc url) throws CoreException, InvalidSiteTypeException {
116         throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READ_DIRECTORY, Messages.error_incorrectDirectoryEntry, null));
117     }
118
119     public SiteModel createSiteMapModel() {
120         BuildTimeSite model = new BuildTimeSite();
121         model.setReportResolutionErrors(reportResolutionErrors);
122         model.setFilter(filterState);
123         model.setRootFeaturesForFilter(rootFeaturesForFilter);
124         model.setRootPluginsForFiler(rootPluginsForFilter);
125         return model;
126     }
127
128     public static void setInstalledBaseSite(String JavaDoc installedBaseSite) {
129         BuildTimeSiteFactory.installedBaseLocation = installedBaseSite;
130     }
131
132     public void setSitePaths(String JavaDoc[] urls) {
133         if (sitePaths == null) {
134             sitePaths = urls;
135             urlsChanged = true;
136             return;
137         }
138
139         //Check if urls are not the same than sitePaths.
140
int i = 0;
141         boolean found = true;
142         while (found && i < sitePaths.length) {
143             found = false;
144             for (int j = 0; j < urls.length; j++) {
145                 if (sitePaths[i].equals(urls[j])) {
146                     found = true;
147                     break;
148                 }
149             }
150             i++;
151         }
152         if (!found) {
153             sitePaths = urls;
154             urlsChanged = true;
155         }
156     }
157
158     /**
159      * Look for the feature.xml files and return a collection of java.io.File objects
160      * which point to their locations. Only look in directories which are direct descendants
161      * of the /features directory. (do not do an infinite depth look-up)
162      */

163     private Collection findFeatureXMLs() {
164         Collection features = new ArrayList();
165         for (int i = 0; i < sitePaths.length; i++) {
166             Collection foundFeatures = Utils.findFiles(new File JavaDoc(sitePaths[i]), DEFAULT_FEATURE_LOCATION, Constants.FEATURE_FILENAME_DESCRIPTOR);
167             if (foundFeatures != null)
168                 features.addAll(foundFeatures);
169         }
170         return features;
171     }
172
173     public void setReportResolutionErrors(boolean value) {
174         reportResolutionErrors = value;
175     }
176
177     public void setInitialState(PDEUIStateWrapper uiState) {
178         this.pdeUIState = uiState;
179     }
180
181     public void setFilterState(boolean b) {
182         this.filterState = b;
183     }
184
185     public void setFilterRoots(List featuresForFilterRoots, List pluginsForFilterRoots) {
186         this.rootFeaturesForFilter = featuresForFilterRoots;
187         this.rootPluginsForFilter = pluginsForFilterRoots;
188     }
189
190 }
191
Popular Tags