App Engine Python SDK version 1.8.4
[gae.git] / java / src / main / com / google / apphosting / utils / config / PluginLoader.java
blob1b93e436c1a15ee5e95b7865e712123a907d2f99
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.apphosting.utils.config;
5 import java.io.File;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.net.URLClassLoader;
9 import java.util.ArrayList;
10 import java.util.ServiceLoader;
11 import java.util.logging.Logger;
13 /**
14 * Utility for loading plugins in AppConfig and DevAppserver.
17 public class PluginLoader {
19 /**
20 * Name of the system property used to specify the plugin search path.
22 public final static String PLUGIN_PATH = "com.google.appengine.plugin.path";
23 private final static Logger logger = Logger.getLogger(PluginLoader.class.getName());
25 private static ClassLoader loader = null;
27 /**
28 * Searches for plugins of the specified type.
30 public static <T> Iterable<T> loadPlugins(Class<T> pluginClass) {
31 return ServiceLoader.load(pluginClass, getPluginClassLoader());
34 private synchronized static ClassLoader getPluginClassLoader() {
35 if (loader == null) {
36 ClassLoader parent = PluginLoader.class.getClassLoader();
37 String path = System.getProperty(PLUGIN_PATH);
38 if (path == null) {
39 loader = parent;
40 } else {
41 String[] paths = path.split(File.pathSeparator);
42 ArrayList<URL> urls = new ArrayList<URL>(paths.length);
43 for (int i = 0; i < paths.length; ++i) {
44 try {
45 urls.add(new File(paths[i]).toURI().toURL());
46 } catch (MalformedURLException ex) {
47 logger.severe("Skipping invalid plugin path " + paths[i]);
50 loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
53 return loader;