diff --git a/jomnigate-mysql/.gitignore b/jomnigate-mysql/.gitignore new file mode 100644 index 00000000..5a6d94ab --- /dev/null +++ b/jomnigate-mysql/.gitignore @@ -0,0 +1,24 @@ +*.class +/target/ +/pom.xml~ +/.settings/ +/.classpath +/.project +/.yardoc +/tmp +/pombak/ +/.sonar_lock + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +.DS_Store + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +/bin/ diff --git a/jomnigate-sql/.gitignore b/jomnigate-sql/.gitignore new file mode 100644 index 00000000..5a6d94ab --- /dev/null +++ b/jomnigate-sql/.gitignore @@ -0,0 +1,24 @@ +*.class +/target/ +/pom.xml~ +/.settings/ +/.classpath +/.project +/.yardoc +/tmp +/pombak/ +/.sonar_lock + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +.DS_Store + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +/bin/ diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/AllTestsCommon.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/AllTestsCommon.java index f32a6631..7aa5b68c 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/AllTestsCommon.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/AllTestsCommon.java @@ -32,7 +32,7 @@ TestStoreObject.class, TestQuery.class, TestQueryHelper.class, TestIndex.class, TestRoundtrip.class, TestOnlyIdMapper.class, TestTrigger.class, TestMassInsert.class, TestKeyGenerator.class, TestGeoSearch.class, TestEncoder.class, TestListExtrems.class, TestReferenced.class, TestFieldConditionCache.class, - TestQueryInterator.class, DataTypesTestSuite.class, ObserverVersioningSuite.class, TestClearDatastore.class, + TestQueryInterator.class, DataTypesTestSuite.class, ObserverSuite.class, TestClearDatastore.class, TestIndexedFields.class, TestUpdate.class }) public class AllTestsCommon { diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/ObserverVersioningSuite.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/ObserverSuite.java similarity index 89% rename from vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/ObserverVersioningSuite.java rename to vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/ObserverSuite.java index 687e4cdf..56e5f3e1 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/ObserverVersioningSuite.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/ObserverSuite.java @@ -23,7 +23,7 @@ * */ @RunWith(Suite.class) -@SuiteClasses({ TestObserverHandler.class, TestDataVersioning.class }) -public class ObserverVersioningSuite { +@SuiteClasses({ TestObserverHandler.class, TestDataVersioning.class, TestJsonSerializationObserver.class }) +public class ObserverSuite { } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestJsonSerializationObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestJsonSerializationObserver.java new file mode 100644 index 00000000..1b07aa82 --- /dev/null +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestJsonSerializationObserver.java @@ -0,0 +1,116 @@ +/* + * #%L + * vertx-pojo-mapper-common-test + * %% + * Copyright (C) 2017 Braintags GmbH + * %% + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * #L% + */ +package de.braintags.vertx.jomnigate.testdatastore; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import org.junit.Test; + +import de.braintags.vertx.jomnigate.init.DataStoreSettings; +import de.braintags.vertx.jomnigate.init.ObserverDefinition; +import de.braintags.vertx.jomnigate.observer.ObserverEventType; +import de.braintags.vertx.jomnigate.observer.impl.JsonSerializationObserver; +import de.braintags.vertx.jomnigate.testdatastore.mapper.SimpleMapper; +import io.vertx.core.json.Json; +import io.vertx.ext.unit.TestContext; + +/** + * Tests to improve correct mapping information for defined observers ( or by settings or annotation ) + * + * @author Michael Remme + * + */ +public class TestJsonSerializationObserver extends AbstractObserverTest { + private static final io.vertx.core.logging.Logger LOGGER = io.vertx.core.logging.LoggerFactory + .getLogger(TestJsonSerializationObserver.class); + + @Test + public void testAfterInsertAndUpdate(TestContext context) throws IOException { + File logDir = getLogDir(); + DataStoreSettings settings = getDataStore(context).getSettings(); + ObserverDefinition os = new ObserverDefinition<>(JsonSerializationObserver.class); + os.getEventTypeList().add(ObserverEventType.AFTER_INSERT); + os.getEventTypeList().add(ObserverEventType.AFTER_UPDATE); + os.getObserverProperties().setProperty(JsonSerializationObserver.DIRECTORY_PROPERTY, logDir.getAbsolutePath()); + settings.getObserverSettings().add(os); + + SimpleMapper sm = new SimpleMapper("testname", "nix"); + sm.intValue = -1; + saveRecord(context, sm); + sm.intValue = 5; + sm.name = "newNAME"; + saveRecord(context, sm); + checkResult(context, logDir, 2, 500); + } + + @Test + public void testAfterInsert(TestContext context) throws IOException { + File logDir = getLogDir(); + DataStoreSettings settings = getDataStore(context).getSettings(); + ObserverDefinition os = new ObserverDefinition<>(JsonSerializationObserver.class); + os.getEventTypeList().add(ObserverEventType.AFTER_INSERT); + os.getObserverProperties().setProperty(JsonSerializationObserver.DIRECTORY_PROPERTY, logDir.getAbsolutePath()); + settings.getObserverSettings().add(os); + LOGGER.debug(Json.encodePrettily(os)); + SimpleMapper sm = new SimpleMapper("testname", "nix"); + sm.intValue = -1; + saveRecord(context, sm); + checkResult(context, logDir, 1, 500); + } + + @Test + public void testDirectoryNotSet(TestContext context) throws IOException { + File logDir = getLogDir(); + DataStoreSettings settings = getDataStore(context).getSettings(); + ObserverDefinition os = new ObserverDefinition<>(JsonSerializationObserver.class); + os.getEventTypeList().add(ObserverEventType.AFTER_INSERT); + settings.getObserverSettings().add(os); + SimpleMapper sm = new SimpleMapper("testname", "nix"); + sm.intValue = -1; + try { + saveRecord(context, sm); + context.fail("we are expecting an exception, cause the log directory is not set"); + } catch (Throwable e) { + // we expect this + } + } + + private void checkResult(TestContext context, File logDir, int fileCount, long waitTicks) { + long start = System.currentTimeMillis(); + while (System.currentTimeMillis() - start < waitTicks) { + File[] files = logDir.listFiles((dir, fileName) -> fileName.endsWith("json")); + if (files.length >= fileCount) { + break; + } + } + context.assertEquals(fileCount, logDir.listFiles((dir, fileName) -> fileName.endsWith("json")).length, + "expected files to be created"); + } + + private File getLogDir() throws IOException { + File logDir = Files.createTempDirectory("jsonSerDirTmp").toFile(); + File[] fl = logDir.listFiles(); + for (File f : fl) { + f.delete(); + } + if (logDir.listFiles().length > 0) { + throw new IllegalArgumentException("the directory was not cleaned"); + } + logDir.mkdir(); + LOGGER.debug("FILE CREATION IN " + logDir.getAbsolutePath()); + return logDir; + } + +} diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverHandler.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverHandler.java index 08de1a6c..17383cee 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverHandler.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverHandler.java @@ -44,7 +44,7 @@ public class TestObserverHandler extends AbstractObserverTest { @Test public void test_AfterMapping_SingleRecord(TestContext context) { - AfterMappingObserver.executed = false; + AfterMappingObserver.reset(); DataStoreSettings settings = getDataStore(context).getSettings(); ObserverDefinition os = new ObserverDefinition<>(AfterMappingObserver.class); os.getEventTypeList().add(ObserverEventType.AFTER_MAPPING); @@ -54,6 +54,10 @@ public void test_AfterMapping_SingleRecord(TestContext context) { sm.intValue = -1; IMapper mapper = getDataStore(context).getMapperFactory().getMapper(SimpleMapper.class); context.assertTrue(AfterMappingObserver.executed, "Observer wasn't executed"); + context.assertTrue(AfterMappingObserver.executedTypes.contains(ObserverEventType.AFTER_MAPPING), + "Observer wasn't executed"); + context.assertFalse(AfterMappingObserver.executedTypes.contains(ObserverEventType.BEFORE_MAPPING), + "Observer should not be executed for event BEFORE_MAPPING"); } @Test diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverMapping.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverMapping.java index 974ce19e..60336440 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverMapping.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/TestObserverMapping.java @@ -25,6 +25,7 @@ import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.ObserverEventType; import de.braintags.vertx.jomnigate.testdatastore.mapper.ObserverAnnotatedMapper; +import de.braintags.vertx.jomnigate.testdatastore.mapper.ObserverAnnotatedMapperWithProperties; import de.braintags.vertx.jomnigate.testdatastore.mapper.ObserverAnnotatedMapper_TwoEvents; import de.braintags.vertx.jomnigate.testdatastore.mapper.Person; import de.braintags.vertx.jomnigate.testdatastore.mapper.PolyMapper; @@ -36,6 +37,7 @@ import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver2; import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver3; import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver4; +import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver5; import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver_NoDefaultConstructor; import io.vertx.ext.unit.TestContext; @@ -49,6 +51,48 @@ public class TestObserverMapping extends AbstractObserverTest { private static final io.vertx.core.logging.Logger LOGGER = io.vertx.core.logging.LoggerFactory .getLogger(TestObserverMapping.class); + /** + * Defines an observer with properties by annotation + * + * @param context + */ + @Test + public void testObserverWithPropertiesByAnnotation(TestContext context) { + + IMapper mapper = getDataStore(context).getMapperFactory() + .getMapper(ObserverAnnotatedMapperWithProperties.class); + List ol = mapper.getObserverHandler().getObserver(ObserverEventType.AFTER_LOAD); + context.assertFalse(ol.isEmpty(), "observer not found"); + context.assertFalse(ol.get(0).getObserverProperties().isEmpty(), "observer properties not found"); + } + + /** + * Defines an observer with properties + * + * @param context + */ + @Test + public void testObserverWithProperties(TestContext context) { + DataStoreSettings settings = getDataStore(context).getSettings(); + ObserverDefinition os = new ObserverDefinition<>(TestObserver5.class); + os.getObserverProperties().setProperty("testProperty", "testValue"); + settings.getObserverSettings().add(os); + + IMapper mapper = getDataStore(context).getMapperFactory() + .getMapper(ObserverAnnotatedMapper.class); + List ol = mapper.getObserverHandler().getObserver(ObserverEventType.AFTER_LOAD); + context.assertFalse(ol.isEmpty(), "observer not found"); + + TestObserver5 os5 = null; + for (IObserver tmp : ol) { + if (tmp.getClass() == TestObserver5.class) { + os5 = (TestObserver5) tmp; + } + } + context.assertNotNull(os5, "required ovbserver not found"); + context.assertFalse(os5.getObserverProperties().isEmpty(), "observ er properties not found"); + } + /** * Defines an observer, which should be executed for any event instanceof BaseRecord. TriggerMapper should not be * handled by this mapper diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/mapper/ObserverAnnotatedMapperWithProperties.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/mapper/ObserverAnnotatedMapperWithProperties.java new file mode 100644 index 00000000..19ba9460 --- /dev/null +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/mapper/ObserverAnnotatedMapperWithProperties.java @@ -0,0 +1,36 @@ +/* + * #%L + * vertx-pojo-mapper-common-test + * %% + * Copyright (C) 2017 Braintags GmbH + * %% + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * #L% + */ +package de.braintags.vertx.jomnigate.testdatastore.mapper; + +import de.braintags.vertx.jomnigate.annotation.Observer; +import de.braintags.vertx.jomnigate.annotation.ObserverOption; +import de.braintags.vertx.jomnigate.testdatastore.mapper.typehandler.BaseRecord; +import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver; + +/** + * + * + * @author Michael Remme + * + */ +@Observer(observerClass = TestObserver.class, priority = 600, observerOptions = { + @ObserverOption(key = "testKey", value = "testValue") }) +public class ObserverAnnotatedMapperWithProperties extends BaseRecord { + + /** + * + */ + public ObserverAnnotatedMapperWithProperties() { + } + +} diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/AfterMappingObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/AfterMappingObserver.java index eae4475b..b2468144 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/AfterMappingObserver.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/AfterMappingObserver.java @@ -12,11 +12,14 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; +import java.util.ArrayList; +import java.util.List; + import de.braintags.vertx.jomnigate.mapping.IMapper; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; import de.braintags.vertx.jomnigate.observer.ObserverEventType; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -25,8 +28,14 @@ * @author Michael Remme * */ -public class AfterMappingObserver implements IObserver { +public class AfterMappingObserver extends AbstractObserver { public static boolean executed = false; + public static List executedTypes = new ArrayList<>(); + + public static void reset() { + executed = false; + executedTypes = new ArrayList<>(); + } /* * (non-Javadoc) @@ -36,14 +45,10 @@ public class AfterMappingObserver implements IObserver { */ @Override public Future handleEvent(IObserverEvent event, IObserverContext context) { + executedTypes.add(event.getEventType()); IMapper mapper = (IMapper) event.getSource(); AfterMappingObserver.executed = true; return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return event.getEventType().equals(ObserverEventType.AFTER_MAPPING); - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeDeleteObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeDeleteObserver.java index 37bead34..373bc1bd 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeDeleteObserver.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeDeleteObserver.java @@ -13,9 +13,9 @@ package de.braintags.vertx.jomnigate.testdatastore.observer; import de.braintags.vertx.jomnigate.dataaccess.delete.IDelete; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -24,7 +24,7 @@ * @author Michael Remme * */ -public class BeforeDeleteObserver implements IObserver { +public class BeforeDeleteObserver extends AbstractObserver { public static boolean executed = false; /* @@ -40,9 +40,4 @@ public Future handleEvent(IObserverEvent event, IObserverContext context) return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return true; - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeLoadObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeLoadObserver.java index 4d039f23..f5881fcf 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeLoadObserver.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeLoadObserver.java @@ -13,9 +13,9 @@ package de.braintags.vertx.jomnigate.testdatastore.observer; import de.braintags.vertx.jomnigate.dataaccess.query.IQuery; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -24,7 +24,7 @@ * @author Michael Remme * */ -public class BeforeLoadObserver implements IObserver { +public class BeforeLoadObserver extends AbstractObserver { public static boolean executed = false; /* @@ -40,9 +40,4 @@ public Future handleEvent(IObserverEvent event, IObserverContext context) return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return true; - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeMappingObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeMappingObserver.java index a7528371..5d017c44 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeMappingObserver.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeMappingObserver.java @@ -12,10 +12,9 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; -import de.braintags.vertx.jomnigate.observer.ObserverEventType; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -24,7 +23,7 @@ * @author Michael Remme * */ -public class BeforeMappingObserver implements IObserver { +public class BeforeMappingObserver extends AbstractObserver { public static boolean executed = false; /* @@ -40,9 +39,4 @@ public Future handleEvent(IObserverEvent event, IObserverContext context) return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return event.getEventType().equals(ObserverEventType.AFTER_MAPPING); - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeSaveObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeSaveObserver.java index ad595006..999c8990 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeSaveObserver.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/BeforeSaveObserver.java @@ -13,9 +13,9 @@ package de.braintags.vertx.jomnigate.testdatastore.observer; import de.braintags.vertx.jomnigate.dataaccess.write.IWrite; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import de.braintags.vertx.jomnigate.testdatastore.mapper.SimpleMapper; import io.vertx.core.Future; @@ -25,7 +25,7 @@ * @author Michael Remme * */ -public class BeforeSaveObserver implements IObserver { +public class BeforeSaveObserver extends AbstractObserver { public static boolean executed = false; /* @@ -49,9 +49,4 @@ public Future handleEvent(IObserverEvent event, IObserverContext context) } } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return true; - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/SimpleMapperObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/SimpleMapperObserver.java index 19f03b90..20f1c23d 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/SimpleMapperObserver.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/SimpleMapperObserver.java @@ -12,9 +12,9 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import de.braintags.vertx.jomnigate.testdatastore.mapper.SimpleMapper; import io.vertx.core.Future; @@ -24,7 +24,7 @@ * @author Michael Remme * */ -public class SimpleMapperObserver implements IObserver { +public class SimpleMapperObserver extends AbstractObserver { public static boolean executed = false; /* diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver.java index 4e30ef42..808f2459 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver.java @@ -12,9 +12,9 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -23,7 +23,8 @@ * @author Michael Remme * */ -public class TestObserver implements IObserver { +public class TestObserver extends AbstractObserver { + public static boolean executed = false; /* * (non-Javadoc) @@ -34,12 +35,8 @@ public class TestObserver implements IObserver { */ @Override public Future handleEvent(IObserverEvent event, IObserverContext context) { + executed = true; return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return false; - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver2.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver2.java index dfef1e54..0b7dc346 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver2.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver2.java @@ -12,9 +12,9 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -23,16 +23,11 @@ * @author Michael Remme * */ -public class TestObserver2 implements IObserver { +public class TestObserver2 extends AbstractObserver { @Override public Future handleEvent(IObserverEvent event, IObserverContext context) { return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return false; - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver3.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver3.java index c6893709..84eb73b0 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver3.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver3.java @@ -12,9 +12,9 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -23,16 +23,11 @@ * @author Michael Remme * */ -public class TestObserver3 implements IObserver { +public class TestObserver3 extends AbstractObserver { @Override public Future handleEvent(IObserverEvent event, IObserverContext context) { return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return false; - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver4.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver4.java index ad3c6196..738c4ce0 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver4.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver4.java @@ -12,9 +12,9 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -23,16 +23,11 @@ * @author Michael Remme * */ -public class TestObserver4 implements IObserver { +public class TestObserver4 extends AbstractObserver { @Override public Future handleEvent(IObserverEvent event, IObserverContext context) { return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return false; - } - } diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver5.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver5.java new file mode 100644 index 00000000..fb74e87a --- /dev/null +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver5.java @@ -0,0 +1,33 @@ +/* + * #%L + * vertx-pojo-mapper-common-test + * %% + * Copyright (C) 2017 Braintags GmbH + * %% + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * #L% + */ +package de.braintags.vertx.jomnigate.testdatastore.observer; + +import de.braintags.vertx.jomnigate.observer.IObserverContext; +import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; +import io.vertx.core.Future; + +/** + * + * + * @author Michael Remme + * + */ +public class TestObserver5 extends AbstractObserver { + + @Override + public Future handleEvent(IObserverEvent event, IObserverContext context) { + return Future.succeededFuture(); + } + +} diff --git a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver_NoDefaultConstructor.java b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver_NoDefaultConstructor.java index afa75364..f4a0bc58 100644 --- a/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver_NoDefaultConstructor.java +++ b/vertx-pojo-mapper-common-test/src/main/java/de/braintags/vertx/jomnigate/testdatastore/observer/TestObserver_NoDefaultConstructor.java @@ -12,9 +12,9 @@ */ package de.braintags.vertx.jomnigate.testdatastore.observer; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -23,7 +23,7 @@ * @author Michael Remme * */ -public class TestObserver_NoDefaultConstructor implements IObserver { +public class TestObserver_NoDefaultConstructor extends AbstractObserver { private TestObserver_NoDefaultConstructor(String name) { // let the unused parameter to provocate exception @@ -34,9 +34,4 @@ public Future handleEvent(IObserverEvent event, IObserverContext context) return Future.succeededFuture(); } - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return false; - } - } diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/annotation/Observer.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/annotation/Observer.java index abc83c44..1352ee50 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/annotation/Observer.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/annotation/Observer.java @@ -56,4 +56,13 @@ * @return defined event types, default is an empty list */ ObserverEventType[] eventTypes() default {}; + + /** + * Define possible options for the resulting IObserver, which can be retrived then as + * {@link IObserver#getObserverProperties()} + * + * @return + */ + ObserverOption[] observerOptions() default {}; + } diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/annotation/ObserverOption.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/annotation/ObserverOption.java new file mode 100644 index 00000000..9a08c069 --- /dev/null +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/annotation/ObserverOption.java @@ -0,0 +1,37 @@ +/* + * #%L + * vertx-pojo-mapper-common + * %% + * Copyright (C) 2017 Braintags GmbH + * %% + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * #L% + */ +package de.braintags.vertx.jomnigate.annotation; + +/** + * Defines an option for an IObserver, which is interpreted by the implementation + * + * @author Michael Remme + * + */ +public @interface ObserverOption { + + /** + * Defines the key of the option + * + * @return + */ + String key(); + + /** + * Defines the value of the option + * + * @return + */ + String value(); + +} diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/init/ObserverDefinition.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/init/ObserverDefinition.java index cf7d559d..15f20ea4 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/init/ObserverDefinition.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/init/ObserverDefinition.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Properties; import de.braintags.vertx.jomnigate.mapping.IMapper; import de.braintags.vertx.jomnigate.observer.IObserver; @@ -31,6 +32,7 @@ public class ObserverDefinition { private List eventTypeList = new ArrayList<>(); private List mapperSettings = new ArrayList<>(); private int priority; + private Properties observerProperties = new Properties(); @SuppressWarnings("unused") private ObserverDefinition() { @@ -117,6 +119,25 @@ public void setPriority(final int priority) { this.priority = priority; } + /** + * properties can be used to define the behaviour of an implementation of {@link IObserver} + * + * @return the observerProperties + */ + public final Properties getObserverProperties() { + return observerProperties; + } + + /** + * properties can be used to define the behaviour of an implementation of {@link IObserver} + * + * @param observerProperties + * the observerProperties to set + */ + public final void setObserverProperties(Properties observerProperties) { + this.observerProperties = observerProperties; + } + /** * Checks wether the current settings are applicable to the given IMapper. The definition is applicable, if no mapper * settings are defined or if mapper settings are defined and one is fitting @@ -165,6 +186,7 @@ public boolean isApplicableFor(final ObserverEventType eventType) { public ObserverDefinition deepCopy() { ObserverDefinition res = new ObserverDefinition<>(getObserverClass()); res.eventTypeList.addAll(getEventTypeList()); + res.observerProperties.putAll(observerProperties); res.priority = getPriority(); for (ObserverMapperSettings mapperSetting : mapperSettings) { diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserver.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserver.java index b800442e..b40be103 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserver.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserver.java @@ -12,7 +12,12 @@ */ package de.braintags.vertx.jomnigate.observer; +import java.util.Properties; + +import de.braintags.vertx.jomnigate.annotation.Observer; +import de.braintags.vertx.jomnigate.init.ObserverDefinition; import io.vertx.core.Future; +import io.vertx.core.Vertx; /** * IObserver is the instance, which is executed on a given event @@ -23,7 +28,26 @@ public interface IObserver { /** - * Checks wether the current instance can handle the given event + * The properties, which can adjust the behaviour of an observer. Those properties can be defined by + * {@link ObserverDefinition} or by annotation {@link Observer} + * + * @return + */ + Properties getObserverProperties(); + + /** + * This method is called directly after the properties are set and can be used to validate and prepare some + * information + * + * @param vertx + */ + void init(Vertx vertx); + + /** + * Checks wether the current instance can handle the given event. NOTE: this method must not handle the event type or + * mapper class, which are defined inside the {@link ObserverDefinition} or by the annotation {@link Observer}, cause + * this decision is done already at the point, where this method is called. This method should return the decision + * based on some more complex information by usong the context or the event properties, for instance * * @param event * @param context diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserverHandler.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserverHandler.java index 4c0e13ef..412170a5 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserverHandler.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/IObserverHandler.java @@ -20,7 +20,6 @@ import de.braintags.vertx.jomnigate.dataaccess.query.IQueryResult; import de.braintags.vertx.jomnigate.dataaccess.write.IWrite; import de.braintags.vertx.jomnigate.dataaccess.write.IWriteResult; -import de.braintags.vertx.jomnigate.init.ObserverDefinition; import de.braintags.vertx.jomnigate.mapping.IMapper; import de.braintags.vertx.jomnigate.observer.impl.handler.DefaultObserverHandler; import io.vertx.core.Future; @@ -46,8 +45,7 @@ public static IObserverHandler createInstance(IMapper mapper) { } /** - * Get all observers, which are registered for the current mapper and the given event. The list should be sorted by - * priority of the underlaying {@link ObserverDefinition} + * Get all observers, which are registered for one {@link ObserverEventType} for the mapper of this * * @param event * @return a list of all fitting IObserver diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/AbstractObserver.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/AbstractObserver.java new file mode 100644 index 00000000..35732330 --- /dev/null +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/AbstractObserver.java @@ -0,0 +1,56 @@ +/* + * #%L + * vertx-pojo-mapper-common + * %% + * Copyright (C) 2017 Braintags GmbH + * %% + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * #L% + */ +package de.braintags.vertx.jomnigate.observer.impl; + +import java.util.Properties; + +import de.braintags.vertx.jomnigate.observer.IObserver; +import de.braintags.vertx.jomnigate.observer.IObserverContext; +import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import io.vertx.core.Vertx; + +/** + * An abstract implementation which contains the properties + * + * @author Michael Remme + * + */ +public abstract class AbstractObserver implements IObserver { + private Properties properties = new Properties(); + + /* + * (non-Javadoc) + * + * @see de.braintags.vertx.jomnigate.observer.IObserver#getObserverProperties() + */ + @Override + public Properties getObserverProperties() { + return properties; + } + + @Override + public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { + return true; + } + + /* + * (non-Javadoc) + * + * @see de.braintags.vertx.jomnigate.observer.IObserver#init(io.vertx.core.Vertx) + */ + @Override + public void init(Vertx vertx) { + // empty implementation here + } + +} diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/JsonSerializationObserver.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/JsonSerializationObserver.java new file mode 100644 index 00000000..7b6b3005 --- /dev/null +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/JsonSerializationObserver.java @@ -0,0 +1,124 @@ +/* + * #%L + * vertx-pojo-mapper-common + * %% + * Copyright (C) 2017 Braintags GmbH + * %% + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * #L% + */ +package de.braintags.vertx.jomnigate.observer.impl; + +import java.util.UUID; + +import de.braintags.vertx.jomnigate.observer.IObserverContext; +import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.util.exception.ParameterRequiredException; +import io.vertx.core.Future; +import io.vertx.core.Vertx; +import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.Json; + +/** + * This observer serializes records as json file into a defined directory. It does not return a Future, so that + * the caller must not wait for the result. Occuring errors are logged. Per record one file is created. + * + * @author Michael Remme + * + */ +public class JsonSerializationObserver extends AbstractObserver { + private static final io.vertx.core.logging.Logger LOGGER = io.vertx.core.logging.LoggerFactory + .getLogger(JsonSerializationObserver.class); + + /** + * The name of the property, which defines the path of the directory, where the output shall be stored + */ + public static final String DIRECTORY_PROPERTY = "directoryPath"; + + private String parentDir; + + /* + * (non-Javadoc) + * + * @see + * de.braintags.vertx.jomnigate.observer.IObserver#handleEvent(de.braintags.vertx.jomnigate.observer.IObserverEvent, + * de.braintags.vertx.jomnigate.observer.IObserverContext) + */ + @Override + public Future handleEvent(IObserverEvent event, IObserverContext context) { + try { + Object source = event.getSource(); + if (source != null) { + Buffer encoded = Buffer.buffer(Json.encode(source)); + String path = createFileName(event); + Vertx vertx = event.getDataStore().getVertx(); + vertx.fileSystem().writeFile(path, encoded, res -> { + if (res.failed()) { + checkCreateDirAndRetry(vertx, encoded, path, res.cause()); + } else { + // we did it + } + }); + } + } catch (Exception e) { + LOGGER.error("error on serializing", e); + } + return null; + } + + private void checkCreateDirAndRetry(Vertx vertx, Buffer encoded, String path, Throwable exception) { + vertx.fileSystem().exists(parentDir, res -> { + if (res.failed()) { + LOGGER.error("directory check failed after exception in file creation", res.cause(), exception); + } else { + if (res.result()) { + // the directory exists, so we can't solve the problem here + LOGGER.error("error in JsonSerializationObserver, can't create output file", res.cause()); + } else { + // create the directory + vertx.fileSystem().mkdirs(parentDir, mkdirsResult -> { + if (mkdirsResult.failed()) { + LOGGER.error("error in JsonSerializationObserver, can't create parent directory", mkdirsResult.cause()); + } else { + // parent directory created, write again + vertx.fileSystem().writeFile(path, encoded, writeResult2 -> { + if (writeResult2.failed()) { + // final error - log it + LOGGER.error("error in JsonSerializationObserver, can't create output file", writeResult2.cause()); + } else { + // we did it + } + }); + } + }); + } + } + }); + } + + private String createFileName(IObserverEvent event) { + return parentDir + event.getSource().getClass().getName() + "_" + UUID.randomUUID().toString() + ".json"; + } + + /* + * (non-Javadoc) + * + * @see de.braintags.vertx.jomnigate.observer.impl.AbstractObserver#init(io.vertx.core.Vertx) + */ + @Override + public void init(Vertx vertx) { + parentDir = getObserverProperties().getProperty(DIRECTORY_PROPERTY); + if (parentDir == null) { + throw new ParameterRequiredException("The property " + DIRECTORY_PROPERTY + " must be set"); + } + if (!parentDir.endsWith("/")) { + parentDir += "/"; + } + // this is done only once per init of observer + vertx.fileSystem().mkdirsBlocking(parentDir); + } + +} diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/handler/DefaultObserverHandler.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/handler/DefaultObserverHandler.java index 5a1c80ed..f8c93eb6 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/handler/DefaultObserverHandler.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/impl/handler/DefaultObserverHandler.java @@ -18,6 +18,7 @@ import java.util.Map; import de.braintags.vertx.jomnigate.annotation.Observer; +import de.braintags.vertx.jomnigate.annotation.ObserverOption; import de.braintags.vertx.jomnigate.annotation.VersionInfo; import de.braintags.vertx.jomnigate.dataaccess.delete.IDelete; import de.braintags.vertx.jomnigate.dataaccess.delete.IDeleteResult; @@ -85,6 +86,10 @@ private void computeObserver() { for (ObserverEventType t : tl) { os.getEventTypeList().add(t); } + ObserverOption[] ooptions = ob.observerOptions(); + for (ObserverOption option : ooptions) { + os.getObserverProperties().setProperty(option.key(), option.value()); + } tmpList.add(os); } tmpList.sort((os1, os2) -> Integer.compare(os2.getPriority(), os1.getPriority())); @@ -97,7 +102,10 @@ public List getObserver(ObserverEventType event) { List ol = new ArrayList<>(); observerList.stream().filter(os -> os.isApplicableFor(event)).forEach(os -> { try { - ol.add(os.getObserverClass().newInstance()); + IObserver observer = os.getObserverClass().newInstance(); + observer.getObserverProperties().putAll(os.getObserverProperties()); + observer.init(mapper.getMapperFactory().getDataStore().getVertx()); + ol.add(observer); } catch (Exception e) { throw new MappingException(e); } diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/package-info.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/package-info.java index 9ea17913..bbe31896 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/package-info.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/observer/package-info.java @@ -119,7 +119,17 @@ *       "eventTypeList": [], *       "mapperSettings": [], *       "priority": 501 + *     }, + *     { + *       "observerClass": "de.braintags.vertx.jomnigate.observer.impl.JsonSerializationObserver", // <6> + *       "eventTypeList": [], + *       "mapperSettings": [], + *       "observerProperties" : { + *          "directoryPath" : "/tmp/jsonLogDir" + *       } + *       "priority": 501 *     } + * *   ] * } * @@ -137,6 +147,8 @@ * * <5> An observer is registered for any event and mapper * + * <6> A JsonSerializationObserver is registered, where properties define the directory, where inside the output is + * stored * * ===== Register observer by annotation * The annotation {@link de.braintags.vertx.jomnigate.annotation.Observer} can be used to register an observer for a diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/ExecuteVersionConverter.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/ExecuteVersionConverter.java index 836f82fd..1ea8f52e 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/ExecuteVersionConverter.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/ExecuteVersionConverter.java @@ -20,9 +20,9 @@ import de.braintags.vertx.jomnigate.annotation.VersionConverterDefinition; import de.braintags.vertx.jomnigate.annotation.VersionInfo; import de.braintags.vertx.jomnigate.exception.MappingException; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.CompositeFuture; import io.vertx.core.Future; @@ -34,7 +34,7 @@ * @author Michael Remme * */ -public class ExecuteVersionConverter implements IObserver { +public class ExecuteVersionConverter extends AbstractObserver { private List converterList = new ArrayList<>(); private long currentVersion; diff --git a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/SetMapperVersionObserver.java b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/SetMapperVersionObserver.java index cd188e2a..98610354 100644 --- a/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/SetMapperVersionObserver.java +++ b/vertx-pojo-mapper-common/src/main/java/de/braintags/vertx/jomnigate/versioning/SetMapperVersionObserver.java @@ -15,10 +15,10 @@ import de.braintags.vertx.jomnigate.annotation.VersionInfo; import de.braintags.vertx.jomnigate.init.ObserverDefinition; import de.braintags.vertx.jomnigate.init.ObserverMapperSettings; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; import de.braintags.vertx.jomnigate.observer.ObserverEventType; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import io.vertx.core.Future; /** @@ -28,7 +28,7 @@ * @author Michael Remme * */ -public class SetMapperVersionObserver implements IObserver { +public class SetMapperVersionObserver extends AbstractObserver { public static ObserverDefinition createObserverSettings() { ObserverDefinition settings = new ObserverDefinition<>(SetMapperVersionObserver.class); @@ -40,17 +40,6 @@ public static ObserverDefinition createObserverSetting return settings; } - /* - * (non-Javadoc) - * - * @see de.braintags.vertx.jomnigate.observer.IObserver#canHandleEvent(de.braintags.vertx.jomnigate.observer. - * IObserverEvent, de.braintags.vertx.jomnigate.observer.IObserverContext) - */ - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { - return true; - } - /* * (non-Javadoc) * diff --git a/vertx-pojo-mapper-common/src/main/java/examples/DemoObserver.java b/vertx-pojo-mapper-common/src/main/java/examples/DemoObserver.java index 17fd815b..22593b17 100644 --- a/vertx-pojo-mapper-common/src/main/java/examples/DemoObserver.java +++ b/vertx-pojo-mapper-common/src/main/java/examples/DemoObserver.java @@ -12,18 +12,13 @@ */ package examples; -import de.braintags.vertx.jomnigate.observer.IObserver; import de.braintags.vertx.jomnigate.observer.IObserverContext; import de.braintags.vertx.jomnigate.observer.IObserverEvent; +import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver; import examples.mapper.SimpleMapper; import io.vertx.core.Future; -public class DemoObserver implements IObserver { - - @Override - public boolean canHandleEvent(IObserverEvent event, IObserverContext context) { // <1> - return true; - } +public class DemoObserver extends AbstractObserver { @Override public Future handleEvent(IObserverEvent event, IObserverContext context) { // <2>