http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/EntityMigrationRule.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/EntityMigrationRule.java b/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/EntityMigrationRule.java index f238f93..291ae0d 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/EntityMigrationRule.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/EntityMigrationRule.java @@ -20,8 +20,7 @@ package org.apache.polygene.migration.assembly; import java.util.Arrays; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.spi.entitystore.helpers.JSONKeys; import org.apache.polygene.spi.entitystore.helpers.StateStore; @@ -33,17 +32,17 @@ public class EntityMigrationRule extends AbstractMigrationRule { private final String[] entityTypes; - private final EntityMigrationOperation operationEntity; + private final EntityMigrationOperation entityOperation; public EntityMigrationRule( String fromVersion, String toVersion, String[] entityTypes, - EntityMigrationOperation operationEntity + EntityMigrationOperation entityOperation ) { super( fromVersion, toVersion ); this.entityTypes = entityTypes; - this.operationEntity = operationEntity; + this.entityOperation = entityOperation; } public String[] entityTypes() @@ -51,29 +50,29 @@ public class EntityMigrationRule return entityTypes; } - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { if( appliesTo( state.getString( JSONKeys.TYPE ) ) ) { - return operationEntity.upgrade( state, stateStore, migrator ); + return entityOperation.upgrade( context, state, stateStore, migrator ); } else { - return false; + context.addFailure( entityOperation.toString() ); + return state; } } - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { if( appliesTo( state.getString( JSONKeys.TYPE ) ) ) { - return operationEntity.downgrade( state, stateStore, migrator ); + return entityOperation.downgrade( context, state, stateStore, migrator ); } else { - return false; + context.addFailure( entityOperation.toString() ); + return state; } } @@ -92,6 +91,6 @@ public class EntityMigrationRule @Override public String toString() { - return fromVersion + "->" + toVersion + ": on " + Arrays.asList( entityTypes ) + " do " + operationEntity; + return fromVersion + "=>" + toVersion + ": on " + Arrays.asList( entityTypes ) + " do " + entityOperation; } }
http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/MigrationContext.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/MigrationContext.java b/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/MigrationContext.java new file mode 100644 index 0000000..8ee057f --- /dev/null +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/assembly/MigrationContext.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.migration.assembly; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class MigrationContext +{ + private boolean changed; + private final List<String> failures = new ArrayList<>(); + + public boolean hasChanged() + { + return changed; + } + + public void markAsChanged() + { + changed = true; + } + + public boolean isSuccess() + { + return failures.isEmpty(); + } + + public boolean hasFailures() + { + return failures.size() > 0; + } + + public List<String> failures() + { + return Collections.unmodifiableList( failures ); + } + + public void addFailure( String operation ) + { + failures.add( operation ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddAssociation.java index 9873ae3..471843b 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddAssociation.java @@ -20,8 +20,8 @@ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; import org.apache.polygene.spi.entitystore.helpers.StateStore; @@ -42,17 +42,15 @@ public class AddAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addAssociation( state, association, defaultValue ); + return migrator.addAssociation( context, state, association, defaultValue ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeAssociation( state, association ); + return migrator.removeAssociation( context, state, association ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddManyAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddManyAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddManyAssociation.java index 0f71acc..82610fe 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddManyAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddManyAssociation.java @@ -21,8 +21,8 @@ package org.apache.polygene.migration.operation; import java.util.Arrays; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; import org.apache.polygene.spi.entitystore.helpers.StateStore; @@ -43,17 +43,15 @@ public class AddManyAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addManyAssociation( state, association, defaultReferences ); + return migrator.addManyAssociation( context, state, association, defaultReferences ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeManyAssociation( state, association ); + return migrator.removeManyAssociation( context, state, association ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddNamedAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddNamedAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddNamedAssociation.java index cfd1c96..90725e8 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddNamedAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddNamedAssociation.java @@ -20,8 +20,8 @@ package org.apache.polygene.migration.operation; import java.util.Map; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; import org.apache.polygene.spi.entitystore.helpers.StateStore; @@ -42,17 +42,15 @@ public class AddNamedAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addNamedAssociation( state, association, defaultReferences ); + return migrator.addNamedAssociation( context, state, association, defaultReferences ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeNamedAssociation( state, association ); + return migrator.removeNamedAssociation( context, state, association ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddProperty.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddProperty.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddProperty.java index 6bef0b9..8f8898a 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddProperty.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/AddProperty.java @@ -20,8 +20,8 @@ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; import org.apache.polygene.spi.entitystore.helpers.StateStore; @@ -42,17 +42,15 @@ public class AddProperty } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addProperty( state, property, defaultValue ); + return migrator.addProperty( context, state, property, defaultValue ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeProperty( state, property ); + return migrator.removeProperty( context, state, property ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveAssociation.java index 5d9f8bb..d1f8771 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveAssociation.java @@ -20,10 +20,10 @@ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.spi.entitystore.helpers.StateStore; /** @@ -43,17 +43,15 @@ public class RemoveAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeAssociation( state, association ); + return migrator.removeAssociation( context, state, association ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addAssociation( state, association, defaultValue ); + return migrator.addAssociation( context, state, association, defaultValue ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveManyAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveManyAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveManyAssociation.java index dd4a0c8..d063338 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveManyAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveManyAssociation.java @@ -21,10 +21,10 @@ package org.apache.polygene.migration.operation; import java.util.Arrays; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.spi.entitystore.helpers.StateStore; /** @@ -43,17 +43,15 @@ public class RemoveManyAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeManyAssociation( state, association ); + return migrator.removeManyAssociation( context, state, association ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addManyAssociation( state, association, defaultReferences ); + return migrator.addManyAssociation( context, state, association, defaultReferences ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveNamedAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveNamedAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveNamedAssociation.java index 33260bf..f97dbea 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveNamedAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveNamedAssociation.java @@ -20,10 +20,10 @@ package org.apache.polygene.migration.operation; import java.util.Map; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.spi.entitystore.helpers.StateStore; /** @@ -42,17 +42,15 @@ public class RemoveNamedAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeNamedAssociation( state, association ); + return migrator.removeNamedAssociation( context, state, association ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addNamedAssociation( state, association, defaultReferences ); + return migrator.addNamedAssociation( context, state, association, defaultReferences ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveProperty.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveProperty.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveProperty.java index 1563928..9ad8673 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveProperty.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RemoveProperty.java @@ -20,8 +20,8 @@ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; import org.apache.polygene.spi.entitystore.helpers.StateStore; @@ -43,17 +43,15 @@ public class RemoveProperty } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.removeProperty( state, property ); + return migrator.removeProperty( context, state, property ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.addProperty( state, property, defaultValue ); + return migrator.addProperty( context, state, property, defaultValue ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameAssociation.java index 52ba536..277de57 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameAssociation.java @@ -20,10 +20,10 @@ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.spi.entitystore.helpers.StateStore; /** @@ -42,17 +42,15 @@ public class RenameAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameAssociation( state, from, to ); + return migrator.renameAssociation( context, state, from, to ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameAssociation( state, to, from ); + return migrator.renameAssociation( context, state, to, from ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameEntity.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameEntity.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameEntity.java index 37cdd59..716a6b1 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameEntity.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameEntity.java @@ -19,11 +19,10 @@ */ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; -import org.apache.polygene.spi.entitystore.helpers.JSONKeys; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.spi.entitystore.helpers.StateStore; /** @@ -42,37 +41,15 @@ public class RenameEntity } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - String type = state.getString( JSONKeys.TYPE ); - - if( type.equals( fromName ) ) - { - migrator.changeEntityType( state, toName ); - return true; - } - else - { - return false; - } + return migrator.changeEntityType( context, state, fromName, toName ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - String type = state.getString( JSONKeys.TYPE ); - - if( type.equals( toName ) ) - { - migrator.changeEntityType( state, fromName ); - return true; - } - else - { - return false; - } + return migrator.changeEntityType( context, state, fromName, fromName ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameManyAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameManyAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameManyAssociation.java index dc207b6..cfc12c5 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameManyAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameManyAssociation.java @@ -20,10 +20,10 @@ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.spi.entitystore.helpers.StateStore; /** @@ -42,17 +42,15 @@ public class RenameManyAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameManyAssociation( state, from, to ); + return migrator.renameManyAssociation( context, state, from, to ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameManyAssociation( state, to, from ); + return migrator.renameManyAssociation( context, state, to, from ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameNamedAssociation.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameNamedAssociation.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameNamedAssociation.java index 0045d15..05fb92a 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameNamedAssociation.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameNamedAssociation.java @@ -19,10 +19,10 @@ */ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.spi.entitystore.helpers.StateStore; /** @@ -41,17 +41,15 @@ public class RenameNamedAssociation } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameNamedAssociation( state, from, to ); + return migrator.renameNamedAssociation( context, state, from, to ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameNamedAssociation( state, to, from ); + return migrator.renameNamedAssociation( context, state, to, from ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameProperty.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameProperty.java b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameProperty.java index b373b76..4f0b80c 100644 --- a/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameProperty.java +++ b/extensions/migration/src/main/java/org/apache/polygene/migration/operation/RenameProperty.java @@ -20,8 +20,8 @@ package org.apache.polygene.migration.operation; -import org.json.JSONException; -import org.json.JSONObject; +import javax.json.JsonObject; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.migration.Migrator; import org.apache.polygene.migration.assembly.EntityMigrationOperation; import org.apache.polygene.spi.entitystore.helpers.StateStore; @@ -42,17 +42,15 @@ public class RenameProperty } @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameProperty( state, fromProperty, toProperty ); + return migrator.renameProperty( context, state, fromProperty, toProperty ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore stateStore, Migrator migrator ) { - return migrator.renameProperty( state, toProperty, fromProperty ); + return migrator.renameProperty( context, state, toProperty, fromProperty ); } @Override http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/migration/src/test/java/org/apache/polygene/migration/MigrationTest.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/test/java/org/apache/polygene/migration/MigrationTest.java b/extensions/migration/src/test/java/org/apache/polygene/migration/MigrationTest.java index 811d79f..1068abb 100644 --- a/extensions/migration/src/test/java/org/apache/polygene/migration/MigrationTest.java +++ b/extensions/migration/src/test/java/org/apache/polygene/migration/MigrationTest.java @@ -22,6 +22,7 @@ package org.apache.polygene.migration; import java.io.IOException; import java.util.List; import java.util.stream.Stream; +import javax.json.JsonObject; import org.apache.polygene.api.activation.ActivationException; import org.apache.polygene.api.identity.Identity; import org.apache.polygene.api.service.importer.NewObjectImporter; @@ -33,18 +34,17 @@ import org.apache.polygene.bootstrap.SingletonAssembler; import org.apache.polygene.bootstrap.unitofwork.DefaultUnitOfWorkAssembler; import org.apache.polygene.migration.assembly.EntityMigrationOperation; import org.apache.polygene.migration.assembly.MigrationBuilder; +import org.apache.polygene.migration.assembly.MigrationContext; import org.apache.polygene.migration.assembly.MigrationOperation; import org.apache.polygene.spi.entitystore.BackupRestore; import org.apache.polygene.spi.entitystore.helpers.JSONKeys; import org.apache.polygene.spi.entitystore.helpers.StateStore; import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.polygene.test.EntityTestAssembler; -import org.hamcrest.CoreMatchers; -import org.json.JSONException; -import org.json.JSONObject; import org.junit.Test; import static java.util.stream.Collectors.toList; +import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; /** @@ -124,9 +124,7 @@ public class MigrationTest id = entity.identity().get(); uow.complete(); - BackupRestore backupRestore = v1.module() - .findService( BackupRestore.class ) - .get(); + BackupRestore backupRestore = v1.module().findService( BackupRestore.class ).get(); try( Stream<String> backup = backupRestore.backup() ) { data_v1 = backup.collect( toList() ); @@ -152,9 +150,9 @@ public class MigrationTest UnitOfWork uow = v1_1.module().unitOfWorkFactory().newUnitOfWork(); TestEntity1_1 entity = uow.get( TestEntity1_1.class, id ); - assertThat( "Property has been renamed", entity.newFoo().get(), CoreMatchers.equalTo( "Some value" ) ); - assertThat( "ManyAssociation has been renamed", entity.newFooManyAssoc().count(), CoreMatchers.equalTo( 1 ) ); - assertThat( "Association has been renamed", entity.newFooAssoc().get(), CoreMatchers.equalTo( entity ) ); + assertThat( "Property has been renamed", entity.newFoo().get(), equalTo( "Some value" ) ); + assertThat( "ManyAssociation has been renamed", entity.newFooManyAssoc().count(), equalTo( 1 ) ); + assertThat( "Association has been renamed", entity.newFooAssoc().get(), equalTo( entity ) ); uow.complete(); try( Stream<String> backup = testData.backup() ) @@ -183,10 +181,10 @@ public class MigrationTest testData.restore( data_v1.stream() ); UnitOfWork uow = v2_0.module().unitOfWorkFactory().newUnitOfWork(); TestEntity2_0 entity = uow.get( TestEntity2_0.class, id ); - assertThat( "Property has been created", entity.bar().get(), CoreMatchers.equalTo( "Some value" ) ); - assertThat( "Custom Property has been created", entity.customBar().get(), CoreMatchers.equalTo( "Hello Some value" ) ); - assertThat( "ManyAssociation has been renamed", entity.newFooManyAssoc().count(), CoreMatchers.equalTo( 1 ) ); - assertThat( "Association has been renamed", entity.newFooAssoc().get(), CoreMatchers.equalTo( entity ) ); + assertThat( "Property has been created", entity.bar().get(), equalTo( "Some value" ) ); + assertThat( "Custom Property has been created", entity.customBar().get(), equalTo( "Hello Some value" ) ); + assertThat( "ManyAssociation has been renamed", entity.newFooManyAssoc().count(), equalTo( 1 ) ); + assertThat( "Association has been renamed", entity.newFooAssoc().get(), equalTo( entity ) ); uow.complete(); } } @@ -221,19 +219,16 @@ public class MigrationTest implements EntityMigrationOperation { @Override - public boolean upgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject upgrade( MigrationContext context, JsonObject state, StateStore store, Migrator migrator ) { - JSONObject properties = (JSONObject) state.get( JSONKeys.PROPERTIES ); - - return migrator.addProperty( state, "customBar", "Hello " + properties.getString( "bar" ) ); + JsonObject properties = state.getJsonObject( JSONKeys.PROPERTIES ); + return migrator.addProperty( context, state, "customBar", "Hello " + properties.getString( "bar" ) ); } @Override - public boolean downgrade( JSONObject state, StateStore stateStore, Migrator migrator ) - throws JSONException + public JsonObject downgrade( MigrationContext context, JsonObject state, StateStore store, Migrator migrator ) { - return migrator.removeProperty( state, "customBar" ); + return migrator.removeProperty( context, state, "customBar" ); } } http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/build.gradle ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/build.gradle b/extensions/serialization-javaxjson/build.gradle new file mode 100644 index 0000000..f4bf36e --- /dev/null +++ b/extensions/serialization-javaxjson/build.gradle @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply plugin: 'polygene-extension' + +description = "Apache Polygene⢠javax.json Serialization Extension" + +jar { manifest { name = "Apache Polygene⢠Extension - Serialization - javax.json" } } + +dependencies { + compile polygene.core.bootstrap + compile libraries.javax_json + + runtime polygene.core.runtime + runtime libraries.johnzon + + testCompile polygene.core.testsupport + + testRuntime libraries.logback +} + http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/dev-status.xml ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/dev-status.xml b/extensions/serialization-javaxjson/dev-status.xml new file mode 100644 index 0000000..0d777be --- /dev/null +++ b/extensions/serialization-javaxjson/dev-status.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + ~ + ~ + --> +<module xmlns="http://polygene.apache.org/schemas/2008/dev-status/1" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://polygene.apache.org/schemas/2008/dev-status/1 + http://polygene.apache.org/schemas/2008/dev-status/1/dev-status.xsd"> + <status> + <!--none,early,beta,stable,mature--> + <codebase>beta</codebase> + + <!-- none, brief, good, complete --> + <documentation>none</documentation> + + <!-- none, some, good, complete --> + <unittests>good</unittests> + </status> + <licenses> + <license>ALv2</license> + </licenses> +</module> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/docs/serialization-javaxjson.txt ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/docs/serialization-javaxjson.txt b/extensions/serialization-javaxjson/src/docs/serialization-javaxjson.txt new file mode 100644 index 0000000..f9aee3f --- /dev/null +++ b/extensions/serialization-javaxjson/src/docs/serialization-javaxjson.txt @@ -0,0 +1,31 @@ +/////////////////////////////////////////////////////////////// + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +/////////////////////////////////////////////////////////////// + +[[extension-serialization-javaxjson,javax.json serialization]] += javax.json serialization = + +[devstatus] +-------------- +source=extensions/serialization-javaxjson/dev-status.xml +-------------- + +// TODO Preamble - link to <<core-api-serialization>> and <<core-spi-serialization>> +// TODO Document usage of JsonSerialization +// TODO Include sample model and its output from test code & resources +// TODO Assembly - Serialization extension or sole Service, settings & adapters http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializationAssembler.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializationAssembler.java b/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializationAssembler.java new file mode 100644 index 0000000..36b84d6 --- /dev/null +++ b/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializationAssembler.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxjson; + +import org.apache.polygene.api.serialization.Deserializer; +import org.apache.polygene.api.serialization.Serialization; +import org.apache.polygene.api.serialization.Serializer; +import org.apache.polygene.bootstrap.Assemblers; +import org.apache.polygene.bootstrap.ModuleAssembly; +import org.apache.polygene.bootstrap.ServiceDeclaration; +import org.apache.polygene.spi.serialization.JsonDeserializer; +import org.apache.polygene.spi.serialization.JsonSerialization; +import org.apache.polygene.spi.serialization.JsonSerializer; + +public class JavaxJsonSerializationAssembler extends Assemblers.VisibilityIdentity<JavaxJsonSerializationAssembler> +{ + private JavaxJsonSettings settings; + + public JavaxJsonSerializationAssembler withJsonSettings( JavaxJsonSettings settings ) + { + this.settings = settings; + return this; + } + + @Override + public void assemble( ModuleAssembly module ) + { + ServiceDeclaration declaration = module.services( JavaxJsonSerializationService.class ) + .withTypes( Serialization.class, + Serializer.class, Deserializer.class, + JsonSerialization.class, + JsonSerializer.class, JsonDeserializer.class ) + .visibleIn( visibility() ); + if( hasIdentity() ) + { + declaration.identifiedBy( identity() ); + } + if( settings != null ) + { + declaration.setMetaInfo( settings ); + } + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/package.html ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/package.html b/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/package.html new file mode 100644 index 0000000..43db1d9 --- /dev/null +++ b/extensions/serialization-javaxjson/src/main/java/org/apache/polygene/serialization/javaxjson/package.html @@ -0,0 +1,24 @@ +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + ~ + ~ + --> +<html> + <body> + <h2>javax.json Serialization.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/CustomJsonAdapterTest.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/CustomJsonAdapterTest.java b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/CustomJsonAdapterTest.java new file mode 100644 index 0000000..bed0492 --- /dev/null +++ b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/CustomJsonAdapterTest.java @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxjson; + +import java.time.LocalDate; +import java.util.function.BiFunction; +import java.util.function.Function; +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonString; +import javax.json.JsonValue; +import org.apache.polygene.api.injection.scope.Service; +import org.apache.polygene.api.property.Property; +import org.apache.polygene.api.serialization.SerializationException; +import org.apache.polygene.api.type.ValueCompositeType; +import org.apache.polygene.api.type.ValueType; +import org.apache.polygene.api.value.ValueBuilder; +import org.apache.polygene.bootstrap.ModuleAssembly; +import org.apache.polygene.test.AbstractPolygeneTest; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class CustomJsonAdapterTest extends AbstractPolygeneTest +{ + @Override + public void assemble( ModuleAssembly module ) + { + new JavaxJsonSerializationAssembler() + .withJsonSettings( new JavaxJsonSettings().withJsonAdapter( new CustomValueAdapter() ) + .withJsonAdapter( new CustomStructureAdapter() ) ) + .assemble( module ); + module.values( SomeValue.class ); + } + + interface SomeValue + { + Property<CustomValue> customValue(); + + Property<CustomStructure> customStructure(); + } + + static class CustomValue + { + String state; + + CustomValue( String state ) + { + this.state = state; + } + } + + static class CustomStructure + { + String foo; + LocalDate bar; + + CustomStructure( String foo, LocalDate bar ) + { + this.foo = foo; + this.bar = bar; + } + } + + static class CustomValueAdapter implements JavaxJsonAdapter<CustomValue> + { + @Override + public Class<CustomValue> type() { return CustomValue.class; } + + @Override + public JsonValue serialize( Object object, Function<Object, JsonValue> serializeFunction ) + { + return JavaxJson.toJsonString( type().cast( object ).state ); + } + + @Override + public CustomValue deserialize( JsonValue json, BiFunction<JsonValue, ValueType, Object> deserializeFunction ) + { + switch( json.getValueType() ) + { + case STRING: + return new CustomValue( ( (JsonString) json ).getString() ); + default: + throw new SerializationException( "Don't know how to deserialize CustomValue from " + json ); + } + } + } + + static class CustomStructureAdapter implements JavaxJsonAdapter<CustomStructure> + { + @Override + public Class<CustomStructure> type() { return CustomStructure.class; } + + @Override + public JsonValue serialize( Object object, Function<Object, JsonValue> serializeFunction ) + { + CustomStructure customStructure = type().cast( object ); + return Json.createObjectBuilder() + .add( "foo", customStructure.foo ) + .add( "bar", serializeFunction.apply( customStructure.bar ) ) + .build(); + } + + @Override + public CustomStructure deserialize( JsonValue json, BiFunction<JsonValue, ValueType, Object> deserializeFunction ) + { + if( json.getValueType() != JsonValue.ValueType.OBJECT ) + { + throw new SerializationException( "Don't know how to deserialize CustomStructure from " + json ); + } + JsonObject jsonObject = (JsonObject) json; + String foo = jsonObject.getString( "foo" ); + LocalDate bar = (LocalDate) deserializeFunction.apply( jsonObject.get( "bar" ), ValueType.of( LocalDate.class ) ); + return new CustomStructure( foo, bar ); + } + } + + @Service + private JavaxJsonSerialization serialization; + + @Test + public void customJsonAdapterForPropertyValue() + { + ValueBuilder<SomeValue> builder = valueBuilderFactory.newValueBuilder( SomeValue.class ); + builder.prototype().customValue().set( new CustomValue( "custom-value-state" ) ); + builder.prototype().customStructure().set( new CustomStructure( "foo", LocalDate.of( 2017, 1, 1 ) ) ); + SomeValue someValue = builder.newInstance(); + + System.out.println( someValue.toString() ); + + JsonValue serialized = serialization.toJson( someValue ); + assertThat( serialized.getValueType(), is( JsonValue.ValueType.OBJECT ) ); + + JsonObject jsonObject = (JsonObject) serialized; + assertThat( jsonObject.getString( "customValue" ), equalTo( "custom-value-state" ) ); + JsonObject structure = jsonObject.getJsonObject( "customStructure" ); + assertThat( structure.getString( "foo" ), equalTo( "foo" ) ); + assertThat( structure.getString( "bar" ), equalTo( "2017-01-01" ) ); + + SomeValue deserialized = serialization.fromJson( module, ValueCompositeType.of( SomeValue.class ), serialized ); + + assertThat( deserialized.customValue().get().state, equalTo( "custom-value-state" ) ); + assertThat( deserialized.customStructure().get().foo, equalTo( "foo" ) ); + assertThat( deserialized.customStructure().get().bar, equalTo( LocalDate.of( 2017, 1, 1 ) ) ); + } + + @Test + public void customJsonAdapterForDirectObject() + { + CustomValue customValueObject = new CustomValue( "custom-value-state" ); + JsonValue serialized = serialization.toJson( customValueObject ); + assertThat( serialized.getValueType(), is( JsonValue.ValueType.STRING ) ); + JsonString jsonString = (JsonString) serialized; + assertThat( jsonString.getString(), equalTo( "custom-value-state" ) ); + + CustomStructure customStructureObject = new CustomStructure( "foo", LocalDate.of( 2017, 1, 1 ) ); + serialized = serialization.toJson( customStructureObject ); + assertThat( serialized.getValueType(), is( JsonValue.ValueType.OBJECT ) ); + JsonObject jsonObject = (JsonObject) serialized; + assertThat( jsonObject.getString( "foo" ), equalTo( "foo" ) ); + assertThat( jsonObject.getString( "bar" ), equalTo( "2017-01-01" ) ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java new file mode 100644 index 0000000..fe98a4f --- /dev/null +++ b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxjson; + +import org.apache.polygene.test.serialization.AbstractCollectionSerializationTest; +import org.junit.Test; + +public class JavaxJsonCollectionSerializationTest extends AbstractCollectionSerializationTest +{ + @Test + public void givenEnumSetWhenSerializingAndDeserializingExcepctEquals() + { + super.givenEnumSetWhenSerializingAndDeserializingExcepctEquals(); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonConfigurationDeserializationTest.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonConfigurationDeserializationTest.java b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonConfigurationDeserializationTest.java new file mode 100644 index 0000000..f054561 --- /dev/null +++ b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonConfigurationDeserializationTest.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxjson; + +import org.apache.polygene.test.entity.AbstractConfigurationDeserializationTest; + +public class JavaxJsonConfigurationDeserializationTest extends AbstractConfigurationDeserializationTest +{ +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDateFormatSerializationTest.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDateFormatSerializationTest.java b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDateFormatSerializationTest.java new file mode 100644 index 0000000..7c0f510 --- /dev/null +++ b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDateFormatSerializationTest.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxjson; + +import org.apache.polygene.test.serialization.AbstractDateFormatSerializationTest; + +public class JavaxJsonDateFormatSerializationTest extends AbstractDateFormatSerializationTest +{ +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonPlainValueSerializationTest.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonPlainValueSerializationTest.java b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonPlainValueSerializationTest.java new file mode 100644 index 0000000..00391e7 --- /dev/null +++ b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonPlainValueSerializationTest.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.serialization.javaxjson; + +import org.apache.polygene.test.serialization.AbstractPlainValueSerializationTest; + +public class JavaxJsonPlainValueSerializationTest extends AbstractPlainValueSerializationTest +{ +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonValueCompositeSerializationTest.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonValueCompositeSerializationTest.java b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonValueCompositeSerializationTest.java new file mode 100644 index 0000000..5fe4f5f --- /dev/null +++ b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonValueCompositeSerializationTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxjson; + +import java.io.StringReader; +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonValue; +import org.apache.polygene.api.injection.scope.Service; +import org.apache.polygene.api.unitofwork.UnitOfWork; +import org.apache.polygene.spi.serialization.JsonSerialization; +import org.apache.polygene.test.serialization.AbstractValueCompositeSerializationTest; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +public class JavaxJsonValueCompositeSerializationTest extends AbstractValueCompositeSerializationTest +{ + @Service + private JsonSerialization jsonSerialization; + + @Test + public void valueCompositeJsonEquality() + { + try( UnitOfWork uow = unitOfWorkFactory.newUnitOfWork() ) + { + Some some = buildSomeValue( moduleInstance, uow, "42" ); + + // Serialize using injected service + JsonValue jsonState = jsonSerialization.toJson( some ); + String stateString = jsonState.toString(); + System.out.println( jsonState.toString() ); + + // Deserialize using Module API + Some some2 = moduleInstance.newValueFromSerializedState( Some.class, stateString ); + + assertThat( "Value equality", some, equalTo( some2 ) ); + + JsonObject jsonState2 = Json.createReader( new StringReader( some2.toString() ) ).readObject(); + + assertThat( "JSON equality", jsonState, equalTo( jsonState2 ) ); + } + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxjson/src/test/resources/configtest.json ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxjson/src/test/resources/configtest.json b/extensions/serialization-javaxjson/src/test/resources/configtest.json new file mode 100644 index 0000000..4c5600b --- /dev/null +++ b/extensions/serialization-javaxjson/src/test/resources/configtest.json @@ -0,0 +1,8 @@ +{ + "identity": "configtest", + "host": { + "ip": "12.23.34.45", + "port": 1234 + }, + "name": "main" +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxxml/build.gradle ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxxml/build.gradle b/extensions/serialization-javaxxml/build.gradle new file mode 100644 index 0000000..494bb81 --- /dev/null +++ b/extensions/serialization-javaxxml/build.gradle @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply plugin: 'polygene-extension' + +description = "Apache Polygene⢠javax.xml Serialization Extension" + +jar { manifest { name = "Apache Polygene⢠Extension - Serialization - javax.xml" } } + +dependencies { + compile polygene.core.bootstrap + + runtime polygene.core.runtime + + testCompile polygene.core.testsupport + testCompile libraries.xmlunit + + testRuntime libraries.logback +} + http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxxml/dev-status.xml ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxxml/dev-status.xml b/extensions/serialization-javaxxml/dev-status.xml new file mode 100644 index 0000000..81841be --- /dev/null +++ b/extensions/serialization-javaxxml/dev-status.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + ~ + ~ + --> +<module xmlns="http://polygene.apache.org/schemas/2008/dev-status/1" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://polygene.apache.org/schemas/2008/dev-status/1 + http://polygene.apache.org/schemas/2008/dev-status/1/dev-status.xsd"> + <status> + <!--none,early,beta,stable,mature--> + <codebase>early</codebase> + + <!-- none, brief, good, complete --> + <documentation>none</documentation> + + <!-- none, some, good, complete --> + <unittests>good</unittests> + </status> + <licenses> + <license>ALv2</license> + </licenses> +</module> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxxml/src/docs/serialization-javaxxml.txt ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxxml/src/docs/serialization-javaxxml.txt b/extensions/serialization-javaxxml/src/docs/serialization-javaxxml.txt new file mode 100644 index 0000000..6fe6d75 --- /dev/null +++ b/extensions/serialization-javaxxml/src/docs/serialization-javaxxml.txt @@ -0,0 +1,30 @@ +/////////////////////////////////////////////////////////////// + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +/////////////////////////////////////////////////////////////// + +[[extension-serialization-javaxxml,javax.xml serialization]] += javax.xml serialization = + +[devstatus] +-------------- +source=extensions/serialization-javaxxml/dev-status.xml +-------------- + +// TODO Document usage of XmlSerialization +// TODO Include sample model and its output from test code & resources +// TODO Assembly - Serialization extension or sole Service, settings & adapters http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXml.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXml.java b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXml.java new file mode 100644 index 0000000..3289d30 --- /dev/null +++ b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXml.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxxml; + +import java.util.Optional; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * javax.xml utilities. + */ +public class JavaxXml +{ + /** + * Find child elements. + * + * @param parent the parent node + * @return a stream of elements + */ + public static Stream<Element> childElements( Node parent ) + { + return toStream( parent.getChildNodes() ).filter( JavaxXml::isElement ) + .map( JavaxXml::castToElement ); + } + + /** + * Find the first child element. + * + * @param parent the parent node + * @return an optional element + */ + public static Optional<Element> firstChildElement( Node parent ) + { + return childElements( parent ).findFirst(); + } + + /** + * Find child elements named {@literal tagName}. + * + * @param parent the parent node + * @param tagName the tag name + * @return a stream of elements named {@literal tagName} + */ + public static Stream<Element> childElementsNamed( Node parent, String tagName ) + { + return childElements( parent ).filter( element -> tagName.equals( element.getTagName() ) ); + } + + /** + * Find the first child element named {@literal tagName}. + * + * @param parent the parent node + * @param tagName the tag name + * @return an optional element named {@literal tagName} + */ + public static Optional<Element> firstChildElementNamed( Node parent, String tagName ) + { + return childElementsNamed( parent, tagName ).findFirst(); + } + + /** + * Find child nodes holding state. + * + * @param parent the parent node + * @return a stream or child state nodes + */ + public static Stream<Node> stateChildNodes( Node parent ) + { + return toStream( parent.getChildNodes() ).filter( JavaxXml::isStateNode ); + } + + /** + * Find the first child node holding state. + * + * @param parent the parent node + * @return an optional child state node + */ + public static Optional<Node> firstStateChildNode( Node parent ) + { + return stateChildNodes( parent ).findFirst(); + } + + /** + * Test if a node holds state. + * + * Types of nodes holding state: + * <ul> + * <li>{@link Node#ELEMENT_NODE}</li> + * <li>{@link Node#CDATA_SECTION_NODE}</li> + * <li>{@link Node#TEXT_NODE}</li> + * </ul> + * + * @param node the node + * @return {@literal true} if {@literal node} holds state + */ + public static boolean isStateNode( Node node ) + { + switch( node.getNodeType() ) + { + case Node.ELEMENT_NODE: + case Node.CDATA_SECTION_NODE: + case Node.TEXT_NODE: + return true; + default: + return false; + } + } + + private static boolean isElement( Node node ) + { + return node.getNodeType() == Node.ELEMENT_NODE; + } + + private static Element castToElement( Node node ) + { + return (Element) node; + } + + private static Stream<Node> toStream( NodeList nodeList ) + { + return StreamSupport.stream( new Spliterators.AbstractSpliterator<Node>( Long.MAX_VALUE, Spliterator.ORDERED ) + { + private int nextIndex = 0; + + @Override + public boolean tryAdvance( Consumer<? super Node> action ) + { + if( nextIndex >= nodeList.getLength() ) + { + return false; + } + action.accept( nodeList.item( nextIndex ) ); + nextIndex++; + return true; + } + }, false ); + } + + private JavaxXml() {} +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapter.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapter.java b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapter.java new file mode 100644 index 0000000..3761198 --- /dev/null +++ b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapter.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxxml; + +import java.util.function.BiFunction; +import java.util.function.Function; +import org.apache.polygene.api.type.ValueType; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +/** + * Adapter for XML (de)serialization. + * + * @param <T> the adapted type + */ +public interface JavaxXmlAdapter<T> +{ + /** + * @return the adapted type + */ + Class<T> type(); + + /** + * Serialize. + * + * @param document the Document to use as a Node factory + * @param object Object to serialize, never null + * @param serializationFunction Serialization function for nested structure serialization + * @return Serialized XML representation + */ + Node serialize( Document document, Object object, Function<Object, Node> serializationFunction ); + + /** + * Deserialize. + * + * @param node XML to deserialize from, never null + * @param deserializationFunction Deserialization function for nested structure deserialization + * @return Deserialized object + */ + T deserialize( Node node, BiFunction<Node, ValueType, Object> deserializationFunction ); +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e4cca11e/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapters.java ---------------------------------------------------------------------- diff --git a/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapters.java b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapters.java new file mode 100644 index 0000000..273789d --- /dev/null +++ b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlAdapters.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.polygene.serialization.javaxxml; + +import java.util.LinkedHashMap; +import java.util.Map; +import org.apache.polygene.api.mixin.Mixins; +import org.apache.polygene.api.type.ValueType; + +import static org.apache.polygene.api.type.HasTypesCollectors.closestType; + +@Mixins( JavaxXmlAdapters.Mixin.class ) +public interface JavaxXmlAdapters +{ + void registerAdapter( ValueType valueType, JavaxXmlAdapter<?> adapter ); + + <T> JavaxXmlAdapter<T> adapterFor( ValueType valueType ); + + default <T> JavaxXmlAdapter<T> adapterFor( Class<T> type ) + { + return adapterFor( ValueType.of( type ) ); + } + + class Mixin implements JavaxXmlAdapters + { + private Map<ValueType, JavaxXmlAdapter<?>> adapters = new LinkedHashMap<>(); + + @Override + public void registerAdapter( final ValueType valueType, final JavaxXmlAdapter<?> adapter ) + { + adapters.put( valueType, adapter ); + } + + @Override + public <T> JavaxXmlAdapter<T> adapterFor( final ValueType valueType ) + { + return castAdapter( adapters.keySet().stream() + .collect( closestType( valueType ) ) + .map( adapters::get ) + .orElse( null ) ); + } + + @SuppressWarnings( "unchecked" ) + private <T> JavaxXmlAdapter<T> castAdapter( JavaxXmlAdapter<?> adapter ) + { + return (JavaxXmlAdapter<T>) adapter; + } + } +}
