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.genericevents.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.plugin.genericevents.GenericEventLogSplitter;
|
---|
23 | import de.ugoe.cs.util.console.Command;
|
---|
24 | import de.ugoe.cs.util.console.Console;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * <p>
|
---|
28 | * TODO comment
|
---|
29 | * </p>
|
---|
30 | *
|
---|
31 | * @author Patrick Harms
|
---|
32 | * @version 1.0
|
---|
33 | */
|
---|
34 | public class CMDsplitDirGenericEvents implements Command {
|
---|
35 |
|
---|
36 | /*
|
---|
37 | * (non-Javadoc)
|
---|
38 | *
|
---|
39 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
40 | */
|
---|
41 | @Override
|
---|
42 | public void run(List<Object> parameters) {
|
---|
43 | String source = null;
|
---|
44 | String dest = null;
|
---|
45 | long timediff = 0;
|
---|
46 |
|
---|
47 | try {
|
---|
48 | for (int i = 0; i < parameters.size(); i++) {
|
---|
49 | String param = (String) parameters.get(i);
|
---|
50 | if (source == null) {
|
---|
51 | source = param;
|
---|
52 | }
|
---|
53 | else if (dest == null) {
|
---|
54 | dest = param;
|
---|
55 | }
|
---|
56 | else {
|
---|
57 | timediff = Long.parseLong(param);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | catch (Exception e) {
|
---|
62 | throw new IllegalArgumentException("illegal parameters provided: " + e);
|
---|
63 | }
|
---|
64 |
|
---|
65 | File sourceFolder = new File(source);
|
---|
66 | if (!sourceFolder.isDirectory()) {
|
---|
67 | Console.printerrln(sourceFolder + " is not a directory");
|
---|
68 | return;
|
---|
69 | }
|
---|
70 |
|
---|
71 | File destFolder = new File(dest);
|
---|
72 | if (destFolder.exists() && !destFolder.isDirectory()) {
|
---|
73 | Console.printerrln(destFolder + " is not a directory");
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (sourceFolder.equals(destFolder)) {
|
---|
78 | Console.printerrln("source and destination must not be the same");
|
---|
79 | return;
|
---|
80 | }
|
---|
81 |
|
---|
82 | splitFile(sourceFolder, destFolder, timediff);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * <p>
|
---|
87 | * recursive method for parsing a directory structures
|
---|
88 | * </p>
|
---|
89 | *
|
---|
90 | * @param file the file object to be parsed. If the file is a folder, the method calls itself
|
---|
91 | * for all children
|
---|
92 | * @param parser the parser to use for parsing the files.
|
---|
93 | */
|
---|
94 | private void splitFile(File source, File dest, long timediff) {
|
---|
95 | if (source.isDirectory()) {
|
---|
96 | String[] children = source.list();
|
---|
97 |
|
---|
98 | if (children != null) {
|
---|
99 | Arrays.sort(children);
|
---|
100 |
|
---|
101 | for (String child : children) {
|
---|
102 | File childFile = new File(source, child);
|
---|
103 | File childDestination = new File(dest, child);
|
---|
104 | splitFile(childFile, childDestination, timediff);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | else if (source.isFile()) {
|
---|
109 | Console.traceln(Level.INFO, "Processing file: " + source.getAbsolutePath());
|
---|
110 |
|
---|
111 | try {
|
---|
112 | new GenericEventLogSplitter().splitLogFile(source, dest.getParentFile(), timediff);
|
---|
113 | }
|
---|
114 | catch (Exception e) {
|
---|
115 | Console.printerrln("Could not split " + source.getAbsolutePath() + ": " + e.getMessage());
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * (non-Javadoc)
|
---|
122 | *
|
---|
123 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
124 | */
|
---|
125 | @Override
|
---|
126 | public String help() {
|
---|
127 | return "splitDirGenericEvents <directory>";
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|