Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FP] ServiceContext fixes #6051

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

josegar74
Copy link
Member

@josegar74 josegar74 added this to the 4.0.6 milestone Nov 22, 2021
- Keep service context independent to address user session null pointer exceptions (geonetwork#5507)
- Review and document use of ServiceContext (geonetwork#5540)
Copy link
Contributor

@jodygarnett jodygarnett left a comment

Choose a reason for hiding this comment

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

Jose this change captures the changes made on 3.10.x thank you.

There are some suggestions on tightening up the API which should be done as a subsequent change.

*/
public class ServiceContext extends BasicContext {
public class ServiceContext extends BasicContext implements AutoCloseable {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good and is the key change, the above javadoc code example could include a try-with-resources example.

 * ServiceContext is auto closable for use with try-with-resource syntaxt:
 * <pre></code>
 * try (ServiceContext context = serviceMan.createServiceContext("AppHandler", appContext)){
 *     ...
 * }
 * </code></pre>

There are other javadocs with this example, but it is nice to call it out in the class description.

* is used during Jeeves startup and has some protection from being cleared. Additional managers such HarvestManager
* also use a shared service context to support background processes.
*/
public static class AppHandlerServiceContext extends ServiceContext {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion only: Public static class could be broken out as its own top-level class, similar to BasicContext.

*/
private static final ThreadLocalPolicy POLICY;
static {
String property = System.getProperty("jeeves.server.context.policy", "TRACE");
Copy link
Contributor

Choose a reason for hiding this comment

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

I was only willing to default to TRACE for maintenance branch, on main we could consider defaulting to STRICT (at least for local development?).

/**
* Language code, or <code>"?"</code> if undefined.
* @param lang language code, or <code>"?"</code> if undefined.
*/
public void setLanguage(final String lang) {
_language = lang;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_language = lang;
if( lang == null ){
lang = "?";
}
_language = lang;

* IP address of request, or <code>"?"</code> for local loopback request.
*
* @param address ip, address or <code>"?"</code> for loopback request.
*/
public void setIpAddress(final String address) {
_ipAddress = address;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_ipAddress = address;
if( address == null ){
address = "?";
}
_ipAddress = address;

// session.loginAs(new User().setUsername("admin").setId(-1).setProfile(Profile.Administrator));
// LOGGER_DATA_MANAGER.debug("Hopefully this is cron job or routinely background task. Who called us?",
// new Exception("Dummy Exception to know the stacktrace"));
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

This could should just be removed, or if we like on main a failure could be raised.

if (context.getUserSession() == null) {
   throw new IllegalStateException( "Anonymous data manager access not available");
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I was being so careful not to break existing workflow I did not introduce any additional strict checking.

Comment on lines +264 to +268
try (ServiceContext context = ApiUtils.createServiceContext(httpRequest)) {
Locale locale = languageUtils.parseAcceptLanguage(request.getLocales());
String language = isoLanguagesMapper.iso639_2T_to_iso639_2B(locale.getISO3Language());
return translationPackBuilder.getPack(language, pack, context);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
try (ServiceContext context = ApiUtils.createServiceContext(httpRequest)) {
Locale locale = languageUtils.parseAcceptLanguage(request.getLocales());
String language = isoLanguagesMapper.iso639_2T_to_iso639_2B(locale.getISO3Language());
return translationPackBuilder.getPack(language, pack, context);
}
try (ServiceContext context = ApiUtils.createServiceContext(httpRequest)) {
Locale locale = languageUtils.parseAcceptLanguage(request.getLocales());
String language = isoLanguagesMapper.iso639_2T_to_iso639_2B(locale.getISO3Language());
return translationPackBuilder.getPack(language, pack, context);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Outdenting the try / finally statement by two characters can vastly reduce the number of lines in the diff as the origional logic remains unchanged.

@jodygarnett
Copy link
Contributor

@josegar74 I resolved the outstanding merge conflicts, please check.

@jahow jahow modified the milestones: 4.0.6, 4.0.7 Feb 2, 2022
@fxprunayre fxprunayre modified the milestones: 4.2.0, 4.2.1 May 19, 2022
@fxprunayre fxprunayre modified the milestones: 4.2.1, 4.2.2 Sep 12, 2022
@fxprunayre fxprunayre modified the milestones: 4.2.2, 4.2.3 Dec 8, 2022
@fxprunayre fxprunayre modified the milestones: 4.2.3, 4.2.4 Mar 7, 2023
@fxprunayre fxprunayre modified the milestones: 4.2.4, 4.2.5 May 10, 2023
@fxprunayre fxprunayre modified the milestones: 4.2.5, 4.2.6 Jul 5, 2023
@fxprunayre fxprunayre modified the milestones: 4.2.6, 4.4.1 Oct 4, 2023
@fxprunayre fxprunayre modified the milestones: 4.4.1, 4.4.2 Nov 22, 2023
@fxprunayre fxprunayre removed this from the 4.4.2 milestone Jan 23, 2024
@fxprunayre fxprunayre added this to the 4.4.3 milestone Jan 23, 2024
@fxprunayre fxprunayre modified the milestones: 4.4.3, 4.4.4 Mar 13, 2024
@fxprunayre fxprunayre modified the milestones: 4.4.4, 4.4.5 Apr 16, 2024
@fxprunayre fxprunayre modified the milestones: 4.4.5, 4.4.6 Jun 4, 2024
@fxprunayre fxprunayre modified the milestones: 4.4.6, 4.4.7 Oct 15, 2024
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@jodygarnett
Copy link
Contributor

@josegar74 while I provided feedback above this PR remains open. May it be closed?

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.

5 participants