From 37e47101836b20ee495962a6d813a9d5f8d9109a Mon Sep 17 00:00:00 2001 From: sg481n Date: Thu, 3 Aug 2017 17:44:44 -0400 Subject:  [AAF-23] Initial code import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I8c0dc03058ae4fbb85c28a050bdd77e6c372d6c3 Signed-off-by: sg481n --- .../main/java/com/att/inno/env/impl/AbsTrans.java | 216 +++++++++++++ .../java/com/att/inno/env/impl/AbsTransJAXB.java | 59 ++++ .../main/java/com/att/inno/env/impl/BasicEnv.java | 337 +++++++++++++++++++++ .../java/com/att/inno/env/impl/BasicTrans.java | 83 +++++ .../java/com/att/inno/env/impl/EnvFactory.java | 68 +++++ .../com/att/inno/env/impl/JavaUtilLogTarget.java | 91 ++++++ .../java/com/att/inno/env/impl/Log4JLogTarget.java | 111 +++++++ .../java/com/att/inno/env/impl/NullLifeCycle.java | 60 ++++ 8 files changed, 1025 insertions(+) create mode 100644 env/src/main/java/com/att/inno/env/impl/AbsTrans.java create mode 100644 env/src/main/java/com/att/inno/env/impl/AbsTransJAXB.java create mode 100644 env/src/main/java/com/att/inno/env/impl/BasicEnv.java create mode 100644 env/src/main/java/com/att/inno/env/impl/BasicTrans.java create mode 100644 env/src/main/java/com/att/inno/env/impl/EnvFactory.java create mode 100644 env/src/main/java/com/att/inno/env/impl/JavaUtilLogTarget.java create mode 100644 env/src/main/java/com/att/inno/env/impl/Log4JLogTarget.java create mode 100644 env/src/main/java/com/att/inno/env/impl/NullLifeCycle.java (limited to 'env/src/main/java/com/att/inno/env/impl') diff --git a/env/src/main/java/com/att/inno/env/impl/AbsTrans.java b/env/src/main/java/com/att/inno/env/impl/AbsTrans.java new file mode 100644 index 0000000..23f3c03 --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/AbsTrans.java @@ -0,0 +1,216 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package com.att.inno.env.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +import com.att.inno.env.Env; +import com.att.inno.env.LogTarget; +import com.att.inno.env.Slot; +import com.att.inno.env.StoreImpl; +import com.att.inno.env.TimeTaken; +import com.att.inno.env.TransStore; + +public abstract class AbsTrans implements TransStore { + private static final float[] EMPTYF = new float[0]; + private static final Object[] EMPTYO = new Object[0]; + + protected ENV delegate; + protected List trail = new ArrayList(30); + private Object[] state; + + + public AbsTrans(ENV delegate) { + this.delegate = delegate; + state = delegate instanceof StoreImpl?((StoreImpl) delegate).newTransState():EMPTYO; + } + + // @Override + public LogTarget fatal() { + return delegate.fatal(); + } + +// @Override + public LogTarget error() { + return delegate.error(); + } + +// @Override + public LogTarget audit() { + return delegate.audit(); + } + +// @Override + public LogTarget init() { + return delegate.init(); + } + +// @Override + public LogTarget warn() { + return delegate.warn(); + } + +// @Override + public LogTarget info() { + return delegate.info(); + } + +// @Override + public LogTarget debug() { + return delegate.debug(); + } + +// @Override + public LogTarget trace() { + return delegate.trace(); + } + + /** + * Let the final Trans Implementation choose the exact kind of TimeTaken to use + * @param name + * @param flag + * @return + */ + protected abstract TimeTaken newTimeTaken(String name, int flag); + +// @Override + public final TimeTaken start(String name, int flag) { + TimeTaken tt = newTimeTaken(name,flag); + trail.add(tt); + return tt; + } + +// @Override + public final void checkpoint(String name) { + TimeTaken tt = newTimeTaken(name,CHECKPOINT); + tt.done(); + trail.add(tt); + } + + public final void checkpoint(String name, int additionalFlag) { + TimeTaken tt = newTimeTaken(name,CHECKPOINT|additionalFlag); + tt.done(); + trail.add(tt); + } + + @Override + public Metric auditTrail(int indent, StringBuilder sb, int ... flags) { + return auditTrail(info(),indent,sb,flags); + } + + @Override + public Metric auditTrail(LogTarget lt, int indent, StringBuilder sb, int ... flags) { + Metric metric = new Metric(); + int last = (metric.entries = trail.size()) -1; + metric.buckets = flags.length==0?EMPTYF:new float[flags.length]; + if(last>=0) { + TimeTaken first = trail.get(0); + // If first entry is sub, then it's actually the last "end" as well + // otherwise, check end + //long end = (first.flag&SUB)==SUB?first.end():trail.get(last).end(); + long end = trail.get(last).end(); + metric.total = (end - first.start) / 1000000f; + } + + if(sb==null) { + for(TimeTaken tt : trail) { + float ms = tt.millis(); + for(int i=0;i stack = new Stack(); + for(TimeTaken tt : trail) { + // Create Indentation based on SUB + while(!stack.isEmpty() && tt.end()>stack.peek()) { + --indent; + stack.pop(); + } + for(int i=0;i T get(Slot slot, T deflt) { + Object o; + try { + o = slot.get(state); + } catch(ArrayIndexOutOfBoundsException e) { + // Env State Size has changed because of dynamic Object creation... Rare event, but needs to be covered + Object[] temp = ((StoreImpl) delegate).newTransState(); + System.arraycopy(state, 0, temp, 0, state.length); + state = temp; + o=null; + } + return o==null?deflt:(T)o; + } + + +} diff --git a/env/src/main/java/com/att/inno/env/impl/AbsTransJAXB.java b/env/src/main/java/com/att/inno/env/impl/AbsTransJAXB.java new file mode 100644 index 0000000..5c15500 --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/AbsTransJAXB.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package com.att.inno.env.impl; + +import javax.xml.namespace.QName; +import javax.xml.validation.Schema; + +import com.att.inno.env.APIException; +import com.att.inno.env.DataFactory; +import com.att.inno.env.EnvJAXB; +import com.att.inno.env.TransJAXB; + +public abstract class AbsTransJAXB extends AbsTrans implements TransJAXB { + public AbsTransJAXB(EnvJAXB env) { + super(env); + } + +// @Override + public DataFactory newDataFactory(Class... classes) throws APIException { + return delegate.newDataFactory(classes); + } + +// @Override + public DataFactory newDataFactory(Schema schema, Class... classes) throws APIException { + return delegate.newDataFactory(schema, classes); + } + +// @Override + public DataFactory newDataFactory(QName qName, Class... classes) throws APIException { + return delegate.newDataFactory(qName, classes); + } + +// @Override + public DataFactory newDataFactory(Schema schema, QName qName, Class... classes) throws APIException { + return delegate.newDataFactory(schema, qName, classes); + } + +} diff --git a/env/src/main/java/com/att/inno/env/impl/BasicEnv.java b/env/src/main/java/com/att/inno/env/impl/BasicEnv.java new file mode 100644 index 0000000..e18248a --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/BasicEnv.java @@ -0,0 +1,337 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package com.att.inno.env.impl; + +import java.applet.Applet; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Properties; + +import javax.xml.namespace.QName; +import javax.xml.validation.Schema; + +import com.att.inno.env.APIException; +import com.att.inno.env.DataFactory; +import com.att.inno.env.Decryptor; +import com.att.inno.env.Encryptor; +import com.att.inno.env.Env; +import com.att.inno.env.EnvJAXB; +import com.att.inno.env.LogTarget; +import com.att.inno.env.StoreImpl; +import com.att.inno.env.TimeTaken; +import com.att.inno.env.TransCreate; +import com.att.inno.env.TransJAXB; +import com.att.inno.env.jaxb.JAXBDF; +import com.att.inno.env.util.Split; + +/** + * An essential Implementation of Env, which will fully function, without any sort + * of configuration. + * + * Use as a basis for Group level Env, just overriding where needed. + * + */ +public class BasicEnv extends StoreImpl implements EnvJAXB, TransCreate{ + protected LogTarget fatal=LogTarget.SYSERR; + protected LogTarget error=LogTarget.SYSERR; + protected LogTarget audit=LogTarget.SYSOUT; + protected LogTarget init=LogTarget.SYSOUT; + protected LogTarget warn=LogTarget.SYSERR; + protected LogTarget info=LogTarget.SYSOUT; + protected LogTarget debug=LogTarget.NULL; + protected LogTarget trace=LogTarget.NULL; +// protected Map props; + +// private boolean sysprops; + + public BasicEnv(String ... args) { + super(null,args); + } + + public BasicEnv(String tag, String[] args) { + super(tag, args); + } + + + /** + * Suitable for use in Applets... obtain all the values + * listed for the variable String arg "tags" + */ + public BasicEnv(Applet applet, String ... tags) { + super(null, tags); +// props = new HashMap(); +// String value; +// for(int i=0;i=0) { + sb.append(" size: "); + sb.append(Long.toString(size)); + } + } + } + }; + } + + // @Override + public String getProperty(String key) { + return get(staticSlot(key),null); + } + + public Properties getProperties(String ... filter) { + Properties props = new Properties(); + boolean yes; + for(String key : existingStaticSlotNames()) { + if(filter.length>0) { + yes = false; + for(String f : filter) { + if(key.startsWith(f)) { + yes = true; + break; + } + } + } else { + yes = true; + } + if(yes) { + String value = getProperty(key); + if(value!=null) { + props.put(key, value); + } + } + } + return props; + } + + // @Override + public String getProperty(String key, String defaultValue) { + return get(staticSlot(key),defaultValue); + } + + // @Override + public String setProperty(String key, String value) { + put(staticSlot(key),value==null?null:value.trim()); + return value; + } + + protected Decryptor decryptor = Decryptor.NULL; + protected Encryptor encryptor = Encryptor.NULL; + + + public Decryptor decryptor() { + return decryptor; + } + + public void set(Decryptor newDecryptor) { + decryptor = newDecryptor; + } + + public Encryptor encryptor() { + return encryptor; + } + + public void set(Encryptor newEncryptor) { + encryptor = newEncryptor; + } + + +// @SuppressWarnings("unchecked") + // @Override + public DataFactory newDataFactory(Class... classes) throws APIException { +// if(String.class.isAssignableFrom(classes[0])) +// return (DataFactory) new StringDF(this); + return new JAXBDF(this,classes); + } + +// @SuppressWarnings("unchecked") + // @Override + public DataFactory newDataFactory(Schema schema, Class... classes) throws APIException { +// if(String.class.isAssignableFrom(classes[0])) +// return (DataFactory) new StringDF(this); + return new JAXBDF(this, schema, classes); + } + +// @SuppressWarnings("unchecked") + // @Override + public DataFactory newDataFactory(QName qName, Class ... classes) throws APIException { +// if(String.class.isAssignableFrom(classes[0])) +// return (DataFactory) new StringDF(this); + return new JAXBDF(this, qName, classes); + } + + // @Override + public DataFactory newDataFactory(Schema schema, QName qName, Class ... classes) throws APIException { + return new JAXBDF(this, schema, qName, classes); + } + + // @Override + public BasicTrans newTrans() { + return new BasicTrans(this); + } + + public void loadFromSystemPropsStartsWith(String ... str) { + for(String name : System.getProperties().stringPropertyNames()) { + for(String s : str) { + if(name.startsWith(s)) { + setProperty(name, System.getProperty(name)); + } + } + } + } + + /** + * + * + */ + public void loadToSystemPropsStartsWith(String ... str) { + String value; + for(String name : existingStaticSlotNames()) { + for(String s : str) { + if(name.startsWith(s)) { + if((value = getProperty(name))!=null) + System.setProperty(name,value); + } + } + } + } + + public void loadPropFiles(String tag, ClassLoader classloader) throws IOException { + String propfiles = getProperty(tag); + if(propfiles!=null) { + for(String pf : Split.splitTrim(File.pathSeparatorChar, propfiles)) { + InputStream is = classloader==null?null:classloader.getResourceAsStream(pf); + if(is==null) { + File f = new File(pf); + if(f.exists()) { + is = new FileInputStream(f); + } + } + if(is!=null) { + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + try { + String line; + while((line=br.readLine())!=null) { + line = line.trim(); + if(!line.startsWith("#")) { + String[] tv = Split.splitTrim('=', line); + if(tv.length==2) { + setProperty(tv[0],tv[1]); + } + } + } + } finally { + try { + br.close(); + } catch (IOException e) { + error().log(e); + } + } + } + } + } + } +} diff --git a/env/src/main/java/com/att/inno/env/impl/BasicTrans.java b/env/src/main/java/com/att/inno/env/impl/BasicTrans.java new file mode 100644 index 0000000..c390cd5 --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/BasicTrans.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package com.att.inno.env.impl; + +import com.att.inno.env.Decryptor; +import com.att.inno.env.Encryptor; +import com.att.inno.env.EnvJAXB; +import com.att.inno.env.Slot; +import com.att.inno.env.StaticSlot; +import com.att.inno.env.TimeTaken; + + +public class BasicTrans extends AbsTransJAXB { + + public BasicTrans(EnvJAXB env) { + super(env); + } + + @Override + protected TimeTaken newTimeTaken(String name, int flag) { + /** + * Note: could have created a different format for Time Taken, but using BasicEnv's instead + */ + return delegate.start(name, flag); + } + + public Slot slot(String name) { + return delegate.slot(name); + } + + public T get(StaticSlot slot) { + return delegate.get(slot); + } + + public T get(StaticSlot slot, T dflt) { + return delegate.get(slot,dflt); + } + + public String setProperty(String tag, String value) { + delegate.setProperty(tag, value); + return value; + } + + public String getProperty(String tag) { + return delegate.getProperty(tag); + } + + public String getProperty(String tag, String deflt) { + return delegate.getProperty(tag, deflt); + } + + @Override + public Decryptor decryptor() { + return delegate.decryptor(); + } + + @Override + public Encryptor encryptor() { + return delegate.encryptor(); + } + +} diff --git a/env/src/main/java/com/att/inno/env/impl/EnvFactory.java b/env/src/main/java/com/att/inno/env/impl/EnvFactory.java new file mode 100644 index 0000000..6018990 --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/EnvFactory.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package com.att.inno.env.impl; + +import com.att.inno.env.EnvJAXB; +import com.att.inno.env.TransCreate; +import com.att.inno.env.TransJAXB; + +/** + * EnvFactory + * + */ +public class EnvFactory { + + public static final String SCHEMA_DIR = "env-schema_dir"; + public static final String DEFAULT_SCHEMA_DIR = "src/main/xsd"; + static BasicEnv singleton; + + static { + singleton = new BasicEnv(); + } + public static BasicEnv singleton() { + return singleton; + } + + public static void setSingleton(BasicEnv be) { + singleton = be; + } + + public static TransJAXB newTrans() { + return new BasicTrans(singleton); + } + + public static TransJAXB newTrans(EnvJAXB env) { + return new BasicTrans(env); + } + + public static TransCreate transCreator() { + return new TransCreate() { + // @Override + public BasicTrans newTrans() { + return singleton.newTrans(); + } + }; + } +} + diff --git a/env/src/main/java/com/att/inno/env/impl/JavaUtilLogTarget.java b/env/src/main/java/com/att/inno/env/impl/JavaUtilLogTarget.java new file mode 100644 index 0000000..6577ef1 --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/JavaUtilLogTarget.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package com.att.inno.env.impl; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.att.inno.env.LogTarget; + +/** + * This LogTarget Implementation is included mostly because the JavaUtil based logging is included in the + * JDK. This makes the default implementation independent of any external Jars. + * + * Log4j is often considered more Enterprise capable. See Log4JLogTarget for that implementation + * + * + */ +public class JavaUtilLogTarget implements LogTarget { + private Level level; + private Logger log; + + public JavaUtilLogTarget(Logger logger, Level theLevel) { + log = logger; + level = theLevel; + } + + public boolean isLoggable() { + return log.isLoggable(level); + } + + public void log(Object ... msgs) { + if(log.isLoggable(level)) { + StringBuilder sb = new StringBuilder(); + String msg; + for(int i=0;i0) { + int sbl = sb.length(); + if(sbl>0) { + char last = sb.charAt(sbl-1); + if(" (.".indexOf(last)<0 && "().".indexOf(msg.charAt(0))<0)sb.append(' '); + } + sb.append(msg); + } + } + log.log(level, sb.toString()); + } + } + + public void log(Throwable e, Object ... msgs) { + String str = e.getLocalizedMessage(); + if(str==null) { + str = e.getMessage(); + } + if(str==null) { + str = e.getClass().getName(); + } + log.log(level,str,msgs); + } + + /* (non-Javadoc) + * @see com.att.inno.env.LogTarget#printf(java.lang.String, java.lang.String[]) + */ + @Override + public void printf(String fmt, Object ... vars) { + if(log.isLoggable(level)) { + log.log(level,String.format(fmt,vars)); + } + } +} diff --git a/env/src/main/java/com/att/inno/env/impl/Log4JLogTarget.java b/env/src/main/java/com/att/inno/env/impl/Log4JLogTarget.java new file mode 100644 index 0000000..295f3dd --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/Log4JLogTarget.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package com.att.inno.env.impl; + +import java.io.PrintWriter; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +import com.att.inno.env.APIException; +import com.att.inno.env.LogTarget; +import com.att.inno.env.util.StringBuilderWriter; + +/** + * Many services have chosen to use Log4J for their lower level Logging Implementation. This LogTarget will allow + * any of the messages sent to be set to the appropriate Log4J level. + * + * + */ +public class Log4JLogTarget implements LogTarget { + private Level level; + private Logger log; + + public Log4JLogTarget(String loggerName, Level level) throws APIException { + this.level = level; + if (loggerName != null && loggerName.length() > 0) { + log = Logger.getLogger(loggerName); + } else { + log = Logger.getRootLogger(); + } + } + + // @Override + public boolean isLoggable() { + return log.isEnabledFor(level); + } + + // @Override + public void log(Object... msgs) { + log(null, msgs); + } + + // @Override + public void log(Throwable e, Object... msgs) { + if (log.isEnabledFor(level)) { + StringBuilder sb = new StringBuilder(); + + String msg; + if (e != null) { + e.printStackTrace(new PrintWriter(new StringBuilderWriter(sb))); + } + for (int i = 0; i < msgs.length; ++i) { + if(msgs[i]!=null) { + msg = msgs[i].toString(); + if (msg != null && msg.length() > 0) { + int sbl = sb.length(); + if (sbl > 0) { + char last = sb.charAt(sbl - 1); + if (" (.".indexOf(last) < 0 + && "().".indexOf(msg.charAt(0)) < 0) + sb.append(' '); + } + sb.append(msg); + } + } + } + log.log(level, sb.toString()); + } + } + + /* (non-Javadoc) + * @see com.att.inno.env.LogTarget#printf(java.lang.String, java.lang.String[]) + */ + @Override + public void printf(String fmt, Object ... vars) { + if(log.isEnabledFor(level)) { + log.log(level,String.format(fmt,vars)); + } + } + + public static void setLog4JEnv(String loggerName, BasicEnv env) throws APIException { + env.fatal = new Log4JLogTarget(loggerName,Level.FATAL); + env.error = new Log4JLogTarget(loggerName,Level.ERROR); + env.warn = env.audit = env.init = new Log4JLogTarget(loggerName,Level.WARN); + env.info = new Log4JLogTarget(loggerName,Level.INFO); + env.debug = new Log4JLogTarget(loggerName,Level.DEBUG); + env.trace = new Log4JLogTarget(loggerName,Level.TRACE); + } + +} diff --git a/env/src/main/java/com/att/inno/env/impl/NullLifeCycle.java b/env/src/main/java/com/att/inno/env/impl/NullLifeCycle.java new file mode 100644 index 0000000..5725cf1 --- /dev/null +++ b/env/src/main/java/com/att/inno/env/impl/NullLifeCycle.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +/** + * + */ +package com.att.inno.env.impl; + +import com.att.inno.env.APIException; +import com.att.inno.env.Env; +import com.att.inno.env.LifeCycle; + + + +/** + *

NullLifeCycle

+ * + * This is a convenience class for those Objects which should + * implement LifeCycle, but don't have anything to do in any of the + * LifeCycle methods defined. Extending + * NullLifeCycle reduces the required methods for the class by 5. + * Any one or two of them can be overloaded.

+ * + * If more are overloaded, it is + * recommended just to implement LifeCycle. + *

+ * + * This only works, though, if the Object doesn't need to extend something + * else, due to Java's Single Extension policy. In other cases, just + * implement LifeCycle, and leave them empty. + * + * + */ +public class NullLifeCycle implements LifeCycle { + public void servicePrestart(Env env) throws APIException {} + public void threadPrestart(Env env) throws APIException {} + public void refresh(Env env) throws APIException {} + public void threadDestroy(Env env) throws APIException {} + public void serviceDestroy(Env env) throws APIException {} +} -- cgit 1.2.3-korg