[AAF-21] Initial code import
[aaf/cadi.git] / client / src / main / java / com / att / cadi / client / EnvAccess.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.client;\r
25 \r
26 import java.io.IOException;\r
27 import java.io.InputStream;\r
28 import java.util.Map.Entry;\r
29 import java.util.Properties;\r
30 \r
31 import com.att.cadi.Access;\r
32 import com.att.cadi.Symm;\r
33 import com.att.inno.env.Decryptor;\r
34 import com.att.inno.env.Env;\r
35 import com.att.inno.env.impl.BasicEnv;\r
36 \r
37 public class EnvAccess implements Access {\r
38         private Env env;\r
39 \r
40         /**\r
41          * String Property tag for files/resources that may contain properties.  Can be null.\r
42          * Resources of ClassLoader will be checked first, if exist. Can be null.\r
43          * @param env\r
44          * @param tag\r
45          * @param cl\r
46          * @throws IOException\r
47          */\r
48         public EnvAccess(BasicEnv env, ClassLoader cl) throws IOException {\r
49                 this.env = env;\r
50                 final Symm s = Symm.obtain(this);\r
51                 env.set(new Decryptor() {\r
52                                 private Symm symm = s;\r
53                                 @Override\r
54                                 public String decrypt(String encrypted) {\r
55                                         try {\r
56                                                 return (encrypted!=null && (encrypted.startsWith(Symm.ENC)))\r
57                                                                 ? symm.depass(encrypted)\r
58                                                                 : encrypted;\r
59                                         } catch (IOException e) {\r
60                                                 return "";\r
61                                         }\r
62                                 }\r
63                         }\r
64                 );\r
65         }\r
66 \r
67         \r
68         /**\r
69          * Construct with the Classloader of Env and CADI_PROP_FILES, if possible\r
70          * \r
71          * @param env\r
72          * @throws IOException\r
73          */\r
74         public EnvAccess(BasicEnv env) throws IOException {\r
75                 this(env, env.getClass().getClassLoader());\r
76         }\r
77         \r
78         @Override\r
79         public void log(Level level, Object... elements) {\r
80                 switch(level) {\r
81                         case AUDIT:\r
82                                 env.audit().log(elements);\r
83                                 break;\r
84                         case DEBUG:\r
85                                 env.debug().log(elements);\r
86                                 break;\r
87                         case ERROR:\r
88                                 env.error().log(elements);\r
89                                 break;\r
90                         case INFO:\r
91                                 env.info().log(elements);\r
92                                 break;\r
93                         case INIT:\r
94                                 env.init().log(elements);\r
95                                 break;\r
96                         case WARN:\r
97                                 env.warn().log(elements);\r
98                                 break;\r
99                         default:\r
100                                 break;\r
101                 }\r
102                 \r
103         }\r
104 \r
105         @Override\r
106         public void log(Exception e, Object... elements) {\r
107                 env.error().log(e,elements);\r
108         }\r
109 \r
110         @Override\r
111         public void printf(Level level, String fmt, Object... elements) {\r
112                 if(willLog(level)) {\r
113                         log(level,String.format(fmt, elements));\r
114                 }\r
115         }\r
116 \r
117 \r
118         @Override\r
119         public boolean willLog(Level level) {\r
120                 switch(level) {\r
121                         case AUDIT:\r
122                                 return env.audit().isLoggable();\r
123                         case DEBUG:\r
124                                 return env.debug().isLoggable();\r
125                         case ERROR:\r
126                                 return env.error().isLoggable();\r
127                         case INFO:\r
128                                 return env.info().isLoggable();\r
129                         case INIT:\r
130                                 return env.init().isLoggable();\r
131                         case WARN:\r
132                                 return env.warn().isLoggable();\r
133                         default:\r
134                                 return false;\r
135                 }\r
136         }\r
137 \r
138 \r
139         @Override\r
140         public void setLogLevel(Level level) {\r
141                 // unused\r
142         }\r
143 \r
144         @Override\r
145         public ClassLoader classLoader() {\r
146                 return env.getClass().getClassLoader();\r
147         }\r
148 \r
149         @Override\r
150         public String getProperty(String string, String def) {\r
151                 return env.getProperty(string, def);\r
152         }\r
153         \r
154         @Override\r
155         public void load(InputStream is) throws IOException {\r
156                 Properties props = new Properties();\r
157                 props.load(is);\r
158                 for(Entry<Object, Object> es :props.entrySet()) {\r
159                         env.setProperty(es.getKey().toString(), es.getValue().toString());\r
160                 }\r
161         }\r
162 \r
163         @Override\r
164         public String decrypt(String encrypted, boolean anytext) throws IOException {\r
165                 return env.decryptor().decrypt(encrypted);\r
166         }\r
167 \r
168 }\r