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<>();
\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
106 public LogTarget fatal() {
\r
110 public void fatal(LogTarget lt) {
\r
115 public LogTarget error() {
\r
119 public void error(LogTarget lt) {
\r
124 public LogTarget audit() {
\r
128 public void audit(LogTarget lt) {
\r
133 public LogTarget init() {
\r
137 public void init(LogTarget lt) {
\r
142 public LogTarget warn() {
\r
146 public void warn(LogTarget lt) {
\r
151 public LogTarget info() {
\r
155 public void info(LogTarget lt) {
\r
160 public LogTarget debug() {
\r
164 public void debug(LogTarget lt) {
\r
169 public LogTarget trace() {
\r
173 public void trace(LogTarget lt) {
\r
178 public TimeTaken start(String name, int flag, Object ... values) {
\r
179 return new TimeTaken(name, flag, values) {
\r
181 * Format to be printed when called upon
\r
184 public void output(StringBuilder sb) {
\r
187 case Env.XML: sb.append("XML "); break;
\r
188 case Env.JSON: sb.append("JSON "); break;
\r
189 case Env.REMOTE: sb.append("REMOTE "); break;
\r
191 sb.append(String.format(name, values));
\r
192 if ((flag & Env.CHECKPOINT) != Env.CHECKPOINT) {
\r
194 sb.append((end-start)/1000000f);
\r
197 sb.append(" size: ");
\r
198 sb.append(Long.toString(size));
\r
206 public String getProperty(String key) {
\r
207 return get(staticSlot(key),null);
\r
210 public Properties getProperties(String ... filter) {
\r
211 Properties props = new Properties();
\r
213 for (String key : existingStaticSlotNames()) {
\r
214 if (filter.length>0) {
\r
216 for (String f : filter) {
\r
217 if (key.startsWith(f)) {
\r
226 String value = getProperty(key);
\r
228 props.put(key, value);
\r
236 public String getProperty(String key, String defaultValue) {
\r
237 return get(staticSlot(key),defaultValue);
\r
241 public String setProperty(String key, String value) {
\r
242 put(staticSlot(key),value==null?null:value.trim());
\r
246 protected Decryptor decryptor = Decryptor.NULL;
\r
247 protected Encryptor encryptor = Encryptor.NULL;
\r
250 public Decryptor decryptor() {
\r
254 public void set(Decryptor newDecryptor) {
\r
255 decryptor = newDecryptor;
\r
258 public Encryptor encryptor() {
\r
262 public void set(Encryptor newEncryptor) {
\r
263 encryptor = newEncryptor;
\r
267 // @SuppressWarnings("unchecked")
\r
269 public <T> DataFactory<T> newDataFactory(Class<?>... classes) throws APIException {
\r
270 // if (String.class.isAssignableFrom(classes[0]))
\r
271 // return (DataFactory<T>) new StringDF(this);
\r
272 return new JAXBDF<T>(this,classes);
\r
275 // @SuppressWarnings("unchecked")
\r
277 public <T> DataFactory<T> newDataFactory(Schema schema, Class<?>... classes) throws APIException {
\r
278 // if (String.class.isAssignableFrom(classes[0]))
\r
279 // return (DataFactory<T>) new StringDF(this);
\r
280 return new JAXBDF<T>(this, schema, classes);
\r
283 // @SuppressWarnings("unchecked")
\r
285 public<T> DataFactory<T> newDataFactory(QName qName, Class<?> ... classes) throws APIException {
\r
286 // if (String.class.isAssignableFrom(classes[0]))
\r
287 // return (DataFactory<T>) new StringDF(this);
\r
288 return new JAXBDF<T>(this, qName, classes);
\r
292 public<T> DataFactory<T> newDataFactory(Schema schema, QName qName, Class<?> ... classes) throws APIException {
\r
293 return new JAXBDF<T>(this, schema, qName, classes);
\r
297 public BasicTrans newTrans() {
\r
298 return new BasicTrans(this);
\r
301 public void loadFromSystemPropsStartsWith(String ... str) {
\r
302 for (String name : System.getProperties().stringPropertyNames()) {
\r
303 for (String s : str) {
\r
304 if (name.startsWith(s)) {
\r
305 setProperty(name, System.getProperty(name));
\r
315 public void loadToSystemPropsStartsWith(String ... str) {
\r
317 for (String name : existingStaticSlotNames()) {
\r
318 for (String s : str) {
\r
319 if (name.startsWith(s)) {
\r
320 if ((value = getProperty(name))!=null)
\r
321 System.setProperty(name,value);
\r
327 public void loadPropFiles(String tag, ClassLoader classloader) throws IOException {
\r
328 String propfiles = getProperty(tag);
\r
329 if (propfiles!=null) {
\r
330 for (String pf : Split.splitTrim(File.pathSeparatorChar, propfiles)) {
\r
331 InputStream is = classloader==null?null:classloader.getResourceAsStream(pf);
\r
333 File f = new File(pf);
\r
335 is = new FileInputStream(f);
\r
339 BufferedReader br = new BufferedReader(new InputStreamReader(is));
\r
342 while ((line=br.readLine())!=null) {
\r
343 line = line.trim();
\r
344 if (!line.startsWith("#")) {
\r
345 String[] tv = Split.splitTrim('=', line);
\r
346 if (tv.length==2) {
\r
347 setProperty(tv[0],tv[1]);
\r
354 } catch (IOException e) {
\r
364 * Create a StaticSlot, and load it from existing Properties
\r
370 public synchronized StaticSlot staticSlot(String name, final String propName) {
\r
371 StaticSlot ss = staticSlot(name);
\r
372 put(ss,getProperty(propName));
\r