-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from xm-online/feature/fix_last_lep_context_t…
…o_map Fix cast lep context to map
- Loading branch information
Showing
9 changed files
with
176 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
xm-commons-lep-groovy/src/test/java/com/icthh/xm/commons/lep/spring/LepIntTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.icthh.xm.commons.lep.spring; | ||
|
||
import com.icthh.xm.commons.lep.XmLepScriptConfigServerResourceLoader; | ||
import com.icthh.xm.commons.lep.api.LepManagementService; | ||
import com.icthh.xm.commons.security.spring.config.XmAuthenticationContextConfiguration; | ||
import com.icthh.xm.commons.tenant.TenantContextHolder; | ||
import com.icthh.xm.commons.tenant.TenantContextUtils; | ||
import com.icthh.xm.commons.tenant.spring.config.TenantContextConfiguration; | ||
import lombok.SneakyThrows; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.Map; | ||
|
||
import static com.icthh.xm.commons.lep.spring.DynamicLepClassResolveIntTest.loadFile; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ContextConfiguration(classes = { | ||
DynamicLepTestConfig.class, | ||
TenantContextConfiguration.class, | ||
XmAuthenticationContextConfiguration.class | ||
}) | ||
@ActiveProfiles("resolveclasstest") | ||
public class LepIntTest { | ||
|
||
@Autowired | ||
private LepManagementService lepManagerService; | ||
|
||
@Autowired | ||
private TenantContextHolder tenantContextHolder; | ||
|
||
@Autowired | ||
private DynamicTestLepService testLepService; | ||
|
||
@Autowired | ||
private XmLepScriptConfigServerResourceLoader resourceLoader; | ||
|
||
@Before | ||
public void init() { | ||
TenantContextUtils.setTenant(tenantContextHolder, "TEST"); | ||
lepManagerService.beginThreadContext(); | ||
} | ||
|
||
@Test | ||
@SneakyThrows | ||
public void testLepContextCastToMap() { | ||
String code = loadFile("lep/TestLepContextCastToMap.groovy"); | ||
resourceLoader.onRefresh("/config/tenants/TEST/testApp/lep/service/TestLepMethodWithInput$$around.groovy", code); | ||
String result = testLepService.testLepMethod(Map.of("parameter", "testValue")); | ||
assertEquals("testValue", result); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
xm-commons-lep-groovy/src/test/resources/lep/TestLepContextCastToMap.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import groovy.util.logging.Slf4j | ||
|
||
@Slf4j | ||
class Service { | ||
Map<String, Object> lepContext | ||
|
||
public Service(Map<String, Object> lepContext) { | ||
log.info("lepContext.inArgs.input.parameter {}", lepContext.inArgs.input.parameter) | ||
this.lepContext = lepContext | ||
} | ||
} | ||
return new Service(lepContext).lepContext.inArgs.input.parameter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
xm-commons-lep/src/main/java/com/icthh/xm/commons/lep/api/LepContextMapSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.icthh.xm.commons.lep.api; | ||
|
||
import com.icthh.xm.commons.logging.util.BasePackageDetector; | ||
import lombok.SneakyThrows; | ||
import org.reflections.Reflections; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.VarHandle; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.function.Predicate.not; | ||
|
||
public class LepContextMapSupport { | ||
private final Map<Class<? extends BaseLepContext>, Map<String, VarHandle>> classFieldHandles = new HashMap<>(); | ||
|
||
public LepContextMapSupport(BasePackageDetector basePackageDetector) { | ||
this(basePackageDetector.getBasePackage()); | ||
} | ||
|
||
public LepContextMapSupport(String basePackage) { | ||
Reflections reflections = new Reflections(basePackage); | ||
Set<Class<? extends BaseLepContext>> classes = reflections.getSubTypesOf(BaseLepContext.class); | ||
initializeVarHandles(classes); | ||
} | ||
|
||
@SneakyThrows | ||
private void initializeVarHandles(Set<Class<? extends BaseLepContext>> classes) { | ||
MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
for (Class<? extends BaseLepContext> type : classes) { | ||
Map<String, VarHandle> fieldHandles = new HashMap<>(); | ||
for (Field field : getFields(type)) { | ||
VarHandle handle = lookup.unreflectVarHandle(field); | ||
fieldHandles.put(field.getName(), handle); | ||
} | ||
classFieldHandles.put(type, fieldHandles); | ||
} | ||
} | ||
|
||
private List<Field> getFields(Class<? extends BaseLepContext> type) { | ||
return Stream.of(type.getFields()) | ||
.filter(not(Field::isSynthetic)) | ||
.filter(it -> !Modifier.isStatic(it.getModifiers())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public Object get(String fieldName, BaseLepContext instance) { | ||
VarHandle handle = classFieldHandles.get(instance.getClass()).get(fieldName); | ||
return handle == null ? null : handle.get(instance); | ||
} | ||
} |
5 changes: 0 additions & 5 deletions
5
xm-commons-lep/src/main/java/com/icthh/xm/commons/lep/impl/utils/ClassPathLepRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters