1+ /**
2+ * Copyright (c) 2017-2024 Nop Platform. All rights reserved.
3+ 4+ * Blog: https://www.zhihu.com/people/canonical-entropy
5+ * Gitee: https://gitee.com/canonical-entropy/nop-entropy
6+ * Github: https://github.com/entropy-cloud/nop-entropy
7+ */
8+ package io .nop .report .spl .execute ;
9+
10+ import com .esproc .jdbc .Server ;
11+ import com .scudata .cellset .datamodel .PgmCellSet ;
12+ import com .scudata .common .DBSession ;
13+ import com .scudata .common .Logger ;
14+ import com .scudata .common .UUID ;
15+ import com .scudata .dm .Context ;
16+ import com .scudata .dm .JobSpace ;
17+ import com .scudata .dm .JobSpaceManager ;
18+ import com .scudata .dm .Param ;
19+ import com .scudata .dm .ParamList ;
20+ import io .nop .api .core .annotations .lang .EvalMethod ;
21+ import io .nop .api .core .exceptions .NopException ;
22+ import io .nop .commons .util .StringHelper ;
23+ import io .nop .core .lang .eval .IEvalScope ;
24+ import io .nop .core .resource .component .ResourceComponentManager ;
25+ import io .nop .report .spl .SplConstants ;
26+ import io .nop .report .spl .model .SplModel ;
27+
28+ import jakarta .annotation .PostConstruct ;
29+ import java .util .Iterator ;
30+ import java .util .Map ;
31+
32+ import static io .nop .report .spl .SplErrors .ARG_PARAM_NAME ;
33+ import static io .nop .report .spl .SplErrors .ARG_PATH ;
34+ import static io .nop .report .spl .SplErrors .ERR_XPT_INVALID_SPL_MODEL_FILE_TYPE ;
35+ import static io .nop .report .spl .SplErrors .ERR_XPT_UNKNOWN_SPL_PARAM ;
36+ import static io .nop .report .spl .execute .SplHelper .spl2CellSet ;
37+
38+ public class SplExecutor {
39+
40+ private String configPath = "config/raqsoftConfig.xml" ;
41+
42+ public void setConfigPath (String configPath ) {
43+ this .configPath = configPath ;
44+ }
45+
46+ @ PostConstruct
47+ public void init () {
48+ try {
49+ Server .getInstance ().initConfig (null , configPath );
50+ } catch (Exception e ) {
51+ throw NopException .adapt (e );
52+ }
53+ }
54+
55+ @ EvalMethod
56+ public Object executeForPath (IEvalScope scope , String path , Map <String , Object > params ) {
57+ String fileType = StringHelper .fileType (path );
58+ if (!SplConstants .FILE_TYPES_SPL_MODEL .contains (fileType ))
59+ throw new NopException (ERR_XPT_INVALID_SPL_MODEL_FILE_TYPE )
60+ .param (ARG_PATH , path );
61+
62+ SplModel model = (SplModel ) ResourceComponentManager .instance ().loadComponentModel (path );
63+ return executeForModel (scope , model , params );
64+ }
65+
66+ @ EvalMethod
67+ public Object executeForModel (IEvalScope scope , SplModel model , Map <String , Object > params ) {
68+ return executeSPL (scope , model .getSource (), params );
69+ }
70+
71+ @ EvalMethod
72+ public Object executeSPL (IEvalScope scope , String source , Map <String , Object > params ) {
73+ PgmCellSet pgmCellSet = spl2CellSet (source ); // dfx, sqlx 二进制文件
74+ Context context = new Context (); //上下文,参数..设置
75+
76+ try {
77+ setParams (pgmCellSet , params );
78+ prepare (context );
79+ pgmCellSet .setContext (context );
80+ Object result = pgmCellSet .execute ();
81+ return SplHelper .normalizeResult (result );
82+ } finally {
83+ close (context );
84+ }
85+ }
86+
87+ private void setParams (PgmCellSet pgmCellSet , Map <String , Object > params ) {
88+ if (params != null && !params .isEmpty ()) {
89+ ParamList list = pgmCellSet .getParamList ();
90+ params .forEach ((name , value ) -> {
91+ Param param = list .get (name );
92+ if (param == null )
93+ throw new NopException (ERR_XPT_UNKNOWN_SPL_PARAM ).param (ARG_PARAM_NAME , name );
94+
95+ param .setValue (value );
96+ });
97+ }
98+ }
99+
100+ private void prepare (Context context ) {
101+ String uuid = UUID .randomUUID ().toString ();
102+ JobSpace jobSpace = JobSpaceManager .getSpace (uuid );
103+ context .setJobSpace (jobSpace );
104+ }
105+
106+ /**
107+ * 这里的资源清理代码拷贝自 InternalConnection的close方法
108+ */
109+ private void close (Context ctx ) {
110+ /* Close automatically opened connections */
111+ Map <String , DBSession > map = ctx .getDBSessionMap ();
112+ if (map != null ) {
113+ Iterator <String > iter = map .keySet ().iterator ();
114+ while (iter .hasNext ()) {
115+ String name = iter .next ().toString ();
116+ DBSession sess = ctx .getDBSession (name );
117+ if (sess == null || sess .isClosed ())
118+ continue ;
119+ Object o = ctx .getDBSession (name ).getSession ();
120+ if (o != null && o instanceof java .sql .Connection ) {
121+ try {
122+ ((java .sql .Connection ) o ).close ();
123+ } catch (Exception e ) {
124+ Logger .warn (e .getMessage (), e );
125+ }
126+ }
127+ }
128+ }
129+ /* Close the connection opened by the user through an expression */
130+ ParamList pl = ctx .getParamList ();
131+ for (int i = 0 ; i < pl .count (); i ++) {
132+ Object o = pl .get (i ).getValue ();
133+ if (o != null && o instanceof java .sql .Connection ) {
134+ try {
135+ ((java .sql .Connection ) o ).close ();
136+ } catch (Exception e ) {
137+ Logger .warn (e .getMessage (), e );
138+ }
139+ }
140+ }
141+ JobSpace jobSpace = ctx .getJobSpace ();
142+ /* Close the JobSpace */
143+ if (jobSpace != null ) {
144+ jobSpace .closeResource ();
145+ JobSpaceManager .closeSpace (jobSpace .getID ());
146+ }
147+ }
148+ }
0 commit comments