KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > packager > PackageConfigScriptGenerator


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.packager;
10
11 import java.io.*;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Properties JavaDoc;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.osgi.service.resolver.BundleDescription;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.pde.internal.build.*;
18 import org.eclipse.pde.internal.build.ant.FileSet;
19 import org.eclipse.pde.internal.build.builder.FeatureBuildScriptGenerator;
20 import org.eclipse.pde.internal.build.builder.ModelBuildScriptGenerator;
21 import org.eclipse.pde.internal.build.site.BuildTimeSiteContentProvider;
22 import org.eclipse.update.core.IFeature;
23
24 public class PackageConfigScriptGenerator extends AssembleConfigScriptGenerator {
25
26     private Properties JavaDoc packagingProperties;
27
28     private String JavaDoc getFinalName(BundleDescription bundle, String JavaDoc shape) {
29         final String JavaDoc JAR = "jar"; //$NON-NLS-1$
30
final String JavaDoc DOT_JAR = '.' + JAR;
31         if (! AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_AS_NORMALIZER)) {
32             Path path = new Path(bundle.getLocation());
33             if (shape.equals(FILE) && !JAR.equalsIgnoreCase(path.getFileExtension()))
34                 return path.lastSegment().concat(DOT_JAR);
35             return path.lastSegment();
36         }
37         if (shape.equals(FILE))
38             return ModelBuildScriptGenerator.getNormalizedName(bundle) + DOT_JAR;
39         return ModelBuildScriptGenerator.getNormalizedName(bundle);
40     }
41
42     private String JavaDoc getFinalName(IFeature feature) {
43         if (! AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_AS_NORMALIZER)) {
44             Path featurePath = new Path(feature.getURL().getPath());
45             return featurePath.segment(featurePath.segmentCount() - 2);
46         }
47         return FeatureBuildScriptGenerator.getNormalizedName(feature);
48     }
49
50     protected void generateGatherBinPartsCalls() { //TODO Here we should try to use cp because otherwise we will loose the permissions
51
String JavaDoc excludedFiles = null;
52         if (AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_AS_NORMALIZER))
53             excludedFiles = "build.properties, .project, .classpath"; //$NON-NLS-1$
54
IPath baseLocation = null;
55         try {
56             String JavaDoc url = ((BuildTimeSiteContentProvider) getSite(false).getSiteContentProvider()).getInstalledBaseURL();
57             if (url != null)
58                 baseLocation = new Path(url);
59         } catch (CoreException e) {
60             //nothing
61
}
62         for (int i = 0; i < plugins.length; i++) {
63             Path pluginLocation = new Path(plugins[i].getLocation());
64             String JavaDoc location = pluginLocation.toOSString();
65             boolean isFolder = isFolder(pluginLocation);
66
67             //try to relate the plugin location to the ${baseLocation} property
68
if (baseLocation != null && baseLocation.isPrefixOf(pluginLocation)) {
69                 IPath relative = pluginLocation.removeFirstSegments(baseLocation.segmentCount());
70                 location = new Path(Utils.getPropertyFormat(PROPERTY_BASE_LOCATION)).append(relative).toOSString();
71             }
72             if (isFolder) {
73                 script.printCopyTask(null, Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + Utils.getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + getFinalName(plugins[i], FOLDER), new FileSet[] {new FileSet(location, null, null, null, excludedFiles, null, null)}, false, false);
74             } else {
75                 script.printCopyFileTask(location, Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + Utils.getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + getFinalName(plugins[i], FILE), false);
76             }
77         }
78
79         for (int i = 0; i < features.length; i++) {
80             IPath featureLocation = new Path(features[i].getURL().getPath()); // Here we assume that all the features are local
81
featureLocation = featureLocation.removeLastSegments(1);
82             String JavaDoc location = featureLocation.toOSString();
83             if (baseLocation != null && baseLocation.isPrefixOf(featureLocation)) {
84                 IPath relative = featureLocation.removeFirstSegments(baseLocation.segmentCount());
85                 location = new Path(Utils.getPropertyFormat(PROPERTY_BASE_LOCATION)).append(relative).toOSString();
86             }
87             script.printCopyTask(null, Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + Utils.getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + getFinalName(features[i]), new FileSet[] {new FileSet(location, null, null, null, null, null, null)}, false, false);
88         }
89
90         if (packagingProperties.size() != 0) {
91             String JavaDoc filesToPackage = null;
92             filesToPackage = packagingProperties.getProperty(ROOT, null);
93             if (filesToPackage != null)
94                 filesToPackage += ',';
95
96             String JavaDoc tmp = packagingProperties.getProperty(ROOT_PREFIX + configInfo.toString("."), null); //$NON-NLS-1$
97
if (tmp != null)
98                 filesToPackage += tmp;
99
100             if (filesToPackage == null)
101                 filesToPackage = "**/**"; //$NON-NLS-1$
102

103             FileSet rootFiles = new FileSet(Utils.getPropertyFormat("tempDirectory") + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + "/eclipse", null, filesToPackage, null, null, null, null); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
104
String JavaDoc target = Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER); //$NON-NLS-1$
105
script.printCopyTask(null, target, new FileSet[] {rootFiles}, false, false);
106
107             Utils.generatePermissions(packagingProperties, configInfo, PROPERTY_ECLIPSE_BASE, script);
108
109             //This is need so that the call in assemble config script generator gather the root files
110
rootFileProviders = new ArrayList JavaDoc(1);
111             rootFileProviders.add("elt"); //$NON-NLS-1$
112
}
113     }
114
115     public String JavaDoc getTargetName() {
116         return "package" + (featureId.equals("") ? "" : ('.' + featureId)) + (configInfo.equals(Config.genericConfig()) ? "" : ('.' + configInfo.toStringReplacingAny(".", ANY_STRING))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
117
}
118
119     private boolean isFolder(Path pluginLocation) {
120         return pluginLocation.toFile().isDirectory();
121     }
122
123     public void setPackagingPropertiesLocation(String JavaDoc packagingPropertiesLocation) throws CoreException {
124         packagingProperties = new Properties JavaDoc();
125         if (packagingPropertiesLocation == null || packagingPropertiesLocation.equals("")) //$NON-NLS-1$
126
return;
127
128         InputStream propertyStream = null;
129         try {
130             propertyStream = new BufferedInputStream(new FileInputStream(packagingPropertiesLocation));
131             try {
132                 packagingProperties.load(new BufferedInputStream(propertyStream));
133             } finally {
134                 propertyStream.close();
135             }
136         } catch (FileNotFoundException e) {
137             String JavaDoc message = NLS.bind(Messages.exception_readingFile, packagingPropertiesLocation);
138             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
139         } catch (IOException e) {
140             String JavaDoc message = NLS.bind(Messages.exception_readingFile, packagingPropertiesLocation);
141             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
142         }
143     }
144
145     protected void generateGatherSourceCalls() {
146         //In the packager, we do not gather source
147
}
148
149     protected FileSet[] generatePermissions(boolean zip) {
150         //In the packager there is nothing to do since, the features we are packaging are pre-built and do not have a build.properties
151
return new FileSet[0];
152     }
153
154     protected void generateGZipTarget(boolean assembling) {
155         super.generateGZipTarget(false);
156     }
157
158     public void generateTarGZTasks(boolean assembling) {
159         super.generateTarGZTasks(false);
160     }
161
162     protected Object JavaDoc[] getFinalShape(BundleDescription bundle) {
163         if (AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_MODE) == true) {
164             String JavaDoc shape = isFolder(new Path(bundle.getLocation())) ? FOLDER : FILE;
165             return new Object JavaDoc[] {getFinalName(bundle, shape), shape};
166         }
167         return super.getFinalShape(bundle);
168     }
169
170     protected Object JavaDoc[] getFinalShape(IFeature feature) {
171         if (AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_MODE) == true)
172             return new Object JavaDoc[] {getFinalName(feature), FOLDER};
173         return super.getFinalShape(feature);
174     }
175 }
176
Popular Tags