commit | author | age
|
5e3521
|
1 |
/* |
JM |
2 |
* Copyright 2014 gitblit.com. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package com.gitblit; |
|
17 |
|
|
18 |
import java.io.File; |
|
19 |
import java.text.MessageFormat; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.List; |
|
22 |
|
50380b
|
23 |
import org.kohsuke.args4j.CmdLineException; |
JM |
24 |
import org.kohsuke.args4j.CmdLineParser; |
|
25 |
import org.kohsuke.args4j.Option; |
|
26 |
|
5e3521
|
27 |
import com.gitblit.manager.IRepositoryManager; |
JM |
28 |
import com.gitblit.manager.IRuntimeManager; |
|
29 |
import com.gitblit.manager.RepositoryManager; |
|
30 |
import com.gitblit.manager.RuntimeManager; |
|
31 |
import com.gitblit.tickets.BranchTicketService; |
|
32 |
import com.gitblit.tickets.FileTicketService; |
|
33 |
import com.gitblit.tickets.ITicketService; |
|
34 |
import com.gitblit.tickets.RedisTicketService; |
|
35 |
import com.gitblit.utils.StringUtils; |
fc3a39
|
36 |
import com.gitblit.utils.XssFilter; |
JM |
37 |
import com.gitblit.utils.XssFilter.AllowXssFilter; |
5e3521
|
38 |
|
JM |
39 |
/** |
|
40 |
* A command-line tool to reindex all tickets in all repositories when the |
|
41 |
* indexes needs to be rebuilt. |
|
42 |
* |
|
43 |
* @author James Moger |
|
44 |
* |
|
45 |
*/ |
|
46 |
public class ReindexTickets { |
|
47 |
|
|
48 |
public static void main(String... args) { |
|
49 |
ReindexTickets reindex = new ReindexTickets(); |
|
50 |
|
|
51 |
// filter out the baseFolder parameter |
|
52 |
List<String> filtered = new ArrayList<String>(); |
|
53 |
String folder = "data"; |
|
54 |
for (int i = 0; i < args.length; i++) { |
|
55 |
String arg = args[i]; |
|
56 |
if (arg.equals("--baseFolder")) { |
|
57 |
if (i + 1 == args.length) { |
|
58 |
System.out.println("Invalid --baseFolder parameter!"); |
|
59 |
System.exit(-1); |
|
60 |
} else if (!".".equals(args[i + 1])) { |
|
61 |
folder = args[i + 1]; |
|
62 |
} |
|
63 |
i = i + 1; |
|
64 |
} else { |
|
65 |
filtered.add(arg); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
Params.baseFolder = folder; |
|
70 |
Params params = new Params(); |
50380b
|
71 |
CmdLineParser parser = new CmdLineParser(params); |
5e3521
|
72 |
try { |
50380b
|
73 |
parser.parseArgument(filtered); |
5e3521
|
74 |
if (params.help) { |
50380b
|
75 |
reindex.usage(parser, null); |
5e3521
|
76 |
return; |
JM |
77 |
} |
50380b
|
78 |
} catch (CmdLineException t) { |
JM |
79 |
reindex.usage(parser, t); |
5e3521
|
80 |
return; |
JM |
81 |
} |
|
82 |
|
|
83 |
// load the settings |
|
84 |
FileSettings settings = params.FILESETTINGS; |
|
85 |
if (!StringUtils.isEmpty(params.settingsfile)) { |
|
86 |
if (new File(params.settingsfile).exists()) { |
|
87 |
settings = new FileSettings(params.settingsfile); |
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
// reindex tickets |
|
92 |
reindex.reindex(new File(Params.baseFolder), settings); |
|
93 |
System.exit(0); |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* Display the command line usage of ReindexTickets. |
|
98 |
* |
50380b
|
99 |
* @param parser |
5e3521
|
100 |
* @param t |
JM |
101 |
*/ |
50380b
|
102 |
protected final void usage(CmdLineParser parser, CmdLineException t) { |
5e3521
|
103 |
System.out.println(Constants.BORDER); |
JM |
104 |
System.out.println(Constants.getGitBlitVersion()); |
|
105 |
System.out.println(Constants.BORDER); |
|
106 |
System.out.println(); |
|
107 |
if (t != null) { |
|
108 |
System.out.println(t.getMessage()); |
|
109 |
System.out.println(); |
|
110 |
} |
50380b
|
111 |
if (parser != null) { |
JM |
112 |
parser.printUsage(System.out); |
5e3521
|
113 |
System.out |
JM |
114 |
.println("\nExample:\n java -gitblit.jar com.gitblit.ReindexTickets --baseFolder c:\\gitblit-data"); |
|
115 |
} |
|
116 |
System.exit(0); |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* Reindex all tickets |
|
121 |
* |
|
122 |
* @param settings |
|
123 |
*/ |
|
124 |
protected void reindex(File baseFolder, IStoredSettings settings) { |
|
125 |
// disable some services |
|
126 |
settings.overrideSetting(Keys.web.allowLuceneIndexing, false); |
|
127 |
settings.overrideSetting(Keys.git.enableGarbageCollection, false); |
|
128 |
settings.overrideSetting(Keys.git.enableMirroring, false); |
|
129 |
settings.overrideSetting(Keys.web.activityCacheDays, 0); |
|
130 |
|
fc3a39
|
131 |
XssFilter xssFilter = new AllowXssFilter(); |
JM |
132 |
IRuntimeManager runtimeManager = new RuntimeManager(settings, xssFilter, baseFolder).start(); |
eecaad
|
133 |
IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, null, null, null).start(); |
5e3521
|
134 |
|
JM |
135 |
String serviceName = settings.getString(Keys.tickets.service, BranchTicketService.class.getSimpleName()); |
|
136 |
if (StringUtils.isEmpty(serviceName)) { |
|
137 |
System.err.println(MessageFormat.format("Please define a ticket service in \"{0}\"", Keys.tickets.service)); |
|
138 |
System.exit(1); |
|
139 |
} |
|
140 |
ITicketService ticketService = null; |
|
141 |
try { |
|
142 |
Class<?> serviceClass = Class.forName(serviceName); |
|
143 |
if (RedisTicketService.class.isAssignableFrom(serviceClass)) { |
|
144 |
// Redis ticket service |
ba5670
|
145 |
ticketService = new RedisTicketService(runtimeManager, null, null, null, repositoryManager).start(); |
5e3521
|
146 |
} else if (BranchTicketService.class.isAssignableFrom(serviceClass)) { |
JM |
147 |
// Branch ticket service |
ba5670
|
148 |
ticketService = new BranchTicketService(runtimeManager, null, null, null, repositoryManager).start(); |
5e3521
|
149 |
} else if (FileTicketService.class.isAssignableFrom(serviceClass)) { |
JM |
150 |
// File ticket service |
ba5670
|
151 |
ticketService = new FileTicketService(runtimeManager, null, null, null, repositoryManager).start(); |
5e3521
|
152 |
} else { |
JM |
153 |
System.err.println("Unknown ticket service " + serviceName); |
|
154 |
System.exit(1); |
|
155 |
} |
|
156 |
} catch (Exception e) { |
|
157 |
e.printStackTrace(); |
|
158 |
System.exit(1); |
|
159 |
} |
|
160 |
|
|
161 |
ticketService.reindex(); |
|
162 |
ticketService.stop(); |
|
163 |
repositoryManager.stop(); |
|
164 |
runtimeManager.stop(); |
|
165 |
} |
|
166 |
|
|
167 |
/** |
50380b
|
168 |
* Parameters. |
5e3521
|
169 |
*/ |
JM |
170 |
public static class Params { |
|
171 |
|
|
172 |
public static String baseFolder; |
|
173 |
|
50380b
|
174 |
@Option(name = "--help", aliases = { "-h"}, usage = "Show this help") |
5e3521
|
175 |
public Boolean help = false; |
JM |
176 |
|
|
177 |
private final FileSettings FILESETTINGS = new FileSettings(new File(baseFolder, Constants.PROPERTIES_FILE).getAbsolutePath()); |
|
178 |
|
50380b
|
179 |
@Option(name = "--repositoriesFolder", usage = "Git Repositories Folder", metaVar = "PATH") |
5e3521
|
180 |
public String repositoriesFolder = FILESETTINGS.getString(Keys.git.repositoriesFolder, "git"); |
JM |
181 |
|
50380b
|
182 |
@Option(name = "--settings", usage = "Path to alternative settings", metaVar = "FILE") |
5e3521
|
183 |
public String settingsfile; |
JM |
184 |
} |
|
185 |
} |