You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/03/21 05:41:51 UTC
[isis] branch master updated: ISIS-2309: allow type classification
via ServiceLoader plugin
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 4df864f ISIS-2309: allow type classification via ServiceLoader plugin
4df864f is described below
commit 4df864f60e993caeccba846c1384804620110534
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sat Mar 21 06:41:44 2020 +0100
ISIS-2309: allow type classification via ServiceLoader plugin
eg. allow JDO module to classify entity types
---
.../core/config/beans/IsisBeanTypeClassifier.java | 47 +++++++++++++++++
.../core/config/beans/IsisBeanTypeRegistry.java | 23 ++++-----
.../entities/IsisBeanTypeClassifierForJdo.java | 59 ++++++++++++++++++++++
...e.isis.core.config.beans.IsisBeanTypeClassifier | 1 +
4 files changed, 116 insertions(+), 14 deletions(-)
diff --git a/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanTypeClassifier.java b/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanTypeClassifier.java
new file mode 100644
index 0000000..f5e0514
--- /dev/null
+++ b/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanTypeClassifier.java
@@ -0,0 +1,47 @@
+/*
+ * 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.isis.core.config.beans;
+
+import org.apache.isis.core.commons.collections.Can;
+import org.apache.isis.core.commons.internal.context._Plugin;
+import org.apache.isis.core.config.beans.IsisBeanTypeRegistry.BeanClassification;
+
+/**
+ * ServiceLoader SPI that allows for implementing instances to have a say during bean type scanning.
+ * @since 2.0
+ * @see IsisBeanTypeRegistry
+ */
+public interface IsisBeanTypeClassifier {
+
+ // -- INTERFACE
+
+ /**
+ * @param type
+ * @return optionally the bean classification for given {@code type},
+ * based on whether this classifier feels responsible for the {@code type}.
+ */
+ BeanClassification classify(Class<?> type);
+
+ // -- LOOKUP
+
+ public static Can<IsisBeanTypeClassifier> get() {
+ return Can.ofCollection(_Plugin.loadAll(IsisBeanTypeClassifier.class));
+ }
+
+}
diff --git a/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanTypeRegistry.java b/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanTypeRegistry.java
index 51ca3de..ea92245 100644
--- a/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanTypeRegistry.java
+++ b/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanTypeRegistry.java
@@ -35,11 +35,11 @@ import org.apache.isis.applib.annotation.DomainObject;
import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.Mixin;
import org.apache.isis.applib.annotation.ViewModel;
+import org.apache.isis.applib.services.metamodel.BeanSort;
+import org.apache.isis.core.commons.collections.Can;
import org.apache.isis.core.commons.internal.base._Strings;
import org.apache.isis.core.commons.internal.collections._Lists;
import org.apache.isis.core.commons.internal.collections._Sets;
-import org.apache.isis.applib.services.metamodel.BeanSort;
-import org.apache.isis.core.commons.internal.reflection._Reflect;
import static org.apache.isis.core.commons.internal.base._With.requires;
import static org.apache.isis.core.commons.internal.reflection._Annotations.findNearestAnnotation;
@@ -61,6 +61,7 @@ public final class IsisBeanTypeRegistry implements IsisComponentScanInterceptor,
* Inbox for introspection, as used by the SpecificationLoader
*/
private final Map<Class<?>, BeanSort> introspectableTypes = new HashMap<>();
+ private final Can<IsisBeanTypeClassifier> classifierPlugins = IsisBeanTypeClassifier.get();
// -- DISTINCT CATEGORIES OF BEAN SORTS
@@ -237,21 +238,15 @@ public final class IsisBeanTypeRegistry implements IsisComponentScanInterceptor,
.delegated(BeanSort.MANAGED_BEAN_CONTRIBUTING, objectType(aDomainService.get()));
}
- //TODO would require ServiceLoader plugin to delegate the responsibility for this categorization to the
- //module, that provides these types
- if(_Reflect.containsAnnotation(type, "javax.jdo.annotations.PersistenceCapable")) {
- //XXX hotfix in support of the @EmbeddedOnly annotation, also requires @Value to be present
- if(_Reflect.containsAnnotation(type, "javax.jdo.annotations.EmbeddedOnly")) {
- //TODO does not honor @PersistenceCapable(embeddedOnly="true")
- if(findNearestAnnotation(type, org.apache.isis.applib.annotation.Value.class).isPresent()) {
-
- return BeanClassification.selfManaged(BeanSort.VALUE);
- }
+ // allow ServiceLoader plugins to have a say, eg. when classifying entity types
+ for(val classifier : classifierPlugins) {
+ val classification = classifier.classify(type);
+ if(classification!=null) {
+ return classification;
}
- //this takes precedence over whatever @DomainObject has to say
- return BeanClassification.selfManaged(BeanSort.ENTITY);
}
+
if(findNearestAnnotation(type, Mixin.class).isPresent()) {
return BeanClassification.selfManaged(BeanSort.MIXIN);
}
diff --git a/persistence/jdo/datanucleus-5/src/main/java/org/apache/isis/persistence/jdo/datanucleus5/entities/IsisBeanTypeClassifierForJdo.java b/persistence/jdo/datanucleus-5/src/main/java/org/apache/isis/persistence/jdo/datanucleus5/entities/IsisBeanTypeClassifierForJdo.java
new file mode 100644
index 0000000..f681929
--- /dev/null
+++ b/persistence/jdo/datanucleus-5/src/main/java/org/apache/isis/persistence/jdo/datanucleus5/entities/IsisBeanTypeClassifierForJdo.java
@@ -0,0 +1,59 @@
+/*
+ * 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.isis.persistence.jdo.datanucleus5.entities;
+
+import javax.jdo.annotations.EmbeddedOnly;
+
+import org.apache.isis.applib.services.metamodel.BeanSort;
+import org.apache.isis.core.config.beans.IsisBeanTypeClassifier;
+import org.apache.isis.core.config.beans.IsisBeanTypeRegistry.BeanClassification;
+import org.apache.isis.core.metamodel.facets.Annotations;
+
+import static org.apache.isis.core.commons.internal.reflection._Annotations.findNearestAnnotation;
+
+import lombok.val;
+
+/**
+ * ServiceLoader plugin, classifies PersistenceCapable types into BeanSort.ENTITY.
+ * @since 2.0
+ */
+public class IsisBeanTypeClassifierForJdo implements IsisBeanTypeClassifier {
+
+ @Override
+ public BeanClassification classify(Class<?> type) {
+
+ val persistenceCapableAnnot = findNearestAnnotation(type, javax.jdo.annotations.PersistenceCapable.class);
+ if(persistenceCapableAnnot.isPresent()) {
+
+ val embeddedOnlyAttribute = persistenceCapableAnnot.get().embeddedOnly();
+ // Whether objects of this type can only be embedded,
+ // hence have no ID that binds them to the persistence layer
+ final boolean embeddedOnly = Boolean.valueOf(embeddedOnlyAttribute)
+ || Annotations.getAnnotation(type, EmbeddedOnly.class)!=null;
+ if(embeddedOnly) {
+ return null; // don't categorize as entity ... fall through in the caller's logic
+ }
+
+ return BeanClassification.selfManaged(BeanSort.ENTITY);
+ }
+
+ return null; // we don't feel responsible to classify given type
+ }
+
+}
diff --git a/persistence/jdo/datanucleus-5/src/main/resources/META-INF/services/org.apache.isis.core.config.beans.IsisBeanTypeClassifier b/persistence/jdo/datanucleus-5/src/main/resources/META-INF/services/org.apache.isis.core.config.beans.IsisBeanTypeClassifier
new file mode 100644
index 0000000..a0a2dd1
--- /dev/null
+++ b/persistence/jdo/datanucleus-5/src/main/resources/META-INF/services/org.apache.isis.core.config.beans.IsisBeanTypeClassifier
@@ -0,0 +1 @@
+org.apache.isis.persistence.jdo.datanucleus5.entities.IsisBeanTypeClassifierForJdo
\ No newline at end of file