Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java

461 lines
10 KiB
Java
Raw Normal View History

2010-10-31 19:22:53 +01:00
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
2010-10-12 09:16:40 +02:00
*/
2010-12-01 14:26:29 +01:00
package sonia.scm.plugin;
2010-10-12 09:16:40 +02:00
//~--- non-JDK imports --------------------------------------------------------
2010-12-13 18:59:00 +01:00
import com.google.inject.Inject;
2010-12-16 07:46:02 +01:00
import com.google.inject.Provider;
2010-12-13 18:59:00 +01:00
import com.google.inject.Singleton;
2010-10-16 11:03:54 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2010-12-13 18:59:00 +01:00
import sonia.scm.ConfigurationException;
import sonia.scm.SCMContext;
2010-12-13 18:59:00 +01:00
import sonia.scm.cache.CacheManager;
import sonia.scm.cache.SimpleCache;
import sonia.scm.config.ScmConfiguration;
2010-12-16 07:46:02 +01:00
import sonia.scm.security.SecurityContext;
2010-12-18 13:37:34 +01:00
import sonia.scm.util.AssertUtil;
2010-12-16 07:46:02 +01:00
import sonia.scm.util.SecurityUtil;
2010-12-01 14:26:29 +01:00
2010-10-12 09:16:40 +02:00
//~--- JDK imports ------------------------------------------------------------
import java.net.URL;
2010-12-01 14:26:29 +01:00
import java.util.Collection;
2010-12-13 18:59:00 +01:00
import java.util.HashMap;
import java.util.HashSet;
2010-12-13 18:59:00 +01:00
import java.util.Map;
import java.util.Set;
2010-10-12 09:16:40 +02:00
2010-12-13 18:59:00 +01:00
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
2010-10-12 09:16:40 +02:00
/**
*
* @author Sebastian Sdorra
*/
2010-12-13 18:59:00 +01:00
@Singleton
2010-12-01 14:26:29 +01:00
public class DefaultPluginManager implements PluginManager
2010-10-12 09:16:40 +02:00
{
/** Field description */
2010-12-13 18:59:00 +01:00
public static final String CACHE_NAME = "sonia.cache.plugins";
2010-12-01 14:26:29 +01:00
/** the logger for DefaultPluginManager */
2010-10-12 09:16:40 +02:00
private static final Logger logger =
2010-12-01 14:26:29 +01:00
LoggerFactory.getLogger(DefaultPluginManager.class);
2010-10-12 09:16:40 +02:00
2010-12-18 13:37:34 +01:00
/** Field description */
public static final PluginFilter FILTER_UPDATES =
new StatePluginFilter(PluginState.UPDATE_AVAILABLE);
2010-12-01 14:26:29 +01:00
//~--- constructors ---------------------------------------------------------
2010-10-12 09:16:40 +02:00
/**
2010-12-01 14:26:29 +01:00
* Constructs ...
2010-10-12 09:16:40 +02:00
*
2010-12-13 18:59:00 +01:00
*
*
2010-12-16 07:46:02 +01:00
*
* @param securityContextProvicer
2010-12-13 18:59:00 +01:00
* @param configuration
* @param pluginLoader
* @param cacheManager
2010-10-12 09:16:40 +02:00
*/
2010-12-13 18:59:00 +01:00
@Inject
2010-12-16 07:46:02 +01:00
public DefaultPluginManager(
Provider<SecurityContext> securityContextProvicer,
ScmConfiguration configuration, PluginLoader pluginLoader,
CacheManager cacheManager)
2010-10-12 09:16:40 +02:00
{
2010-12-16 07:46:02 +01:00
this.securityContextProvicer = securityContextProvicer;
2010-12-13 18:59:00 +01:00
this.configuration = configuration;
this.cache = cacheManager.getSimpleCache(String.class, PluginCenter.class,
CACHE_NAME);
installedPlugins = new HashMap<String, PluginInformation>();
for (Plugin plugin : pluginLoader.getInstalledPlugins())
{
PluginInformation info = plugin.getInformation();
2010-12-16 07:46:02 +01:00
if ((info != null) && info.isValid())
2010-12-13 18:59:00 +01:00
{
installedPlugins.put(info.getId(), plugin.getInformation());
2010-12-13 18:59:00 +01:00
}
}
2010-10-12 09:16:40 +02:00
2010-12-01 14:26:29 +01:00
try
2010-10-12 09:16:40 +02:00
{
2010-12-13 18:59:00 +01:00
unmarshaller =
JAXBContext.newInstance(PluginCenter.class).createUnmarshaller();
2010-12-01 14:26:29 +01:00
}
2010-12-13 18:59:00 +01:00
catch (JAXBException ex)
2010-12-01 14:26:29 +01:00
{
2010-12-13 18:59:00 +01:00
throw new ConfigurationException(ex);
2010-10-12 09:16:40 +02:00
}
}
2010-12-01 14:26:29 +01:00
//~--- methods --------------------------------------------------------------
2010-10-12 09:16:40 +02:00
/**
* Method description
*
*
2010-12-13 18:59:00 +01:00
* @param id
2010-10-12 09:16:40 +02:00
*/
2010-12-01 14:26:29 +01:00
@Override
2010-12-13 18:59:00 +01:00
public void install(String id)
2010-10-12 09:16:40 +02:00
{
2010-12-16 07:46:02 +01:00
SecurityUtil.assertIsAdmin(securityContextProvicer);
if (pluginHandler == null)
{
getPluginCenter();
}
pluginHandler.install(id);
2010-12-13 18:59:00 +01:00
}
2010-12-01 14:26:29 +01:00
2010-12-13 18:59:00 +01:00
/**
* Method description
*
*
* @param id
*/
@Override
public void uninstall(String id)
{
2010-12-16 07:46:02 +01:00
SecurityUtil.assertIsAdmin(securityContextProvicer);
2010-12-13 18:59:00 +01:00
throw new UnsupportedOperationException("Not supported yet.");
2010-10-12 09:16:40 +02:00
}
2010-12-18 13:37:34 +01:00
/**
* Method description
*
*
* @param id
*/
@Override
public void update(String id)
{
SecurityUtil.assertIsAdmin(securityContextProvicer);
throw new UnsupportedOperationException("Not supported yet.");
}
2010-10-12 09:16:40 +02:00
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
2010-12-13 18:59:00 +01:00
* @param id
*
2010-10-12 09:16:40 +02:00
* @return
*/
2010-12-01 14:26:29 +01:00
@Override
2010-12-13 18:59:00 +01:00
public PluginInformation get(String id)
2010-10-12 09:16:40 +02:00
{
2010-12-16 07:46:02 +01:00
SecurityUtil.assertIsAdmin(securityContextProvicer);
PluginInformation result = null;
for (PluginInformation info : getPluginCenter().getPlugins())
{
if (id.equals(info.getId()))
{
result = info;
break;
}
}
return result;
2010-10-12 09:16:40 +02:00
}
2010-12-18 13:37:34 +01:00
/**
* Method description
*
*
* @param filter
*
* @return
*/
@Override
public Set<PluginInformation> get(PluginFilter filter)
{
AssertUtil.assertIsNotNull(filter);
SecurityUtil.assertIsAdmin(securityContextProvicer);
Set<PluginInformation> infoSet = new HashSet<PluginInformation>();
filter(infoSet, installedPlugins.values(), filter);
filter(infoSet, getPluginCenter().getPlugins(), filter);
return infoSet;
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<PluginInformation> getAll()
{
SecurityUtil.assertIsAdmin(securityContextProvicer);
Set<PluginInformation> infoSet = new HashSet<PluginInformation>();
infoSet.addAll(installedPlugins.values());
infoSet.addAll(getPluginCenter().getPlugins());
for ( PluginInformation pi : infoSet )
{
System.out.println( pi.getName() + ": " + pi.hashCode() );
}
return infoSet;
}
2010-12-01 14:26:29 +01:00
/**
* Method description
*
*
2010-12-13 18:59:00 +01:00
* @return
2010-12-01 14:26:29 +01:00
*/
2010-12-13 18:59:00 +01:00
@Override
public Collection<PluginInformation> getAvailable()
2010-12-01 14:26:29 +01:00
{
2010-12-16 07:46:02 +01:00
SecurityUtil.assertIsAdmin(securityContextProvicer);
Set<PluginInformation> availablePlugins = new HashSet<PluginInformation>();
Set<PluginInformation> centerPlugins = getPluginCenter().getPlugins();
for (PluginInformation info : centerPlugins)
{
if (!installedPlugins.containsKey(info.getId()))
{
availablePlugins.add(info);
}
}
return availablePlugins;
2010-12-01 14:26:29 +01:00
}
2010-12-18 13:37:34 +01:00
/**
* Method description
*
*
* @return
*/
@Override
public Collection<PluginInformation> getAvailableUpdates()
{
SecurityUtil.assertIsAdmin(securityContextProvicer);
return get(FILTER_UPDATES);
}
2010-10-12 09:16:40 +02:00
/**
* Method description
*
*
2010-12-13 18:59:00 +01:00
* @return
2010-10-12 09:16:40 +02:00
*/
2010-12-13 18:59:00 +01:00
@Override
public Collection<PluginInformation> getInstalled()
2010-10-12 09:16:40 +02:00
{
2010-12-16 07:46:02 +01:00
SecurityUtil.assertIsAdmin(securityContextProvicer);
2010-12-13 18:59:00 +01:00
return installedPlugins.values();
}
2010-12-01 14:26:29 +01:00
2010-12-18 13:37:34 +01:00
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param target
* @param source
* @param filter
*/
private void filter(Set<PluginInformation> target,
Collection<PluginInformation> source, PluginFilter filter)
{
for (PluginInformation info : source)
{
if (filter.accept(info))
{
target.add(info);
}
}
}
/**
* Method description
*
*
* @param available
*/
private void preparePlugin(PluginInformation available)
{
PluginState state = PluginState.AVAILABLE;
for (PluginInformation installed : installedPlugins.values())
{
if (isSamePlugin(available, installed))
{
if (installed.getVersion().equals(available.getVersion()))
{
available.setState(PluginState.INSTALLED);
}
else
{
available.setState(PluginState.UPDATE_AVAILABLE);
}
}
}
available.setState(state);
}
/**
* Method description
*
*
* @param pc
*/
private void preparePlugins(PluginCenter pc)
{
Set<PluginInformation> infoSet = pc.getPlugins();
if (infoSet != null)
{
for (PluginInformation available : infoSet)
{
preparePlugin(available);
}
}
}
//~--- get methods ----------------------------------------------------------
2010-12-13 18:59:00 +01:00
/**
* Method description
*
*
* @return
*/
private PluginCenter getPluginCenter()
{
PluginCenter center = cache.get(PluginCenter.class.getName());
2010-10-12 09:16:40 +02:00
2010-12-13 18:59:00 +01:00
if (center == null)
{
synchronized (DefaultPluginManager.class)
2010-12-13 18:59:00 +01:00
{
if (logger.isInfoEnabled())
{
logger.info("fetch plugin informations from {}",
configuration.getPluginUrl());
}
try
{
center = (PluginCenter) unmarshaller.unmarshal(
new URL(configuration.getPluginUrl()));
2010-12-18 13:37:34 +01:00
preparePlugins(center);
cache.put(PluginCenter.class.getName(), center);
if (pluginHandler == null)
{
pluginHandler = new AetherPluginHandler(SCMContext.getContext());
}
pluginHandler.setPluginRepositories(center.getRepositories());
}
catch (Exception ex)
{
throw new PluginLoadException(ex);
}
2010-12-13 18:59:00 +01:00
}
2010-10-12 09:16:40 +02:00
}
2010-12-13 18:59:00 +01:00
return center;
}
2010-12-01 14:26:29 +01:00
2010-12-18 13:37:34 +01:00
/**
* Method description
*
*
* @param p1
* @param p2
*
* @return
*/
private boolean isSamePlugin(PluginInformation p1, PluginInformation p2)
{
return p1.getGroupId().equals(p2.getGroupId())
&& p1.getArtifactId().equals(p2.getArtifactId());
}
2010-10-12 09:16:40 +02:00
//~--- fields ---------------------------------------------------------------
/** Field description */
2010-12-13 18:59:00 +01:00
private SimpleCache<String, PluginCenter> cache;
/** Field description */
private ScmConfiguration configuration;
/** Field description */
private Map<String, PluginInformation> installedPlugins;
/** Field description */
private AetherPluginHandler pluginHandler;
2010-12-16 07:46:02 +01:00
/** Field description */
private Provider<SecurityContext> securityContextProvicer;
2010-12-13 18:59:00 +01:00
/** Field description */
private Unmarshaller unmarshaller;
2010-10-12 09:16:40 +02:00
}