From 176b801295fbe616689390f8ff4c11b18d1884fc Mon Sep 17 00:00:00 2001 From: Dariusz Luksza Date: Tue, 25 Mar 2014 03:15:44 +0100 Subject: [PATCH] Add SecureStore implementation Right now it is not used, only just hook into build system Change-Id: Ibbb15ad2aad3e77aa64f738bc3bc0a1177e61a26 Signed-off-by: Dariusz Luksza scan(File file, String pluginName, + Class annotation) throws InvalidPluginException, + IOException { + Map, Iterable> result = + scan(new JarFile(file), pluginName, + Arrays.> asList(annotation)); + return result.get(annotation); + } + public static Map, Iterable> scan( JarFile jarFile, String pluginName, Iterable> annotations) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java index 9a1ca5e308..b4d8df1a25 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java @@ -77,6 +77,10 @@ public class PluginLoader implements LifecycleListener { static final String PLUGIN_TMP_PREFIX = "plugin_"; static final Logger log = LoggerFactory.getLogger(PluginLoader.class); + public static String getPluginName(File srcFile) throws IOException { + return Objects.firstNonNull(getGerritPluginName(srcFile), nameOf(srcFile)).toLowerCase(); + } + private final File pluginsDir; private final File dataDir; private final File tmpDir; @@ -661,7 +665,7 @@ public class PluginLoader implements LifecycleListener { // If multiple plugin files provide the same plugin name, then only // the first plugin remains active and all other plugins with the same // name are disabled. - private static Multimap prunePlugins(File pluginsDir) { + public static Multimap prunePlugins(File pluginsDir) { List jars = scanJarsInPluginsDirectory(pluginsDir); Multimap map; try { @@ -757,8 +761,7 @@ public class PluginLoader implements LifecycleListener { throws IOException { Multimap map = LinkedHashMultimap.create(); for (File srcFile : plugins) { - map.put(Objects.firstNonNull(getGerritPluginName(srcFile), - nameOf(srcFile)), srcFile); + map.put(getPluginName(srcFile), srcFile); } return map; } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStore.java b/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStore.java new file mode 100644 index 0000000000..3fe00f4b55 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStore.java @@ -0,0 +1,27 @@ +// Copyright (C) 2013 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.securestore; + +import com.google.gerrit.extensions.annotations.ExtensionPoint; + +@ExtensionPoint +public interface SecureStore { + + String get(String section, String subsection, String name); + + void set(String section, String subsection, String name, String value); + + void unset(String section, String subsection, String name); +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStoreData.java b/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStoreData.java new file mode 100644 index 0000000000..b92510547f --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStoreData.java @@ -0,0 +1,77 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.securestore; + +import com.google.common.base.Objects; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; + +public class SecureStoreData { + public final File pluginFile; + public final String storeName; + public final String className; + + public SecureStoreData(String pluginName, String className, File jarFile, + String storeName) { + this.className = className; + this.pluginFile = jarFile; + this.storeName = String.format("%s/%s", pluginName, storeName); + } + + public String getStoreName() { + return storeName; + } + + public Class load() { + return load(pluginFile); + } + + @SuppressWarnings("unchecked") + public Class load(File pluginFile) { + try { + URL[] pluginJarUrls = new URL[] {pluginFile.toURI().toURL()}; + ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); + final URLClassLoader newClassLoader = + new URLClassLoader(pluginJarUrls, currentCL); + Thread.currentThread().setContextClassLoader(newClassLoader); + return (Class) newClassLoader.loadClass(className); + } catch (Exception e) { + throw new SecureStoreException(String.format( + "Cannot load secure store implementation for %s", storeName), e); + } + } + + @Override + public String toString() { + return Objects.toStringHelper(this).add("storeName", storeName) + .add("className", className).add("file", pluginFile).toString(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof SecureStoreData) { + SecureStoreData o = (SecureStoreData) obj; + return storeName.equals(o.storeName); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(storeName); + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStoreException.java b/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStoreException.java new file mode 100644 index 0000000000..01450f8809 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/securestore/SecureStoreException.java @@ -0,0 +1,27 @@ +// Copyright (C) 2013 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.securestore; + +public class SecureStoreException extends RuntimeException { + private static final long serialVersionUID = 5581700510568485065L; + + SecureStoreException(String msg) { + super(msg); + } + + SecureStoreException(String msg, Exception e) { + super(msg, e); + } +}