Skip to content

Commit

Permalink
Remove direct dependency of alibaba dubbo (apache#13218)
Browse files Browse the repository at this point in the history
* Remove direct dependency of alibaba dubbo

* disable in ExtensionLoader

* Fix license

* Fix test

* Fix case

* Fix test
  • Loading branch information
AlbumenJ authored Oct 19, 2023
1 parent 416374c commit 0d91d41
Show file tree
Hide file tree
Showing 130 changed files with 7,178 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* 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.dubbo.common.compact;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Dubbo2ActivateUtils {
private static final Class<? extends Annotation> ACTIVATE_CLASS;
private static final Method GROUP_METHOD;
private static final Method VALUE_METHOD;
private static final Method BEFORE_METHOD;
private static final Method AFTER_METHOD;
private static final Method ORDER_METHOD;
private static final Method ON_CLASS_METHOD;

static {
ACTIVATE_CLASS = loadClass();
GROUP_METHOD = loadMethod("group");
VALUE_METHOD = loadMethod("value");
BEFORE_METHOD = loadMethod("before");
AFTER_METHOD = loadMethod("after");
ORDER_METHOD = loadMethod("order");
ON_CLASS_METHOD = loadMethod("onClass");
}

@SuppressWarnings("unchecked")
private static Class<? extends Annotation> loadClass() {
try {
Class<?> clazz = Class.forName("com.alibaba.dubbo.common.extension.Activate");
if (clazz.isAnnotation()) {
return (Class<? extends Annotation>) clazz;
} else {
return null;
}
} catch (Throwable e) {
return null;
}
}

public static boolean isActivateLoaded() {
return ACTIVATE_CLASS != null;
}

public static Class<? extends Annotation> getActivateClass() {
return ACTIVATE_CLASS;
}

private static Method loadMethod(String name) {
if (ACTIVATE_CLASS == null) {
return null;
}
try {
return ACTIVATE_CLASS.getMethod(name);
} catch (Throwable e) {
return null;
}
}

public static String[] getGroup(Annotation annotation) {
if (GROUP_METHOD == null) {
return null;
}
try {
Object result = GROUP_METHOD.invoke(annotation);
if (result instanceof String[]) {
return (String[]) result;
} else {
return null;
}
} catch (Throwable e) {
return null;
}
}

public static String[] getValue(Annotation annotation) {
if (VALUE_METHOD == null) {
return null;
}
try {
Object result = VALUE_METHOD.invoke(annotation);
if (result instanceof String[]) {
return (String[]) result;
} else {
return null;
}
} catch (Throwable e) {
return null;
}
}

public static String[] getBefore(Annotation annotation) {
if (BEFORE_METHOD == null) {
return null;
}
try {
Object result = BEFORE_METHOD.invoke(annotation);
if (result instanceof String[]) {
return (String[]) result;
} else {
return null;
}
} catch (Throwable e) {
return null;
}
}

public static String[] getAfter(Annotation annotation) {
if (AFTER_METHOD == null) {
return null;
}
try {
Object result = AFTER_METHOD.invoke(annotation);
if (result instanceof String[]) {
return (String[]) result;
} else {
return null;
}
} catch (Throwable e) {
return null;
}
}

public static int getOrder(Annotation annotation) {
if (ORDER_METHOD == null) {
return 0;
}
try {
Object result = ORDER_METHOD.invoke(annotation);
if (result instanceof Integer) {
return (Integer) result;
} else {
return 0;
}
} catch (Throwable e) {
return 0;
}
}

public static String[] getOnClass(Annotation annotation) {
if (ON_CLASS_METHOD == null) {
return null;
}
try {
Object result = ON_CLASS_METHOD.invoke(annotation);
if (result instanceof String[]) {
return (String[]) result;
} else {
return null;
}
} catch (Throwable e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.dubbo.common.compact;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.utils.StringUtils;

import java.lang.annotation.Annotation;

public class Dubbo2CompactUtils {
private static volatile boolean enabled = true;
private static final Class<? extends Annotation> REFERENCE_CLASS;
private static final Class<? extends Annotation> SERVICE_CLASS;
private static final Class<?> ECHO_SERVICE_CLASS;
private static final Class<?> GENERIC_SERVICE_CLASS;

static {
initEnabled();
REFERENCE_CLASS = loadAnnotation("com.alibaba.dubbo.config.annotation.Reference");
SERVICE_CLASS = loadAnnotation("com.alibaba.dubbo.config.annotation.Service");
ECHO_SERVICE_CLASS = loadClass("com.alibaba.dubbo.rpc.service.EchoService");
GENERIC_SERVICE_CLASS = loadClass("com.alibaba.dubbo.rpc.service.GenericService");
}

private static void initEnabled() {
try {
String fromProp = System.getProperty(CommonConstants.DUBBO2_COMPACT_ENABLE);
if (StringUtils.isNotEmpty(fromProp)) {
enabled = Boolean.parseBoolean(fromProp);
return;
}
String fromEnv = System.getenv(CommonConstants.DUBBO2_COMPACT_ENABLE);
if (StringUtils.isNotEmpty(fromEnv)) {
enabled = Boolean.parseBoolean(fromEnv);
return;
}
fromEnv = System.getenv(StringUtils.toOSStyleKey(CommonConstants.DUBBO2_COMPACT_ENABLE));
enabled = !StringUtils.isNotEmpty(fromEnv) || Boolean.parseBoolean(fromEnv);
} catch (Throwable t) {
enabled = true;
}
}

public static boolean isEnabled() {
return enabled;
}

public static void setEnabled(boolean enabled) {
Dubbo2CompactUtils.enabled = enabled;
}

private static Class<?> loadClass(String name) {
try {
return Class.forName(name);
} catch (Throwable e) {
return null;
}
}

@SuppressWarnings("unchecked")
private static Class<? extends Annotation> loadAnnotation(String name) {
try {
Class<?> clazz = Class.forName(name);
if (clazz.isAnnotation()) {
return (Class<? extends Annotation>) clazz;
} else {
return null;
}
} catch (Throwable e) {
return null;
}
}
public static boolean isReferenceClassLoaded() {
return REFERENCE_CLASS != null;
}

public static Class<? extends Annotation> getReferenceClass() {
return REFERENCE_CLASS;
}

public static boolean isServiceClassLoaded() {
return SERVICE_CLASS != null;
}

public static Class<? extends Annotation> getServiceClass() {
return SERVICE_CLASS;
}

public static boolean isEchoServiceClassLoaded() {
return ECHO_SERVICE_CLASS != null;
}

public static Class<?> getEchoServiceClass() {
return ECHO_SERVICE_CLASS;
}

public static boolean isGenericServiceClassLoaded() {
return GENERIC_SERVICE_CLASS != null;
}

public static Class<?> getGenericServiceClass() {
return GENERIC_SERVICE_CLASS;
}
}
Loading

0 comments on commit 0d91d41

Please sign in to comment.