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