JAXB and Guice: How to integrate and visualize? - java

I find using JAXB together with Guice possible, but challenging: Both libraries "fight" for control over object creation, you have to be careful to avoid cyclic dependencies, and it can get messy with all the JAXB Adapters and Guice Providers and stuff. My questions are:
How do you deal with this configuration? What general strategies / rules of thumb can be applied?
Can you point me to a good tutorial or well written sample code?
How to visualize the dependencies (including the Adapters and Providers)?

For some sample code, some example work was done here: http://jersey.576304.n2.nabble.com/Injecting-JAXBContextProvider-Contextprovider-lt-JAXBContext-gt-with-Guice-td5183058.html
At the line that says "Wrong?", put in the recommended line.
I looks like this:
#Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = { UserBasic.class, UserBasicInformation.class };
public JAXBContextResolver() throws Exception {
this.context =
new JSONJAXBContext(
JSONConfiguration.natural().build(), types);
}
public JAXBContext getContext(Class<?> objectType) {
/*
for (Class type : types) {
if (type == objectType) {
return context;
}
} // There should be some kind of exception for the wrong type.
*/
return context;
}
}
//My resource method:
#GET
#Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public JAXBElement<UserBasic> get(#QueryParam("userName") String userName) {
ObjectFactory ob = new ObjectFactory();
UserDTO dto = getUserService().getByUsername(userName);
if(dto==null) throw new NotFoundException();
UserBasic ub = new UserBasic();
ub.setId(dto.getId());
ub.setEmailAddress(dto.getEmailAddress());
ub.setName(dto.getName());
ub.setPhoneNumber(dto.getPhoneNumber());
return ob.createUserBasic(ub);
}
//My Guice configuration module:
public class MyServletModule extends ServletModule {
public static Module[] getRequiredModules() {
return new Module[] {
new MyServletModule(),
new ServiceModule(),
new CaptchaModule()
};
}
#Override
protected void configureServlets() {
bind(UserHttpResource.class);
bind(JAXBContextResolver.class);
serve("/*").with(GuiceContainer.class);
}
}

Related

Refactor polymorphism using Java 8

I have an old code base that I need to refactor using Java 8, so I have an interface, which tells whether my current site supports the platform.
public interface PlatformSupportHandler {
public abstract boolean isPaltformSupported(String platform);
}
and I have multiple classes implementing it and each class supports a different platform.
A few of the implementing classes are:
#Component("bsafePlatformSupportHandler")
public class BsafePlatoformSupportHandler implements PlatformSupportHandler {
String[] supportedPlatform = {"iPad", "Android", "iPhone"};
Set<String> supportedPlatformSet = new HashSet<>(Arrays.asList(supportedPlatform));
#Override
public boolean isPaltformSupported(String platform) {
return supportedPlatformSet.contains(platform);
}
}
Another implementation:
#Component("discountPlatformSupportHandler")
public class DiscountPlatoformSupportHandler implements PlatformSupportHandler{
String[] supportedPlatform = {"Android", "iPhone"};
Set<String> supportedPlatformSet = new HashSet<>(Arrays.asList(supportedPlatform));
#Override
public boolean isPaltformSupported(String platform) {
return supportedPlatformSet.contains(platform);
}
}
At runtime in my filter, I get the required bean which I want:
platformSupportHandler = (PlatformSupportHandler) ApplicationContextUtil
.getBean(subProductType + Constants.PLATFORM_SUPPORT_HANDLER_APPEND);
and call isPlatformSupported to get whether my current site supports the following platform or not.
I am new to Java 8, so is there any way I can refactor this code without creating multiple classes? As the interface only contains one method, can I somehow use lambda to refactor it?
If you want to stick to the current design, you could do something like this:
public class MyGeneralPurposeSupportHandler implements PlatformSupportHandler {
private final Set<String> supportedPlatforms;
public MyGeneralPurposeSupportHandler(Set<String> supportedPlatforms) {
this.supportedPlatforms = supportedPlatforms;
}
public boolean isPlatformSupported(String platform) {
return supportedPlatforms.contains(platform);
}
}
// now in configuration:
#Configuration
class MySpringConfig {
#Bean
#Qualifier("discountPlatformSupportHandler")
public PlatformSupportHandler discountPlatformSupportHandler() {
return new MyGeneralPurposeSupportHandler(new HashSefOf({"Android", "iPhone"})); // yeah its not a java syntax, but you get the idea
}
#Bean
#Qualifier("bsafePlatformSupportHandler")
public PlatformSupportHandler bsafePlatformSupportHandler() {
return new MyGeneralPurposeSupportHandler(new HashSefOf({"Android", "iPhone", "iPad"}));
}
}
This method has an advantage of not creating class per type (discount, bsafe, etc), so this answers the question.
Going step further, what happens if there no type that was requested, currently it will fail because the bean does not exist in the application context - not a really good approach.
So you could create a map of type to the set of supported platforms, maintain the map in the configuration or something an let spring do its magic.
You'll end up with something like this:
public class SupportHandler {
private final Map<String, Set<String>> platformTypeToSuportedPlatforms;
public SupportHandler(Map<String, Set<String>> map) {
this.platformTypeToSupportedPlatforms = map;
}
public boolean isPaltformSupported(String type) {
Set<String> supportedPlatforms = platformTypeToSupportedPlatforms.get(type);
if(supportedPlatforms == null) {
return false; // or maybe throw an exception, the point is that you don't deal with spring here which is good since spring shouldn't interfere with your business code
}
return supportedPlatforms.contains(type);
}
}
#Configuration
public class MyConfiguration {
// Configuration conf is supposed to be your own way to read configurations in the project - so you'll have to implement it somehow
#Bean
public SupportHandler supportHandler(Configuration conf) {
return new SupportHandler(conf.getRequiredMap());
}
}
Now if you follow this approach, adding a new supported types becomes codeless at all, you only add a configuration, by far its the best method I can offer.
Both methods however lack the java 8 features though ;)
You can use the following in your config class where you can create beans:
#Configuration
public class AppConfiguration {
#Bean(name = "discountPlatformSupportHandler")
public PlatformSupportHandler discountPlatformSupportHandler() {
String[] supportedPlatforms = {"Android", "iPhone"};
return getPlatformSupportHandler(supportedPlatforms);
}
#Bean(name = "bsafePlatformSupportHandler")
public PlatformSupportHandler bsafePlatformSupportHandler() {
String[] supportedPlatforms = {"iPad", "Android", "iPhone"};
return getPlatformSupportHandler(supportedPlatforms);
}
private PlatformSupportHandler getPlatformSupportHandler(String[] supportedPlatforms) {
return platform -> Arrays.asList(supportedPlatforms).contains(platform);
}
}
Also, when you want to use the bean, it is again very easy:
#Component
class PlatformSupport {
// map of bean name vs bean, automatically created by Spring for you
private final Map<String, PlatformSupportHandler> platformSupportHandlers;
#Autowired // Constructor injection
public PlatformSupport(Map<String, PlatformSupportHandler> platformSupportHandlers) {
this.platformSupportHandlers = platformSupportHandlers;
}
public void method1(String subProductType) {
PlatformSupportHandler platformSupportHandler = platformSupportHandlers.get(subProductType + Constants.PLATFORM_SUPPORT_HANDLER_APPEND);
}
}
As it was written in Mark Bramnik's answer you can move this to configuration.
Suppose that it would be in yaml in that way:
platforms:
bsafePlatformSupportHandler: ["iPad", "Android", "iPhone"]
discountPlatformSupportHandler: ["Android", "iPhone"]
Then you can create config class to read this:
#Configuration
#EnableConfigurationProperties
#ConfigurationProperties
public class Config {
private Map<String, List<String>> platforms = new HashMap<String, List<String>>();
// getters and setters
You can than create handler with checking code.
Or place it in your filter like below:
#Autowired
private Config config;
...
public boolean isPlatformSupported(String subProductType, String platform) {
String key = subProductType + Constants.PLATFORM_SUPPORT_HANDLER_APPEND;
return config.getPlatforms()
.getOrDefault(key, Collections.emptyList())
.contains(platform);
}

how to create multiple instance of same type using google Guice

We have used Google Guice framework for dependency injection. I need to create multiple insatnce of an interface in java.
The execution starts from here: KfhRecordValidator.java class in the below code:
public class KfhRecordValidator implements RequestHandler<Request, Response> {
public Response handleRequest(Request request, Context context)
{
// Resolve the necessary dependencies, and process the request.
Injector injector = Guice.createInjector(new
DependencyModule());
Processor processor =
injector.getInstance(Processor.class);
return processor.process(request, context);
}}
The process class is having reference of RecordValidationHelper class and the injection is through constructor.
IRecordValidationService.java is an interface that is having validate method.
public interface IRecordValidationService {
void validate(Record record) throws ValidationException;}
class processor is having one method called process that is being called in RecordValidationHelper class.
class Processor {
private final RecordValidationHelper recordValidationHelper;
#Inject
#SuppressWarnings({"WeakerAccess"})
public Processor(IRecordValidationService recordValidationService,
IRecordService<ErrorRecord> recordService,
S3UtilsInterface s3Utils, IEnvironmentVariableReader
environmentVariableReader) {
this.recordValidationHelper = new
RecordValidationHelper(recordValidationService);
this.errorRecordHelper = new
ErrorRecordHelper(recordService, environmentVariableReader);
}
public Response process(Request request, #SuppressWarnings("unused") Context context) {
// Validate records
List<LambdaRecord> records = recordValidationHelper.processRecords(request.getRecords());}
Class DependencyModule.java extneds AbstractModule class of Guice injection that is having configure method.
class DependencyModule extends AbstractModule {
#Override
protected void configure() {
String validationType = System.getenv("ValidationType");
validationType= validationType.toLowerCase(Locale.ENGLISH);
String valType[]= validationType.split(",");
int length= valType.length;
for(int i=0;i<length;i++){
switch(valType[i]){
case "json":
bind(IRecordValidationService.class).to(JsonValidationService.class);
break;
case "avro":
bind(IRecordValidationService.class).to(AvroSchemaValidationService.class);
break;
case "clientlogging":
bind(IRecordValidationService.class).to(ClientLoggingValidationService.class);
break;
case "servicelogs":
bind(IRecordValidationService.class).to(ServiceLoggingValidationService.class);
break;
default:
throw new UnsupportedOperationException(String.format("Encountered an unsupported ValidationType of '%s'.", valType[i]));
}
} } }
SO the issue is if I am getting validation type as AVRO, JSON then it will bind IRecordValidationService to respective JsonValidationService/AvroSchemaValidationService class. I need to create multiple instance for that but it supports only once instance at a time.
Below is the RecordValidationHelper.java class
public class RecordValidationHelper extends AbstractModule {
private final IRecordValidationService recordValidationService;
#Inject
public RecordValidationHelper(IRecordValidationService recordValidationService) {
this.recordValidationService = recordValidationService;
}
public List processRecords(List requestRecords) {
List records = new ArrayList<>();
for (RequestRecord record : requestRecords) {
try {
Record domainRecord = new Record();
domainRecord.setKey(record.getRecordId());
domainRecord.setValue(new String(Base64.getDecoder().decode(record.getData())));
// Use the injected logic to validate the record.
((IRecordValidationService)
recordValidationService).validate(domainRecord);}
catch (ValidationException ex) {}}}
return records;}
Anyone having any idea about how it should be implemented to get multiple instance suitable for this.
Use #Named bindings
In your DependencyModule, bind using names:
bind(IRecordValidationService.class)
.annotatedWith(Names.named("json"))
.to(JsonValidationService.class);
bind(IRecordValidationService.class)
.annotatedWith(Names.named("avro"))
.to(AvroSchemaValidationService.class);
bind(IRecordValidationService.class)
.annotatedWith(Names.named("clientlogging"))
.to(ClientLoggingValidationService.class);
bind(IRecordValidationService.class)
.annotatedWith(Names.named("servicelogs"))
.to(ServiceLoggingValidationService.class);
Then in your injectee:
#Inject
public RecordValidationHelper(
#Named("json") IRecordValidationService jsonValidation,
#Named("avro") IRecordValidationService avroValidation,
#Named("clientlogging") IRecordValidationService clientLoggingValidation,
#Named("servicelogs") IRecordValidationService serviceLogsValidation,
) {
this.jsonValidation = jsonValidation;
this.avroValidation = avroValidation;
this.clientLoggingValidation = clientLoggingValidation;
this.serviceLogsValidation = serviceLogsValidation;
}
See Guice's BindingAnnotation wiki page for more info.

Best way for imlement Strategy design pattern in Spring

I want implement strategy design pattern in spring boot application. I create BeanPostProcessor for construct strategy resolver:
#Component
public class HandlerInAnnotationBeanPostProcessor implements BeanPostProcessor {
private final UnpHandlersResolver unpHandlersResolver;
public HandlerInAnnotationBeanPostProcessor(UnpHandlersResolver unpHandlersResolver) {
this.unpHandlersResolver = unpHandlersResolver;
}
#Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
Annotation[] annotations = bean.getClass().getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof HandlerIn) {
if (bean.getClass() != UnpHandler.class)
throw new RuntimeException("Not UnpHandler bean annotated by HandlerIn");
SmevMessageType[] type = ((HandlerIn) annotation).type();
for (SmevMessageType smevMessageType : type) {
unpHandlersResolver.setHandler(smevMessageType, (UnpHandler) bean);
}
}
}
return bean;
}
}
And I create resolver:
#Slf4j
#Component
public class UnpHandlersResolverImpl implements UnpHandlersResolver {
private Map<SmevMessageType, UnpHandler> map = new HashMap<>();
#Override
public void setHandler(SmevMessageType messageType, UnpHandler unpHandler) {
map.put(messageType, unpHandler);
}
#Override
public UnpHandler getUnpHandler(SmevMessageType type) {
UnpHandler sendRequestHandler = map.get(type);
if (sendRequestHandler == null)
throw new IllegalArgumentException("Invalid SendRequestHandler type: " + type);
return sendRequestHandler;
}
}
My BeanPostProcessor scan all beans with annotation HandlerIn and add to resolver's mup. I think it's wrong to do that:
unpHandlersResolver.setHandler(smevMessageType, (UnpHandler) bean);
But I not understand how can I add find beans to resolver. Before this implementation I faind beans in #Postconstruct method of resolver like:
context.getBeansWithAnnotation(HandlerIn.class);
But in this solution I have context in resolver and I think is bad.
Tell me how to properly implement what I want? In short, I want to have a set of classes that implement different behaviors. And the class that controls them. Give the class a parameter so that he chooses the right strategy and gives it to me. Like this:
Handler handler = handlersResolver.getHandler(messageType);
Result result = handler.somthing(param);
I'm going to try to make a simple example.
Interface Greeting {
void sayHello();
String getSupportedLanguage();
}
Then you have X number of implementations and you can loop through them in your "resolver"'s constructor to build the map. (I've seen this called a Proxy or a Decorator in code though, i.e. GreetingProxy or GreetingDecorator)
#Service
public GreetingResolver {
private Map<String, Greeting> languageToGreetingMap = new HashMap<>();
#Autowired
public GreetingResolver(List<Greeting> greetings) {
for (Greeting greeting : greetings) {
languageToGreetingMap.put(greeting.getSupportedLanguage(), greeting);
}
}
public void sayGreetingForLanguage(String language) {
languageToGreetingMap.get(language).sayHello();
}
}
This is a basic example of how one can do the strategy pattern in Spring. Every interface implementation of "Greeting" only knows about itself and what it can support. We then autowire all implementations in a list and loop through to create the map once and then during runtime only the relevant entry from the map in retrieved and used.
Note: this was typed "free hand" directly in the web page so please forgive any typos in the code.

Adding mapper to myBatis configuration in Java

I want to migrate from XML configuration to Java configuration.
sqlSessionFactory.getConfiguration().setEnvironment(new Environment("development", new org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory(), getDataSouroce()));
I managed to replace all <environments> section with Java configuration (I have removed <environments> from XML configuration file), but I can't get rid of:
<mappers><mapper resource="mailbox/db/map/FileMapper.xml"/> </mappers>
I tried to write:
sqlSessionFactory.getConfiguration().addMapper(FileMapper.class);
but there are exceptions:
SqlSession sqlSession = MyBatisConnectionFactory.instance.getSqlSessionFactory().openSession();
FileExample fe = new FileExample();
Criteria f = fe.createCriteria().andIdBetween(0L, 5L);
FileMapper mapper = (FileMapper) sqlSession.getMapper(FileMapper.class);
List<File> allRecords = mapper.selectByExample(fe);
// Mapped Statements collection does not contain value for mailbox.db.dao.FileMapper.selectByExample
I am using below abstract mapper factory where DbUtil.getInstance().getDataSource() and registerMappers() are the key points.
public abstract class AbstractMapperFactory implements MapperFactory {
private ThreadLocal<SqlSessionManager> sessionManagerThreadLocal = new ThreadLocal<SqlSessionManager>();
public <T> T getMapper(Class<T> clazz) throws DaoException {
if(sessionManagerThreadLocal.get() == null) {
initialize();
}
return sessionManagerThreadLocal.get().getMapper(clazz);
}
public void closeSession() {
if(sessionManagerThreadLocal.get() != null) {
sessionManagerThreadLocal.get().close();
sessionManagerThreadLocal.remove();
}
}
private void initialize() throws DaoException {
Environment environment = new Environment("env", new ManagedTransactionFactory(), DbUtil.getInstance().getDataSource());
Configuration configuration = new Configuration(environment);
registerMappers(configuration);
sessionManagerThreadLocal.set(SqlSessionManager.newInstance(new SqlSessionFactoryBuilder().build(configuration)));
}
protected abstract void registerMappers(Configuration configuration);
}
Where DbUtil.getInstance().getDataSource() is responsible for getting the java.sql.DataSource instance, whether it is managed or simple.
registerMappers() is an abstract method where subclass can register their mappers using code like below:
protected void registerMappers(Configuration configuration) {
configuration.addMapper(PartMapper.class);
configuration.addMapper(StatusMapper.class);
configuration.addMapper(NoteTypeMapper.class);
configuration.addMapper(AssetTypeMapper.class);
}

Jersey REST/ JAXB error , mapping an Interface

I have to use an interface in my REST web service. Here is the Interface Specs.java :
#XmlJavaTypeAdapter(MyAdapter.class)
public interface Specs {
public BaseProperties getBaseProps();
public void setBaseProps(BaseProperties baseProps);
}
MyAdapter.java :
public class MyAdapter extends XmlAdapter<Object,Object>
{
public Object unmarshal(Object v)
{
return v;
}
public Object marshal(Object v)
{
return v;
}
}
RegSpecs.java
#XmlType
public class RegSpecs implements Specs{
private BaseProperties baseProps;
public BaseProperties getBaseProps()
{
return baseProps;
}
public void setBaseProps(BaseProperties baseProps)
{
this.baseProps = baseProps;
}
}
MapSpecs.java
#XmlType
public class MagSpecs implements Specs {
private BaseProperties baseProps;
private Features features;
public BaseProperties getBaseProps()
{
return baseProps;
}
public void setBaseProps(BaseProperties baseProps)
{
this.baseProps = baseProps;
}
public Features getFeatures() {
return features;
}
public void setFeatures(Features features) {
this.features = features;
}
}
Accessing this service throws the following error :
javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: class entities.MagSpecs nor any of its super class is known to this context.]
How to modify my context ? I am using JAXB bundled with Jersey 1.5
Thanks !
EDIT : In an attempt to update my context, I added this code to my client (resource) class :
public class BookService implements ContextResolver<JAXBContext>
{
private JAXBContext jaxbContext;
public BookService() {
try {
// Bootstrap your JAXBContext will all necessary classes
jaxbContext = JAXBContext.newInstance(Specs.class,MagSpecs.class, RegSpecs.class);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
public JAXBContext getContext(Class<?> clazz) {
if(BookService.class == clazz) {
return jaxbContext;
}
return null;
}
In this case I get error :
entities.Specs is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at entities.Specs
entities.Specs does not have a no-arg default constructor.
this problem is related to the following location:
at entities.Specs
The client of the Specs interface needs to know that MagSpecs can be an instance of it so that it knows to look at it for tooling purposes. The easiest way of doing this is to put an #XmlSeeAlso annotation on the Specs interface:
#XmlSeeAlso({ MagSpecs.class, RegSpecs.class })
#XmlJavaTypeAdapter(MyAdapter.class) // Never needed this annotation myself...
public interface Specs {
public BaseProperties getBaseProps();
public void setBaseProps(BaseProperties baseProps);
}
In general, whenever I'm working with JAXB annotations I make sure I write plenty of tests to check that an XML schema can be generated from the classes in question, checking that from each (sane) entry point into the web of classes and interfaces I can generate a sensible schema without exceptions. For example (and I apologize for this being a bit long):
private SchemaOutputResolver sink;
StringWriter schema;
#Before
public void init() {
schema = new StringWriter();
sink = new SchemaOutputResolver() {
#Override
public Result createOutput(String namespaceUri,
String suggestedFileName) throws IOException {
StreamResult sr = new StreamResult(schema);
sr.setSystemId("/dev/null");
return sr;
}
};
Assert.assertTrue(schema.toString().isEmpty());
}
private void testJAXB(Class<?>... classes) throws Exception {
JAXBContext.newInstance(classes).generateSchema(sink);
Assert.assertTrue(schema.toString().length() > 0);
}
#Test
public void testJAXBForSpecs() throws Exception {
testJAXB(Specs.class);
}
[EDIT]: You also need to change the Specs interface into a class and have the current implementations inherit from it. It can be a fully abstract class if you want. As long as you're not putting serious functionality in the classes, it should work.
EclipseLink JAXB (MOXy) can map interfaces to XML (Note I'm the tech lead). You need to be sure to have a create method on the corresponding object factory to return a concrete impl:
http://bdoughan.blogspot.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html
MOXy integrates cleaning into REST environments:
http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-35.html

Categories

Resources