本文整理汇总了Java中com.holonplatform.core.Context类的典型用法代码示例。如果您正苦于以下问题:Java Context类的具体用法?Java Context怎么用?Java Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Context类属于com.holonplatform.core包,在下文中一共展示了Context类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: deserialize
import com.holonplatform.core.Context; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public PropertyBox deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
// get property set
final PropertySet<?> propertySet = Context.get().resource(PropertySet.CONTEXT_KEY, PropertySet.class)
.orElseThrow(() -> new JsonParseException("Missing PropertySet instance to build a PropertyBox. "
+ "A PropertySet instance must be available as context resource to perform PropertyBox deserialization."));
// get json object
final JsonObject obj = json.getAsJsonObject();
// property box
PropertyBox.Builder builder = PropertyBox.builder(propertySet).invalidAllowed(true);
propertySet.forEach(p -> {
getPathName(p).ifPresent(n -> {
final Object value = obj.get(n) != null ? context.deserialize(obj.get(n), p.getType()) : null;
if (value != null) {
builder.setIgnoreReadOnly(p, value);
}
});
});
return builder.build();
}
开发者ID:holon-platform,项目名称:holon-json,代码行数:26,代码来源:GsonPropertyBoxDeserializer.java
示例2: lookupResource
import com.holonplatform.core.Context; //导入依赖的package包/类
/**
* Lookup a resource of given <code>resourceType</code> type, either using a suitable {@link ContextResolver} if
* <code>providers</code> is not <code>null</code> and a {@link ContextResolver} for given <code>resourceType</code>
* is available, or trying to obtain the resource from {@link Context} using given <code>resourceType</code> as
* context resource key.
* @param <R> Resource type
* @param caller Caller class
* @param resourceType Resource type to lookup (not null)
* @param providers JAX-RS {@link Providers}, if available
* @return Resource instance, or an empty optional if not available
*/
public static <R> Optional<R> lookupResource(Class<?> caller, Class<R> resourceType, Providers providers) {
ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null");
R resource = null;
// try to use a ContextResolver, if available
ContextResolver<R> resolver = providers.getContextResolver(resourceType, null);
if (resolver != null) {
resource = resolver.getContext(caller);
}
if (resource == null) {
// lookup in context
resource = Context.get()
.resource(resourceType,
(caller != null) ? caller.getClassLoader() : ClassUtils.getDefaultClassLoader())
.orElse(null);
}
return Optional.ofNullable(resource);
}
开发者ID:holon-platform,项目名称:holon-jaxrs,代码行数:33,代码来源:ResourceUtils.java
示例3: main
import com.holonplatform.core.Context; //导入依赖的package包/类
@SuppressWarnings({ "unused", "resource" })
public static void main(String[] args) {
// Init Spring context using Configuration class
ApplicationContext ctx = new AnnotationConfigApplicationContext(Configuration.class);
// Get the ExampleResource by type
Context.get().resource(ExampleResource.class).ifPresent(resource -> {
System.out.println("Message: " + resource.getMessage());
});
// Get the ExampleResource by resource key (Spring Bean name)
Context.get().resource("exampleResource", ExampleResource.class).ifPresent(resource -> {
System.out.println("Message: " + resource.getMessage());
});
// Use the current Thread context to override the ExampleResource instance
Context.get().executeThreadBound("exampleResource", new ExampleResourceImpl("Thread resource"), () -> {
Context.get().resource("exampleResource", ExampleResource.class).ifPresent(resource -> {
System.out.println("Message: " + resource.getMessage());
});
});
}
开发者ID:holon-platform,项目名称:holon-examples,代码行数:25,代码来源:Main.java
示例4: main
import com.holonplatform.core.Context; //导入依赖的package包/类
public static void main(String[] args) {
// Run
SpringApplication.run(Application.class, args);
// Get the ExampleResource by type
Context.get().resource(ExampleResource.class).ifPresent(resource -> {
System.out.println("Message: " + resource.getMessage());
});
// Get the ExampleResource by resource key (Spring Bean name)
Context.get().resource("exampleResource", ExampleResource.class).ifPresent(resource -> {
System.out.println("Message: " + resource.getMessage());
});
// Use the current Thread context to override the ExampleResource instance
Context.get().executeThreadBound("exampleResource", new ExampleResourceImpl("Thread resource"), () -> {
Context.get().resource("exampleResource", ExampleResource.class).ifPresent(resource -> {
System.out.println("Message: " + resource.getMessage());
});
});
}
开发者ID:holon-platform,项目名称:holon-examples,代码行数:23,代码来源:Application.java
示例5: getContextScope
import com.holonplatform.core.Context; //导入依赖的package包/类
public void getContextScope() {
ClassLoader aClassLoader = ClassLoader.getSystemClassLoader();
Object resourceInstance = new String();
// tag::getscope[]
Optional<ContextScope> scope = Context.get().scope("scopeName"); // <1>
scope = Context.get().scope("scopeName", aClassLoader); // <2>
// end::getscope[]
// tag::threadscope[]
Optional<ContextScope> threadScope = Context.get().threadScope(); // <1>
threadScope = Context.get().threadScope(aClassLoader); // <2>
Context.get().executeThreadBound("resourceKey", resourceInstance, () -> {
// do something // <3>
});
Context.get().executeThreadBound("resourceKey", resourceInstance, () -> {
// do something // <4>
return null;
});
// end::threadscope[]
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:27,代码来源:ExampleContext.java
示例6: testRealmContext
import com.holonplatform.core.Context; //导入依赖的package包/类
@Test
public void testRealmContext() {
TestUtils.expectedException(IllegalStateException.class, new Runnable() {
@Override
public void run() {
Realm.require();
}
});
String name = Context.get().executeThreadBound(Realm.CONTEXT_KEY,
Realm.builder().name("rlm").withDefaultAuthorizer().build(), () -> {
return Realm.getCurrent().orElseThrow(() -> new IllegalStateException("Missing Realm")).getName()
.orElse(null);
});
assertEquals("rlm", name);
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:21,代码来源:TestRealm.java
示例7: testContext
import com.holonplatform.core.Context; //导入依赖的package包/类
@Test
public void testContext() {
final TenantResolver tr = () -> Optional.of("T1");
Context.get().threadScope().map(s -> s.put(TenantResolver.CONTEXT_KEY, tr));
assertEquals(tr, TenantResolver.getCurrent().orElse(null));
TenantResolver.execute("T2", () -> {
assertEquals("T2", TenantResolver.getCurrent().map(r -> r.getTenantId().orElse(null)).orElse(null));
});
String result = TenantResolver.execute("T3", () -> {
assertEquals("T3", TenantResolver.getCurrent().map(r -> r.getTenantId().orElse(null)).orElse(null));
return "T3";
});
assertEquals("T3", result);
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:21,代码来源:TestTenantResolver.java
示例8: testContext
import com.holonplatform.core.Context; //导入依赖的package包/类
@Test
public void testContext() {
final TenantDataSourceProvider tdsp = tenantId -> {
if ("test".equals(tenantId)) {
return new DefaultBasicDataSource();
}
return null;
};
DataSource ds = Context.get().executeThreadBound(TenantDataSourceProvider.CONTEXT_KEY, tdsp, () -> {
return TenantDataSourceProvider.getCurrent().orElseThrow(() -> new IllegalStateException("Missing Realm"))
.getDataSource("test");
});
assertNotNull(ds);
}
开发者ID:holon-platform,项目名称:holon-jdbc,代码行数:19,代码来源:TestMultiTenantDataSource.java
示例9: navigateTo
import com.holonplatform.core.Context; //导入依赖的package包/类
/**
* Navigate to given navigation state
* @param navigationState Navigation state
*/
public void navigateTo(String navigationState) {
final Optional<ViewNavigator> previous = Context.get().threadScope()
.flatMap(s -> s.get(ViewNavigator.CONTEXT_KEY, ViewNavigator.class));
try {
Context.get().threadScope().map(s -> s.put(ViewNavigator.CONTEXT_KEY, navigator));
// check fallback to default
if (isDefaultViewAvailable() && !isViewAvailable(navigationState)) {
if (navigationState == null || navigationState.trim().equals("")
|| isNavigateToDefaultViewWhenViewNotAvailable()) {
navigateToDefault();
return;
}
}
// call default navigation method
try {
navigateToState(null, sanitizeNavigationState(navigationState));
} catch (Exception e) {
throw new ViewNavigationException(null, "Failed to navigate to state: [" + navigationState + "]", e);
}
// check navigation is not towards a Window
if (!viewWindows.containsKey(navigationState)) {
// close all current Views displayed in a Window
closeAllViewWindows();
}
// track navigation state in history if current View is not volatile
if (navigator.getCurrentView() != null && !isVolatile(navigator.getCurrentView(), navigationState)) {
trackInHistory(navigationState);
}
} finally {
Context.get().threadScope().map(s -> s.remove(ViewNavigator.CONTEXT_KEY));
previous.ifPresent((n) -> Context.get().threadScope().map(s -> s.put(ViewNavigator.CONTEXT_KEY, n)));
}
}
开发者ID:holon-platform,项目名称:holon-vaadin7,代码行数:44,代码来源:NavigatorActuator.java
示例10: injectContext
import com.holonplatform.core.Context; //导入依赖的package包/类
/**
* Inject {@link Context} field, if any, in a given View instance
* @param viewConfigurationProvider ViewConfigurationProvider
* @param view View instance
* @return View
* @throws ViewConfigurationException Error in context data injection
*/
public static View injectContext(ViewConfigurationProvider viewConfigurationProvider, View view)
throws ViewConfigurationException {
if (view != null) {
ViewConfiguration configuration = viewConfigurationProvider.getViewConfiguration(view.getClass());
if (configuration != null) {
Collection<ViewContextField> fields = configuration.getContextInjectionFields();
if (fields != null) {
for (final ViewContextField vcf : fields) {
final Class<?> type = vcf.getField().getType();
final String key = (vcf.getContextResourceKey() != null) ? vcf.getContextResourceKey()
: type.getName();
Optional<?> resource = Context.get().resource(key, type);
if (resource.isPresent()) {
try {
FieldUtils.writeField(vcf.getField(), view, resource.get(), true);
} catch (Exception e) {
throw new ViewConfigurationException("Failed to inject context resource type " + type
+ " in field " + vcf.getField().getName() + " of view class "
+ view.getClass().getName(), e);
}
} else {
if (vcf.isRequired()) {
throw new ViewConfigurationException("Failed to inject context resource type " + type
+ " in field " + vcf.getField().getName() + " of view class "
+ view.getClass().getName() + ": context resource not available");
}
}
}
}
} else {
LOGGER.warn("No ViewConfiguration available for view class " + view.getClass().getName()
+ ": Context informations injection skipped");
}
}
return view;
}
开发者ID:holon-platform,项目名称:holon-vaadin7,代码行数:47,代码来源:ViewNavigationUtils.java
示例11: create
import com.holonplatform.core.Context; //导入依赖的package包/类
@Override
public RestClient create(ClassLoader classLoader) throws RestClientCreationException {
// Try to obtain a RestTemplate
Optional<JaxrsClientBuilder> builder = Context.get().resource("restTemplateBuilder", JaxrsClientBuilder.class,
classLoader);
if (builder.isPresent()) {
return new JaxrsClientRestClient(builder.get().build());
}
LOGGER.debug(() -> "No JaxrsClientBuilder type Context resource available - RestClient creation skipped");
return null;
}
开发者ID:holon-platform,项目名称:holon-jaxrs,代码行数:12,代码来源:JaxrsClientBuilderRestClientFactory.java
示例12: init
import com.holonplatform.core.Context; //导入依赖的package包/类
@BeforeClass
public static void init() {
// create a LocalizationContext with a MessageProvider which never returns a translation
LocalizationContext ctx = LocalizationContext.builder().messageProvider((locale, code) -> Optional.empty())
.withInitialLocale(Locale.US).build();
// register it as a ClassLoader-scoped resource
Context.get().classLoaderScope().ifPresent(scope -> scope.put(LocalizationContext.CONTEXT_KEY, ctx));
// register the DatasetService as a ClassLoader-scoped resource
Context.get().classLoaderScope()
.ifPresent(scope -> scope.put(DatasetService.class.getName(), new DatasetServiceImpl()));
}
开发者ID:holon-platform,项目名称:holon-examples,代码行数:14,代码来源:TestPropertyModel.java
示例13: propertyPresenter2
import com.holonplatform.core.Context; //导入依赖的package包/类
@Test
public void propertyPresenter2() {
// presentation using a PropertyBox
PropertyBox box = PropertyBox.builder(PRODUCT).set(ID, 1L).set(CATEGORY, "C1").build();
// present the CATEGORY property value
String presentation = box.present(CATEGORY);
assertEquals("C1", presentation);
// Register a new presenter
PropertyValuePresenterRegistry.get().register(
// Condition: the property has a "DATASET" named configuration parameter with a non null value
p -> p.getConfiguration().hasNotNullParameter("DATASET"),
// Presenter: use the DatasetService (retrieved from Context) to obtain the value description
(p, v) -> {
return Context.get().resource(DatasetService.class)
.orElseThrow(() -> new IllegalStateException("The DatasetService resource is missing"))
// get the value description using DatasetService "getDescription" method
.getDescription(p.getConfiguration().getParameter("DATASET", String.class).orElse(null),
(String) v);
});
// present the CATEGORY property value: the new presenter will be used
presentation = box.present(CATEGORY);
assertEquals("Category C1", presentation);
}
开发者ID:holon-platform,项目名称:holon-examples,代码行数:29,代码来源:TestPropertyModel.java
示例14: create
import com.holonplatform.core.Context; //导入依赖的package包/类
@Override
public RestClient create(ClassLoader classLoader) throws RestClientCreationException {
// Try to obtain a RestTemplate
Optional<RestTemplate> restTemplate = Context.get().resource("restTemplate", RestTemplate.class, classLoader);
if (restTemplate.isPresent()) {
return new RestTemplateRestClient(restTemplate.get());
}
LOGGER.debug(() -> "No RestTemplate type Context resource available - RestClient creation skipped");
return null;
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:11,代码来源:RestTemplateRestClientFactory.java
示例15: testScope
import com.holonplatform.core.Context; //导入依赖的package包/类
@Test
public void testScope() {
Optional<ContextScope> scope = Context.get().scope(BeanFactoryScope.NAME);
assertTrue(scope.isPresent());
Optional<ResourceTest> tr = Context.get().resource(ResourceTest.CONTEXT_KEY, ResourceTest.class);
assertTrue(tr.isPresent());
assertEquals(1, tr.get().getId());
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:12,代码来源:TestBeanContextByName.java
示例16: getResource
import com.holonplatform.core.Context; //导入依赖的package包/类
public void getResource() {
ClassLoader aClassLoader = ClassLoader.getSystemClassLoader();
// tag::get[]
Optional<ResourceType> resource = Context.get().resource("resourceKey", ResourceType.class); // <1>
resource = Context.get().resource("resourceKey", ResourceType.class, aClassLoader); // <2>
resource = Context.get().resource(ResourceType.class); // <3>
// end::get[]
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:12,代码来源:ExampleContext.java
示例17: create
import com.holonplatform.core.Context; //导入依赖的package包/类
@Override
public RestClient create(ClassLoader classLoader) throws RestClientCreationException {
// Try to obtain a RestTemplate
Optional<RestTemplateBuilder> restTemplateBuilder = Context.get().resource("restTemplateBuilder",
RestTemplateBuilder.class, classLoader);
if (restTemplateBuilder.isPresent()) {
return new RestTemplateRestClient(restTemplateBuilder.get().build());
}
LOGGER.debug(() -> "No RestTemplateBuilder type Context resource available - RestClient creation skipped");
return null;
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:12,代码来源:SpringBootRestTemplateRestClientFactory.java
示例18: testAuthContextResource
import com.holonplatform.core.Context; //导入依赖的package包/类
@Test
public void testAuthContextResource() {
TestUtils.expectedException(IllegalStateException.class, new Runnable() {
@Override
public void run() {
AuthContext.require();
}
});
boolean ia = Context.get().executeThreadBound(AuthContext.CONTEXT_KEY,
AuthContext.create(Realm.builder().withDefaultAuthorizer().build()), () -> {
return AuthContext.getCurrent().orElseThrow(() -> new IllegalStateException("Missing AuthContext"))
.isAuthenticated();
});
assertFalse(ia);
ia = Context.get().executeThreadBound(AuthContext.CONTEXT_KEY,
AuthContext.create(Realm.builder().withDefaultAuthorizer().build()), () -> {
return AuthContext.require().isAuthenticated();
});
assertFalse(ia);
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:28,代码来源:TestAuthContext.java
示例19: execute
import com.holonplatform.core.Context; //导入依赖的package包/类
/**
* Execute given {@link Runnable} <code>operation</code> on behalf of given <code>tenantId</code>, binding to
* current thread a static {@link TenantResolver} context resource with default {@link TenantResolver#CONTEXT_KEY}
* key providing the specified tenant id.
* @param tenantId Tenant id
* @param operation Operation to execute (not null)
* @throws RuntimeException Exception during operation execution
*/
static void execute(final String tenantId, final Runnable operation) throws RuntimeException {
ObjectUtils.argumentNotNull(operation, "Runnable operation must be not null");
try {
Context.get().threadScope()
.map(s -> s.put(TenantResolver.CONTEXT_KEY, TenantResolver.staticTenantResolver(tenantId)));
operation.run();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
Context.get().threadScope().map(s -> s.remove(TenantResolver.CONTEXT_KEY));
}
}
开发者ID:holon-platform,项目名称:holon-core,代码行数:21,代码来源:TenantResolver.java
示例20: getCurrent
import com.holonplatform.core.Context; //导入依赖的package包/类
/**
* Get the current {@link ViewNavigator}, if available as {@link Context} resource or from current UI.
* @return Current ViewNavigator, or an empty Optinal if not available
*/
static Optional<ViewNavigator> getCurrent() {
return Optional.ofNullable(Context.get().resource(ViewNavigator.CONTEXT_KEY, ViewNavigator.class)
.orElse(ViewNavigationUtils.getCurrentUIViewNavigator()));
}
开发者ID:holon-platform,项目名称:holon-vaadin7,代码行数:9,代码来源:ViewNavigator.java
注:本文中的com.holonplatform.core.Context类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论