2 * ============LICENSE_START====================================================
\r
4 * ===========================================================================
\r
5 * Copyright (c) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
22 package org.onap.aaf.misc.env.impl;
\r
24 import java.applet.Applet;
\r
25 import java.io.BufferedReader;
\r
26 import java.io.File;
\r
27 import java.io.FileInputStream;
\r
28 import java.io.IOException;
\r
29 import java.io.InputStream;
\r
30 import java.io.InputStreamReader;
\r
31 import java.util.Properties;
\r
33 import javax.xml.namespace.QName;
\r
34 import javax.xml.validation.Schema;
\r
36 import org.onap.aaf.misc.env.APIException;
\r
37 import org.onap.aaf.misc.env.DataFactory;
\r
38 import org.onap.aaf.misc.env.Decryptor;
\r
39 import org.onap.aaf.misc.env.Encryptor;
\r
40 import org.onap.aaf.misc.env.Env;
\r
41 import org.onap.aaf.misc.env.EnvJAXB;
\r
42 import org.onap.aaf.misc.env.LogTarget;
\r
43 import org.onap.aaf.misc.env.StaticSlot;
\r
44 import org.onap.aaf.misc.env.StoreImpl;
\r
45 import org.onap.aaf.misc.env.TimeTaken;
\r
46 import org.onap.aaf.misc.env.TransCreate;
\r
47 import org.onap.aaf.misc.env.TransJAXB;
\r
48 import org.onap.aaf.misc.env.jaxb.JAXBDF;
\r
49 import org.onap.aaf.misc.env.util.Split;
\r
52 * An essential Implementation of Env, which will fully function, without any sort
\r
55 * Use as a basis for Group level Env, just overriding where needed.
\r
59 public class BasicEnv extends StoreImpl implements EnvJAXB, TransCreate<TransJAXB>{
\r
60 protected LogTarget fatal=LogTarget.SYSERR;
\r
61 protected LogTarget error=LogTarget.SYSERR;
\r
62 protected LogTarget audit=LogTarget.SYSOUT;
\r
63 protected LogTarget init=LogTarget.SYSOUT;
\r
64 protected LogTarget warn=LogTarget.SYSERR;
\r
65 protected LogTarget info=LogTarget.SYSOUT;
\r
66 protected LogTarget debug=LogTarget.NULL;
\r
67 protected LogTarget trace=LogTarget.NULL;
\r
68 // protected Map<String, String> props;
\r
70 // private boolean sysprops;
\r
72 public BasicEnv(String ... args) {
\r
76 public BasicEnv(String tag, String[] args) {
\r
82 * Suitable for use in Applets... obtain all the values
\r
83 * listed for the variable String arg "tags"
\r
85 public BasicEnv(Applet applet, String ... tags) {
\r
87 // props = new HashMap<String, String>();
\r
89 // for(int i=0;i<tags.length;++i) {
\r
90 // value = applet.getParameter(tags[i]);
\r
91 // if(value!=null) {
\r
92 // props.put(tags[i], value);
\r
97 public BasicEnv(Properties props) {
\r
101 public BasicEnv(String tag, Properties props) {
\r
108 public LogTarget fatal() {
\r
113 public LogTarget error() {
\r
119 public LogTarget audit() {
\r
124 public LogTarget init() {
\r
129 public LogTarget warn() {
\r
134 public LogTarget info() {
\r
139 public LogTarget debug() {
\r
143 public void debug(LogTarget lt) {
\r
148 public LogTarget trace() {
\r
153 public TimeTaken start(String name, int flag) {
\r
154 return new TimeTaken(name, flag) {
\r
156 * Format to be printed when called upon
\r
159 public void output(StringBuilder sb) {
\r
162 case Env.XML: sb.append("XML "); break;
\r
163 case Env.JSON: sb.append("JSON "); break;
\r
164 case Env.REMOTE: sb.append("REMOTE "); break;
\r
167 if(flag != Env.CHECKPOINT) {
\r
169 sb.append((end-start)/1000000f);
\r
172 sb.append(" size: ");
\r
173 sb.append(Long.toString(size));
\r
181 public String getProperty(String key) {
\r
182 return get(staticSlot(key),null);
\r
185 public Properties getProperties(String ... filter) {
\r
186 Properties props = new Properties();
\r
188 for(String key : existingStaticSlotNames()) {
\r
189 if(filter.length>0) {
\r
191 for(String f : filter) {
\r
192 if(key.startsWith(f)) {
\r
201 String value = getProperty(key);
\r
203 props.put(key, value);
\r
211 public String getProperty(String key, String defaultValue) {
\r
212 return get(staticSlot(key),defaultValue);
\r
216 public String setProperty(String key, String value) {
\r
217 put(staticSlot(key),value==null?null:value.trim());
\r
221 protected Decryptor decryptor = Decryptor.NULL;
\r
222 protected Encryptor encryptor = Encryptor.NULL;
\r
225 public Decryptor decryptor() {
\r
229 public void set(Decryptor newDecryptor) {
\r
230 decryptor = newDecryptor;
\r
233 public Encryptor encryptor() {
\r
237 public void set(Encryptor newEncryptor) {
\r
238 encryptor = newEncryptor;
\r
242 // @SuppressWarnings("unchecked")
\r
244 public <T> DataFactory<T> newDataFactory(Class<?>... classes) throws APIException {
\r
245 // if(String.class.isAssignableFrom(classes[0]))
\r
246 // return (DataFactory<T>) new StringDF(this);
\r
247 return new JAXBDF<T>(this,classes);
\r
250 // @SuppressWarnings("unchecked")
\r
252 public <T> DataFactory<T> newDataFactory(Schema schema, Class<?>... classes) throws APIException {
\r
253 // if(String.class.isAssignableFrom(classes[0]))
\r
254 // return (DataFactory<T>) new StringDF(this);
\r
255 return new JAXBDF<T>(this, schema, classes);
\r
258 // @SuppressWarnings("unchecked")
\r
260 public<T> DataFactory<T> newDataFactory(QName qName, Class<?> ... classes) throws APIException {
\r
261 // if(String.class.isAssignableFrom(classes[0]))
\r
262 // return (DataFactory<T>) new StringDF(this);
\r
263 return new JAXBDF<T>(this, qName, classes);
\r
267 public<T> DataFactory<T> newDataFactory(Schema schema, QName qName, Class<?> ... classes) throws APIException {
\r
268 return new JAXBDF<T>(this, schema, qName, classes);
\r
272 public BasicTrans newTrans() {
\r
273 return new BasicTrans(this);
\r
276 public void loadFromSystemPropsStartsWith(String ... str) {
\r
277 for(String name : System.getProperties().stringPropertyNames()) {
\r
278 for(String s : str) {
\r
279 if(name.startsWith(s)) {
\r
280 setProperty(name, System.getProperty(name));
\r
290 public void loadToSystemPropsStartsWith(String ... str) {
\r
292 for(String name : existingStaticSlotNames()) {
\r
293 for(String s : str) {
\r
294 if(name.startsWith(s)) {
\r
295 if((value = getProperty(name))!=null)
\r
296 System.setProperty(name,value);
\r
302 public void loadPropFiles(String tag, ClassLoader classloader) throws IOException {
\r
303 String propfiles = getProperty(tag);
\r
304 if(propfiles!=null) {
\r
305 for(String pf : Split.splitTrim(File.pathSeparatorChar, propfiles)) {
\r
306 InputStream is = classloader==null?null:classloader.getResourceAsStream(pf);
\r
308 File f = new File(pf);
\r
310 is = new FileInputStream(f);
\r
314 BufferedReader br = new BufferedReader(new InputStreamReader(is));
\r
317 while((line=br.readLine())!=null) {
\r
318 line = line.trim();
\r
319 if(!line.startsWith("#")) {
\r
320 String[] tv = Split.splitTrim('=', line);
\r
322 setProperty(tv[0],tv[1]);
\r
329 } catch (IOException e) {
\r
339 * Create a StaticSlot, and load it from existing Properties
\r
345 public synchronized StaticSlot staticSlot(String name, final String propName) {
\r
346 StaticSlot ss = staticSlot(name);
\r
347 put(ss,getProperty(propName));
\r