mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-20 14:10:44 +00:00
Merge pull request #85 from Kingsrook/feature/backend-module-self-registration
Feature/backend module self registration
This commit is contained in:
@ -155,18 +155,6 @@ public class QBackendMetaData implements TopLevelMetaDataInterface
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter, returning generically, to help sub-class fluent flows
|
||||
*******************************************************************************/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends QBackendMetaData> T withBackendType(String backendType)
|
||||
{
|
||||
this.backendType = backendType;
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -27,21 +27,18 @@ import java.util.Map;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QModuleDispatchException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
|
||||
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** This class is responsible for loading a backend module, by its name, and
|
||||
** returning an instance.
|
||||
**
|
||||
** TODO - make this mapping runtime-bound, not pre-compiled in.
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class QBackendModuleDispatcher
|
||||
{
|
||||
private static final QLogger LOG = QLogger.getLogger(QBackendModuleDispatcher.class);
|
||||
|
||||
private static Map<String, String> backendTypeToModuleClassNameMap;
|
||||
private static Map<String, String> backendTypeToModuleClassNameMap = new HashMap<>();
|
||||
|
||||
|
||||
|
||||
@ -50,51 +47,6 @@ public class QBackendModuleDispatcher
|
||||
*******************************************************************************/
|
||||
public QBackendModuleDispatcher()
|
||||
{
|
||||
initBackendTypeToModuleClassNameMap();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static void initBackendTypeToModuleClassNameMap()
|
||||
{
|
||||
if(backendTypeToModuleClassNameMap != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> newMap = new HashMap<>();
|
||||
|
||||
String[] moduleClassNames = new String[]
|
||||
{
|
||||
// todo - let modules somehow "export" their types here?
|
||||
// e.g., backend-core shouldn't need to "know" about the modules.
|
||||
"com.kingsrook.qqq.backend.core.modules.backend.implementations.mock.MockBackendModule",
|
||||
"com.kingsrook.qqq.backend.core.modules.backend.implementations.memory.MemoryBackendModule",
|
||||
"com.kingsrook.qqq.backend.core.modules.backend.implementations.enumeration.EnumerationBackendModule",
|
||||
"com.kingsrook.qqq.backend.module.rdbms.RDBMSBackendModule",
|
||||
"com.kingsrook.qqq.backend.module.filesystem.local.FilesystemBackendModule",
|
||||
"com.kingsrook.qqq.backend.module.filesystem.s3.S3BackendModule",
|
||||
"com.kingsrook.qqq.backend.module.api.APIBackendModule"
|
||||
};
|
||||
|
||||
for(String moduleClassName : moduleClassNames)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<?> moduleClass = Class.forName(moduleClassName);
|
||||
QBackendModuleInterface module = (QBackendModuleInterface) moduleClass.getConstructor().newInstance();
|
||||
newMap.put(module.getBackendType(), moduleClassName);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
LOG.debug("Backend module could not be loaded", e, logPair("moduleClassName", moduleClassName));
|
||||
}
|
||||
}
|
||||
|
||||
backendTypeToModuleClassNameMap = newMap;
|
||||
}
|
||||
|
||||
|
||||
@ -104,7 +56,6 @@ public class QBackendModuleDispatcher
|
||||
*******************************************************************************/
|
||||
public static void registerBackendModule(QBackendModuleInterface moduleInstance)
|
||||
{
|
||||
initBackendTypeToModuleClassNameMap();
|
||||
String backendType = moduleInstance.getBackendType();
|
||||
if(backendTypeToModuleClassNameMap.containsKey(backendType))
|
||||
{
|
||||
|
@ -40,7 +40,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableBackendDetails
|
||||
/*******************************************************************************
|
||||
** Interface that a QBackendModule must implement.
|
||||
**
|
||||
** Note, some methods all have a default version, which throws a 'not implemented'
|
||||
** Note, all methods have a default version, which throws a 'not implemented'
|
||||
** exception.
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -25,6 +25,7 @@ package com.kingsrook.qqq.backend.core.modules.backend.implementations.enumerati
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.CountInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.QueryInterface;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableBackendDetails;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleDispatcher;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
|
||||
|
||||
|
||||
@ -37,6 +38,10 @@ import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
|
||||
*******************************************************************************/
|
||||
public class EnumerationBackendModule implements QBackendModuleInterface
|
||||
{
|
||||
static
|
||||
{
|
||||
QBackendModuleDispatcher.registerBackendModule(new EnumerationBackendModule());
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
** Method where a backend module must be able to provide its type (name).
|
||||
|
@ -29,6 +29,7 @@ import com.kingsrook.qqq.backend.core.actions.interfaces.InsertInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.QStorageInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.QueryInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.UpdateInterface;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleDispatcher;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
|
||||
|
||||
|
||||
@ -43,6 +44,12 @@ import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
|
||||
*******************************************************************************/
|
||||
public class MemoryBackendModule implements QBackendModuleInterface
|
||||
{
|
||||
static
|
||||
{
|
||||
QBackendModuleDispatcher.registerBackendModule(new MemoryBackendModule());
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Method where a backend module must be able to provide its type (name).
|
||||
*******************************************************************************/
|
||||
|
@ -28,6 +28,7 @@ import com.kingsrook.qqq.backend.core.actions.interfaces.InsertInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.QueryInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.UpdateInterface;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleDispatcher;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
|
||||
|
||||
|
||||
@ -40,6 +41,11 @@ import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
|
||||
*******************************************************************************/
|
||||
public class MockBackendModule implements QBackendModuleInterface
|
||||
{
|
||||
static
|
||||
{
|
||||
QBackendModuleDispatcher.registerBackendModule(new MockBackendModule());
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
** Method where a backend module must be able to provide its type (name).
|
||||
*******************************************************************************/
|
||||
|
Reference in New Issue
Block a user