77d1894f2c999082557fb27772091ff208c7d114
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / BaseCmd.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cmd;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.LocatorException;
29 import org.onap.aaf.misc.env.APIException;
30
31
32 public class BaseCmd<CMD extends Cmd> extends Cmd  {
33     protected List<Cmd>     cmds;
34
35     public BaseCmd(AAFcli aafcli, String name, Param ... params) {
36         super(aafcli, null, name, params);
37         cmds = new ArrayList<>();
38     }
39     
40     public BaseCmd(CMD parent, String name, Param ... params) {
41         super(parent.aafcli, parent, name, params);
42         cmds = new ArrayList<>();
43     }
44
45     
46     @Override
47     public int _exec( int idx, final String ... args) throws CadiException, APIException, LocatorException {
48         if (args.length-idx<1) {
49             pw().println(build(new StringBuilder(),null).toString());
50         } else {
51             String s = args[idx];
52             String name;
53             Cmd empty = null;
54             for (Cmd c: cmds) {
55                 name = c.getName();
56                 if (name==null && empty==null) { // Mark with Command is null, and take the first one.  
57                     empty = c;
58                 } else if (s.equalsIgnoreCase(c.getName()))
59                     return c.exec(idx+1, args);
60             }
61             if (empty!=null) {
62                 return empty.exec(idx, args); // If name is null, don't account for it on command line.  Jonathan 4-29
63             }
64             pw().println("Instructions not understood.");
65         }
66         return 0;
67     }
68 }