Skip to content

Commit

Permalink
Merge pull request #2920 from chanikag/seqTask
Browse files Browse the repository at this point in the history
Support executing sequence at the MI server startup
  • Loading branch information
chanikag authored Sep 6, 2023
2 parents c81c7d5 + 7ccf3ac commit 0079a7f
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public static Artifact populateArtifact(OMElement artifactEle) {
// read top level attributes
artifact.setName(readAttribute(artifactEle, Artifact.NAME));
artifact.setVersion(readAttribute(artifactEle, Artifact.VERSION));
artifact.setMainSequence(readAttribute(artifactEle, Artifact.MAIN_SEQUENCE));
artifact.setType(readAttribute(artifactEle, Artifact.TYPE));
artifact.setServerRole(readAttribute(artifactEle, Artifact.SERVER_ROLE));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class CarbonApplication {
private String appFilePath;
private String appVersion;
private boolean deploymentCompleted;
private String mainSequence;

private ApplicationConfiguration appConfig;

Expand Down Expand Up @@ -100,5 +101,13 @@ public boolean isDeploymentCompleted() {
public void setDeploymentCompleted(boolean deploymentCompleted) {
this.deploymentCompleted = deploymentCompleted;
}

public String getMainSequence() {
return mainSequence;
}

public void setMainSequence(String mainSequence) {
this.mainSequence = mainSequence;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class ApplicationConfiguration {

private String appName;
private String appVersion;
private String mainSequence;
private org.wso2.micro.application.deployer.config.Artifact applicationArtifact;

/**
Expand Down Expand Up @@ -157,6 +158,7 @@ private void buildConfiguration(OMElement documentElement) throws CarbonExceptio
}
this.appName = appArtifact.getName();
this.appVersion = appArtifact.getVersion();
this.setMainSequence(appArtifact.getMainSequence());

String[] serverRoles = AppDeployerUtils.readServerRoles();
List<org.wso2.micro.application.deployer.config.Artifact.Dependency> depsToRemove = new ArrayList<org.wso2.micro.application.deployer.config.Artifact.Dependency>();
Expand Down Expand Up @@ -194,4 +196,11 @@ private void handleException(String msg, Exception e) throws CarbonException {
throw new CarbonException(msg, e);
}

public String getMainSequence() {
return mainSequence;
}

public void setMainSequence(String mainSequence) {
this.mainSequence = mainSequence;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Artifact {
public static final String NAME = "name";
public static final String TYPE = "type";
public static final String VERSION = "version";

public static final String MAIN_SEQUENCE = "mainSequence";
public static final String ARTIFACT_XML = "artifact.xml";
public static final String REG_INFO_XML = "registry-info.xml";

Expand All @@ -47,6 +49,7 @@ public class Artifact {
private String extractedPath;
private String runtimeObjectName;
private String deploymentStatus;
private String mainSequence;

private List<Dependency> dependencies;
private List<Artifact> subArtifacts;
Expand Down Expand Up @@ -155,6 +158,14 @@ public void setDeploymentStatus(String deploymentStatus) {
this.deploymentStatus = deploymentStatus;
}

public String getMainSequence() {
return mainSequence;
}

public void setMainSequence(String mainSequence) {
this.mainSequence = mainSequence;
}

public static class Dependency {
private String name;
private String version;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.micro.integrator.initializer;

import org.apache.synapse.MessageContext;
import org.apache.synapse.SequenceFlowObserver;
import org.wso2.micro.core.util.CoreServerInitializerHolder;

public class MicroIntegratorSequenceController implements SequenceFlowObserver {

private String name;

private String seqName;

@Override
public void setName(String observerName) {
name = observerName;
}

@Override
public void start(MessageContext messageContext, String observedSeq) {
if (messageContext.getProperty(ServiceBusConstants.AUTOMATION_MODE_MAIN_SEQ_PROPERTY) != null) {
seqName = (String) messageContext.getProperty(ServiceBusConstants.AUTOMATION_MODE_MAIN_SEQ_PROPERTY);
}
}

@Override
public void complete(MessageContext messageContext, String observedSeq) {
if (observedSeq != null && observedSeq.equals(seqName)) {
CoreServerInitializerHolder coreServerInitializerHolder = CoreServerInitializerHolder.getInstance();
coreServerInitializerHolder.shutdownGracefully();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,11 @@ public static final class RegistryStore {
public static final String SYNAPSE_CONNECTOR_PACKAGE = "org.wso2.carbon.connector";
public static final String DISABLE_CONNECTOR_INIT_SYSTEM_PROPERTY = "esb.connector.startup.init.disable";

// Constants used to start MI in automation mode

public static final String AUTOMATION_MODE_CAR_NAME_SYSTEM_PROPERTY = "automation.mode.seq.car.name";

public static final String AUTOMATION_MODE_MAIN_SEQ_PROPERTY = "AUTOMATION_MODE_MAIN_SEQUENCE";

}

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.AbstractExtendedSynapseHandler;
import org.apache.synapse.MessageContext;
import org.apache.synapse.ServerConfigurationInformation;
import org.apache.synapse.ServerConfigurationInformationFactory;
import org.apache.synapse.ServerContextInformation;
Expand All @@ -35,6 +36,7 @@
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.debug.SynapseDebugInterface;
import org.apache.synapse.debug.SynapseDebugManager;
import org.apache.synapse.mediators.base.SequenceMediator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationAdmin;
Expand All @@ -47,11 +49,13 @@
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.inbound.endpoint.EndpointListenerLoader;
import org.wso2.carbon.securevault.SecretCallbackHandlerService;
import org.wso2.micro.application.deployer.CarbonApplication;
import org.wso2.micro.core.Constants;
import org.wso2.micro.core.ServerShutdownHandler;
import org.wso2.micro.integrator.core.services.Axis2ConfigurationContextService;
import org.wso2.micro.integrator.core.services.CarbonServerConfigurationService;
import org.wso2.micro.integrator.core.util.MicroIntegratorBaseUtils;
import org.wso2.micro.integrator.initializer.deployment.application.deployer.CappDeployer;
import org.wso2.micro.integrator.initializer.handler.ProxyLogHandler;
import org.wso2.micro.integrator.initializer.handler.SynapseExternalPropertyConfigurator;
import org.wso2.micro.integrator.initializer.handler.transaction.TransactionCountHandler;
Expand Down Expand Up @@ -218,6 +222,18 @@ protected void activate(ComponentContext ctxt) {
configurationManager);*/
// Start Inbound Endpoint Listeners
EndpointListenerLoader.loadListeners();
String injectCarName = System.getProperty(ServiceBusConstants.AUTOMATION_MODE_CAR_NAME_SYSTEM_PROPERTY);
if (injectCarName != null && !injectCarName.isEmpty()) {
String sequenceName = getMainSequenceName(injectCarName);
if (sequenceName != null) {
MessageContext synCtx = synapseEnvironment.createMessageContext();
SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration().
getSequence(sequenceName);
synapseEnvironment.getSequenceObservers().add(new MicroIntegratorSequenceController());
synCtx.setProperty(ServiceBusConstants.AUTOMATION_MODE_MAIN_SEQ_PROPERTY, sequenceName);
synCtx.getEnvironment().injectMessage(synCtx, seq);
}
}
} catch (Exception e) {
handleFatal("Couldn't initialize the ESB...", e);
} catch (Throwable t) {
Expand All @@ -228,6 +244,20 @@ protected void activate(ComponentContext ctxt) {
}
}

private String getMainSequenceName(String cappName) {
CarbonApplication capp = CappDeployer.getCarbonAppByName(cappName);
if (capp == null) {
log.error("Invalid cApp name. cApp name: " + cappName + " not found");
return null;
}
String mainSeq = capp.getMainSequence();
if (mainSeq == null) {
log.error("Invalid main sequence name. Main sequence: " + mainSeq + " not found");
return null;
}
return capp.getMainSequence();
}

@Deactivate
protected void deactivate(ComponentContext ctxt) {
if (Objects.nonNull(transactionCountHandlerComponent)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ private CarbonApplication buildCarbonApplication(String targetCAppPath, String c
if (appVersion != null && !("").equals(appVersion)) {
carbonApplication.setAppVersion(appVersion);
}
String mainSeq = appConfig.getMainSequence();
if (mainSeq != null && !("").equals(mainSeq)) {
carbonApplication.setMainSequence(mainSeq);
}
return carbonApplication;
}

Expand Down Expand Up @@ -341,6 +345,15 @@ public static List<CarbonApplication> getCarbonApps() {
return Collections.unmodifiableList(cAppMap);
}

public static CarbonApplication getCarbonAppByName(String cAppName) {
for (CarbonApplication capp : cAppMap) {
if (cAppName.equals(capp.getAppName())) {
return capp;
}
}
return null;
}

/**
* Checks whether a given file is a jar or an aar file.
*
Expand Down
8 changes: 8 additions & 0 deletions distribution/src/assembly/bin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,14 @@
<fileMode>644</fileMode>
</file>

<file>
<source>src/conf/sequence-observers.xml</source>
<outputDirectory>wso2mi-${pom.version}/conf</outputDirectory>
<destName>sequence-observers.xml</destName>
<filtered>true</filtered>
<fileMode>644</fileMode>
</file>

<file>
<source>src/conf/security/cipher-text.properties</source>
<outputDirectory>wso2mi-${pom.version}/conf/security</outputDirectory>
Expand Down
26 changes: 26 additions & 0 deletions distribution/src/conf/sequence-observers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
~ Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 LLC. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<observers>
<!--
Example
<observer name="IntegratorSequenceObserver"
class="org.wso2.micro.integrator.initializer.MicroIntegratorSequenceController"/>
-->
</observers>
9 changes: 8 additions & 1 deletion distribution/src/scripts/micro-integrator.bat
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ if ""%1""==""stop"" goto stopServer
if ""%1""==""-stop"" goto stopServer
if ""%1""==""--stop"" goto stopServer

if ""%1""==""car"" goto setCar
if ""%1""==""-car"" goto setCar
if ""%1""==""--car"" goto setCar

shift
goto setupArgs

Expand Down Expand Up @@ -139,6 +143,9 @@ goto findJdk
if "%OS%"=="Windows_NT" @setlocal
if "%OS%"=="WINNT" @setlocal

:setCar
set CAR_NAME=%2

rem ---------- Handle the SSL Issue with proper JDK version --------------------
rem find the version of the jdk
:findJdk
Expand Down Expand Up @@ -190,7 +197,7 @@ if "%profileSet%" == "false" (
set profile=-Dprofile=micro-integrator-default
)

set CMD_LINE_ARGS=-Xbootclasspath/a:%CARBON_XBOOTCLASSPATH% -Xms256m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="%CARBON_HOME%\repository\logs\heap-dump.hprof" -Dcom.sun.management.jmxremote -classpath %CARBON_CLASSPATH% %JAVA_OPTS% -Djava.endorsed.dirs=%JAVA_ENDORSED% -DandesConfig=broker.xml -Dcarbon.registry.root=/ -Dcarbon.home="%CARBON_HOME%" -Dlogfiles.home="%CARBON_HOME%\repository\logs" -Dwso2.server.standalone=true -Djava.command="%JAVA_HOME%\bin\java" -Djava.opts="%JAVA_OPTS%" -Djava.io.tmpdir="%CARBON_HOME%\tmp" -Dcatalina.base="%CARBON_HOME%\wso2\lib\tomcat" -Dwso2.carbon.xml="%CARBON_HOME%\conf\carbon.xml" -Dwso2.registry.xml="%CARBON_HOME%\conf\registry.xml" -Dwso2.user.mgt.xml="%CARBON_HOME%\conf\user-mgt.xml" -Dwso2.transports.xml="%CARBON_HOME%\conf\mgt-transports.xml" -Djava.util.logging.config.file="%CARBON_HOME%\conf\log4j.properties" -Dcarbon.config.dir.path="%CARBON_HOME%\conf" -DNonUserCoreMode=true -DNonRegistryMode=true -Dcarbon.logs.path="%CARBON_HOME%\repository\logs" -Dcomponents.repo="%CARBON_HOME%\wso2\components\plugins" -Dcarbon.config.dir.path="%CARBON_HOME%\conf" -Dcarbon.components.dir.path="%CARBON_HOME%\wso2\components" -Dcarbon.dropins.dir.path="%CARBON_HOME%\dropins" -Dcarbon.external.lib.dir.path="%CARBON_HOME%\lib" -Dcarbon.patches.dir.path="%CARBON_HOME%\patches" -Dcarbon.internal.lib.dir.path="%CARBON_HOME%\wso2\lib" -Dconf.location="%CARBON_HOME%\conf" -Dcom.atomikos.icatch.file="%CARBON_HOME%\wso2\lib\transactions.properties" -Dei.extendedURIBasedDispatcher=org.wso2.micro.integrator.core.handlers.IntegratorStatefulHandler -Dcom.atomikos.icatch.hide_init_file_path="true" -Dorg.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true -Dcom.sun.jndi.ldap.connect.pool.authentication=simple -Dcom.sun.jndi.ldap.connect.pool.timeout=3000 -Dorg.terracotta.quartz.skipUpdateCheck=true -Dcarbon.classpath=%CARBON_CLASSPATH% -Dfile.encoding=UTF8 -Dlogger.server.name="micro-integrator" -Dqpid.conf="\conf\advanced" -Dproperties.file.path=default -DavoidConfigHashRead=true -DenableReadinessProbe=true -DenableLivenessProbe=true -DenableManagementApi=true -DskipStartupExtensions=false -Dlog4j2.contextSelector="org.apache.logging.log4j.core.async.AsyncLoggerContextSelector" -Dorg.ops4j.pax.logging.logReaderEnabled=false -Dorg.ops4j.pax.logging.eventAdminEnabled=false %JAVA_VER_BASED_OPTS% %profile% -Dorg.apache.activemq.SERIALIZABLE_PACKAGES="*"
set CMD_LINE_ARGS=-Xbootclasspath/a:%CARBON_XBOOTCLASSPATH% -Xms256m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="%CARBON_HOME%\repository\logs\heap-dump.hprof" -Dcom.sun.management.jmxremote -classpath %CARBON_CLASSPATH% %JAVA_OPTS% -Djava.endorsed.dirs=%JAVA_ENDORSED% -DandesConfig=broker.xml -Dcarbon.registry.root=/ -Dcarbon.home="%CARBON_HOME%" -Dlogfiles.home="%CARBON_HOME%\repository\logs" -Dwso2.server.standalone=true -Djava.command="%JAVA_HOME%\bin\java" -Djava.opts="%JAVA_OPTS%" -Djava.io.tmpdir="%CARBON_HOME%\tmp" -Dcatalina.base="%CARBON_HOME%\wso2\lib\tomcat" -Dwso2.carbon.xml="%CARBON_HOME%\conf\carbon.xml" -Dwso2.registry.xml="%CARBON_HOME%\conf\registry.xml" -Dwso2.user.mgt.xml="%CARBON_HOME%\conf\user-mgt.xml" -Dwso2.transports.xml="%CARBON_HOME%\conf\mgt-transports.xml" -Djava.util.logging.config.file="%CARBON_HOME%\conf\log4j.properties" -Dcarbon.config.dir.path="%CARBON_HOME%\conf" -DNonUserCoreMode=true -DNonRegistryMode=true -Dcarbon.logs.path="%CARBON_HOME%\repository\logs" -Dcomponents.repo="%CARBON_HOME%\wso2\components\plugins" -Dcarbon.config.dir.path="%CARBON_HOME%\conf" -Dcarbon.components.dir.path="%CARBON_HOME%\wso2\components" -Dcarbon.dropins.dir.path="%CARBON_HOME%\dropins" -Dcarbon.external.lib.dir.path="%CARBON_HOME%\lib" -Dcarbon.patches.dir.path="%CARBON_HOME%\patches" -Dcarbon.internal.lib.dir.path="%CARBON_HOME%\wso2\lib" -Dconf.location="%CARBON_HOME%\conf" -Dcom.atomikos.icatch.file="%CARBON_HOME%\wso2\lib\transactions.properties" -Dei.extendedURIBasedDispatcher=org.wso2.micro.integrator.core.handlers.IntegratorStatefulHandler -Dcom.atomikos.icatch.hide_init_file_path="true" -Dorg.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true -Dcom.sun.jndi.ldap.connect.pool.authentication=simple -Dcom.sun.jndi.ldap.connect.pool.timeout=3000 -Dorg.terracotta.quartz.skipUpdateCheck=true -Dcarbon.classpath=%CARBON_CLASSPATH% -Dfile.encoding=UTF8 -Dlogger.server.name="micro-integrator" -Dqpid.conf="\conf\advanced" -Dproperties.file.path=default -DavoidConfigHashRead=true -DenableReadinessProbe=true -DenableLivenessProbe=true -DenableManagementApi=true -DskipStartupExtensions=false -Dautomation.mode.seq.car.name="%CAR_NAME%" -Dlog4j2.contextSelector="org.apache.logging.log4j.core.async.AsyncLoggerContextSelector" -Dorg.ops4j.pax.logging.logReaderEnabled=false -Dorg.ops4j.pax.logging.eventAdminEnabled=false %JAVA_VER_BASED_OPTS% %profile% -Dorg.apache.activemq.SERIALIZABLE_PACKAGES="*"

:runJava
rem echo JAVA_HOME environment variable is set to %JAVA_HOME%
Expand Down
8 changes: 8 additions & 0 deletions distribution/src/scripts/micro-integrator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,18 @@ do
CMD="version"
elif [ "$c" = "--restart" ] || [ "$c" = "-restart" ] || [ "$c" = "restart" ]; then
CMD="restart"
elif [ "$c" = "--car" ] || [ "$c" = "-car" ] || [ "$c" = "car" ]; then
ARGUMENT="car"
OPTARG="$2"
else
args="$args $c"
fi
done

if [ "$ARGUMENT" = "car" ]; then
CAR_NAME="$OPTARG"
fi

if [ "$CMD" = "--debug" ]; then
if [ "$PORT" = "" ]; then
echo " Please specify the debug port after the --debug option"
Expand Down Expand Up @@ -331,6 +338,7 @@ do
-DenableLivenessProbe=true \
-DenableManagementApi=true \
-DskipStartupExtensions=false \
-Dautomation.mode.seq.car.name="$CAR_NAME" \
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector \
-Dorg.ops4j.pax.logging.logReaderEnabled=false \
-Dorg.ops4j.pax.logging.eventAdminEnabled=false \
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@
<com.sun.jaxb.version>2.3.0</com.sun.jaxb.version>
<com.sun.jaxb.impl.version>2.3.1</com.sun.jaxb.impl.version>

<synapse.version>4.0.0-wso2v45</synapse.version>
<synapse.version>4.0.0-wso2v47</synapse.version>
<imp.pkg.version.synapse>[4.0.0, 4.0.1)</imp.pkg.version.synapse>
<carbon.mediation.version>4.7.175</carbon.mediation.version>
<carbon.crypto.version>1.1.3</carbon.crypto.version>
Expand Down

0 comments on commit 0079a7f

Please sign in to comment.