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