Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.googlecode.fascinator.api.PluginManager Maven / Gradle / Ivy
Go to download
https://sites.google.com/site/fascinatorhome/home/documentation/technical/documents/plugin-api
/*
* The Fascinator - Plugin API
* Copyright (C) 2008-2009 University of Southern Queensland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.googlecode.fascinator.api;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.text.StrSubstitutor;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.googlecode.fascinator.api.access.AccessControl;
import com.googlecode.fascinator.api.access.AccessControlManager;
import com.googlecode.fascinator.api.authentication.Authentication;
import com.googlecode.fascinator.api.harvester.Harvester;
import com.googlecode.fascinator.api.indexer.Indexer;
import com.googlecode.fascinator.api.roles.Roles;
import com.googlecode.fascinator.api.storage.Storage;
import com.googlecode.fascinator.api.subscriber.Subscriber;
import com.googlecode.fascinator.api.transaction.TransactionException;
import com.googlecode.fascinator.api.transaction.TransactionManager;
import com.googlecode.fascinator.api.transformer.Transformer;
import com.googlecode.fascinator.api.transformer.TransformerException;
import groovy.lang.GroovyClassLoader;
/**
* Factory class to get plugin instances
*
* @author Oliver Lucido
*/
public class PluginManager {
/** System property key */
public static final String SYSTEM_KEY = "fascinator.home";
/** Default Fascinator home directory */
public static final String DEFAULT_PATH = StrSubstitutor
.replaceSystemProperties(FilenameUtils
.separatorsToSystem("${user.home}/.fascinator"));
public static Cache cache = CacheBuilder.newBuilder()
.concurrencyLevel(4).weakKeys().maximumSize(10000)
.expireAfterWrite(1, TimeUnit.DAYS).build();
/**
* Gets an access control plugin
*
* @param id plugin identifier
* @return an access control plugin, or null if not found
*/
public static AccessControl getAccessControl(String id) {
Class clazz = (Class) cache
.getIfPresent("accessControl" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader
.load(AccessControl.class);
for (AccessControl plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile(
"plugins/accessControl/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("accessControl" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of access control plugins
*
* @return map of access control plugin, or empty map if not found
*/
public static Map getAccessControlPlugins() {
Map access_plugins = new HashMap();
ServiceLoader plugins = ServiceLoader
.load(AccessControl.class);
for (AccessControl plugin : plugins) {
access_plugins.put(plugin.getId(), plugin);
}
return access_plugins;
}
/**
* Get the access manager. Used in The indexer if the portal isn't running
*
* @param id plugin identifier
* @return an access manager plugin, or null if not found
*/
public static AccessControlManager getAccessManager(String id) {
Class clazz = (Class) cache
.getIfPresent("accessManager" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader
.load(AccessControlManager.class);
for (AccessControlManager plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile(
"plugins/accessManager/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("accessManager" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Gets an authentication plugin
*
* @param id plugin identifier
* @return an authentication plugin, or null if not found
*/
public static Authentication getAuthentication(String id) {
Class clazz = (Class) cache
.getIfPresent("authentication" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader
.load(Authentication.class);
for (Authentication plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile(
"plugins/authentication/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("authentication" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of authentication plugins
*
* @return map of authentication plugins, or empty map if not found
*/
public static Map getAuthenticationPlugins() {
Map authenticators = new HashMap();
ServiceLoader plugins = ServiceLoader
.load(Authentication.class);
for (Authentication plugin : plugins) {
authenticators.put(plugin.getId(), plugin);
}
return authenticators;
}
/**
* Gets a harvester plugin
*
* @param id plugin identifier
* @param storage a storage instance
* @return a harvester plugin, or null if not found
*/
public static Harvester getHarvester(String id, Storage storage) {
Class clazz = (Class) cache
.getIfPresent("harvester" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader.load(Harvester.class);
for (Harvester plugin : plugins) {
if (id.equals(plugin.getId())) {
plugin.setStorage(storage);
return plugin;
}
}
File groovyFile = getPathFile("plugins/harvester/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("harvester" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of harvester plugins
*
* @return map of harvester plugins, or empty map if not found
*/
public static Map getHarvesterPlugins() {
Map harvesters = new HashMap();
ServiceLoader plugins = ServiceLoader.load(Harvester.class);
for (Harvester plugin : plugins) {
harvesters.put(plugin.getId(), plugin);
}
return harvesters;
}
/**
* Gets a indexer plugin
*
* @param id plugin identifier
* @return a indexer plugin, or null if not found
*/
public static Indexer getIndexer(String id) {
Class clazz = (Class) cache
.getIfPresent("indexer" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader.load(Indexer.class);
for (Indexer plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile("plugins/indexer/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("indexer" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of indexer plugins
*
* @return map of indexer plugins, or empty map if not found
*/
public static Map getIndexerPlugins() {
Map indexers = new HashMap();
ServiceLoader plugins = ServiceLoader.load(Indexer.class);
for (Indexer plugin : plugins) {
indexers.put(plugin.getId(), plugin);
}
return indexers;
}
/**
* Gets a roles plugin
*
* @param id plugin identifier
* @return a roles plugin, or null if not found
*/
public static Roles getRoles(String id) {
Class clazz = (Class) cache.getIfPresent("roles" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader.load(Roles.class);
for (Roles plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile("plugins/roles/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("roles" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of roles plugins
*
* @return map of roles plugins, or empty map if not found
*/
public static Map getRolesPlugins() {
Map roles = new HashMap();
ServiceLoader plugins = ServiceLoader.load(Roles.class);
for (Roles plugin : plugins) {
roles.put(plugin.getId(), plugin);
}
return roles;
}
/**
* Gets a storage plugin
*
* @param id plugin identifier
* @return a storage plugin, or null if not found
*/
public static Storage getStorage(String id) {
Class clazz = (Class) cache
.getIfPresent("storage" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader.load(Storage.class);
for (Storage plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile("plugins/storage/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("storage" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of storage plugins
*
* @return map of storage plugins, or empty map if not found
*/
public static Map getStoragePlugins() {
Map storageMap = new HashMap();
ServiceLoader plugins = ServiceLoader.load(Storage.class);
for (Storage plugin : plugins) {
storageMap.put(plugin.getId(), plugin);
}
return storageMap;
}
/**
* Gets a TransactionManager plugin
*
* @param id Plugin identifier
* @return TransactionManager Instantiated plugin, or null if not found
*/
public static TransactionManager getTransactionManager(String id)
throws TransactionException {
Class clazz = (Class) cache
.getIfPresent("transactionManager" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader
.load(TransactionManager.class);
for (TransactionManager plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile(
"plugins/transactionManager/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("transactionManager" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of TransactionManager plugins
*
* @return Map of TransactionManager plugins and their IDs, or empty map if
* not found
*/
public static Map getTransactionManagerPlugins() {
Map managers = new HashMap();
ServiceLoader plugins = ServiceLoader
.load(TransactionManager.class);
for (TransactionManager plugin : plugins) {
managers.put(plugin.getId(), plugin);
}
return managers;
}
/**
* Gets a transformer plugin
*
* @param id plugin identifier
* @return a transformer plugin, or null if not found
*/
public static Transformer getTransformer(String id)
throws TransformerException {
Class clazz = (Class) cache
.getIfPresent("transformer" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader
.load(Transformer.class);
for (Transformer plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile("plugins/transformer/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("transformer" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of transformer plugins
*
* @return map of transformer plugins, or empty map if not found
*/
public static Map getTransformerPlugins() {
Map transformers = new HashMap();
ServiceLoader plugins = ServiceLoader
.load(Transformer.class);
for (Transformer plugin : plugins) {
transformers.put(plugin.getId(), plugin);
}
return transformers;
}
/**
* Gets an subscriber plugin
*
* @param id plugin identifier
* @return an Subscriber plugin, or null if not found
*/
public static Subscriber getSubscriber(String id) {
Class clazz = (Class) cache
.getIfPresent("subscriber" + id);
if (clazz != null) {
try {
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
ServiceLoader plugins = ServiceLoader
.load(Subscriber.class);
for (Subscriber plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
File groovyFile = getPathFile("plugins/subscriber/" + id + ".groovy");
if (groovyFile.exists()) {
GroovyClassLoader gcl = new GroovyClassLoader();
try {
clazz = gcl.parseClass(groovyFile);
cache.put("subscriber" + id, clazz);
return clazz.newInstance();
} catch (Exception e) {
// Throwing RuntimeException to keep it unchecked
// TODO: Remove later
throw new RuntimeException(e);
}
}
return null;
}
/**
* Get a list of Subscriber plugins
*
* @return map of Subscriber plugin, or empty map if not found
*/
public static Map getSubscriberPlugins() {
Map access_plugins = new HashMap();
ServiceLoader plugins = ServiceLoader
.load(Subscriber.class);
for (Subscriber plugin : plugins) {
access_plugins.put(plugin.getId(), plugin);
}
return access_plugins;
}
private static File getPathFile(String subDir) {
String homePath = System.getenv("FASCINATOR_HOME");
if (homePath == null) {
homePath = DEFAULT_PATH;
}
String path = System.getProperty(SYSTEM_KEY, homePath);
String filePath = path + File.separator
+ FilenameUtils.separatorsToSystem(subDir);
return new File(filePath);
}
}