a6481aaeb472b56c1e91cf66964267d08181b99d
[policy/engine.git] / PolicyEngineUtils / src / main / java / org / onap / policy / utils / PolicyAccess.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.policy.utils;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Date;
26 import java.util.Properties;
27
28 import org.apache.log4j.Logger;
29
30 import com.att.cadi.Access;
31
32 /**
33  * PolicyAccess used by AAF for logging purposes. 
34  *  
35  */
36 public class PolicyAccess implements Access {
37         private static final Logger logger = Logger.getLogger(PolicyAccess.class.getName());
38         
39         private Properties properties = new Properties(); 
40         private Access.Level logLevel = Access.Level.INFO;
41         
42         public PolicyAccess(Properties properties, Level level) {
43                 this.properties = properties;
44                 if(level!=null){
45                         logLevel = level;
46                 }
47         }
48         
49         @Override
50         public ClassLoader classLoader() {
51                 return getClass().getClassLoader();
52         }
53
54         @Override
55         public String decrypt(String enc, boolean arg1) throws IOException {
56                 return enc;
57         }
58
59         @Override
60         public String getProperty(String prop, String def) {
61                 return properties.getProperty(prop, def);
62         }
63
64         @Override
65         public void load(InputStream in) throws IOException {
66                 properties.load(in);
67         }
68
69         @Override
70         public void log(Level level, Object... args) {
71                 if (logLevel.compareTo(level) > 0) {
72             return;
73         }
74                 StringBuilder sb = new StringBuilder();
75         sb.append(new Date()).append(' ').append(level);
76         logtail(sb, args);
77         }
78
79         @Override
80         public void log(Exception e, Object... args) {
81                 StringBuilder sb = new StringBuilder();
82         sb.append(new Date()).append(" EXCEPTION ").append(e.getMessage());
83         logtail(sb, args);
84         logger.error(e.getMessage() + e);
85         }
86
87         @Override
88         public void setLogLevel(Level level) {
89                 logLevel = level;
90         }
91         
92         private void logtail(StringBuilder sb, Object[] args) {
93         for (Object o: args) {
94             String s = o.toString();
95             if (s.length() > 0) {
96                 sb.append(' ').append(s);
97             }
98         }
99         logger.info(sb.toString());
100     }
101
102         @Override
103         public boolean willLog(Level arg0) {
104                 return false;
105         }
106 }