1414import dev .objz .commandbridge .logging .Summary ;
1515import dev .objz .commandbridge .scripting .model .Script ;
1616import dev .objz .commandbridge .scripting .model .enums .ArgType ;
17+ import dev .objz .commandbridge .scripting .model .enums .Location ;
1718import dev .objz .commandbridge .scripting .model .enums .RunAs ;
1819import dev .objz .commandbridge .scripting .model .records .mapping .ArgMapping ;
1920import dev .objz .commandbridge .scripting .model .records .mapping .CmdMapping ;
@@ -163,7 +164,7 @@ private void dispatchCommand(ExecutionContext ctx, CmdMapping cmd) {
163164 var targets = Optional .ofNullable (cmd .execute ()).orElse (List .of ());
164165
165166 if (targets .isEmpty ()) {
166- Log .warn ("Command '{}' has no execution targets" , cmd .command ());
167+ Log .warn ("Command '{}' has no execution targets defined " , cmd .command ());
167168 notifyExecutionError (ctx .source (), cmd .command (), "No execution targets configured" );
168169 return ;
169170 }
@@ -172,25 +173,22 @@ private void dispatchCommand(ExecutionContext ctx, CmdMapping cmd) {
172173 }
173174
174175 private void dispatchToTarget (ExecutionContext ctx , CmdMapping cmd , IdMapping target ) {
175- var dispatcher = switch (target .location ()) {
176- case VELOCITY -> velocityDispatcher (ctx , cmd );
177- case BACKEND -> remoteDispatcher (ctx , cmd );
178- };
179- dispatcher .accept (target .id ());
180- }
176+ String targetId = target .id ();
177+ Location targetLoc = target .location ();
181178
182- private Consumer <String > velocityDispatcher (ExecutionContext ctx , CmdMapping cmd ) {
183- return targetId -> {
179+ if (targetLoc == Location .VELOCITY ) {
184180 if (velocityExecutor .isLocal (targetId )) {
185181 executeLocally (ctx , cmd );
186182 } else {
187- dispatchToRemoteSession (ctx , cmd , targetId );
183+ dispatchToRemoteSession (ctx , cmd , targetId , Location . VELOCITY );
188184 }
189- };
190- }
191-
192- private Consumer <String > remoteDispatcher (ExecutionContext ctx , CmdMapping cmd ) {
193- return targetId -> dispatchToRemoteSession (ctx , cmd , targetId );
185+ } else if (targetLoc == Location .BACKEND ) {
186+ dispatchToRemoteSession (ctx , cmd , targetId , Location .BACKEND );
187+ } else {
188+ Log .warn ("Unknown location type '{}' for target '{}'" , targetLoc , targetId );
189+ notifyExecutionError (ctx .source (), cmd .command (),
190+ "Unknown location type: " + targetLoc );
191+ }
194192 }
195193
196194 private void executeLocally (ExecutionContext ctx , CmdMapping cmd ) {
@@ -204,7 +202,6 @@ private void executeLocally(ExecutionContext ctx, CmdMapping cmd) {
204202 notifyExecutionError (ctx .source (), cmd .command (),
205203 "Command execution failed" );
206204
207- // Log using feedback system
208205 Feedback feedback = new Feedback (1 , 0 , 1 , List .of (),
209206 List .of ("Local command execution failed: "
210207 + cmd .command ()));
@@ -223,19 +220,23 @@ private void executeLocally(ExecutionContext ctx, CmdMapping cmd) {
223220 });
224221 }
225222
226- private void dispatchToRemoteSession (ExecutionContext ctx , CmdMapping cmd , String targetId ) {
227- findSession (targetId )
223+ private void dispatchToRemoteSession (ExecutionContext ctx , CmdMapping cmd , String targetId ,
224+ Location requiredLocation ) {
225+ findSession (targetId , requiredLocation )
228226 .filter (this ::isSessionConnected )
229227 .ifPresentOrElse (
230228 session -> sendExecuteCommand (session , ctx , cmd , targetId ),
231229 () -> {
232- Log .warn ("Target '{}' not found or not connected" , targetId );
230+ Log .warn ("Target '{}' ({}) not found or not connected" ,
231+ targetId ,
232+ requiredLocation );
233233 notifyExecutionError (ctx .source (), cmd .command (),
234- "Backend server '" + targetId
234+ requiredLocation + " server '" + targetId
235235 + "' is not connected" );
236236
237237 Feedback feedback = new Feedback (1 , 0 , 1 , List .of (),
238- List .of ("Backend not connected: " + targetId ));
238+ List .of (requiredLocation + " not connected: "
239+ + targetId ));
239240 Summary .feedbackSummary ("Execution Failed" , feedback , targetId );
240241 });
241242 }
@@ -366,9 +367,9 @@ private Optional<CommandSource> resolveSource(SenderContext sender) {
366367 };
367368 }
368369
369- private Optional <ClientSession > findSession (String id ) {
370+ private Optional <ClientSession > findSession (String id , Location requiredLocation ) {
370371 return StreamSupport .stream (sessions .spliterator (), false )
371- .filter (s -> id .equals (s .id ()))
372+ .filter (s -> id .equals (s .id ()) && s . location () == requiredLocation )
372373 .findFirst ();
373374 }
374375
0 commit comments