1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.inno.env.impl;
\r
25 import java.applet.Applet;
\r
26 import java.io.BufferedReader;
\r
27 import java.io.File;
\r
28 import java.io.FileInputStream;
\r
29 import java.io.IOException;
\r
30 import java.io.InputStream;
\r
31 import java.io.InputStreamReader;
\r
32 import java.util.Properties;
\r
34 import javax.xml.namespace.QName;
\r
35 import javax.xml.validation.Schema;
\r
37 import com.att.inno.env.APIException;
\r
38 import com.att.inno.env.DataFactory;
\r
39 import com.att.inno.env.Decryptor;
\r
40 import com.att.inno.env.Encryptor;
\r
41 import com.att.inno.env.Env;
\r
42 import com.att.inno.env.EnvJAXB;
\r
43 import com.att.inno.env.LogTarget;
\r
44 import com.att.inno.env.StoreImpl;
\r
45 import com.att.inno.env.TimeTaken;
\r
46 import com.att.inno.env.TransCreate;
\r
47 import com.att.inno.env.TransJAXB;
\r
48 import com.att.inno.env.jaxb.JAXBDF;
\r
49 import com.att.inno.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
58 public class BasicEnv extends StoreImpl implements EnvJAXB, TransCreate<TransJAXB>{
\r
59 protected LogTarget fatal=LogTarget.SYSERR;
\r
60 protected LogTarget error=LogTarget.SYSERR;
\r
61 protected LogTarget audit=LogTarget.SYSOUT;
\r
62 protected LogTarget init=LogTarget.SYSOUT;
\r
63 protected LogTarget warn=LogTarget.SYSERR;
\r
64 protected LogTarget info=LogTarget.SYSOUT;
\r
65 protected LogTarget debug=LogTarget.NULL;
\r
66 protected LogTarget trace=LogTarget.NULL;
\r
67 // protected Map<String, String> props;
\r
69 // private boolean sysprops;
\r
71 public BasicEnv(String ... args) {
\r
75 public BasicEnv(String tag, String[] args) {
\r
81 * Suitable for use in Applets... obtain all the values
\r
82 * listed for the variable String arg "tags"
\r
84 public BasicEnv(Applet applet, String ... tags) {
\r
86 // props = new HashMap<String, String>();
\r
88 // for(int i=0;i<tags.length;++i) {
\r
89 // value = applet.getParameter(tags[i]);
\r
90 // if(value!=null) {
\r
91 // props.put(tags[i], value);
\r
96 public BasicEnv(Properties props) {
\r
100 public BasicEnv(String tag, Properties props) {
\r
107 public LogTarget fatal() {
\r
112 public LogTarget error() {
\r
118 public LogTarget audit() {
\r
123 public LogTarget init() {
\r
128 public LogTarget warn() {
\r
133 public LogTarget info() {
\r
138 public LogTarget debug() {
\r
142 public void debug(LogTarget lt) {
\r
147 public LogTarget trace() {
\r
152 public TimeTaken start(String name, int flag) {
\r
153 return new TimeTaken(name, flag) {
\r
155 * Format to be printed when called upon
\r
158 public void output(StringBuilder sb) {
\r
161 case Env.XML: sb.append("XML "); break;
\r
162 case Env.JSON: sb.append("JSON "); break;
\r
163 case Env.REMOTE: sb.append("REMOTE "); break;
\r
166 if(flag != Env.CHECKPOINT) {
\r
168 sb.append((end-start)/1000000f);
\r
171 sb.append(" size: ");
\r
172 sb.append(Long.toString(size));
\r
180 public String getProperty(String key) {
\r
181 return get(staticSlot(key),null);
\r
184 public Properties getProperties(String ... filter) {
\r
185 Properties props = new Properties();
\r
187 for(String key : existingStaticSlotNames()) {
\r
188 if(filter.length>0) {
\r
190 for(String f : filter) {
\r
191 if(key.startsWith(f)) {
\r
200 String value = getProperty(key);
\r
202 props.put(key, value);
\r
210 public String getProperty(String key, String defaultValue) {
\r
211 return get(staticSlot(key),defaultValue);
\r
215 public String setProperty(String key, String value) {
\r
216 put(staticSlot(key),value==null?null:value.trim());
\r
220 protected Decryptor decryptor = Decryptor.NULL;
\r
221 protected Encryptor encryptor = Encryptor.NULL;
\r
224 public Decryptor decryptor() {
\r
228 public void set(Decryptor newDecryptor) {
\r
229 decryptor = newDecryptor;
\r
232 public Encryptor encryptor() {
\r
236 public void set(Encryptor newEncryptor) {
\r
237 encryptor = newEncryptor;
\r
241 // @SuppressWarnings("unchecked")
\r
243 public <T> DataFactory<T> newDataFactory(Class<?>... classes) throws APIException {
\r
244 // if(String.class.isAssignableFrom(classes[0]))
\r
245 // return (DataFactory<T>) new StringDF(this);
\r
246 return new JAXBDF<T>(this,classes);
\r
249 // @SuppressWarnings("unchecked")
\r
251 public <T> DataFactory<T> newDataFactory(Schema schema, Class<?>... classes) throws APIException {
\r
252 // if(String.class.isAssignableFrom(classes[0]))
\r
253 // return (DataFactory<T>) new StringDF(this);
\r
254 return new JAXBDF<T>(this, schema, classes);
\r
257 // @SuppressWarnings("unchecked")
\r
259 public<T> DataFactory<T> newDataFactory(QName qName, Class<?> ... classes) throws APIException {
\r
260 // if(String.class.isAssignableFrom(classes[0]))
\r
261 // return (DataFactory<T>) new StringDF(this);
\r
262 return new JAXBDF<T>(this, qName, classes);
\r
266 public<T> DataFactory<T> newDataFactory(Schema schema, QName qName, Class<?> ... classes) throws APIException {
\r
267 return new JAXBDF<T>(this, schema, qName, classes);
\r
271 public BasicTrans newTrans() {
\r
272 return new BasicTrans(this);
\r
275 public void loadFromSystemPropsStartsWith(String ... str) {
\r
276 for(String name : System.getProperties().stringPropertyNames()) {
\r
277 for(String s : str) {
\r
278 if(name.startsWith(s)) {
\r
279 setProperty(name, System.getProperty(name));
\r
289 public void loadToSystemPropsStartsWith(String ... str) {
\r
291 for(String name : existingStaticSlotNames()) {
\r
292 for(String s : str) {
\r
293 if(name.startsWith(s)) {
\r
294 if((value = getProperty(name))!=null)
\r
295 System.setProperty(name,value);
\r
301 public void loadPropFiles(String tag, ClassLoader classloader) throws IOException {
\r
302 String propfiles = getProperty(tag);
\r
303 if(propfiles!=null) {
\r
304 for(String pf : Split.splitTrim(File.pathSeparatorChar, propfiles)) {
\r
305 InputStream is = classloader==null?null:classloader.getResourceAsStream(pf);
\r
307 File f = new File(pf);
\r
309 is = new FileInputStream(f);
\r
313 BufferedReader br = new BufferedReader(new InputStreamReader(is));
\r
316 while((line=br.readLine())!=null) {
\r
317 line = line.trim();
\r
318 if(!line.startsWith("#")) {
\r
319 String[] tv = Split.splitTrim('=', line);
\r
321 setProperty(tv[0],tv[1]);
\r
328 } catch (IOException e) {
\r