1313 * limitations under the License.
1414 */
1515
16- package com .aliyun .migrationx . common .command . appbase ;
16+ package com .aliyun .dataworks . client .command ;
1717
1818import java .io .File ;
1919import java .io .IOException ;
2020import java .nio .charset .StandardCharsets ;
2121import java .util .Map ;
22+ import java .util .Objects ;
23+ import java .util .stream .Stream ;
2224
23- import com .aliyun .migrationx .common .exception .BizException ;
24- import com .aliyun .migrationx .common .exception .ErrorCode ;
25- import com .aliyun .migrationx .common .utils .GsonUtils ;
25+ import com .alibaba .fastjson2 .JSON ;
2626
2727import com .google .common .base .Joiner ;
28- import com .google .gson .reflect .TypeToken ;
28+ import com .google .common .reflect .TypeToken ;
2929import lombok .extern .slf4j .Slf4j ;
3030import org .apache .commons .cli .CommandLine ;
3131import org .apache .commons .cli .CommandLineParser ;
@@ -46,7 +46,7 @@ public static void main(String[] args) throws IOException {
4646 String currentDir = System .getProperty ("currentDir" );
4747 AppType appType = AppType .valueOf (System .getProperty ("appType" ));
4848
49- Map <AppType , Map <String , AppMeta >> apps = loadApps (Joiner .on (File .separator ).join (currentDir , "conf" ));
49+ Map <AppType , Map <String , AppMeta >> apps = loadApps (Joiner .on (File .separator ).join (currentDir , "conf" ), appType );
5050
5151 Options options = new Options ();
5252 options .addRequiredOption ("a" , "app" , true , "app name" );
@@ -58,7 +58,7 @@ public static void main(String[] args) throws IOException {
5858 String appName = commandLine .getOptionValue ("a" );
5959
6060 if (!apps .get (appType ).containsKey (appName )) {
61- throw new BizException ( ErrorCode . UNKNOWN_COMMAND_APP ). with ( appName );
61+ throw new RuntimeException ( "unknown app: " + appName );
6262 }
6363
6464 CommandApp app = CommandAppFactory .create (appType , appName );
@@ -68,7 +68,7 @@ public static void main(String[] args) throws IOException {
6868 log .info ("app command success" );
6969 } catch (ParseException e ) {
7070 log .error ("app command parse error: {}" , e .getMessage ());
71- String footer = Joiner .on (" " ).join ("\n Available apps: \n " , GsonUtils . toJsonString (CommandAppFactory .getApps ()));
71+ String footer = Joiner .on (" " ).join ("\n Available apps: \n " , JSON . toJSONString (CommandAppFactory .getApps ()));
7272 helpFormatter .printHelp ("Options" , "migrationx" , options , footer );
7373 } catch (Exception e ) {
7474 log .error ("app command failed: " , e );
@@ -77,28 +77,32 @@ public static void main(String[] args) throws IOException {
7777 }
7878
7979 @ SuppressWarnings ("unchecked" )
80- private static Map <AppType , Map <String , AppMeta >> loadApps (String conf ) throws IOException {
80+ private static Map <AppType , Map <String , AppMeta >> loadApps (String conf , AppType ... appTypes ) throws IOException {
8181 File config = new File (conf );
8282 File appsJson = config ;
8383 if (config .isDirectory ()) {
8484 appsJson = new File (config , "apps.json" );
8585 }
8686
8787 String json = FileUtils .readFileToString (appsJson , StandardCharsets .UTF_8 );
88- Map <AppType , Map <String , AppMeta >> apps = GsonUtils . fromJsonString (json , new TypeToken <Map <AppType , Map <String , AppMeta >>>() {}.getType ());
88+ Map <AppType , Map <String , AppMeta >> apps = JSON . parseObject (json , new TypeToken <Map <AppType , Map <String , AppMeta >>>() {}.getType ());
8989 MapUtils .emptyIfNull (apps ).forEach ((appType , map ) -> MapUtils .emptyIfNull (map ).forEach ((appName , appMeta ) -> {
90+ if (appTypes != null && Stream .of (appTypes ).noneMatch (t -> Objects .equals (t , appType ))) {
91+ log .info ("skip load app type: {}, appType needs load: {}" , appType , appTypes );
92+ return ;
93+ }
94+
9095 try {
9196 appMeta .setName (appName );
9297 appMeta .setType (appType );
9398 log .info ("register command app type: {}, name: {}, class: {}" , appType , appName , appMeta .getAppClass ());
94- CommandAppFactory .register (appType , appName , (Class <? extends CommandApp >) Class .forName (appMeta .getAppClass ()));
99+ CommandAppFactory .register (appType , appName , (Class <? extends CommandApp >)Class .forName (appMeta .getAppClass ()));
95100 } catch (ClassNotFoundException e ) {
96- log .info ("register command app failed, appType: {}, appName: {}, class: {}, error: {}" ,
97- appType , appName , appMeta .getAppClass (), e );
101+ log .info ("register command app failed, appType: {}, appName: {}, class: {}, error: " , appType , appName , appMeta .getAppClass (), e );
98102 }
99103 }));
100104
101- log .info ("apps map: {}" , GsonUtils . toJsonString (apps ));
105+ log .info ("apps map: {}" , JSON . toJSONString (apps ));
102106 return apps ;
103107 }
104108}
0 commit comments