blob: f75c783ed0dfb0f3f629c7c9a15d1745e161a289 [file] [log] [blame]
/*******************************************************************************
*
* Copyright (c) 2018, 2020 Robert Bosch GmbH.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
*******************************************************************************/
package org.eclipse.app4mc.transformation.starter;
import java.io.File;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.app4mc.transformation.ServiceConstants;
import org.eclipse.app4mc.transformation.TransformationProcessor;
import org.eclipse.osgi.service.environment.EnvironmentInfo;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
/**
* Immediate component that gets activated once the Equinox
* {@link EnvironmentInfo} is available. Triggers the Transformation process based on
* the command line arguments. Via -console parameter it is also possible to
* simply start the application in an interactive mode by keeping the OSGi
* console open. Otherwise the application will immediately closed after
* execution.
* <p>
* <b>Note:</b><br>
* Even when starting the app with the Equinox OSGi framework via bnd launcher,
* the {@link EnvironmentInfo} will be available. But it will not contain the
* necessary information that typically gets set the the Equinox launcher.
* Therefore the Transformation start will not be processed if the
* {@link EnvironmentInfo} is not initialized properly.
* </p>
*/
@Component(immediate = true,
property = EventConstants.EVENT_TOPIC + "=" + "org/osgi/service/cm/ConfigurationEvent/CM_DELETED"
)
public class EquinoxTransformationStarter implements EventHandler {
private AtomicBoolean started = new AtomicBoolean(false);
/**
* Launcher arguments provided by the Equinox launcher.
*/
@Reference
EnvironmentInfo environmentInfo;
@Reference
protected TransformationProcessor transformationProcessor;
@Activate
void activate() {
// only process model Transformation activation if the EnvironmentInfo is initialized
// properly
if (this.environmentInfo.getNonFrameworkArgs() != null) {
// check if a product or an application was started
// if yes this starter should do nothing
boolean isProduct =
Arrays.stream(environmentInfo.getNonFrameworkArgs()).anyMatch(arg -> "-product".equals(arg))
|| this.environmentInfo.getProperty("eclipse.product") != null;
boolean ignoreApp = Boolean.valueOf(System.getProperty("eclipse.ignoreApp"));
boolean isApplication = !ignoreApp &&
(Arrays.stream(environmentInfo.getNonFrameworkArgs()).anyMatch(arg -> "-application".equals(arg))
|| this.environmentInfo.getProperty("eclipse.application") != null);
if (!isProduct && !isApplication && this.started.compareAndSet(false, true)) {
startTransformation(this.environmentInfo.getNonFrameworkArgs());
}
}
}
protected void startTransformation(String[] args) {
String propertiesFilePath = null;
String workingDirectory=null;
// TODO add support for single parameter values instead of properties file path
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.equals("--properties")) {
propertiesFilePath = args[++i];
} else if(arg.equals("--workingDirectory")) {
workingDirectory = args[++i];
}
}
if (propertiesFilePath == null || propertiesFilePath.isEmpty()) {
System.err.println("Model transformation can not be performed as Properties file path is not supplied");
System.exit(0);
}
File propertiesFile = new File(propertiesFilePath);
if (!propertiesFile.exists()) {
if (workingDirectory != null) {
propertiesFile = new File(workingDirectory, propertiesFilePath);
if (!propertiesFile.exists()) {
// the file seems to not exist
throw new IllegalArgumentException(
"input properties file doesn't seem to exist. Check the value of command line parameter '--properties' ");
}
} else {
throw new IllegalArgumentException(
"input properties file doesn't seem to exist, also '--workingDirectory' commandline parameter is not supplied");
}
}
this.transformationProcessor.startTransformation(propertiesFile, workingDirectory);
}
@Override
public void handleEvent(Event event) {
if (this.started.get()
&& event.getProperty("cm.factoryPid").equals(ServiceConstants.SESSION_CONFIGURATION_PID)) {
// shut down the application if this immediate component started the
// transformation and the session configuration was deleted
System.exit(0);
}
}
}