-
Notifications
You must be signed in to change notification settings - Fork 103
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
[JEP-216] Add SPI for localized resource lookup #156
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2019 Daniel Beck | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided | ||
* that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this list of | ||
* conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright notice, this list of | ||
* conditions and the following disclaimer in the documentation and/or other materials | ||
* provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS | ||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS | ||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package org.kohsuke.stapler; | ||
|
||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Service provider interface allowing to hook into webapp resource lookup. | ||
* | ||
* This cannot be made a property of WebApp as other behavior customizations, as webapp resource lookup is done before we have StaplerRequest/StaplerResponse. | ||
*/ | ||
public abstract class LocaleDrivenResourceProvider { | ||
/** | ||
* Returns the URL corresponding to the specified resource path. | ||
* | ||
* The path can take two forms: | ||
* | ||
* <ul> | ||
* <li>A full URL represented as string. In Jenkins this is typically plugin webapp resources.</li> | ||
* <li>A path with leading slash. These are actual webapp resources, with that being the implicit base directory.</li> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A potentially better design would be to have independent methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't figure out the use cases. The interface method may not need to take care of those details. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm generally in favor of using explicitly named separate methods when there is a clear separation of responsibility or implementation. In this case, though, I'm not sure that separation exists. Both forms are valid types of URLs. Is there some reason we think the caller or implementor might need to handle these two forms differently or separately? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's most of what https://github.com/daniel-beck/localization-support-plugin/blob/master/src/main/java/io/jenkins/plugins/localization/support/stapler/LocaleDrivenResourceProviderImpl.java does. "From a plugin or not" is the same distinction as the two variants documented here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note that the second one (plugin resources) is just |
||
* </ul> | ||
* | ||
* @param path the path to the resource | ||
* @return the URL to the file, if it is to be overridden, null otherwise. | ||
*/ | ||
@CheckForNull | ||
abstract public URL lookup(@Nonnull String path); | ||
|
||
private static List<LocaleDrivenResourceProvider> localeDrivenResourceProviders; | ||
|
||
private static final Logger LOGGER = Logger.getLogger(LocaleDrivenResourceProvider.class.getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: field declarations should be done before methods declarations |
||
|
||
private static synchronized List<LocaleDrivenResourceProvider> getLocaleDrivenResourceProviders() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can limit the synchronization to after you check for null (but then check for null again). Make the field volatile and you've got a threadsafe lazy initialized value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going with @jglick's verbatim advice from an earlier equivalent change here, and not touching this 😃 |
||
if (localeDrivenResourceProviders == null) { | ||
localeDrivenResourceProviders = new ArrayList<>(); | ||
for (LocaleDrivenResourceProvider provider : ServiceLoader.load(LocaleDrivenResourceProvider.class)) { | ||
localeDrivenResourceProviders.add(provider); | ||
LOGGER.log(Level.INFO, "Registered LocaleDrivenResourceProvider: " + provider); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably could be made |
||
} | ||
} | ||
return localeDrivenResourceProviders; | ||
} | ||
|
||
/* package */ static URL lookupResource(String path) { | ||
for (LocaleDrivenResourceProvider provider : getLocaleDrivenResourceProviders()) { | ||
try { | ||
URL url = provider.lookup(path); | ||
if (url != null) { | ||
return url; | ||
} | ||
} catch (Exception e) { | ||
LOGGER.log(Level.WARNING, "Failed to look up URL for " + path + " from " + provider, e); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the
implements ExtensionPoint
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't, that's in Jenkins.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also https://github.com/jenkinsci/jenkins/pull/3729/files#diff-ca4047332ddf7ab84f19e41ee830640eR40