Create method of Logging to O/S from Container
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / Access.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.cadi;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Properties;
27
28 /**
29  * Various Environments require different logging mechanisms, or at least allow
30  * for different ones. We need the Framework to be able to hook into any particular instance of logging
31  * mechanism, whether it be a Logging Object within a Servlet Context, or a direct library like log4j.
32  * This interface, therefore, allows maximum pluggability in a variety of different app styles.  
33  *  
34  * @author Jonathan
35  *
36  */
37 public interface Access {
38         // levels to use
39         public enum Level {
40                 DEBUG(0x1), INFO(0x10), AUDIT(0x100), WARN(0x2000), ERROR(0x4000), INIT(0x8000),TRACE(0x10000),NONE(0XFFFF);
41                 private final int bit;
42                 
43                 Level(int ord) {
44                         bit = ord;
45                 }
46                 
47                 public boolean inMask(int mask) {
48                         return (mask & bit) == bit;
49                 }
50                 
51                 public int addToMask(int mask) {
52                         return mask | bit;
53                 }
54
55                 public int delFromMask(int mask) {
56                         return mask & ~bit;
57                 }
58
59                 public int toggle(int mask) {
60                         if(inMask(mask)) {
61                                 return delFromMask(mask);
62                         } else {
63                                 return addToMask(mask);
64                         }
65                 }
66
67
68                 public int maskOf() {
69                         int mask=0;
70                         for(Level l : values()) {
71                                 if(ordinal()<=l.ordinal() && l!=NONE) {
72                                         mask|=l.bit;
73                                 }
74                         }
75                         return mask;
76                 }
77         }
78
79         /**
80          * Write a variable list of Object's text via the toString() method with appropriate space, etc.
81          * @param elements
82          */
83         public void log(Level level, Object ... elements);
84
85         /**
86          * Printf mechanism for Access
87          * @param level
88          * @param fmt
89          * @param elements
90          */
91         public void printf(Level level, String fmt, Object ... elements);
92         
93         /** 
94          * Check if message will log before constructing
95          * @param level
96          * @return
97          */
98         public boolean willLog(Level level);
99
100         /**
101          * Write the contents of an exception, followed by a variable list of Object's text via the 
102          * toString() method with appropriate space, etc.
103          * 
104          * The Loglevel is always "ERROR"
105          * 
106          * @param elements
107          */
108         public void log(Exception e, Object ... elements);
109         
110         /**
111          * Set the Level to compare logging too
112          */
113         public void setLogLevel(Level level);
114                 
115         /**
116          * It is important in some cases to create a class from within the same Classloader that created
117          * Security Objects.  Specifically, it's pretty typical for Web Containers to separate classloaders
118          * so as to allow Apps with different dependencies. 
119          * @return
120          */
121         public ClassLoader classLoader();
122
123         public String getProperty(String string, String def);
124
125         public Properties getProperties();
126
127         public void load(InputStream is) throws IOException;
128
129         /**
130          * if "anytext" is true, then decryption will always be attempted.  Otherwise, only if starts with 
131          * Symm.ENC
132          * @param encrypted
133          * @param anytext
134          * @return
135          * @throws IOException
136          */
137         public String decrypt(String encrypted, boolean anytext) throws IOException;
138
139         public static final Access NULL = new Access() {
140                 public void log(Level level, Object... elements) {
141                 }
142
143                 @Override
144                 public void printf(Level level, String fmt, Object... elements) {
145                 }
146
147                 public void log(Exception e, Object... elements) {
148                 }
149
150                 public ClassLoader classLoader() {
151                         return ClassLoader.getSystemClassLoader();
152                 }
153
154                 public String getProperty(String string, String def) {
155                         return null;
156                 }
157
158                 public void load(InputStream is) throws IOException {
159                 }
160
161                 public void setLogLevel(Level level) {
162                 }
163
164                 public String decrypt(String encrypted, boolean anytext) throws IOException {
165                         return encrypted;
166                 }
167
168                 @Override
169                 public boolean willLog(Level level) {
170                         return false;
171                 }
172
173                 @Override
174                 public Properties getProperties() {
175                         return new Properties();
176                 }
177         };
178
179
180 }