Repository navigation
WW-3476 Add package-scoped constants #2005
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
88fd4c7
WW-3476 docs: add design spec for package-scoped constants
lukaszlenart 43115aa
WW-3476 docs: add implementation plan for package-scoped constants
lukaszlenart b2a5b0d
WW-3476 feat(config): store scoped constants on PackageConfig
lukaszlenart 06d6e3c
WW-3476 feat(config): add struts-7.5.dtd with <scoped-constant>
lukaszlenart 2050da7
WW-3476 feat(config): reject unregistered scoped constants at startup
lukaszlenart 617faf1
WW-3476 feat(config): add ScopedConstantProvider resolving package ov…
lukaszlenart 545c570
WW-3476 test(config): pin ScopedConstantProvider wiring on the Dispat…
lukaszlenart 4931d34
WW-3476 test(config): drop the xwork prefix from scoped-constant fixt…
lukaszlenart 6562ac2
WW-3476 fix(config): skip ScopableConstants bean names without an ins…
lukaszlenart dc4305a
WW-3476 refactor(config): resolve Configuration and scopable names fr…
lukaszlenart 7645bc6
WW-3476 refactor(config): address Sonar findings on scoped constants
lukaszlenart 2b25184
WW-3476 test(config): name scoped-constant fixtures struts-*.xml
lukaszlenart 197f2cc
WW-3476 fix(mock): return an empty set from MockContainer.getInstance…
lukaszlenart 8a81250
WW-3476 docs(config): document ScopedConstantProvider as a bean-selec…
lukaszlenart c939805
WW-3476 refactor(config): move StrutsScopedConstantProvider to config…
lukaszlenart c8fa01c
WW-3476 refactor(config): complete the StrutsScopedConstantProvider move
lukaszlenart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
core/src/main/java/org/apache/struts2/config/ScopableConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.struts2.config; | ||
|
|
||
| import org.apache.struts2.inject.Container; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Declares constants a package may override with {@code <scoped-constant>}. Register implementations as named beans; | ||
| * the scopable set is the union over all of them. | ||
| * | ||
| * @since 7.5.0 | ||
| */ | ||
| public interface ScopableConstants { | ||
|
|
||
| Set<String> getNames(); | ||
|
|
||
| static Set<String> collectNames(Container container) { | ||
| Set<String> names = new HashSet<>(); | ||
| for (String beanName : container.getInstanceNames(ScopableConstants.class)) { | ||
| ScopableConstants scopableConstants = container.getInstance(ScopableConstants.class, beanName); | ||
| if (scopableConstants != null) { | ||
| names.addAll(scopableConstants.getNames()); | ||
| } | ||
| } | ||
| return Collections.unmodifiableSet(names); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
core/src/main/java/org/apache/struts2/config/ScopedConstantProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.struts2.config; | ||
|
|
||
| /** | ||
| * Resolves a scopable constant for the current action invocation: the package's {@code <scoped-constant>}, inherited | ||
| * through {@code extends}, or else the global constant. | ||
| * | ||
| * @since 7.5.0 | ||
| */ | ||
| public interface ScopedConstantProvider { | ||
|
|
||
| /** | ||
| * @param name a constant registered through a {@link ScopableConstants} bean | ||
| * @return the scoped value, or the global value ({@code null} when there is none) | ||
| * @throws IllegalArgumentException when {@code name} is not scopable | ||
| */ | ||
| String getValue(String name); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/java/org/apache/struts2/config/providers/StrutsScopedConstantProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.struts2.config.providers; | ||
|
|
||
| import org.apache.struts2.ActionContext; | ||
| import org.apache.struts2.ActionInvocation; | ||
| import org.apache.struts2.ActionProxy; | ||
| import org.apache.struts2.config.Configuration; | ||
| import org.apache.struts2.config.ScopableConstants; | ||
| import org.apache.struts2.config.ScopedConstantProvider; | ||
| import org.apache.struts2.config.entities.PackageConfig; | ||
| import org.apache.struts2.inject.Container; | ||
| import org.apache.struts2.inject.Inject; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * @since 7.5.0 | ||
| */ | ||
| public class StrutsScopedConstantProvider implements ScopedConstantProvider { | ||
|
|
||
| private Container container; | ||
| private Configuration configuration; | ||
| private Set<String> scopableNames; | ||
|
|
||
| @Inject | ||
| public void setContainer(Container container) { | ||
| this.container = container; | ||
| this.configuration = container.getInstance(Configuration.class); | ||
| this.scopableNames = ScopableConstants.collectNames(container); | ||
| } | ||
|
lukaszlenart marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public String getValue(String name) { | ||
| if (!scopableNames.contains(name)) { | ||
| throw new IllegalArgumentException(String.format( | ||
| "Constant [%s] is not scopable; register it through a %s bean", name, ScopableConstants.class.getName())); | ||
| } | ||
| return currentPackage() | ||
| .map(packageConfig -> packageConfig.getAllScopedConstants().get(name)) | ||
| .orElseGet(() -> container.getInstance(String.class, name)); | ||
| } | ||
|
|
||
| private Optional<PackageConfig> currentPackage() { | ||
| ActionContext context = ActionContext.getContext(); | ||
| ActionInvocation invocation = context != null ? context.getActionInvocation() : null; | ||
| ActionProxy proxy = invocation != null ? invocation.getProxy() : null; | ||
| if (proxy == null || proxy.getConfig() == null) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.ofNullable(configuration.getPackageConfig(proxy.getConfig().getPackageName())); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.