Remove Tabs, per Jococo
[aaf/authz.git] / misc / log4j / src / main / java / org / onap / aaf / misc / env / log4j / LogFileNamer.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 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  */\r
21 package org.onap.aaf.misc.env.log4j;\r
22 \r
23 import java.io.File;\r
24 import java.io.IOException;\r
25 import java.net.URL;\r
26 \r
27 public class LogFileNamer {\r
28     private final String root;\r
29     private final String dir;\r
30 \r
31     public LogFileNamer(final String dir, final String root) {\r
32         this.dir = dir;\r
33         if (root == null || "".equals(root) || root.endsWith("/")) {\r
34             this.root = root;\r
35         } else {\r
36             this.root = root + "-";\r
37         }\r
38     }\r
39 \r
40     public LogFileNamer noPID() {\r
41         return this;\r
42     }\r
43 \r
44     private static final String FIRST_FILE_FORMAT_STR = "%s/%s%s.log";\r
45     private static final String FILE_FORMAT_STR = "%s/%s%s.%d.log";\r
46 \r
47     /**\r
48      * Accepts a String. If Separated by "|" then first part is the Appender name,\r
49      * and the second is used in the FileNaming (This is to allow for shortened\r
50      * Logger names, and more verbose file names) ONAP: jna code has license issues.\r
51      * Just do Date + Unique Number\r
52      * \r
53      * @param appender\r
54      * \r
55      *            returns the String Appender\r
56      * @throws IOException\r
57      */\r
58     public String setAppender(String appender) throws IOException {\r
59         File f = new File(String.format(FIRST_FILE_FORMAT_STR, dir, root, appender));\r
60         File lock = new File(f.getAbsoluteFile()+".lock");\r
61         if(f.exists()) {\r
62             if(lock.exists()) {\r
63                 int i = 0;\r
64                 while ((f = new File(String.format(FILE_FORMAT_STR, dir, root, appender, i))).exists() &&\r
65                        (lock = new File(f.getAbsoluteFile()+".lock")).exists()) {\r
66                     ++i;\r
67                 }\r
68             }\r
69         }\r
70         \r
71         try {\r
72             lock.createNewFile();\r
73             lock.deleteOnExit();\r
74             f.createNewFile();\r
75         } catch (IOException e) {\r
76             throw new IOException("Cannot create file '" + f.getCanonicalPath() + '\'', e);\r
77         }\r
78         System.setProperty("LOG4J_FILENAME_" + appender, f.getCanonicalPath());\r
79         return appender;\r
80     }\r
81 \r
82     public void configure(final String path, final String fname, final String log_level) throws IOException {\r
83         final String fullPath = path + '/' + fname;\r
84         if (new File(fullPath).exists()) {\r
85             org.apache.log4j.PropertyConfigurator.configureAndWatch(fullPath, 60 * 1000L);\r
86         } else {\r
87             URL rsrc = ClassLoader.getSystemResource(fname);\r
88             if (rsrc == null) {\r
89                 String msg = "Neither File: " + path + '/' + fname + " nor resource on Classpath " + fname + " exist";\r
90                 throw new IOException(msg);\r
91             }\r
92             org.apache.log4j.PropertyConfigurator.configure(rsrc);\r
93         }\r
94 \r
95     }\r
96 }\r