@@ -82,10 +82,11 @@ public Session getCurrentSession(String sessionId, boolean oneTimeIsAllowed) {
8282 * @param timeout 超时时间
8383 * @param sessionId session ID,如果为null则创建临时session
8484 * @param authSubject 认证主体,如果不为null则应用到session
85+ * @param userId 用户 ID,用于统计上报
8586 * @return 执行结果
8687 */
8788 @ Override
88- public Map <String , Object > executeSync (String commandLine , long timeout , String sessionId , Object authSubject ) {
89+ public Map <String , Object > executeSync (String commandLine , long timeout , String sessionId , Object authSubject , String userId ) {
8990 Session session = null ;
9091 boolean oneTimeAccess = false ;
9192
@@ -98,6 +99,12 @@ public Map<String, Object> executeSync(String commandLine, long timeout, String
9899 session .getSessionId (), authSubject .getClass ().getSimpleName ());
99100 }
100101
102+ // 设置 userId 到 session,用于统计上报
103+ if (userId != null && !userId .trim ().isEmpty ()) {
104+ session .setUserId (userId );
105+ logger .debug ("Set userId to session: {} (userId: {})" , session .getSessionId (), userId );
106+ }
107+
101108 if (session .get (ONETIME_SESSION_KEY ) != null ) {
102109 oneTimeAccess = true ;
103110 }
@@ -349,84 +356,6 @@ public void setSessionUserId(String sessionId, String userId) {
349356 }
350357 }
351358
352- /**
353- * 同步执行命令,支持指定 userId
354- */
355- @ Override
356- public Map <String , Object > executeSync (String commandLine , long timeout , String sessionId , Object authSubject , String userId ) {
357- Session session = null ;
358- boolean oneTimeAccess = false ;
359-
360- try {
361- session = getCurrentSession (sessionId , true );
362-
363- if (authSubject != null ) {
364- session .put (SUBJECT_KEY , authSubject );
365- logger .debug ("Applied auth subject to session: {} (authSubject: {})" ,
366- session .getSessionId (), authSubject .getClass ().getSimpleName ());
367- }
368-
369- // 设置 userId 到 session
370- if (userId != null && !userId .trim ().isEmpty ()) {
371- session .setUserId (userId );
372- logger .debug ("Set userId to session: {} (userId: {})" , session .getSessionId (), userId );
373- }
374-
375- if (session .get (ONETIME_SESSION_KEY ) != null ) {
376- oneTimeAccess = true ;
377- }
378-
379- PackingResultDistributorImpl resultDistributor = new PackingResultDistributorImpl (session );
380- Job job = this .createJob (commandLine , session , resultDistributor );
381-
382- if (job == null ) {
383- logger .error ("Failed to create job for command: {}" , commandLine );
384- return createErrorResult (commandLine , "Failed to create job" );
385- }
386-
387- job .run ();
388- boolean finished = waitForJob (job , (int ) timeout );
389- if (!finished ) {
390- logger .warn ("Command timeout after {} ms: {}" , timeout , commandLine );
391- job .interrupt ();
392- return createTimeoutResult (commandLine , timeout );
393- }
394-
395- Map <String , Object > result = new TreeMap <>();
396- result .put ("command" , commandLine );
397- result .put ("success" , true );
398- result .put ("sessionId" , session .getSessionId ());
399- result .put ("executionTime" , System .currentTimeMillis ());
400-
401- List <ResultModel > results = resultDistributor .getResults ();
402- if (results != null && !results .isEmpty ()) {
403- result .put ("results" , results );
404- result .put ("resultCount" , results .size ());
405- } else {
406- result .put ("results" , results );
407- result .put ("resultCount" , 0 );
408- }
409-
410- return result ;
411-
412- } catch (SessionNotFoundException e ) {
413- logger .error ("Session error for command: {}" , commandLine , e );
414- return createErrorResult (commandLine , e .getMessage ());
415- } catch (Exception e ) {
416- logger .error ("Error executing command: {}" , commandLine , e );
417- return createErrorResult (commandLine , "Error executing command: " + e .getMessage ());
418- } finally {
419- if (oneTimeAccess && session != null ) {
420- try {
421- sessionManager .removeSession (session .getSessionId ());
422- logger .debug ("Destroyed one-time session {}" , session .getSessionId ());
423- } catch (Exception e ) {
424- logger .warn ("Error removing one-time session" , e );
425- }
426- }
427- }
428- }
429-
430359 private boolean waitForJob (Job job , int timeout ) {
431360 long startTime = System .currentTimeMillis ();
432361 while (true ) {
0 commit comments