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

Last change on this file since 2146 was 1496, checked in by pharms, 10 years ago
  • adapted parsing of HTML files to have more power in specifying replacements
File size: 6.1 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            HTMLLogParser parser = new HTMLLogParser(null);
93            try {
94                parser.parseFile(file);
95            }
96            catch (Exception e) {
97                // ignore as long as we were able to determine a server
98            }
99           
100            for (IGUIElement server : parser.getGuiModel().getRootElements()) {
101                if (server instanceof HTMLServer) {
102                    serverName = ((HTMLServer) server).getHost() + "_" +
103                        ((HTMLServer) server).getPort();
104                }
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 not 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                // we seem to need both comparisons, as the first one does not always work
126                if (!file.equals(destination) &&
127                    !file.getAbsolutePath().equals(destination.getAbsolutePath()))
128                {
129                    if (file.renameTo(destination)) {
130                        Console.println("moved "  + file + " to " + destination);
131
132                        if ((file.getParentFile().list() == null) ||
133                            (file.getParentFile().list().length == 0))
134                        {
135                            if (file.getParentFile().delete()) {
136                                Console.println("removed directory "  + file.getParentFile());
137                            }
138                            else {
139                                Console.println
140                                    ("could not remove directory "  + file.getParentFile());                           
141                            }
142                        }
143                    }
144                    else {
145                        Console.println("could not move "  + file + " to " + destination);
146                    }
147                }
148            }
149            else {
150                Console.printerrln("Could not determine a server name in " + source);
151            }
152        }
153    }
154
155    /*
156     * (non-Javadoc)
157     *
158     * @see de.ugoe.cs.util.console.Command#help()
159     */
160    @Override
161    public String help() {
162        return "correctHTMLLogDirs <directory>";
163    }
164
165}
Note: See TracBrowser for help on using the repository browser.