1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.apphosting
.utils
.config
;
6 import java
.net
.MalformedURLException
;
8 import java
.net
.URLClassLoader
;
9 import java
.util
.ArrayList
;
10 import java
.util
.ServiceLoader
;
11 import java
.util
.logging
.Logger
;
14 * Utility for loading plugins in AppConfig and DevAppserver.
17 public class PluginLoader
{
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;
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() {
36 ClassLoader parent
= PluginLoader
.class.getClassLoader();
37 String path
= System
.getProperty(PLUGIN_PATH
);
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
) {
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
);