|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006 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 - 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.*; |
| 16 |
import org.eclipse.osgi.service.resolver.BundleDescription; |
| 17 |
import org.eclipse.osgi.util.NLS; |
| 18 |
import org.eclipse.pde.internal.build.site.PDEState; |
| 19 |
|
| 20 |
public class FeatureGenerator extends AbstractScriptGenerator { |
| 21 |
private String featureId = null; |
| 22 |
private String productFile = null; |
| 23 |
private String[] pluginList = null; |
| 24 |
private String[] featureList = null; |
| 25 |
|
| 26 |
private ProductFile product = null; |
| 27 |
|
| 28 |
private boolean verify = false; |
| 29 |
|
| 30 |
/* (non-Javadoc) |
| 31 |
* @see org.eclipse.pde.internal.build.AbstractScriptGenerator#generate() |
| 32 |
*/ |
| 33 |
public void generate() throws CoreException { |
| 34 |
initialize(); |
| 35 |
|
| 36 |
List plugins = pluginList != null ? Arrays.asList(pluginList) : new ArrayList(); |
| 37 |
List features = featureList != null ? Arrays.asList(featureList) : new ArrayList(); |
| 38 |
if (product != null) { |
| 39 |
List productElements = product.useFeatures() ? product.getFeatures() : product.getPlugins(); |
| 40 |
if (productElements != null && productElements.size() > 0) { |
| 41 |
if (product.useFeatures()) |
| 42 |
features.addAll(productElements); |
| 43 |
else |
| 44 |
plugins.addAll(productElements); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
try { |
| 49 |
createFeature(featureId, plugins, features); |
| 50 |
} catch (FileNotFoundException e) { |
| 51 |
//error |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public void setProductFile(String productFile) { |
| 56 |
this.productFile = productFile; |
| 57 |
} |
| 58 |
|
| 59 |
public void setPluginList(String[] pluginList) { |
| 60 |
this.pluginList = pluginList; |
| 61 |
} |
| 62 |
|
| 63 |
public void setFeatureList(String[] featureList) { |
| 64 |
this.featureList = featureList; |
| 65 |
} |
| 66 |
|
| 67 |
public void setFeatureId(String featureId) { |
| 68 |
this.featureId = featureId; |
| 69 |
} |
| 70 |
|
| 71 |
private void initialize(){ |
| 72 |
if (productFile != null && !productFile.startsWith("${")) { //$NON-NLS-1$ |
| 73 |
String productPath = findFile(productFile, false); |
| 74 |
if (productPath == null) |
| 75 |
productPath = productFile; |
| 76 |
File f = new File(productPath); |
| 77 |
if (f.exists() && f.isFile()) { |
| 78 |
try { |
| 79 |
product = new ProductFile(productPath, null); |
| 80 |
} catch (CoreException e) { |
| 81 |
// log |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
protected void createFeature(String feature, List plugins, List features) throws CoreException, FileNotFoundException { |
| 88 |
String location = IPDEBuildConstants.DEFAULT_FEATURE_LOCATION + '/' + feature; |
| 89 |
File directory = new File(getWorkingDirectory(), location); |
| 90 |
if (!directory.exists()) |
| 91 |
directory.mkdirs(); |
| 92 |
|
| 93 |
PDEState state = verify ? getSite(false).getRegistry() : null; |
| 94 |
|
| 95 |
//Create feature.xml |
| 96 |
File file = new File(directory, IPDEBuildConstants.DEFAULT_FEATURE_FILENAME_DESCRIPTOR); |
| 97 |
OutputStream output = new FileOutputStream(file); |
| 98 |
XMLWriter writer = null; |
| 99 |
try { |
| 100 |
writer = new XMLWriter(output); |
| 101 |
} catch (UnsupportedEncodingException e) { |
| 102 |
//should not happen |
| 103 |
return; |
| 104 |
} |
| 105 |
Map parameters = new HashMap(); |
| 106 |
|
| 107 |
parameters.put("id", feature); //$NON-NLS-1$ |
| 108 |
parameters.put("version", "1.0.0"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 109 |
writer.startTag("feature", parameters, true); //$NON-NLS-1$ |
| 110 |
|
| 111 |
for (Iterator iter = plugins.iterator(); iter.hasNext();) { |
| 112 |
String name = (String) iter.next(); |
| 113 |
boolean unpack = true; |
| 114 |
if (verify) { |
| 115 |
BundleDescription bundle = state.getResolvedBundle(name); |
| 116 |
if (bundle != null){ |
| 117 |
String [] bundleClasspath = (String[]) state.getExtraData().get(new Long(bundle.getBundleId())); |
| 118 |
unpack = !(bundleClasspath == null || bundleClasspath.length == 0 || bundleClasspath[0].equals(".")); //$NON-NLS-1$ |
| 119 |
} else { |
| 120 |
//throw error |
| 121 |
String message = NLS.bind(Messages.exception_missingPlugin, name); |
| 122 |
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PLUGIN_MISSING, message, null)); |
| 123 |
} |
| 124 |
} |
| 125 |
parameters.clear(); |
| 126 |
parameters.put("id", name); //$NON-NLS-1$ |
| 127 |
parameters.put("version", "0.0.0"); //$NON-NLS-1$//$NON-NLS-2$ |
| 128 |
parameters.put("unpack", unpack ? "true" : "false"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ |
| 129 |
writer.printTag("plugin", parameters, true, true, true); //$NON-NLS-1$ |
| 130 |
} |
| 131 |
|
| 132 |
for (Iterator iter = features.iterator(); iter.hasNext();) { |
| 133 |
String name = (String) iter.next(); |
| 134 |
if (verify) { |
| 135 |
//this will throw an exception if the feature is not found. |
| 136 |
getSite(false).findFeature(name, null, true); |
| 137 |
} |
| 138 |
parameters.clear(); |
| 139 |
parameters.put("id", name); //$NON-NLS-1$ |
| 140 |
parameters.put("version", "0.0.0"); //$NON-NLS-1$//$NON-NLS-2$ |
| 141 |
writer.printTag("includes", parameters, true, true, true); //$NON-NLS-1$ |
| 142 |
|
| 143 |
} |
| 144 |
writer.endTag("feature"); //$NON-NLS-1$ |
| 145 |
writer.close(); |
| 146 |
|
| 147 |
//create build.properties |
| 148 |
file = new File(directory, IPDEBuildConstants.PROPERTIES_FILE); |
| 149 |
Properties prop = new Properties(); |
| 150 |
prop.put("pde", "marker"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 151 |
FileOutputStream stream = null; |
| 152 |
try{ |
| 153 |
stream = new FileOutputStream(file); |
| 154 |
prop.store(stream, "Marker File so that the file gets written"); //$NON-NLS-1$ |
| 155 |
stream.flush(); |
| 156 |
} catch (IOException e) { |
| 157 |
if( stream != null) { |
| 158 |
try { |
| 159 |
stream.close(); |
| 160 |
} catch (IOException e1) { |
| 161 |
// nothing |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
public void setVerify(boolean verify) { |
| 169 |
this.verify = verify; |
| 170 |
} |
| 171 |
} |