blob: 8792f88fbc11ca177887cdeab846dcc399e9a400 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2014 Bosch Software Innovations GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Bosch Software Innovations GmbH - Please refer to git log
*
*******************************************************************************/
package org.eclipse.vorto.codegen.internal.handler;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.vorto.api.console.ConsoleDisplayMgr;
import org.eclipse.vorto.codegen.api.ICodeGenerator;
import org.eclipse.vorto.functionblock.FunctionblockModel;
public class GenerateService {
public static final String CODE_GEN_JOBFAMILY = "IoTGenerator";
public static void runGenerateCodeInBackground(
final IConfigurationElement[] configElements,
final FunctionblockModel functionBlockModel) {
try {
ConsoleDisplayMgr.getDefault().printInfo("GenerateService invoked");
for (IConfigurationElement e : configElements) {
final Object o = e.createExecutableExtension("class");
if (o instanceof ICodeGenerator) {
final String generatorName = e.getAttribute("menuLabel");
Job job = new Job("Generating Code using " + generatorName) {
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("Invoking generator ["
+ generatorName + "]",
configElements.length);
try {
((ICodeGenerator) o).generate(
functionBlockModel, monitor);
monitor.worked(1);
if (monitor.isCanceled()) {
monitor.done();
return Status.CANCEL_STATUS;
}
} catch (Exception e) {
e.printStackTrace();
return Status.CANCEL_STATUS;
}
monitor.done();
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return family == CODE_GEN_JOBFAMILY;
}
};
job.setUser(true);
job.schedule();
// job.join(); // execute synchronously
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}