b48dd74d8c458a7df79bd7284d40431eb56faf6f
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / config / Get.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.cadi.config;
23
24 import java.lang.reflect.Method;
25
26 import org.onap.aaf.cadi.Access;
27 import org.onap.aaf.cadi.Access.Level;
28
29 public interface Get {
30     public String get(String name, String def, boolean print);
31     
32     
33     /**
34      * A class for Getting info out of "JavaBean" format
35      * @author Jonathan
36      *
37      */
38     public static class Bean implements Get {
39         private Object bean;
40         private Class<?> bc;
41         private Class<?>[] params;
42         private Object[] args;
43         
44         public Bean(Object bean) {
45             this.bean = bean;
46             bc = bean.getClass();
47             params = new Class<?>[0]; // note, this will allow to go out of scope after config
48             args = new Object[0];
49         }
50         
51         public String get(String name, String def, boolean print) {
52             String str = null;
53             String gname = "get"+Character.toUpperCase(name.charAt(0))+name.substring(1);
54             try {
55                 Method meth = bc.getMethod(gname, params);
56                 Object obj = meth.invoke(bean, args);
57                 str = obj==null?null:obj.toString(); // easy string convert... 
58             } catch (Exception e) {
59             }
60             
61             // Take def if nothing else
62             if (str==null) {
63                 str = def;
64                 // don't log defaults
65             } else {
66                 str = str.trim(); // this is vital in Property File based values, as spaces can hide easily
67             }
68             // Note: Can't log during configuration
69             return str;
70         }
71     }
72
73     public static Get NULL = new Get() {
74         public String get(String name, String def, boolean print) {
75             return def;
76         }
77     };
78
79     public static class AccessGet implements Get {
80         private Access access;
81         public AccessGet(Access access) {
82             this.access = access;
83         }
84         public String get(String name, String def, boolean print) {
85             String gotten = access.getProperty(name, def);
86             if (print) {
87                 if (gotten == null) {
88                     access.log(Level.INIT,name, "is not set");
89                 } else {
90                     access.log(Level.INIT,name, "is set to", gotten);
91                 }
92             }
93             return gotten;
94         }
95     }
96
97 }