[1221] | 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 |
|
---|
| 15 | package de.ugoe.cs.autoquest.plugin.html.commands;
|
---|
| 16 |
|
---|
| 17 | import java.io.File;
|
---|
| 18 | import java.util.Arrays;
|
---|
| 19 | import java.util.List;
|
---|
| 20 | import java.util.logging.Level;
|
---|
| 21 |
|
---|
| 22 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
|
---|
| 23 | import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
|
---|
| 24 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer;
|
---|
| 25 | import de.ugoe.cs.util.console.Command;
|
---|
| 26 | import 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 | */
|
---|
| 37 | public 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 |
|
---|
[2232] | 81 | if (children != null) {
|
---|
| 82 | Arrays.sort(children);
|
---|
| 83 |
|
---|
| 84 | for (String child : children) {
|
---|
| 85 | File childFile = new File(file, child);
|
---|
| 86 | correctDirectory(childFile, mainFolder);
|
---|
| 87 | }
|
---|
[1221] | 88 | }
|
---|
| 89 | }
|
---|
| 90 | else if (file.isFile()) {
|
---|
| 91 | String source = file.getAbsolutePath();
|
---|
| 92 | Console.traceln(Level.INFO, "Processing file: " + source);
|
---|
| 93 | String serverName = null;
|
---|
| 94 |
|
---|
[1496] | 95 | HTMLLogParser parser = new HTMLLogParser(null);
|
---|
[1221] | 96 | try {
|
---|
| 97 | parser.parseFile(file);
|
---|
| 98 | }
|
---|
| 99 | catch (Exception e) {
|
---|
[1235] | 100 | // ignore as long as we were able to determine a server
|
---|
[1221] | 101 | }
|
---|
| 102 |
|
---|
[1235] | 103 | for (IGUIElement server : parser.getGuiModel().getRootElements()) {
|
---|
| 104 | if (server instanceof HTMLServer) {
|
---|
| 105 | serverName = ((HTMLServer) server).getHost() + "_" +
|
---|
| 106 | ((HTMLServer) server).getPort();
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[1221] | 110 | if (serverName != null) {
|
---|
| 111 | File destination = new File(mainFolder, serverName);
|
---|
| 112 | destination = new File(destination, file.getParentFile().getName());
|
---|
| 113 |
|
---|
| 114 | if (!destination.exists()) {
|
---|
| 115 | if (!destination.mkdirs()) {
|
---|
[1231] | 116 | Console.printerrln("Could not create directory " + destination);
|
---|
[1221] | 117 | Console.printerrln("not moving " + file);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | else if (!destination.isDirectory()) {
|
---|
| 121 | Console.printerrln
|
---|
| 122 | ("Destination " + destination + " already exists but is not a directory");
|
---|
| 123 | Console.printerrln("not moving " + file);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | destination = new File(destination, file.getName());
|
---|
| 127 |
|
---|
[1231] | 128 | // we seem to need both comparisons, as the first one does not always work
|
---|
| 129 | if (!file.equals(destination) &&
|
---|
| 130 | !file.getAbsolutePath().equals(destination.getAbsolutePath()))
|
---|
| 131 | {
|
---|
[1272] | 132 | if (file.renameTo(destination)) {
|
---|
| 133 | Console.println("moved " + file + " to " + destination);
|
---|
| 134 |
|
---|
[2233] | 135 | String[] children = file.getParentFile().list();
|
---|
| 136 | if ((children == null) || (children.length == 0)) {
|
---|
[1272] | 137 | if (file.getParentFile().delete()) {
|
---|
| 138 | Console.println("removed directory " + file.getParentFile());
|
---|
| 139 | }
|
---|
| 140 | else {
|
---|
| 141 | Console.println
|
---|
| 142 | ("could not remove directory " + file.getParentFile());
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
[1221] | 145 | }
|
---|
[1272] | 146 | else {
|
---|
| 147 | Console.println("could not move " + file + " to " + destination);
|
---|
| 148 | }
|
---|
[1221] | 149 | }
|
---|
| 150 | }
|
---|
[1235] | 151 | else {
|
---|
| 152 | Console.printerrln("Could not determine a server name in " + source);
|
---|
| 153 | }
|
---|
[1221] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /*
|
---|
| 158 | * (non-Javadoc)
|
---|
| 159 | *
|
---|
| 160 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 161 | */
|
---|
| 162 | @Override
|
---|
| 163 | public String help() {
|
---|
| 164 | return "correctHTMLLogDirs <directory>";
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | }
|
---|