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.
/**
* The Abiquo Platform
* Cloud management application for hybrid clouds
* Copyright (C) 2008 - Abiquo Holdings S.L.
*
* This application is free software; you can redistribute it and/or
* modify it under the terms of the GNU LESSER GENERAL PUBLIC
* LICENSE as published by the Free Software Foundation under
* version 3 of the License
*
* This software 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
* LESSER GENERAL PUBLIC LICENSE v.3 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.abiquo.commons.plugin;
import static com.google.common.base.Predicates.in;
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.unmodifiableMap;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.abiquo.commons.model.enumerator.ConstraintKey;
import com.abiquo.commons.plugin.annotation.PluginMetadata;
import com.abiquo.commons.plugin.exception.ComputeException;
import com.abiquo.commons.plugin.exception.HypervisorPluginError;
import com.abiquo.commons.plugin.exception.HypervisorPluginException;
import com.abiquo.commons.plugin.internal.ContextPropertiesInvocationHandler;
import com.abiquo.commons.plugin.internal.PluginInvocationHandler;
import com.abiquo.commons.plugin.internal.TryInvocationHandler;
import com.abiquo.commons.plugin.predicate.MethodNameEquivalence;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Equivalence.Wrapper;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.reflect.Reflection;
/**
* Hold {@link AbsPluginInterface} instances loaded using a {@link ServiceLoader}
*/
public class AbstractPluginManager
{
private final static Logger LOG = LoggerFactory.getLogger(AbstractPluginManager.class);
private final static TypeResolver typeResolver = new TypeResolver();
private final PluginAccessControl plugins;
private final Class absPluginInterfaceClass;
private final Class absIsPluginSupportedClass;
private final Class absContextPropertiesClass;
private Class< ? > genericPluginClass;
protected class LoadedAbsPlugin
{
private final Class connectionClass;
private final AbsPluginInterface proxy;
private final PluginMetadata metadata;
private final Map constraints;
private final AbsIsPluginSupported triable;
private final AbsContextProperties contextProperties;
private final Map>> operations;
LoadedAbsPlugin(final String type, final Class connectionClass,
final AbsPluginInterface computableObject)
{
this.connectionClass = connectionClass;
this.proxy =
Reflection.newProxy(absPluginInterfaceClass,
new PluginInvocationHandler(computableObject));
this.metadata = computableObject.getClass().getAnnotation(PluginMetadata.class);
this.triable =
Reflection.newProxy(absIsPluginSupportedClass,
new TryInvocationHandler(computableObject, type, genericPluginClass));
this.contextProperties =
Reflection.newProxy(absContextPropertiesClass,
new ContextPropertiesInvocationHandler(computableObject));
this.constraints = unmodifiableMap(loadConstraints(computableObject));
this.operations = getOperations(computableObject, type);
}
}
/**
* Stores the plugins and the white list.
*
* The plugin white list is mutable and controlled by the Abiquo licenses. Any access to get a
* plugin instance must be "approved" by the whitelist; otherwise the plugin is not returned.
*/
private class PluginAccessControl
{
private final Map instances;
private final Set whiteList;
public PluginAccessControl(final Map instances)
{
this.instances = Collections.unmodifiableMap(instances);
this.whiteList = Collections.synchronizedSet(new HashSet<>(instances.keySet()));
}
public Set loadedTypes()
{
synchronized (whiteList)
{
return Sets.intersection(whiteList, instances.keySet());
}
}
public boolean has(final String type)
{
return whiteList.contains(type) && instances.containsKey(type);
}
public Optional tryGet(final String type)
{
return has(type) ? Optional.of(instances.get(type)) : Optional
. absent();
}
public void setEnabledPlugins(final Collection types)
{
synchronized (whiteList)
{
whiteList.clear();
whiteList.addAll(types);
}
}
public void enableAllPlugins()
{
setEnabledPlugins(instances.keySet());
}
}
protected AbstractPluginManager(final Class< ? > genericPluginClass,
final Set otherPluginTypes,
final Class absPluginInterfaceClass,
final Class absIsPluginSupportedClass,
final Class absContextPropertiesClass)
{
this.genericPluginClass = genericPluginClass;
this.absPluginInterfaceClass = absPluginInterfaceClass;
this.absIsPluginSupportedClass = absIsPluginSupportedClass;
this.absContextPropertiesClass = absContextPropertiesClass;
Map loadedPlugins = Maps.newHashMap();
for (AbsPluginInterface pluggable : ServiceLoader.load(absPluginInterfaceClass))
{
Optional