This is an automated email from the ASF dual-hosted git repository.
mxsm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git
The following commit(s) were added to refs/heads/master by this push:
new 643f059ac [ISSUE #4129]Enhance the functionality of
EventMeshExtensionFactory (#4131)
643f059ac is described below
commit 643f059ac43f4b9e18b98f67ff8ed112f04c49cf
Author: mxsm <[email protected]>
AuthorDate: Tue Aug 29 15:06:17 2023 +0800
[ISSUE #4129]Enhance the functionality of EventMeshExtensionFactory (#4131)
---
.../eventmesh/spi/EventMeshExtensionFactory.java | 96 +++++++++++++++++-----
.../spi/EventMeshExtensionFactoryTest.java | 9 ++
.../spi/example/AnotherSingletonExtension.java | 30 +++++++
.../spi/example/TestAnotherSingletonExtension.java | 30 +++++++
...tmesh.spi.example.TestAnotherSingletonExtension | 17 ++++
5 files changed, 163 insertions(+), 19 deletions(-)
diff --git
a/eventmesh-spi/src/main/java/org/apache/eventmesh/spi/EventMeshExtensionFactory.java
b/eventmesh-spi/src/main/java/org/apache/eventmesh/spi/EventMeshExtensionFactory.java
index 5a65e43a5..62307109b 100644
---
a/eventmesh-spi/src/main/java/org/apache/eventmesh/spi/EventMeshExtensionFactory.java
+++
b/eventmesh-spi/src/main/java/org/apache/eventmesh/spi/EventMeshExtensionFactory.java
@@ -29,6 +29,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@@ -43,7 +44,7 @@ public class EventMeshExtensionFactory {
private static final List<ExtensionClassLoader> EXTENSION_CLASS_LOADERS =
new ArrayList<>();
- private static final ConcurrentHashMap<String, Object>
EXTENSION_INSTANCE_CACHE = new ConcurrentHashMap<>(16);
+ private static final ConcurrentHashMap<Extension, Object>
EXTENSION_INSTANCE_CACHE = new ConcurrentHashMap<>(16);
static {
EXTENSION_CLASS_LOADERS.add(MetaInfExtensionClassLoader.getInstance());
@@ -56,52 +57,72 @@ public class EventMeshExtensionFactory {
}
/**
- * @param extensionType extension plugin class type
- * @param extensionName extension instance name
- * @param <T> the type of the plugin
+ * Get an instance of an extension plugin.
+ *
+ * @param extensionClass extension plugin class type
+ * @param extensionName extension instance name
+ * @param <T> the type of the plugin
* @return plugin instance
*/
- public static <T> T getExtension(Class<T> extensionType, String
extensionName) {
- if (extensionType == null) {
- throw new ExtensionException("extensionType is null");
+ public static <T> T getExtension(Class<T> extensionClass, String
extensionName) {
+ if (extensionClass == null) {
+ throw new ExtensionException("extensionClass is null");
}
if (StringUtils.isEmpty(extensionName)) {
throw new ExtensionException("extensionName is null");
}
- if (!extensionType.isInterface() ||
!extensionType.isAnnotationPresent(EventMeshSPI.class)) {
- throw new ExtensionException(String.format("extensionType:%s is
invalided", extensionType));
+ if (!extensionClass.isInterface() ||
!extensionClass.isAnnotationPresent(EventMeshSPI.class)) {
+ throw new ExtensionException(String.format("extensionClass:%s is
invalided", extensionClass));
}
- EventMeshSPI eventMeshSPIAnnotation =
extensionType.getAnnotation(EventMeshSPI.class);
+ EventMeshSPI eventMeshSPIAnnotation =
extensionClass.getAnnotation(EventMeshSPI.class);
if (eventMeshSPIAnnotation.isSingleton()) {
- return getSingletonExtension(extensionType, extensionName);
+ return getSingletonExtension(extensionClass,
eventMeshSPIAnnotation, extensionName);
}
- return getPrototypeExtension(extensionType, extensionName);
+ return getPrototypeExtension(extensionClass, extensionName);
}
+ /**
+ * Get a singleton instance of an extension plugin.
+ *
+ * @param extensionClass the type of the extension plugin
+ * @param spi the type of the spi
+ * @param extensionInstanceName the name of the extension instance
+ * @param <T> the type of the extension plugin
+ * @return a singleton instance of the extension plugin
+ */
@SuppressWarnings("unchecked")
- private static <T> T getSingletonExtension(Class<T> extensionType, String
extensionInstanceName) {
- return (T)
EXTENSION_INSTANCE_CACHE.computeIfAbsent(extensionInstanceName, name -> {
- Class<T> extensionInstanceClass =
getExtensionInstanceClass(extensionType, extensionInstanceName);
+ private static <T> T getSingletonExtension(Class<T> extensionClass,
EventMeshSPI spi, String extensionInstanceName) {
+ return (T) EXTENSION_INSTANCE_CACHE.computeIfAbsent(new Extension(spi,
extensionInstanceName), name -> {
+ Class<T> extensionInstanceClass =
getExtensionInstanceClass(extensionClass, extensionInstanceName);
if (extensionInstanceClass == null) {
+ log.warn("Get extension instance class {} is null",
extensionClass.getName());
return null;
}
try {
T extensionInstance =
extensionInstanceClass.getDeclaredConstructor().newInstance();
ConfigService.getInstance().populateConfigForObject(extensionInstance);
- log.info("initialize extension instance success,
extensionType: {}, extensionInstanceName: {}",
- extensionType, extensionInstanceName);
+ log.info("initialize extension instance success,
extensionClass: {}, extensionInstanceName: {}",
+ extensionClass, extensionInstanceName);
return extensionInstance;
} catch (NoSuchMethodException | InstantiationException |
IllegalAccessException | InvocationTargetException e) {
throw new ExtensionException("Extension initialize error", e);
} catch (NoSuchFieldException | IOException e) {
- log.error("initialize extension instance config failed,
extensionType: {}, extensionInstanceName: {}",
- extensionType, extensionInstanceName, e);
+ log.error("initialize extension instance config failed,
extensionClass: {}, extensionInstanceName: {}",
+ extensionClass, extensionInstanceName, e);
throw new ExtensionException("Extension initialize error", e);
}
});
}
+ /**
+ * Get a new instance of an extension plugin.
+ *
+ * @param extensionType the type of the extension plugin
+ * @param extensionInstanceName the name of the extension instance
+ * @param <T> the type of the extension plugin
+ * @return a new instance of the extension plugin
+ */
private static <T> T getPrototypeExtension(Class<T> extensionType, String
extensionInstanceName) {
Class<T> extensionInstanceClass =
getExtensionInstanceClass(extensionType, extensionInstanceName);
if (extensionInstanceClass == null) {
@@ -123,6 +144,14 @@ public class EventMeshExtensionFactory {
}
}
+ /**
+ * Get the class of an extension instance.
+ *
+ * @param extensionType the type of the extension instance
+ * @param extensionInstanceName the name of the extension instance
+ * @param <T> the type of the extension instance
+ * @return the class of the extension instance
+ */
@SuppressWarnings("unchecked")
private static <T> Class<T> getExtensionInstanceClass(Class<T>
extensionType, String extensionInstanceName) {
for (ExtensionClassLoader extensionClassLoader :
EXTENSION_CLASS_LOADERS) {
@@ -134,4 +163,33 @@ public class EventMeshExtensionFactory {
}
return null;
}
+
+ private static class Extension {
+
+ private EventMeshSPI spi;
+
+ private String extensionInstanceName;
+
+ public Extension(EventMeshSPI spi, String extensionInstanceName) {
+ this.spi = spi;
+ this.extensionInstanceName = extensionInstanceName;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof Extension)) {
+ return false;
+ }
+ Extension extension = (Extension) o;
+ return Objects.equals(spi, extension.spi) &&
Objects.equals(extensionInstanceName, extension.extensionInstanceName);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(spi, extensionInstanceName);
+ }
+ }
}
diff --git
a/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/EventMeshExtensionFactoryTest.java
b/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/EventMeshExtensionFactoryTest.java
index 55b3156c7..e374fc796 100644
---
a/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/EventMeshExtensionFactoryTest.java
+++
b/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/EventMeshExtensionFactoryTest.java
@@ -17,6 +17,7 @@
package org.apache.eventmesh.spi;
+import org.apache.eventmesh.spi.example.TestAnotherSingletonExtension;
import org.apache.eventmesh.spi.example.TestPrototypeExtension;
import org.apache.eventmesh.spi.example.TestSingletonExtension;
@@ -30,6 +31,14 @@ public class EventMeshExtensionFactoryTest {
TestSingletonExtension extensionA =
EventMeshExtensionFactory.getExtension(TestSingletonExtension.class,
"singletonExtension");
TestSingletonExtension extensionB =
EventMeshExtensionFactory.getExtension(TestSingletonExtension.class,
"singletonExtension");
Assert.assertSame(extensionA, extensionB);
+
+ TestAnotherSingletonExtension singletonExtension =
EventMeshExtensionFactory.getExtension(TestAnotherSingletonExtension.class,
+ "singletonExtension");
+ Assert.assertNotNull(singletonExtension);
+ TestSingletonExtension singletonExtension1 =
EventMeshExtensionFactory.getExtension(TestSingletonExtension.class,
"singletonExtension");
+ Assert.assertNotNull(singletonExtension1);
+
+
}
@Test
diff --git
a/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/example/AnotherSingletonExtension.java
b/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/example/AnotherSingletonExtension.java
new file mode 100644
index 000000000..4663204e1
--- /dev/null
+++
b/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/example/AnotherSingletonExtension.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.eventmesh.spi.example;
+
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class AnotherSingletonExtension implements
TestAnotherSingletonExtension {
+
+ @Override
+ public void hello() {
+ log.info("I am SingletonExtension");
+ }
+}
diff --git
a/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/example/TestAnotherSingletonExtension.java
b/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/example/TestAnotherSingletonExtension.java
new file mode 100644
index 000000000..92d7b1fd3
--- /dev/null
+++
b/eventmesh-spi/src/test/java/org/apache/eventmesh/spi/example/TestAnotherSingletonExtension.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.eventmesh.spi.example;
+
+import org.apache.eventmesh.spi.EventMeshExtensionType;
+import org.apache.eventmesh.spi.EventMeshSPI;
+
+/**
+ * TestAnotherSingletonExtension
+ */
+@EventMeshSPI(eventMeshExtensionType = EventMeshExtensionType.SECURITY)
+public interface TestAnotherSingletonExtension {
+
+ void hello();
+}
diff --git
a/eventmesh-spi/src/test/resources/META-INF/eventmesh/org.apache.eventmesh.spi.example.TestAnotherSingletonExtension
b/eventmesh-spi/src/test/resources/META-INF/eventmesh/org.apache.eventmesh.spi.example.TestAnotherSingletonExtension
new file mode 100644
index 000000000..c8db961c3
--- /dev/null
+++
b/eventmesh-spi/src/test/resources/META-INF/eventmesh/org.apache.eventmesh.spi.example.TestAnotherSingletonExtension
@@ -0,0 +1,17 @@
+#
+# 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.
+
+singletonExtension=org.apache.eventmesh.spi.example.AnotherSingletonExtension
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]