source: trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/commands/CMDcorrectHTMLLogDirs.java @ 1221

Last change on this file since 1221 was 1221, checked in by pharms, 11 years ago
  • added support for sorting log files depending on the server, for which they were

created

File size: 5.4 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.plugin.html.commands;
16
17import java.io.File;
18import java.util.Arrays;
19import java.util.List;
20import java.util.logging.Level;
21
22import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
23import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
24import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer;
25import de.ugoe.cs.util.console.Command;
26import de.ugoe.cs.util.console.Console;
27
28/**
29 * <p>
30 * Command to restructure log files of the HTML monitor to be able to easily separate the logs
31 * of different web sites.
32 * </p>
33 *
34 * @author Patrick Harms
35 * @version 1.0
36 */
37public class CMDcorrectHTMLLogDirs implements Command {
38
39    /*
40     * (non-Javadoc)
41     *
42     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
43     */
44    @Override
45    public void run(List<Object> parameters) {
46        String path;
47
48        try {
49            path = (String) parameters.get(0);
50        }
51        catch (Exception e) {
52            throw new IllegalArgumentException("illegal parameters provided: " + e);
53        }
54
55        File folder = new File(path);
56        if (!folder.isDirectory()) {
57            Console.printerrln(path + " is not a directory");
58            return;
59        }
60
61        correctDirectory(folder, folder);
62
63    }
64
65    /**
66     * <p>
67     * method for recursively checking each file in a directory structure. Each file is parsed
68     * and if a server could be determined in the file, the file is moved into a new directory
69     * structure which starts with the server and its port followed by the user session id.
70     * </p>
71     *
72     * @param file       the file to be handled. If it is a folder, the method calls itself for
73     *                   each of the children of the folder
74     * @param mainFolder the main folder into which the new directory structure is to be created
75     *                   (usually the same directory from which the recursion began)
76     */
77    private void correctDirectory(File file, File mainFolder) {
78        if (file.isDirectory()) {
79            String[] children = file.list();
80            Arrays.sort(children);
81           
82            for (String child : children) {
83                File childFile = new File(file, child);
84                correctDirectory(childFile, mainFolder);
85            }
86        }
87        else if (file.isFile()) {
88            String source = file.getAbsolutePath();
89            Console.traceln(Level.INFO, "Processing file: " + source);
90            String serverName = null;
91           
92            try {
93                HTMLLogParser parser = new HTMLLogParser();
94                parser.parseFile(file);
95               
96                for (IGUIElement server : parser.getGuiModel().getRootElements()) {
97                    if (server instanceof HTMLServer) {
98                        serverName = ((HTMLServer) server).getHost() + "_" +
99                            ((HTMLServer) server).getPort();
100                    }
101                }
102            }
103            catch (Exception e) {
104                Console.printerrln("Could not parse " + source + ": " + e.getMessage());
105            }
106           
107            if (serverName != null) {
108                File destination = new File(mainFolder, serverName);
109                destination = new File(destination, file.getParentFile().getName());
110               
111                if (!destination.exists()) {
112                    if (!destination.mkdirs()) {
113                        Console.printerrln("Could create directory " + destination);
114                        Console.printerrln("not moving " + file);
115                    }
116                }
117                else if (!destination.isDirectory()) {
118                    Console.printerrln
119                        ("Destination " + destination + " already exists but is not a directory");
120                    Console.printerrln("not moving " + file);
121                }
122               
123                destination = new File(destination, file.getName());
124               
125                if (!file.equals(destination)) {
126                    file.renameTo(destination);
127               
128                    Console.println("moved "  + file + " to " + destination);
129               
130                    if ((file.getParentFile().list() == null) ||
131                        (file.getParentFile().list().length == 0))
132                    {
133                        file.getParentFile().delete();
134                        Console.println("removed directory "  + file.getParentFile());
135                    }
136                }
137            }
138        }
139    }
140
141    /*
142     * (non-Javadoc)
143     *
144     * @see de.ugoe.cs.util.console.Command#help()
145     */
146    @Override
147    public String help() {
148        return "correctHTMLLogDirs <directory>";
149    }
150
151}
Note: See TracBrowser for help on using the repository browser.