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