KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > ProductGenerator


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.internal.build;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.osgi.service.resolver.BundleDescription;
18 import org.eclipse.pde.internal.build.site.PDEState;
19
20 public class ProductGenerator extends AbstractScriptGenerator {
21     private static final String JavaDoc BUNDLE_EQUINOX_COMMON = "org.eclipse.equinox.common"; //$NON-NLS-1$
22
private static final String JavaDoc BUNDLE_OSGI = "org.eclipse.osgi"; //$NON-NLS-1$
23
private static final String JavaDoc BUNDLE_CORE_RUNTIME = "org.eclipse.core.runtime"; //$NON-NLS-1$
24
private static final String JavaDoc BUNDLE_UPDATE_CONFIGURATOR = "org.eclipse.update.configurator"; //$NON-NLS-1$
25
private static final String JavaDoc START_LEVEL_2 = "@2:start"; //$NON-NLS-1$
26
private static final String JavaDoc START_LEVEL_3 = "@3:start"; //$NON-NLS-1$
27
private static final String JavaDoc START = "@start"; //$NON-NLS-1$
28

29     private String JavaDoc product = null;
30     private ProductFile productFile = null;
31     private String JavaDoc root = null;
32     private boolean refactoredRuntime = false;
33     private Properties buildProperties;
34
35     /* (non-Javadoc)
36      * @see org.eclipse.pde.internal.build.AbstractScriptGenerator#generate()
37      */

38     public void generate() throws CoreException {
39         initialize();
40
41         if (productFile == null)
42             return;
43
44         //we need at least a product id
45
if (productFile.getId() == null) {
46             return;
47         }
48
49         String JavaDoc custom = findFile(productFile.getConfigIniPath(), false);
50         String JavaDoc location = null, fileList = null;
51         for (Iterator iter = getConfigInfos().iterator(); iter.hasNext();) {
52             Config config = (Config) iter.next();
53             location = DEFAULT_PRODUCT_ROOT_FILES_DIR + '/' + config.toStringReplacingAny(".", ANY_STRING); //$NON-NLS-1$
54

55             String JavaDoc rootLocation = root + location;
56             File rootDir = new File(rootLocation);
57             if ((!rootDir.exists() && !rootDir.mkdirs()) || rootDir.isFile())
58                 continue; //we will fail trying to create the files, TODO log warning/error
59

60             //add generated root files to build.properties
61
if (buildProperties != null) {
62                 fileList = buildProperties.getProperty(ROOT_PREFIX + config.toString("."), ""); //$NON-NLS-1$ //$NON-NLS-2$
63
fileList += (fileList.length() > 0) ? ',' + location : location;
64                 buildProperties.put(ROOT_PREFIX + config.toString("."), fileList); //$NON-NLS-1$
65
}
66
67             //configuration/config.ini
68
if (custom != null) {
69                 copyFile(custom, rootLocation + "/configuration/config.ini"); //$NON-NLS-1$
70
} else {
71                 createConfigIni(config, rootLocation);
72             }
73
74             //only the config.ini makes sense in the any config
75
if (config.getOs().equals(Config.ANY))
76                 continue;
77
78             //.eclipseproduct
79
createEclipseProductFile(rootLocation);
80
81             //eclipse.ini
82
createLauncherIniFile(rootLocation, config.getOs());
83         }
84
85     }
86
87     private void initialize() throws CoreException {
88         loadProduct();
89
90         PDEState state = getSite(false).getRegistry();
91         refactoredRuntime = state.getResolvedBundle(BUNDLE_EQUINOX_COMMON) != null;
92     }
93
94     private void loadProduct() throws CoreException {
95         if (product == null || product.startsWith("${")) { //$NON-NLS-1$
96
productFile = null;
97             return;
98         }
99         String JavaDoc productPath = findFile(product, false);
100         File f = null;
101         if (productPath != null) {
102             f = new File(productPath);
103         } else {
104             // couldn't find productFile, try it as a path directly
105
f = new File(product);
106             if (!f.exists() || !f.isFile()) {
107                 // doesn't exist, try it as a path relative to the working directory
108
f = new File(getWorkingDirectory(), product);
109                 if (!f.exists() || !f.isFile()) {
110                     f = new File(getWorkingDirectory() + "/" + DEFAULT_PLUGIN_LOCATION, product); //$NON-NLS-1$
111
}
112             }
113         }
114
115         //the ProductFile uses the OS to determine which icons to return, we don't care so can use null
116
//this is better since this generator may be used for multiple OS's
117
productFile = new ProductFile(f.getAbsolutePath(), null);
118     }
119
120     private void copyFile(String JavaDoc src, String JavaDoc dest) {
121         File source = new File(src);
122         if (!source.exists())
123             return;
124         File destination = new File(dest);
125         File destDir = destination.getParentFile();
126         if ((!destDir.exists() && !destDir.mkdirs()) || destDir.isFile())
127             return; //we will fail trying to create the file, TODO log warning/error
128

129         InputStream in = null;
130         OutputStream out = null;
131         try {
132             in = new BufferedInputStream(new FileInputStream(source));
133             out = new BufferedOutputStream(new FileOutputStream(destination));
134
135             //Transfer bytes from in to out
136
byte[] buf = new byte[1024];
137             int len;
138             while ((len = in.read(buf)) > 0) {
139                 out.write(buf, 0, len);
140             }
141         } catch (IOException e) {
142             //nothing
143
} finally {
144             if (in != null) {
145                 try {
146                     in.close();
147                 } catch (IOException e) {
148                     //nothing
149
}
150             }
151             if (out != null) {
152                 try {
153                     out.close();
154                 } catch (IOException e) {
155                     //nothing
156
}
157             }
158         }
159     }
160
161     private void createConfigIni(Config config, String JavaDoc location) throws CoreException {
162         File configDir = new File(location + "/configuration"); //$NON-NLS-1$
163
if ((!configDir.exists() && !configDir.mkdirs()) || configDir.isFile())
164             return; //we will fail trying to create the file, TODO log warning/error
165

166         PDEState state = getSite(false).getRegistry();
167
168         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
169         buffer.append("#Product Runtime Configuration File\n"); //$NON-NLS-1$
170

171         String JavaDoc splash = getSplashLocation(config);
172         if (splash != null)
173             buffer.append("osgi.splashPath=" + splash + '\n'); //$NON-NLS-1$
174

175         String JavaDoc application = productFile.getApplication();
176         if (application != null)
177             buffer.append("eclipse.application=" + application + '\n'); //$NON-NLS-1$
178
buffer.append("eclipse.product=" + productFile.getId() + '\n'); //$NON-NLS-1$
179
buffer.append("osgi.bundles="); //$NON-NLS-1$
180
//When update configurator is present or when feature based product
181
if (productFile.useFeatures() || productFile.containsPlugin(BUNDLE_UPDATE_CONFIGURATOR)) {
182             if (refactoredRuntime) {
183                 //start levels for eclipse 3.2
184
//org.eclipse.equinox.common@2:start,
185
buffer.append(BUNDLE_EQUINOX_COMMON);
186                 buffer.append(START_LEVEL_2);
187                 buffer.append(',');
188                 //org.eclipse.update.configurator@3:start
189
buffer.append(BUNDLE_UPDATE_CONFIGURATOR);
190                 buffer.append(START_LEVEL_3);
191                 buffer.append(',');
192                 //org.eclipse.core.runtime
193
buffer.append(BUNDLE_CORE_RUNTIME);
194                 buffer.append(START);
195                 buffer.append('\n');
196             } else {
197                 //start level for 3.1 and 3.0
198
buffer.append(BUNDLE_CORE_RUNTIME);
199                 buffer.append(START_LEVEL_2);
200                 buffer.append(',');
201                 buffer.append(BUNDLE_UPDATE_CONFIGURATOR);
202                 buffer.append(START_LEVEL_3);
203                 buffer.append('\n');
204             }
205         } else {
206             //When the plugins are all listed.
207
Dictionary environment = new Hashtable(3);
208             environment.put("osgi.os", config.getOs()); //$NON-NLS-1$
209
environment.put("osgi.ws", config.getWs()); //$NON-NLS-1$
210
environment.put("osgi.arch", config.getArch()); //$NON-NLS-1$
211

212             List pluginList = productFile.getPlugins();
213             BundleHelper helper = BundleHelper.getDefault();
214             boolean first = true;
215             for (Iterator iter = pluginList.iterator(); iter.hasNext();) {
216                 String JavaDoc id = (String JavaDoc) iter.next();
217                 BundleDescription bundle = state.getResolvedBundle(id);
218                 if (bundle == null) {
219                     //TODO error?
220
continue;
221                 }
222                 String JavaDoc filter = bundle.getPlatformFilter();
223                 if (filter == null || helper.createFilter(filter).match(environment)) {
224                     if (BUNDLE_OSGI.equals(id))
225                         continue;
226                     if (first)
227                         first = false;
228                     else
229                         buffer.append(","); //$NON-NLS-1$
230
buffer.append(id);
231                     if (BUNDLE_EQUINOX_COMMON.equals(id)) {
232                         buffer.append(START_LEVEL_2);
233                     } else if (BUNDLE_CORE_RUNTIME.equals(id)) {
234                         if (refactoredRuntime) {
235                             buffer.append(START);
236                         } else {
237                             buffer.append(START_LEVEL_2);
238                         }
239                     }
240                 }
241             }
242             buffer.append('\n');
243         }
244         buffer.append("osgi.bundles.defaultStartLevel=4\n"); //$NON-NLS-1$
245

246         FileWriter writer = null;
247         try {
248             writer = new FileWriter(new File(configDir, "config.ini")); //$NON-NLS-1$
249
writer.write(buffer.toString());
250         } catch (IOException e) {
251             //nothing
252
} finally {
253             try {
254                 if (writer != null)
255                     writer.close();
256             } catch (IOException e) {
257                 //nothing
258
}
259         }
260
261     }
262
263     private void createEclipseProductFile(String JavaDoc directory) throws CoreException {
264         File dir = new File(directory);
265         if ((!dir.exists() && !dir.mkdirs()) || dir.isFile())
266             return; //we will fail trying to create the file, TODO log warning/error
267

268         Properties properties = new Properties();
269         if (productFile.getProductName() != null)
270             properties.put("name", productFile.getProductName()); //$NON-NLS-1$
271
if (productFile.getId() != null)
272             properties.put("id", productFile.getId()); //$NON-NLS-1$
273

274         BundleDescription bundle = getSite(false).getRegistry().getResolvedBundle(getBrandingPlugin());
275         if (bundle != null)
276             properties.put("version", bundle.getVersion().toString()); //$NON-NLS-1$
277

278         OutputStream stream = null;
279         try {
280             File file = new File(dir, ".eclipseproduct"); //$NON-NLS-1$
281
stream = new BufferedOutputStream(new FileOutputStream(file));
282             properties.store(stream, "Eclipse Product File"); //$NON-NLS-1$
283
stream.flush();
284         } catch (IOException e) {
285             //nothing
286
} finally {
287             if (stream != null) {
288                 try {
289                     stream.close();
290                 } catch (IOException e) {
291                     //nothing
292
}
293             }
294         }
295     }
296
297     private String JavaDoc getBrandingPlugin() {
298         String JavaDoc id = productFile.getId();
299         if (id == null)
300             return null;
301         int dot = id.lastIndexOf('.');
302         return (dot != -1) ? id.substring(0, dot) : null;
303     }
304
305     private String JavaDoc getSplashLocation(Config config) throws CoreException {
306         String JavaDoc plugin = productFile.getSplashLocation();
307
308         if (plugin == null) {
309             plugin = getBrandingPlugin();
310         }
311
312         if (plugin == null)
313             return null;
314
315         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("platform:/base/plugins/"); //$NON-NLS-1$
316
buffer.append(plugin);
317
318         Dictionary environment = new Hashtable(4);
319         environment.put("osgi.os", config.getOs()); //$NON-NLS-1$
320
environment.put("osgi.ws", config.getWs()); //$NON-NLS-1$
321
environment.put("osgi.arch", config.getArch()); //$NON-NLS-1$
322

323         PDEState state = getSite(false).getRegistry();
324         BundleHelper helper = BundleHelper.getDefault();
325         BundleDescription bundle = state.getResolvedBundle(plugin);
326         if (bundle != null) {
327             BundleDescription[] fragments = bundle.getFragments();
328             for (int i = 0; i < fragments.length; i++) {
329                 String JavaDoc filter = fragments[i].getPlatformFilter();
330                 if (filter == null || helper.createFilter(filter).match(environment)) {
331                     String JavaDoc fragmentId = fragments[i].getSymbolicName();
332                     if (productFile.containsPlugin(fragmentId)) {
333                         buffer.append(",platform:/base/plugins/"); //$NON-NLS-1$
334
buffer.append(fragmentId);
335                     }
336                 }
337             }
338         }
339         return buffer.toString();
340     }
341
342     private void createLauncherIniFile(String JavaDoc directory, String JavaDoc os) {
343         String JavaDoc launcher = getLauncherName();
344
345         if (os.equals(Platform.OS_MACOSX)) {
346             directory += "/" + launcher + ".app/Contents/MacOS"; //$NON-NLS-1$//$NON-NLS-2$
347
}
348         File dir = new File(directory);
349         if ((!dir.exists() && !dir.mkdirs()) || dir.isFile())
350             return; //we will fail trying to create the file TODO log warning/error
351

352         String JavaDoc programArgs = productFile.getProgramArguments(os);
353         String JavaDoc vmArgs = productFile.getVMArguments(os);
354
355         if ((programArgs == null || programArgs.length() == 0) && (vmArgs == null || vmArgs.length() == 0))
356             return;
357
358         String JavaDoc lineDelimiter = Platform.OS_WIN32.equals(os) ? "\r\n" : "\n"; //$NON-NLS-1$ //$NON-NLS-2$
359
PrintWriter writer = null;
360         try {
361             writer = new PrintWriter(new FileWriter(new File(dir, launcher + ".ini"))); //$NON-NLS-1$
362
if (programArgs != null && programArgs.length() > 0) {
363                 StringReader reader = new StringReader(programArgs);
364                 StreamTokenizer tokenizer = new StreamTokenizer(reader);
365                 tokenizer.resetSyntax();
366                 tokenizer.whitespaceChars(0,0x20);
367                 tokenizer.wordChars(0x21, 0xFF);
368                 tokenizer.quoteChar('"');
369                 tokenizer.quoteChar('\'');
370                 while (tokenizer.nextToken() != StreamTokenizer.TT_EOF){
371                     writer.print(tokenizer.sval);
372                     writer.print(lineDelimiter);
373                 }
374             }
375             if (vmArgs != null && vmArgs.length() > 0) {
376                 writer.print("-vmargs"); //$NON-NLS-1$
377
writer.print(lineDelimiter);
378                 StringReader reader = new StringReader(vmArgs);
379                 StreamTokenizer tokenizer = new StreamTokenizer(reader);
380                 tokenizer.resetSyntax();
381                 tokenizer.whitespaceChars(0,0x20);
382                 tokenizer.wordChars(0x21, 0xFF);
383                 tokenizer.quoteChar('"');
384                 tokenizer.quoteChar('\'');
385                 while (tokenizer.nextToken() != StreamTokenizer.TT_EOF){
386                     writer.print(tokenizer.sval);
387                     writer.print(lineDelimiter);
388                 }
389             }
390         } catch (IOException e) {
391             //nothing
392
} finally {
393             if (writer != null) {
394                 writer.close();
395             }
396         }
397     }
398
399     private String JavaDoc getLauncherName() {
400         String JavaDoc name = productFile.getLauncherName();
401
402         if (name != null && name.length() > 0) {
403             name = name.trim();
404             if (name.endsWith(".exe")) //$NON-NLS-1$
405
name = name.substring(0, name.length() - 4);
406             return name;
407         }
408         return "eclipse"; //$NON-NLS-1$
409
}
410
411     public void setProduct(String JavaDoc product) {
412         this.product = product;
413     }
414
415     public void setRoot(String JavaDoc root) {
416         this.root = root;
417     }
418
419     public void setBuildProperties(Properties buildProperties) {
420         this.buildProperties = buildProperties;
421     }
422
423 }
424
Popular Tags