Skip to content

Commit 019c844

Browse files
committed
Plugins: plugins should now be in extracted format under ES_HOME/plugins, closes elastic#438.
1 parent 0a3d187 commit 019c844

File tree

2 files changed

+55
-115
lines changed

2 files changed

+55
-115
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/plugins/PluginManager.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
import org.elasticsearch.common.collect.Tuple;
55
import org.elasticsearch.common.http.client.HttpDownloadHelper;
66
import org.elasticsearch.common.io.FileSystemUtils;
7+
import org.elasticsearch.common.io.Streams;
78
import org.elasticsearch.common.settings.Settings;
89
import org.elasticsearch.env.Environment;
910
import org.elasticsearch.node.internal.InternalSettingsPerparer;
1011

1112
import java.io.File;
12-
import java.io.FileNotFoundException;
13+
import java.io.FileOutputStream;
1314
import java.io.IOException;
1415
import java.net.URL;
16+
import java.util.Enumeration;
17+
import java.util.zip.ZipEntry;
18+
import java.util.zip.ZipFile;
1519

1620
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.*;
1721

@@ -33,16 +37,48 @@ public void downloadPlugin(String name) throws IOException {
3337
HttpDownloadHelper downloadHelper = new HttpDownloadHelper();
3438

3539
URL pluginUrl = new URL(url + "/" + name + "/elasticsearch-" + name + "-" + Version.number() + ".zip");
36-
downloadHelper.download(pluginUrl, new File(environment.pluginsFile(), name + ".zip"), new HttpDownloadHelper.VerboseProgress(System.out));
40+
File pluginFile = new File(environment.pluginsFile(), name + ".zip");
41+
downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
42+
43+
// extract the plugin
44+
File extractLocation = new File(environment.pluginsFile(), name);
45+
ZipFile zipFile = null;
46+
try {
47+
zipFile = new ZipFile(pluginFile);
48+
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
49+
while (zipEntries.hasMoreElements()) {
50+
ZipEntry zipEntry = zipEntries.nextElement();
51+
if (!(zipEntry.getName().endsWith(".jar") || zipEntry.getName().endsWith(".zip"))) {
52+
continue;
53+
}
54+
String zipName = zipEntry.getName().replace('\\', '/');
55+
File target = new File(extractLocation, zipName);
56+
target.getParentFile().mkdirs();
57+
Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target));
58+
}
59+
} catch (Exception e) {
60+
System.err.println("failed to extract plugin [" + pluginFile + "]");
61+
} finally {
62+
if (zipFile != null) {
63+
try {
64+
zipFile.close();
65+
} catch (IOException e) {
66+
// ignore
67+
}
68+
}
69+
pluginFile.delete();
70+
}
3771
}
3872

3973
public void removePlugin(String name) throws IOException {
40-
File pluginToDelete = new File(environment.pluginsFile(), name + ".zip");
41-
if (!pluginToDelete.exists()) {
42-
throw new FileNotFoundException("Plugin [" + name + "] does not exists");
74+
File pluginToDelete = new File(environment.pluginsFile(), name);
75+
if (pluginToDelete.exists()) {
76+
FileSystemUtils.deleteRecursively(pluginToDelete, true);
77+
}
78+
pluginToDelete = new File(environment.pluginsFile(), name + ".zip");
79+
if (pluginToDelete.exists()) {
80+
pluginToDelete.delete();
4381
}
44-
pluginToDelete.delete();
45-
FileSystemUtils.deleteRecursively(new File(new File(environment.workFile(), "plugins"), name), true);
4682
}
4783

4884
public static void main(String[] args) {

modules/elasticsearch/src/main/java/org/elasticsearch/plugins/PluginsService.java

Lines changed: 12 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@
2727
import org.elasticsearch.common.component.LifecycleComponent;
2828
import org.elasticsearch.common.inject.Inject;
2929
import org.elasticsearch.common.inject.Module;
30-
import org.elasticsearch.common.io.Streams;
3130
import org.elasticsearch.common.settings.Settings;
3231
import org.elasticsearch.env.Environment;
3332
import org.elasticsearch.index.CloseableIndexComponent;
3433

35-
import java.io.*;
34+
import java.io.File;
35+
import java.io.IOException;
36+
import java.io.InputStream;
3637
import java.lang.reflect.Method;
3738
import java.net.URL;
3839
import java.util.*;
39-
import java.util.zip.ZipEntry;
40-
import java.util.zip.ZipFile;
4140

4241
import static org.elasticsearch.common.collect.Maps.*;
43-
import static org.elasticsearch.common.io.FileSystemUtils.*;
4442

4543
/**
4644
* @author kimchy (shay.banon)
@@ -163,115 +161,21 @@ private void loadPluginsIntoClassLoader() {
163161

164162
File[] pluginsFiles = pluginsFile.listFiles();
165163
for (File pluginFile : pluginsFiles) {
166-
if (!pluginFile.getName().endsWith(".zip")) {
167-
if (pluginFile.isDirectory()) {
168-
logger.trace("--- adding expanded plugin [" + pluginFile.getAbsolutePath() + "]");
169-
try {
170-
// add the root
171-
addURL.invoke(classLoader, pluginFile.toURI().toURL());
172-
// if there are jars in it, add it as well
173-
for (File jarToAdd : pluginFile.listFiles()) {
174-
if (!(jarToAdd.getName().endsWith(".jar") || jarToAdd.getName().endsWith(".zip"))) {
175-
continue;
176-
}
177-
addURL.invoke(classLoader, jarToAdd.toURI().toURL());
178-
}
179-
} catch (Exception e) {
180-
logger.warn("failed to add plugin [" + pluginFile + "]", e);
181-
}
182-
}
183-
184-
continue;
185-
}
186-
if (logger.isTraceEnabled()) {
187-
logger.trace("processing [{}]", pluginFile);
188-
}
189-
190-
String pluginNameNoExtension = pluginFile.getName().substring(0, pluginFile.getName().lastIndexOf('.'));
191-
File extractedPluginDir = new File(new File(environment.workFile(), "plugins"), pluginNameNoExtension);
192-
extractedPluginDir.mkdirs();
193-
194-
File stampsDir = new File(new File(environment.workFile(), "plugins"), "_stamps");
195-
stampsDir.mkdirs();
196-
197-
boolean extractPlugin = true;
198-
File stampFile = new File(stampsDir, pluginNameNoExtension + ".stamp");
199-
if (stampFile.exists()) {
200-
// read it, and check if its the same size as the pluginFile
201-
RandomAccessFile raf = null;
202-
try {
203-
raf = new RandomAccessFile(stampFile, "r");
204-
long size = raf.readLong();
205-
if (size == pluginFile.length()) {
206-
extractPlugin = false;
207-
if (logger.isTraceEnabled()) {
208-
logger.trace("--- no need to extract plugin, same size [" + size + "]");
209-
}
210-
}
211-
} catch (Exception e) {
212-
// ignore and extract the plugin
213-
} finally {
214-
if (raf != null) {
215-
try {
216-
raf.close();
217-
} catch (IOException e) {
218-
// ignore
219-
}
220-
}
221-
}
222-
}
223-
224-
if (extractPlugin) {
225-
if (logger.isTraceEnabled()) {
226-
logger.trace("--- extracting plugin to [" + extractedPluginDir + "]");
227-
}
228-
deleteRecursively(extractedPluginDir, false);
229-
230-
ZipFile zipFile = null;
164+
if (pluginFile.isDirectory()) {
165+
logger.trace("--- adding plugin [" + pluginFile.getAbsolutePath() + "]");
231166
try {
232-
zipFile = new ZipFile(pluginFile);
233-
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
234-
while (zipEntries.hasMoreElements()) {
235-
ZipEntry zipEntry = zipEntries.nextElement();
236-
if (!(zipEntry.getName().endsWith(".jar") || zipEntry.getName().endsWith(".zip"))) {
167+
// add the root
168+
addURL.invoke(classLoader, pluginFile.toURI().toURL());
169+
// if there are jars in it, add it as well
170+
for (File jarToAdd : pluginFile.listFiles()) {
171+
if (!(jarToAdd.getName().endsWith(".jar") || jarToAdd.getName().endsWith(".zip"))) {
237172
continue;
238173
}
239-
String name = zipEntry.getName().replace('\\', '/');
240-
File target = new File(extractedPluginDir, name);
241-
Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target));
174+
addURL.invoke(classLoader, jarToAdd.toURI().toURL());
242175
}
243176
} catch (Exception e) {
244-
logger.warn("failed to extract plugin [" + pluginFile + "], ignoring...", e);
245-
continue;
246-
} finally {
247-
if (zipFile != null) {
248-
try {
249-
zipFile.close();
250-
} catch (IOException e) {
251-
// ignore
252-
}
253-
}
177+
logger.warn("failed to add plugin [" + pluginFile + "]", e);
254178
}
255-
256-
try {
257-
RandomAccessFile raf = new RandomAccessFile(stampFile, "rw");
258-
raf.writeLong(pluginFile.length());
259-
raf.close();
260-
} catch (Exception e) {
261-
// ignore
262-
}
263-
264-
}
265-
266-
try {
267-
for (File jarToAdd : extractedPluginDir.listFiles()) {
268-
if (!(jarToAdd.getName().endsWith(".jar") || jarToAdd.getName().endsWith(".zip"))) {
269-
continue;
270-
}
271-
addURL.invoke(classLoader, jarToAdd.toURI().toURL());
272-
}
273-
} catch (Exception e) {
274-
logger.warn("failed to add plugin [" + pluginFile + "]", e);
275179
}
276180
}
277181
}

0 commit comments

Comments
 (0)