AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / impl / BasicEnv.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.misc.env.impl;
23
24 import java.applet.Applet;
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.util.Properties;
32
33 import javax.xml.namespace.QName;
34 import javax.xml.validation.Schema;
35
36 import org.onap.aaf.misc.env.APIException;
37 import org.onap.aaf.misc.env.DataFactory;
38 import org.onap.aaf.misc.env.Decryptor;
39 import org.onap.aaf.misc.env.Encryptor;
40 import org.onap.aaf.misc.env.Env;
41 import org.onap.aaf.misc.env.EnvJAXB;
42 import org.onap.aaf.misc.env.LogTarget;
43 import org.onap.aaf.misc.env.StaticSlot;
44 import org.onap.aaf.misc.env.StoreImpl;
45 import org.onap.aaf.misc.env.TimeTaken;
46 import org.onap.aaf.misc.env.TransCreate;
47 import org.onap.aaf.misc.env.TransJAXB;
48 import org.onap.aaf.misc.env.jaxb.JAXBDF;
49 import org.onap.aaf.misc.env.util.Split;
50
51 /**
52  * An essential Implementation of Env, which will fully function, without any sort
53  * of configuration.
54  * 
55  * Use as a basis for Group level Env, just overriding where needed.
56  * @author Jonathan
57  *
58  */
59 public class BasicEnv extends StoreImpl implements EnvJAXB, TransCreate<TransJAXB>{
60         protected LogTarget fatal=LogTarget.SYSERR;
61         protected LogTarget error=LogTarget.SYSERR;
62         protected LogTarget audit=LogTarget.SYSOUT;
63         protected LogTarget init=LogTarget.SYSOUT;
64         protected LogTarget warn=LogTarget.SYSERR;
65         protected LogTarget info=LogTarget.SYSOUT;
66         protected LogTarget debug=LogTarget.NULL;
67         protected LogTarget trace=LogTarget.NULL;
68 //      protected Map<String, String> props;
69         
70 //      private boolean sysprops;
71
72         public BasicEnv(String ... args) {
73                 super(null,args);
74         }
75
76         public BasicEnv(String tag, String[] args) {
77                 super(tag, args);
78         }
79         
80
81         /**
82          * Suitable for use in Applets... obtain all the values 
83          * listed for the variable String arg "tags"
84          */
85         public BasicEnv(Applet applet, String ... tags) {
86                 super(null, tags);
87 //              props = new HashMap<String, String>();
88 //              String value;
89 //              for(int i=0;i<tags.length;++i) {
90 //                      value = applet.getParameter(tags[i]);
91 //                      if(value!=null) {
92 //                              props.put(tags[i], value);
93 //                      }
94 //              }
95         }
96
97         public BasicEnv(Properties props) {
98                 super(null, props);
99         }
100
101         public BasicEnv(String tag, Properties props) {
102                 super(tag, props);
103         }
104
105
106
107         // @Override
108         public LogTarget fatal() {
109                 return fatal;
110         }
111
112         // @Override
113         public LogTarget error() {
114                 return error;
115         }
116
117         
118         // @Override
119         public LogTarget audit() {
120                 return audit;
121         }
122
123         // @Override
124         public LogTarget init() {
125                 return init;
126         }
127
128         // @Override
129         public LogTarget warn() {
130                 return warn;
131         }
132
133         // @Override
134         public LogTarget info() {
135                 return info;
136         }
137
138         // @Override
139         public LogTarget debug() {
140                 return debug;
141         }
142
143         public void debug(LogTarget lt) {
144                 debug = lt;
145         }
146
147         // @Override
148         public LogTarget trace() {
149                 return trace;
150         }
151
152         // @Override
153         public TimeTaken start(String name, int flag) {
154                 return new TimeTaken(name, flag) {
155                         /**
156                          * Format to be printed when called upon
157                          */
158                         // @Override
159                         public void output(StringBuilder sb) {
160         
161                                 switch(flag) {
162                                         case Env.XML: sb.append("XML "); break;
163                                         case Env.JSON: sb.append("JSON "); break;
164                                         case Env.REMOTE: sb.append("REMOTE "); break;
165                                 }
166                                 sb.append(name);
167                                 if(flag != Env.CHECKPOINT) {
168                                         sb.append(' ');
169                                         sb.append((end-start)/1000000f);
170                                         sb.append("ms");
171                                         if(size>=0) {
172                                                 sb.append(" size: ");
173                                                 sb.append(Long.toString(size));
174                                         }
175                                 }
176                         }
177                 };
178         }
179
180         // @Override
181         public String getProperty(String key) {
182                 return get(staticSlot(key),null);
183         }
184         
185         public Properties getProperties(String ... filter) {
186                 Properties props = new Properties();
187                 boolean yes;
188                 for(String key : existingStaticSlotNames()) {
189                         if(filter.length>0) {
190                                 yes = false;
191                                 for(String f : filter) {
192                                         if(key.startsWith(f)) {
193                                                 yes = true;
194                                                 break;
195                                         }
196                                 }
197                         } else {
198                                 yes = true;
199                         }
200                         if(yes) {
201                                 String value = getProperty(key);
202                                 if(value!=null) {
203                                         props.put(key, value);
204                                 }
205                         }
206                 }
207                 return props;
208         }
209         
210         // @Override
211         public String getProperty(String key, String defaultValue) {
212                 return get(staticSlot(key),defaultValue);
213         }
214
215         // @Override
216         public String setProperty(String key, String value) {
217                 put(staticSlot(key),value==null?null:value.trim());
218                 return value;
219         }
220         
221         protected Decryptor decryptor = Decryptor.NULL;
222         protected Encryptor encryptor = Encryptor.NULL;
223
224         
225         public Decryptor decryptor() {
226                 return decryptor; 
227         }
228         
229         public void set(Decryptor newDecryptor) {
230                 decryptor = newDecryptor;
231         }
232         
233         public Encryptor encryptor() {
234                 return encryptor; 
235         }
236         
237         public void set(Encryptor newEncryptor) {
238                 encryptor = newEncryptor;
239         }
240
241         
242 //      @SuppressWarnings("unchecked")
243         // @Override
244         public <T> DataFactory<T> newDataFactory(Class<?>... classes) throws APIException {
245 //              if(String.class.isAssignableFrom(classes[0])) 
246 //                      return (DataFactory<T>) new StringDF(this);
247                 return new JAXBDF<T>(this,classes);
248         }
249
250 //      @SuppressWarnings("unchecked")
251         // @Override
252         public <T> DataFactory<T> newDataFactory(Schema schema, Class<?>... classes) throws APIException {
253 //              if(String.class.isAssignableFrom(classes[0])) 
254 //                      return (DataFactory<T>) new StringDF(this);
255                 return new JAXBDF<T>(this, schema, classes);
256         }
257
258 //      @SuppressWarnings("unchecked")
259         // @Override
260         public<T> DataFactory<T> newDataFactory(QName qName, Class<?> ... classes) throws APIException {
261 //              if(String.class.isAssignableFrom(classes[0])) 
262 //                      return (DataFactory<T>) new StringDF(this);
263                 return new JAXBDF<T>(this, qName, classes);
264         }
265
266         // @Override
267         public<T> DataFactory<T> newDataFactory(Schema schema, QName qName, Class<?> ... classes) throws APIException {
268                 return new JAXBDF<T>(this, schema, qName, classes);
269         }
270
271         // @Override
272         public BasicTrans newTrans() {
273                 return new BasicTrans(this);
274         }
275
276         public void loadFromSystemPropsStartsWith(String ... str) {
277                  for(String name : System.getProperties().stringPropertyNames()) {
278                         for(String s : str) {
279                                 if(name.startsWith(s)) {
280                                         setProperty(name, System.getProperty(name));
281                                 }
282                         }
283                 }
284         }
285
286         /**
287          * 
288          * 
289          */
290         public void loadToSystemPropsStartsWith(String ... str) {
291                 String value;
292                 for(String name : existingStaticSlotNames()) {
293                         for(String s : str) {
294                                 if(name.startsWith(s)) {
295                                         if((value = getProperty(name))!=null)
296                                                 System.setProperty(name,value);
297                                 }
298                         }
299                  }
300         }
301         
302         public void loadPropFiles(String tag, ClassLoader classloader) throws IOException {
303                 String propfiles = getProperty(tag);
304                 if(propfiles!=null) {
305                         for(String pf : Split.splitTrim(File.pathSeparatorChar, propfiles)) {
306                                 InputStream is = classloader==null?null:classloader.getResourceAsStream(pf);
307                                 if(is==null) {
308                                         File f = new File(pf);
309                                         if(f.exists()) {
310                                                 is = new FileInputStream(f);
311                                         }
312                                 }
313                                 if(is!=null) {
314                                         BufferedReader br = new BufferedReader(new InputStreamReader(is));
315                                         try {
316                                                 String line;
317                                                 while((line=br.readLine())!=null) {
318                                                         line = line.trim();
319                                                         if(!line.startsWith("#")) {
320                                                                 String[] tv = Split.splitTrim('=', line);
321                                                                 if(tv.length==2) {
322                                                                         setProperty(tv[0],tv[1]);
323                                                                 }
324                                                         }
325                                                 }
326                                         } finally {
327                                                 try {
328                                                         br.close();
329                                                 } catch (IOException e) {
330                                                         error().log(e);
331                                                 }
332                                         }
333                                 }
334                         }
335                 }
336         }
337         
338         /**
339          * Create a StaticSlot, and load it from existing Properties
340          * 
341          * @param name
342          * @param propName
343          * @return
344          */
345         public synchronized StaticSlot staticSlot(String name, final String propName) {
346                 StaticSlot ss = staticSlot(name);
347                 put(ss,getProperty(propName));
348                 return ss;
349         }
350
351
352 }