2012-03-04 11:13:18 +01:00
|
|
|
/**
|
2012-04-07 10:27:06 +02:00
|
|
|
* Copyright (c) 2010, Sebastian Sdorra
|
|
|
|
|
* All rights reserved.
|
2012-03-04 11:13:18 +01:00
|
|
|
*
|
|
|
|
|
* 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,
|
2012-04-07 10:27:06 +02:00
|
|
|
* 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.
|
2012-03-04 11:13:18 +01:00
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
2012-04-07 10:27:06 +02:00
|
|
|
* 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.
|
2012-03-04 11:13:18 +01:00
|
|
|
*
|
|
|
|
|
* http://bitbucket.org/sdorra/scm-manager
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sonia.scm;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2012-03-18 15:56:19 +01:00
|
|
|
import com.google.common.base.Function;
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import com.google.inject.Module;
|
|
|
|
|
|
2012-03-04 11:13:18 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2012-03-04 19:46:44 +01:00
|
|
|
import sonia.scm.util.AssertUtil;
|
2012-03-25 13:05:49 +02:00
|
|
|
import sonia.scm.util.Util;
|
2012-03-04 19:46:44 +01:00
|
|
|
|
2012-03-04 11:13:18 +01:00
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2012-03-25 13:05:49 +02:00
|
|
|
import java.util.Collections;
|
2012-03-04 11:13:18 +01:00
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
|
import javax.xml.bind.JAXBException;
|
|
|
|
|
import javax.xml.bind.annotation.XmlAccessType;
|
|
|
|
|
import javax.xml.bind.annotation.XmlAccessorType;
|
|
|
|
|
import javax.xml.bind.annotation.XmlElement;
|
|
|
|
|
import javax.xml.bind.annotation.XmlRootElement;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
@XmlRootElement(name = "overrides")
|
|
|
|
|
@XmlAccessorType(XmlAccessType.FIELD)
|
|
|
|
|
public class ClassOverrides implements Iterable<ClassOverride>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String OVERRIDE_PATH = "META-INF/scm/override.xml";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* the logger for ClassOverrides
|
|
|
|
|
*/
|
|
|
|
|
private static final Logger logger =
|
|
|
|
|
LoggerFactory.getLogger(ClassOverrides.class);
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public static ClassOverrides findOverrides()
|
|
|
|
|
{
|
|
|
|
|
ClassOverrides overrides = new ClassOverrides();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Enumeration<URL> overridesEnm =
|
|
|
|
|
getClassLoader().getResources(OVERRIDE_PATH);
|
|
|
|
|
JAXBContext context = JAXBContext.newInstance(ClassOverrides.class);
|
|
|
|
|
|
|
|
|
|
while (overridesEnm.hasMoreElements())
|
|
|
|
|
{
|
|
|
|
|
URL overrideUrl = overridesEnm.nextElement();
|
|
|
|
|
|
2012-03-04 13:58:26 +01:00
|
|
|
if (logger.isInfoEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.info("load override from {}", overrideUrl.toExternalForm());
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-04 11:13:18 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ClassOverrides co =
|
|
|
|
|
(ClassOverrides) context.createUnmarshaller().unmarshal(
|
|
|
|
|
overrideUrl);
|
2012-03-04 13:59:01 +01:00
|
|
|
|
2012-03-04 14:00:19 +01:00
|
|
|
overrides.append(co);
|
2012-03-04 11:13:18 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.error("could not load ".concat(overrideUrl.toExternalForm()),
|
|
|
|
|
ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
logger.error("could not load overrides", ex);
|
|
|
|
|
}
|
|
|
|
|
catch (JAXBException ex)
|
|
|
|
|
{
|
|
|
|
|
logger.error("could not create jaxb context for ClassOverrides", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return overrides;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- get methods ----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private static ClassLoader getClassLoader()
|
|
|
|
|
{
|
|
|
|
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
|
|
|
|
|
|
|
|
if (classLoader == null)
|
|
|
|
|
{
|
|
|
|
|
classLoader = ClassOverrides.class.getClassLoader();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return classLoader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param overrides
|
|
|
|
|
*/
|
|
|
|
|
public void append(ClassOverrides overrides)
|
|
|
|
|
{
|
2012-03-04 19:46:44 +01:00
|
|
|
AssertUtil.assertIsNotNull(overrides);
|
|
|
|
|
|
|
|
|
|
for (ClassOverride co : overrides)
|
|
|
|
|
{
|
|
|
|
|
if (co.isValid())
|
|
|
|
|
{
|
|
|
|
|
getOverrides().add(co);
|
|
|
|
|
}
|
|
|
|
|
else if (logger.isWarnEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.warn("could not append ClassOverride, because it is not valid");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-03-18 15:56:19 +01:00
|
|
|
|
|
|
|
|
getModuleClasses().addAll(overrides.getModuleClasses());
|
2012-03-04 11:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Iterator<ClassOverride> iterator()
|
|
|
|
|
{
|
|
|
|
|
return getOverrides().iterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- get methods ----------------------------------------------------------
|
|
|
|
|
|
2012-03-18 15:56:19 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public List<Class<? extends Module>> getModuleClasses()
|
|
|
|
|
{
|
|
|
|
|
if (moduleClasses == null)
|
|
|
|
|
{
|
|
|
|
|
moduleClasses = new ArrayList<Class<? extends Module>>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return moduleClasses;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public List<? extends Module> getModules()
|
|
|
|
|
{
|
2012-03-25 13:05:49 +02:00
|
|
|
List<? extends Module> modules = null;
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(moduleClasses))
|
2012-03-18 15:56:19 +01:00
|
|
|
{
|
2012-03-25 13:05:49 +02:00
|
|
|
modules = Lists.transform(moduleClasses,
|
|
|
|
|
new Function<Class<? extends Module>, Module>()
|
2012-03-18 15:56:19 +01:00
|
|
|
{
|
2012-03-25 13:05:49 +02:00
|
|
|
@Override
|
|
|
|
|
public Module apply(Class<? extends Module> moduleClass)
|
2012-03-18 15:56:19 +01:00
|
|
|
{
|
2012-03-25 13:05:49 +02:00
|
|
|
Module module = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
module = moduleClass.newInstance();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.error(
|
|
|
|
|
"could not create module instance of ".concat(
|
|
|
|
|
moduleClass.getName()), ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return module;
|
2012-03-18 15:56:19 +01:00
|
|
|
}
|
2012-03-25 13:05:49 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
modules = Collections.EMPTY_LIST;
|
|
|
|
|
}
|
2012-03-18 15:56:19 +01:00
|
|
|
|
2012-03-25 13:05:49 +02:00
|
|
|
return modules;
|
2012-03-18 15:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
2012-03-04 13:58:26 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param clazz
|
|
|
|
|
* @param <T>
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public <T> Class<T> getOverride(Class<T> clazz)
|
|
|
|
|
{
|
|
|
|
|
Class<T> implementation = null;
|
|
|
|
|
|
|
|
|
|
for (ClassOverride co : getOverrides())
|
|
|
|
|
{
|
|
|
|
|
if (co.getBind().equals(clazz))
|
|
|
|
|
{
|
|
|
|
|
implementation = (Class<T>) co.getTo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implementation;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-04 11:13:18 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public List<ClassOverride> getOverrides()
|
|
|
|
|
{
|
|
|
|
|
if (overrides == null)
|
|
|
|
|
{
|
|
|
|
|
overrides = new ArrayList<ClassOverride>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return overrides;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- set methods ----------------------------------------------------------
|
|
|
|
|
|
2012-03-18 15:56:19 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param moduleClasses
|
|
|
|
|
*/
|
|
|
|
|
public void setModuleClasses(List<Class<? extends Module>> moduleClasses)
|
|
|
|
|
{
|
|
|
|
|
this.moduleClasses = moduleClasses;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-04 11:13:18 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param overrides
|
|
|
|
|
*/
|
|
|
|
|
public void setOverrides(List<ClassOverride> overrides)
|
|
|
|
|
{
|
|
|
|
|
this.overrides = overrides;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- fields ---------------------------------------------------------------
|
|
|
|
|
|
2012-03-18 15:56:19 +01:00
|
|
|
/** Field description */
|
|
|
|
|
@XmlElement(name = "module")
|
|
|
|
|
private List<Class<? extends Module>> moduleClasses;
|
|
|
|
|
|
2012-03-04 11:13:18 +01:00
|
|
|
/** Field description */
|
|
|
|
|
@XmlElement(name = "override")
|
|
|
|
|
private List<ClassOverride> overrides;
|
|
|
|
|
}
|