Skip to content

Commit

Permalink
Removed Reflections from the dependencies
Browse files Browse the repository at this point in the history
* Updated ClassUtils by adding method allowing to scan a package
* Updated ExtensionLoader and ExtensionManager
* Added ExtensionLoadingException
  • Loading branch information
tduchateau committed Jul 3, 2013
1 parent 67b5b5b commit f6f1ed1
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 217 deletions.
6 changes: 0 additions & 6 deletions datatables-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9-RC1</version>
</dependency>

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* [The "BSD licence"]
* Copyright (c) 2012 Dandelion
* 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 Dandelion 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 AUTHOR ``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 AUTHOR 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.
*/
package com.github.dandelion.datatables.core.exception;

public class ExtensionLoadingException extends Exception {

private static final long serialVersionUID = 3243845798907773547L;

public ExtensionLoadingException() {
};

public ExtensionLoadingException(String message) {
super(message);
}

public ExtensionLoadingException(Throwable cause) {
super(cause);
}

public ExtensionLoadingException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.github.dandelion.datatables.core.asset.ResourceType;
import com.github.dandelion.datatables.core.asset.WebResources;
import com.github.dandelion.datatables.core.exception.BadConfigurationException;
import com.github.dandelion.datatables.core.exception.ExtensionLoadingException;
import com.github.dandelion.datatables.core.html.HtmlTable;
import com.github.dandelion.datatables.core.util.CollectionUtils;
import com.github.dandelion.datatables.core.util.JsonIndentingWriter;
Expand Down Expand Up @@ -104,7 +105,7 @@ public ExtensionLoader(HtmlTable table, JsResource mainJsFile, Map<String, Objec
* @throws IOException
*/
public void load(Set<? extends Extension> extensions)
throws BadConfigurationException, IOException {
throws ExtensionLoadingException {

if (extensions != null && !extensions.isEmpty()) {

Expand Down Expand Up @@ -139,7 +140,7 @@ public void load(Set<? extends Extension> extensions)
* The extension to load.
* @throws BadConfigurationException
*/
private void loadJsResources(Extension extension) throws BadConfigurationException {
private void loadJsResources(Extension extension) throws ExtensionLoadingException {

JsResource extensionJsFile = null;
String resourceName = null;
Expand All @@ -157,8 +158,13 @@ private void loadJsResources(Extension extension) throws BadConfigurationExcepti

// All JS resources are merged
for (JsResource jsResource : extension.getJsResources()) {
jsContent.append(ResourceHelper.getFileContentFromClasspath(jsResource
.getLocation()));
try {
jsContent.append(ResourceHelper.getFileContentFromClasspath(jsResource
.getLocation()));
} catch (IOException e) {
throw new ExtensionLoadingException("Unable to read the content of the file "
+ jsResource.getLocation(), e);
}
}

extensionJsFile.setContent(jsContent.toString());
Expand Down Expand Up @@ -198,7 +204,7 @@ public boolean apply(CssResource cssResource) {
* The extension to load.
* @throws BadConfigurationException
*/
private void loadCssResources(HtmlTable table, Extension extension) throws BadConfigurationException {
private void loadCssResources(HtmlTable table, Extension extension) throws ExtensionLoadingException {

CssResource pluginsSourceCssFile = null;
String resourceName = null;
Expand All @@ -218,8 +224,13 @@ private void loadCssResources(HtmlTable table, Extension extension) throws BadCo
for (CssResource cssResource : extension.getCssResources()) {
// Most of CSS resource have a type different from EXTERNAL, which is theme-specific
if(!cssResource.getType().equals(ResourceType.EXTERNAL)){
cssContent.append(ResourceHelper.getFileContentFromClasspath(cssResource
.getLocation()));
try {
cssContent.append(ResourceHelper.getFileContentFromClasspath(cssResource
.getLocation()));
} catch (IOException e) {
throw new ExtensionLoadingException("Unable to read the content of the file "
+ cssResource.getLocation(), e);
}
}
}

Expand All @@ -231,9 +242,9 @@ private void loadCssResources(HtmlTable table, Extension extension) throws BadCo
/**
*
* @param extension
* @throws IOException
* @throws ExtensionLoadingException
*/
private void injectIntoMainJsFile(Extension extension) throws IOException {
private void injectIntoMainJsFile(Extension extension) throws ExtensionLoadingException {

// Extension configuration loading
if (extension.getBeforeAll() != null) {
Expand Down Expand Up @@ -265,7 +276,11 @@ private void injectIntoMainJsFile(Extension extension) throws IOException {
Map<String, Object> conf = extension.getConfigGenerator().generateConfig(table);

// Allways pretty prints the JSON
JSONValue.writeJSONString(conf, writer);
try {
JSONValue.writeJSONString(conf, writer);
} catch (IOException e) {
throw new ExtensionLoadingException("Unable to convert the configuration into JSON", e);
}

mainJsFile.appendToDataTablesExtraConf(writer.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@
*/
package com.github.dandelion.datatables.core.extension;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dandelion.datatables.core.asset.JsResource;
import com.github.dandelion.datatables.core.asset.WebResources;
import com.github.dandelion.datatables.core.exception.BadConfigurationException;
import com.github.dandelion.datatables.core.exception.ExtensionLoadingException;
import com.github.dandelion.datatables.core.extension.feature.AbstractFeature;
import com.github.dandelion.datatables.core.extension.plugin.AbstractPlugin;
import com.github.dandelion.datatables.core.extension.theme.AbstractTheme;
import com.github.dandelion.datatables.core.html.HtmlTable;
import com.github.dandelion.datatables.core.util.ClassUtils;
import com.github.dandelion.datatables.core.util.StringUtils;
Expand All @@ -51,7 +59,23 @@ public class ExtensionManager {
// Logger
private Logger logger = LoggerFactory.getLogger(this.getClass());


private HtmlTable table;

public ExtensionManager(HtmlTable table){
this.table = table;
}

public void loadExtensions(JsResource mainJsFile, Map<String, Object> mainConf, WebResources webResources) throws ExtensionLoadingException {

registerCustomExtensions(table);

ExtensionLoader extensionLoader = new ExtensionLoader(table, mainJsFile, mainConf, webResources);
extensionLoader.load(table.getTableConfiguration().getInternalExtensions());
if(table.getTableConfiguration().getExtraTheme() != null){
extensionLoader.load(new HashSet<AbstractTheme>(Arrays.asList(table.getTableConfiguration().getExtraTheme())));
}
}

/**
* Add custom extensions (for now features and plugins) to the current table
* if they're activated.
Expand All @@ -60,54 +84,72 @@ public class ExtensionManager {
* the HtmlTable to update with custom extensions.
* @throws BadConfigurationException
*/
public void registerCustomExtensions(HtmlTable table) throws BadConfigurationException{
public void registerCustomExtensions(HtmlTable table) throws ExtensionLoadingException {

if (StringUtils.isNotBlank(table.getTableConfiguration().getBasePackage())) {

logger.debug("Scanning custom extensions...");

// Scanning custom extension based on the base.package property
List<AbstractFeature> customFeatures = ClassUtils.scanForFeatures(table.getTableConfiguration()
.getBasePackage());

// Load custom extension if enabled
if (customFeatures != null && !customFeatures.isEmpty() && table.getTableConfiguration().getExtraCustomFeatures() != null) {
for (String extensionToRegister : table.getTableConfiguration().getExtraCustomFeatures()) {
for (AbstractFeature customFeature : customFeatures) {
if (extensionToRegister.equals(customFeature.getName().toLowerCase())) {
table.getTableConfiguration().registerFeature(customFeature);
logger.debug("Feature {} (version: {}) registered", customFeature.getName(),
customFeature.getVersion());
continue;
}
}
}
} else {
logger.debug("No custom feature found");
}

// Scanning custom extension based on the base.package property
List<AbstractPlugin> customPlugins = ClassUtils.scanForPlugins(table.getTableConfiguration()
List<AbstractExtension> customExtensions = scanCustomExtensions(table.getTableConfiguration()
.getBasePackage());

// Load custom extension if enabled
if (customPlugins != null && !customPlugins.isEmpty() && table.getTableConfiguration().getExtraCustomPlugins() != null) {
for (String extensionToRegister : table.getTableConfiguration().getExtraCustomPlugins()) {
for (AbstractPlugin customPlugin : customPlugins) {
if (extensionToRegister.equals(customPlugin.getName().toLowerCase())) {
table.getTableConfiguration().registerPlugin(customPlugin);
logger.debug("Plugin {} (version: {}) registered", customPlugin.getName(),
customPlugin.getVersion());
if (customExtensions != null && !customExtensions.isEmpty() && table.getTableConfiguration().getExtraCustomExtensions() != null) {
for (String extensionToRegister : table.getTableConfiguration().getExtraCustomExtensions()) {
for (AbstractExtension customExtension : customExtensions) {
if (extensionToRegister.equals(customExtension.getName().toLowerCase())) {
table.getTableConfiguration().registerExtension(customExtension);
logger.debug("Extension {} (version: {}) registered", customExtension.getName(),
customExtension.getVersion());
continue;
}
}
}
} else {
logger.debug("No custom plugin found");
logger.warn("No custom extension found");
}
}
else{
logger.debug("The 'base.package' property is blank. Unable to scan any class.");
}
}
}

/**
*
* @param packageName
* @return
* @throws ExtensionLoadingException
*/
public List<AbstractExtension> scanCustomExtensions(String packageName) throws ExtensionLoadingException {

// Init return value
List<AbstractExtension> retval = new ArrayList<AbstractExtension>();

List<Class<?>> customExtensionClassList = null;

try {
customExtensionClassList = ClassUtils.getSubClassesInPackage(packageName, AbstractExtension.class);
} catch (ClassNotFoundException e) {
throw new ExtensionLoadingException("Unable to load custom extensions", e);
} catch (IOException e) {
throw new ExtensionLoadingException("Unable to access the package '" + packageName + "'", e);
}

// Instanciate all found classes
for (Class<?> clazz : customExtensionClassList) {

try {
retval.add((AbstractFeature) ClassUtils.getClass(clazz.getName()).newInstance());
} catch (InstantiationException e) {
throw new ExtensionLoadingException("Unable to instanciate the class " + clazz.getName(), e);
} catch (IllegalAccessException e) {
throw new ExtensionLoadingException("Unable to access the class " + clazz.getName(), e);
} catch (ClassNotFoundException e) {
throw new ExtensionLoadingException("Unable to load the class " + clazz.getName(), e);
}
}

return retval;
}
}

0 comments on commit f6f1ed1

Please sign in to comment.