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

Last change on this file since 1439 was 1339, checked in by pharms, 10 years ago
  • added support for parameterizing the parsing of GUI models to ignore ids or indexes
File size: 6.2 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.HashMap;
20import java.util.List;
21import java.util.logging.Level;
22
23import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
24import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
25import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer;
26import de.ugoe.cs.util.console.Command;
27import de.ugoe.cs.util.console.Console;
28
29/**
30 * <p>
31 * Command to restructure log files of the HTML monitor to be able to easily separate the logs
32 * of different web sites.
33 * </p>
34 *
35 * @author Patrick Harms
36 * @version 1.0
37 */
38public class CMDcorrectHTMLLogDirs implements Command {
39
40    /*
41     * (non-Javadoc)
42     *
43     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
44     */
45    @Override
46    public void run(List<Object> parameters) {
47        String path;
48
49        try {
50            path = (String) parameters.get(0);
51        }
52        catch (Exception e) {
53            throw new IllegalArgumentException("illegal parameters provided: " + e);
54        }
55
56        File folder = new File(path);
57        if (!folder.isDirectory()) {
58            Console.printerrln(path + " is not a directory");
59            return;
60        }
61
62        correctDirectory(folder, folder);
63
64    }
65
66    /**
67     * <p>
68     * method for recursively checking each file in a directory structure. Each file is parsed
69     * and if a server could be determined in the file, the file is moved into a new directory
70     * structure which starts with the server and its port followed by the user session id.
71     * </p>
72     *
73     * @param file       the file to be handled. If it is a folder, the method calls itself for
74     *                   each of the children of the folder
75     * @param mainFolder the main folder into which the new directory structure is to be created
76     *                   (usually the same directory from which the recursion began)
77     */
78    private void correctDirectory(File file, File mainFolder) {
79        if (file.isDirectory()) {
80            String[] children = file.list();
81            Arrays.sort(children);
82           
83            for (String child : children) {
84                File childFile = new File(file, child);
85                correctDirectory(childFile, mainFolder);
86            }
87        }
88        else if (file.isFile()) {
89            String source = file.getAbsolutePath();
90            Console.traceln(Level.INFO, "Processing file: " + source);
91            String serverName = null;
92           
93            HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>());
94            try {
95                parser.parseFile(file);
96            }
97            catch (Exception e) {
98                // ignore as long as we were able to determine a server
99            }
100           
101            for (IGUIElement server : parser.getGuiModel().getRootElements()) {
102                if (server instanceof HTMLServer) {
103                    serverName = ((HTMLServer) server).getHost() + "_" +
104                        ((HTMLServer) server).getPort();
105                }
106            }
107           
108            if (serverName != null) {
109                File destination = new File(mainFolder, serverName);
110                destination = new File(destination, file.getParentFile().getName());
111               
112                if (!destination.exists()) {
113                    if (!destination.mkdirs()) {
114                        Console.printerrln("Could not create directory " + destination);
115                        Console.printerrln("not moving " + file);
116                    }
117                }
118                else if (!destination.isDirectory()) {
119                    Console.printerrln
120                        ("Destination " + destination + " already exists but is not a directory");
121                    Console.printerrln("not moving " + file);
122                }
123               
124                destination = new File(destination, file.getName());
125               
126                // we seem to need both comparisons, as the first one does not always work
127                if (!file.equals(destination) &&
128                    !file.getAbsolutePath().equals(destination.getAbsolutePath()))
129                {
130                    if (file.renameTo(destination)) {
131                        Console.println("moved "  + file + " to " + destination);
132
133                        if ((file.getParentFile().list() == null) ||
134                            (file.getParentFile().list().length == 0))
135                        {
136                            if (file.getParentFile().delete()) {
137                                Console.println("removed directory "  + file.getParentFile());
138                            }
139                            else {
140                                Console.println
141                                    ("could not remove directory "  + file.getParentFile());                           
142                            }
143                        }
144                    }
145                    else {
146                        Console.println("could not move "  + file + " to " + destination);
147                    }
148                }
149            }
150            else {
151                Console.printerrln("Could not determine a server name in " + source);
152            }
153        }
154    }
155
156    /*
157     * (non-Javadoc)
158     *
159     * @see de.ugoe.cs.util.console.Command#help()
160     */
161    @Override
162    public String help() {
163        return "correctHTMLLogDirs <directory>";
164    }
165
166}
Note: See TracBrowser for help on using the repository browser.