blob: de86130d530012ed9ee7f6949bbad57befd0dd7e [file] [log] [blame]
package org.eclipse.epp.installer.core;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.epp.installer.core.model.IInstallStep;
import org.eclipse.epp.installer.core.model.IStepProcessor;
import org.eclipse.epp.installer.core.model.InstallStep;
import org.eclipse.epp.installer.core.model.Installer;
import org.eclipse.osgi.framework.internal.core.AbstractBundle;
import org.osgi.framework.Bundle;
public class ExtendableInstallConfiguration implements IConfigurationProvider {
public static final String EXTENSION_POINT_NAME =
"org.eclipse.epp.installer.core.steps"; //$NON-NLS-1$
private String id;
public void configure(Installer installer) {
final InstallOptions options = installer.getOptions();
installer.setTitle(
options.getString(InstallOptions.OPTION_PRODUCT_NAME) + " Installer"); //$NON-NLS-1$
InstallStepFactory factory = InstallStepFactory.getInstance();
IExtensionRegistry registry = org.eclipse.core.runtime.Platform.getExtensionRegistry();
IExtensionPoint extensionPoint = registry.getExtensionPoint(EXTENSION_POINT_NAME);
IExtension[] extensions = extensionPoint.getExtensions();
for (int a = 0; a < extensions.length; a++) {
IExtension extension = extensions[a];
AbstractBundle bundle = (AbstractBundle) org.eclipse.core.runtime.Platform.getBundle(
extension.getContributor().getName());
IConfigurationElement[] elements = extension.getConfigurationElements();
for (int i = 0; i < elements.length; i++) {
if ("steps".equals(elements[i].getName()) &&
getId() != null && getId().equals(elements[i].getAttribute("providerId"))) {
IConfigurationElement[] steps = elements[i].getChildren();
for (int b=0; b < steps.length; b++) {
IConfigurationElement element = steps[b];
String entryName = element.getName();
if ("step".equals(entryName)) { //$NON-NLS-1$
String id = element.getAttribute("id"); //$NON-NLS-1$
IInstallStep step = factory.createStep(id, installer);
if (step != null) {
String title = element.getAttribute("title"); //$NON-NLS-1$
String description = resolveNewLineCharacters(element.getAttribute("description")); //$NON-NLS-1$
if (step instanceof InstallStep) {
((InstallStep) step).setTitle(title);
((InstallStep) step).setDescription(description);
}
IConfigurationElement[] childs = element.getChildren();
for (int j = 0; j < childs.length; j++) {
String childName = childs[j].getName();
if ("parameter".equals(childName)) { //$NON-NLS-1$
String key = childs[j].getAttribute("key"); //$NON-NLS-1$
String value = resolveNewLineCharacters(childs[j].getAttribute("value")); //$NON-NLS-1$
step.setProperty(key, value);
}
else if ("process".equals(childName)) { //$NON-NLS-1$
getStepProcessor(childs[j].getAttribute("class"), bundle).process(step); //$NON-NLS-1$
}
else if ("aboutToStep".equals(childName)) { //$NON-NLS-1$
if (step instanceof InstallStep) {
IConfigurationElement[] aboutToStepChilds = childs[j].getChildren();
for (int k = 0; k < aboutToStepChilds.length; k++) {
String aboutToStepChildName = aboutToStepChilds[k].getName();
if ("parameter".equals(aboutToStepChildName)) { //$NON-NLS-1$
String key = aboutToStepChilds[k].getAttribute("key"); //$NON-NLS-1$
String value = resolveNewLineCharacters(aboutToStepChilds[k].getAttribute("value")); //$NON-NLS-1$
((InstallStep) step).aboutToStepSetProperty(key, value);
}
else if ("process".equals(aboutToStepChildName)) { //$NON-NLS-1$
IStepProcessor processor = getStepProcessor(aboutToStepChilds[k].getAttribute("class"), bundle); //$NON-NLS-1$
((InstallStep) step).aboutToStepAddProcessor(processor);
}
}
}
}
}
installer.add(step);
}
else {
//TODO: log error
System.out.println("Unable to create install step \"" + id + "\"");
}
}
}
}
}
}
}
private String resolveNewLineCharacters(String s) {
if (s == null) {
return null;
}
StringBuffer resultString = new StringBuffer();
int begin = 0;
while (true) {
int end = s.indexOf("\\n", begin);
if (end != -1) {
resultString.append(s.substring(begin, end));
resultString.append("\n");
begin = end + 2;
}
else {
resultString.append(s.substring(begin));
break;
}
}
return resultString.toString();
}
private IStepProcessor getStepProcessor(String className, Bundle bundle) {
try {
Class processClass = bundle.loadClass(className);
IStepProcessor processor = (IStepProcessor) processClass.newInstance();
return processor;
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}