Skip to content

Significantly improved performance of DocumentUtilities #12#13

Merged
nemonik merged 3 commits intoSTIXProject:masterfrom
manonthegithub:wip-bugfix-#12-cached-jaxb-context
Aug 18, 2017
Merged

Significantly improved performance of DocumentUtilities #12#13
nemonik merged 3 commits intoSTIXProject:masterfrom
manonthegithub:wip-bugfix-#12-cached-jaxb-context

Conversation

@manonthegithub
Copy link
Contributor

Refs #12

@manonthegithub
Copy link
Contributor Author

manonthegithub commented Aug 16, 2017

You may check performance just running existing unit tests. Initialisation takes the same time but other tests are 10-20 times faster than before.

private static final String XML_SCHEMA_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance";
private static final String XML_NAMESPACE = "http://www.w3.org/2000/xmlns/";

private static final JAXBContext STIX_CONTEXT = initDefaultContext();
Copy link
Contributor Author

@manonthegithub manonthegithub Aug 17, 2017

Choose a reason for hiding this comment

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

It can be initialized lazily, if needed. @nemonik WDYT?

@gtback
Copy link
Contributor

gtback commented Aug 17, 2017

@nemonik can you look at this and merge if you think it's OK? You should have permission to merge it, but if not, let me know.


private static JAXBContext initDefaultContext(){
try {
return JAXBContext.newInstance("org.mitre.stix.stix_1");

Choose a reason for hiding this comment

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

This JAXBContext instance seems very coupled to just the STIX_1 namespace. I noticed that other generated binding classes use the same DocumentUtilitites.toDocument() method (i.e. types org.mitre.cybox.cybox_2).

Will this JAXBContext still work in the case where the code is just calling the toDocument method on the cybox object? The cybox binding is under the package namespace that starts with "org.mitre" but is not "org.mitre.stix.stix_1".

Copy link
Contributor Author

@manonthegithub manonthegithub Aug 17, 2017

Choose a reason for hiding this comment

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

It should work. STIXPackage is the most general container that comprises all other types in org.mitre, and it is in org.mitre.stix.stix_1. So when JAXBContext is initialised it should scan all the types in org.mitre.stix.stix_1 and all other types that are referenced by STIXPackage recursively and irrespectively to the initial package. So all types in full org.mitre are in the context. Please let me know if I'm wrong, because I'm not an expert in JAXB. The unit tests which construct STIXPackage with cybox contents and then invoke toDocument in the project would probably fail if I was wrong.

Choose a reason for hiding this comment

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

I do not appear to be as familiar with the JAXB contextPath's class scanning and loading process as you. I do agree with your point on the "org.mitre" and working with the simple vanilla STIX package and cybox indicator element.

I'm not a maintainer and/or familiar with the STIX v1 binding jar as a whole. Inspecting the built binding jar and the generated classes, there are definitely other package namespaces that do not start with "org.mitre" and their classes use/call the DocumentUtilitites.toDocument() utility method. Theoretically, they could possibly fall into the same issue of always instantiating a new JAXBContext.

This comes back to the possible recommendation of DocumentUtilities just maintaining an internal lazy loaded cache of called upon JAXBContext objects for marshaling. This approach would make that one line of code not so coupled/fixed to the context namespace of "org.mitre.stix.stix_1" and starts with "org.mitre" condition. This is probably minor, but just a suggestion to cover other unexpected/possible usage of this binding jar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think these elements from other packages will be (often) directly converted with toDocument, I regard it as unlikely case. To me it is much more sensible to use it with STIXPackage. As for me, I think, that some more comments should be added to this method, preventing users from converting any possible element with it.

private static final String XML_SCHEMA_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance";
private static final String XML_NAMESPACE = "http://www.w3.org/2000/xmlns/";

public static JAXBContext stixJaxbContext() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made this method public intentionally, because I lacked it in my project and find it useful for users who want more control over marshalling/unmarshalling.

@nemonik nemonik merged commit 43aee99 into STIXProject:master Aug 18, 2017
@manonthegithub manonthegithub deleted the wip-bugfix-#12-cached-jaxb-context branch August 18, 2017 10:00
@manonthegithub
Copy link
Contributor Author

Can you guys please release the latest changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants