Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

modified JaxpRequest and JaxpResponse's load method #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

duanshiqiang
Copy link

JaxpRequest and JaxpResponse both have a static method "JaxpRequest load(File fileXmlRequest)" to load a xml file to a corresponding object. The load methods uses JAXB to unmarshal the XML.

When using JAXB to unmarshal XML, it's very expensive to create a JaxbContext, so I modified the methods to use a private static attribute to store and re-use a single JaxbContext.

The load method returns null if the XML's first node is a comment node, I fixed that also.

Node nodeRoot = document.getDocumentElement();

if(context == null) {
init();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding separate method, just call in context = JAXBContext.newInstance(RequestType.class); here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because every call to JAXBContext.newInstance will create a new instance of JAXBContext which is very expensive. According to Unofficial JAXB Guide - Performance and thread-safety — Project Kenai, JAXBContext is thread safe and there should be only 1 instance for the whole application.

Also Blaise Doughan's stackover flow answer supports this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What i was saying, you already have a state defined context of a class JaxpRequest , what i don't like you are modifying state of this class, better create separate Singelton class and JaxpRequest will get instance of a context from that singelton class (global access) would be easier for testing aslo. Although, singleton is a bad practice but you need some sort of life cyle management outside request level.

I don't like you are adding init method in the request level which should be outside request management. I don't like another aspect of adding synchronization in the code.


JAXBContext context = JAXBContext.newInstance(ResponseType.class);
if(context == null) {
init();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

@duanshiqiang
Copy link
Author

The init method to create context object is removed. I agree having a separate Singleton class would be better but I think in this case it would be a little overkill.

Node nodeRoot = document.getDocumentElement();

if(context == null) {
synchronized (JaxpRequest.class) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you are breaking DRY principle, again -1 for that. Thing, again to make it better, separate class is always a good option.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come? The two context in JaxpRequest nad JaxpResponse are different.



private static JAXBContext context = null;

protected JaxpResponse() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is protected ?


JAXBContext context = JAXBContext.newInstance(ResponseType.class);
if(context == null) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont need all this just do this

static {
        try {
            // one time instance creation
            jaxbContext = JAXBContext.newInstance(Request.class, Response.class);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

for both request and respone in a singleton class and reuse as required.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do this, jaxbContext will be initialized at application boot time no matter user need to use load method or not. Put it into the load method essentially lazy load it to save some memory.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok, it is a trade off better than having two places with synchronized block. You need it anyhow definitely.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel it's good, we use the JaxpResponse to load XACML response string to an Response object and we construct the ResponseType object by our own unmarshal code. So it make sense to use the class without using it's own load method.

Copy link

@jainh jainh Oct 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is your own unmarshal code ? it is being performed here inside the response/request class. I totally see scalability issue in this patch and not comfortable in giving thumbs up, What you need is single instance of context (which is a good thought) but in code you are doing it at two different places pretty much in a similar way just flipping class type. If you really need to parameterize class type, again create singleton class and initialize it with custom class type (request or response). But, don't patch or modify existing class with synchronized block.

IMHO, we should avoid synchronization, whenever possible and have things thread local and immutable.

@jainh
Copy link

jainh commented Oct 21, 2016

@duanshiqiang : I have a question probably tangential to your pull request, are you using this Xacml implementation ? Can you give me a few more information as follows

  1. How much complete implementation is this source code ?
  2. Does it have Xacml 3.0 implementation ?
  3. Where did you find the documentation around it ?

Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants