Skip to content
Permalink
Browse files

Improved: Use ‘Path’ for component config ‘rootLocation’ attribute

(OFBIZ-11192)

The method ‘ComponentConfig#getRootLocation’ has been removed
and replaced by ‘ComponentConfig#rootLocation’.

The callers have been adapted.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1868028 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 5, 2019
1 parent c982df7 commit 60f7041c3d067f1500f2f00130a7ddedd259b8d4
@@ -18,9 +18,11 @@
*******************************************************************************/
package org.apache.ofbiz.base.component;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Collection;
@@ -226,7 +228,7 @@ public static KeystoreInfo getKeystoreInfo(String componentName, String keystore

public static String getRootLocation(String componentName) throws ComponentException {
ComponentConfig cc = getComponentConfig(componentName);
return cc.getRootLocation();
return cc.rootLocation().toString();
}

public static InputStream getStream(String componentName, String resourceLoaderName, String location) throws ComponentException {
@@ -257,7 +259,7 @@ public static WebappInfo getWebAppInfo(String serverName, String contextRoot) {
// ========== ComponentConfig instance ==========

private final String globalName;
private final String rootLocation;
private final Path rootLocation;
private final String componentName;
private final boolean enabled;
private final Map<String, ResourceLoaderInfo> resourceLoaderInfos;
@@ -280,7 +282,8 @@ public static WebappInfo getWebAppInfo(String serverName, String contextRoot) {
*/
private ComponentConfig(Builder b) {
this.globalName = b.globalName;
this.rootLocation = b.rootLocation;
String rootLocation = (b.rootLocation == null) ? "" : b.rootLocation;
this.rootLocation = Paths.get(rootLocation.replace('\\', '/')).normalize().toAbsolutePath();
this.componentName = b.componentName;
this.enabled = b.enabled;
this.resourceLoaderInfos = b.resourceLoaderInfos;
@@ -392,17 +395,13 @@ public ComponentConfig create() {
* @throws NullPointerException when {@code rootLocation} is {@code null}
*/
private ComponentConfig(String globalName, String rootLocation) throws ComponentException {
if (!rootLocation.endsWith("/")) {
rootLocation = rootLocation + "/";
}
this.rootLocation = rootLocation.replace('\\', '/');
File rootLocationDir = new File(rootLocation);
if (!rootLocationDir.exists()) {
this.rootLocation = Paths.get(rootLocation.replace('\\', '/')).normalize().toAbsolutePath();
if (Files.notExists(this.rootLocation)) {
throw new ComponentException("The component root location does not exist: " + rootLocation);
} else if (!rootLocationDir.isDirectory()) {
} else if (!Files.isDirectory(this.rootLocation)) {
throw new ComponentException("The component root location is not a directory: " + rootLocation);
}
String xmlFilename = this.rootLocation + "/" + OFBIZ_COMPONENT_XML_FILENAME;
String xmlFilename = this.rootLocation.resolve(OFBIZ_COMPONENT_XML_FILENAME).toString();
URL xmlUrl = UtilURL.fromFilename(xmlFilename);
if (xmlUrl == null) {
throw new ComponentException("Could not find the " + OFBIZ_COMPONENT_XML_FILENAME
@@ -486,6 +485,7 @@ public String getFullLocation(String resourceLoaderName, String location) throws
// pre-pend component root location if this is a type component resource-loader
if ("component".equals(resourceLoaderInfo.type)) {
buf.append(rootLocation);
buf.append('/');
}

if (UtilValidate.isNotEmpty(resourceLoaderInfo.prependEnv)) {
@@ -520,8 +520,13 @@ public String getGlobalName() {
return this.resourceLoaderInfos;
}

public String getRootLocation() {
return this.rootLocation;
/**
* Provides the root location of the component definition.
*
* @return a normalized absolute path
*/
public Path rootLocation() {
return rootLocation;
}

public List<ServiceResourceInfo> getServiceResourceInfos() {
@@ -633,7 +638,7 @@ private synchronized ComponentConfig fromRootLocation(String rootLocation) {

private synchronized ComponentConfig put(ComponentConfig config) {
String globalName = config.getGlobalName();
String fileLocation = config.getRootLocation();
String fileLocation = config.rootLocation().toString();
componentLocations.put(fileLocation, globalName);
return componentConfigs.put(globalName, config);
}
@@ -1070,7 +1075,7 @@ public String getDescription() {
}

public String getLocation() {
return componentConfig.getRootLocation() + location;
return componentConfig.rootLocation().resolve(location).toString();
}

public String getName() {
@@ -25,7 +25,6 @@
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -335,8 +334,7 @@ private void loadSingleComponent(ComponentConfig config) throws IOException, Com
*/
private static Classpath buildClasspathFromComponentConfig(ComponentConfig config) throws IOException {
Classpath classPath = new Classpath();
String configRoot = config.getRootLocation().replace('\\', '/');
configRoot = configRoot.endsWith("/") ? configRoot : configRoot + "/";
Path configRoot = config.rootLocation();
List<ComponentConfig.ClasspathInfo> classpathInfos = config.getClasspathInfos();

for (ComponentConfig.ClasspathInfo cp: classpathInfos) {
@@ -348,7 +346,7 @@ private static Classpath buildClasspathFromComponentConfig(ComponentConfig confi

location = location.startsWith("/") ? location.substring(1) : location;
String dirLoc = location.endsWith("/*") ? location.substring(0, location.length() - 2) : location;
Path path = Paths.get(configRoot + dirLoc).toAbsolutePath().normalize();
Path path = configRoot.resolve(dirLoc).normalize();
if (Files.exists(path)) {
classPath.add(path, cp.type);
} else {
@@ -19,6 +19,8 @@
package org.apache.ofbiz.base.util;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@@ -100,32 +102,24 @@ public static String findRealPathAndFileForClass(String fullyQualifiedClassName)

Collection<ComponentConfig> allComponentConfigs = ComponentConfig.getAllComponents();
for (ComponentConfig cc: allComponentConfigs) {
String rootDirectory = cc.getRootLocation();
if (!rootDirectory.endsWith(File.separatorChar + "")) {
rootDirectory += File.separatorChar;
}
rootDirectory += "src" + File.separatorChar;

File rootDirFile = new File(rootDirectory);
if (!rootDirFile.exists()) {
Path rootDirectory = cc.rootLocation().resolve("src");
if (Files.notExists(rootDirectory)) {
// no src directory, move along
continue;
}

String classDir = rootDirectory + sourceSubPath;
File classDirFile = new File(classDir);
if (!classDirFile.exists()) {
Path classDir = rootDirectory.resolve(sourceSubPath);
if (Files.notExists(classDir)) {
// no src class sub-directory, move along
continue;
}

String fullPathAndFile = classDir + File.separatorChar + classFileName;
File classFile = new File(fullPathAndFile);
if (classFile.exists()) {
Path fullPathAndFile = classDir.resolve(classFileName);
if (Files.notExists(fullPathAndFile)) {
if (Debug.verboseOn()) {
Debug.logVerbose("In findRealPathAndFileForClass for [" + fullyQualifiedClassName + "]: [" + fullPathAndFile + "]", module);
}
return fullPathAndFile;
return fullPathAndFile.toString();
}
}

@@ -561,12 +561,10 @@ private static StandardContext prepareContext(Host host, ContainerConfig.Configu
}

private static String getWebappRootLocation(ComponentConfig.WebappInfo appInfo) {
String location = appInfo.componentConfig.getRootLocation() + appInfo.location;
location = location.replace('\\', '/');
if (location.endsWith("/")) {
location = location.substring(0, location.length() - 1);
}
return location;
return appInfo.componentConfig.rootLocation()
.resolve(appInfo.location.replace('\\', '/'))
.normalize()
.toString();
}

private static String getWebappMountPoint(ComponentConfig.WebappInfo appInfo) {
@@ -279,7 +279,7 @@ private static void createOrUpdateComponentEntities(Delegator baseDelegator,
for (ComponentConfig config : allComponents) {
GenericValue componentEntry = baseDelegator.makeValue("Component");
componentEntry.set("componentName", config.getComponentName());
componentEntry.set("rootLocation", config.getRootLocation());
componentEntry.set("rootLocation", config.rootLocation().toString());
try {
GenericValue componentCheck = EntityQuery.use(baseDelegator)
.from("Component")
@@ -33,7 +33,7 @@ components.each { component ->
webApps.each { webApp ->
componentMap = [:]
componentMap.compName = component.getComponentName()
componentMap.rootLocation = component.getRootLocation()
componentMap.rootLocation = component.rootLocation().toString()
componentMap.enabled = (component.enabled() == true? "Y" : "N")
componentMap.webAppName = webApp.getName()
componentMap.contextRoot = webApp.getContextRoot()
@@ -46,7 +46,7 @@ components.each { component ->
if (!webApps) {
componentMap = [:]
componentMap.compName = component.getComponentName()
componentMap.rootLocation = component.getRootLocation()
componentMap.rootLocation = component.rootLocation().toString()
componentMap.enabled = (component.enabled() == true? "Y" : "N")
componentList.add(componentMap)
componentMap.webAppName = ""
@@ -395,7 +395,7 @@ public ArtifactInfoBase getArtifactInfoByNameAndType(String artifactName, String
private Callable<Void> prepareTaskForComponentAnalysis(final ComponentConfig componentConfig) {
return () -> {
String componentName = componentConfig.getGlobalName();
String rootComponentPath = componentConfig.getRootLocation();
String rootComponentPath = componentConfig.rootLocation().toString();
List<File> screenFiles = new ArrayList<>();
List<File> formFiles = new ArrayList<>();
List<File> controllerFiles = new ArrayList<>();
@@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
@@ -84,16 +85,13 @@ protected static void loadLabelFiles() throws IOException {
List<ClasspathInfo> cpInfos = ComponentConfig.getAllClasspathInfos();
for (ClasspathInfo cpi : cpInfos) {
if ("dir".equals(cpi.type)) {
String configRoot = cpi.componentConfig.getRootLocation();
configRoot = configRoot.replace('\\', '/');
if (!configRoot.endsWith("/")) {
configRoot = configRoot + "/";
}
Path configRoot = cpi.componentConfig.rootLocation();
String location = cpi.location.replace('\\', '/');
if (location.startsWith("/")) {
location = location.substring(1);
}
List<File> resourceFiles = FileUtil.findXmlFiles(configRoot + location, null, "resource", null);
Path fullLocation = configRoot.resolve(location);
List<File> resourceFiles = FileUtil.findXmlFiles(fullLocation.toString(), null, "resource", null);
for (File resourceFile : resourceFiles) {
filesFound.put(resourceFile.getName(), new LabelFile(resourceFile, cpi.componentConfig.getComponentName()));
}
@@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
@@ -67,7 +68,7 @@
protected DispatchContext dispatchContext;
protected Map<String, LabelInfo> labels;
protected Set<String> labelSet = new HashSet<>();
protected Set<String> rootFolders = new HashSet<>();
protected Set<Path> rootFolders = new HashSet<>();

public LabelReferences(Delegator delegator, LabelManagerFactory factory) {
this.delegator = delegator;
@@ -93,11 +94,7 @@ public LabelReferences(Delegator delegator, LabelManagerFactory factory) {
}
Collection<ComponentConfig> componentConfigs = ComponentConfig.getAllComponents();
for (ComponentConfig config : componentConfigs) {
String rootFolder = config.getRootLocation();
rootFolder = rootFolder.replace('\\', '/');
if (!rootFolder.endsWith("/")) {
rootFolder = rootFolder + "/";
}
Path rootFolder = config.rootLocation();
this.rootFolders.add(rootFolder);
}
}
@@ -117,9 +114,9 @@ public LabelReferences(Delegator delegator, LabelManagerFactory factory) {
getLabelsFromSimpleMethodFiles();
// get labels from widgets files
List<File> fileList = new LinkedList<>();
for (String rootFolder : this.rootFolders) {
fileList.addAll(FileUtil.findXmlFiles(rootFolder + "webapp", null, null, null));
fileList.addAll(FileUtil.findXmlFiles(rootFolder + "widget", null, null, null));
for (Path rootFolder : this.rootFolders) {
fileList.addAll(FileUtil.findXmlFiles(rootFolder.resolve("webapp").toString(), null, null, null));
fileList.addAll(FileUtil.findXmlFiles(rootFolder.resolve("widget").toString(), null, null, null));
}
for (File file : fileList) {
String inFile = FileUtil.readString("UTF-8", file);
@@ -159,8 +156,8 @@ private void setLabelReference(String labelKey, String filePath) {
}

private void getLabelsFromFtlFiles() throws IOException {
for (String rootFolder : this.rootFolders) {
List<File> ftlFiles = FileUtil.findFiles("ftl", rootFolder, null, null);
for (Path rootFolder : this.rootFolders) {
List<File> ftlFiles = FileUtil.findFiles("ftl", rootFolder.toString(), null, null);
for (File file : ftlFiles) {
String inFile = FileUtil.readString("UTF-8", file);
inFile = inFile.replaceAll(getResourceRegex, getResource);
@@ -182,8 +179,8 @@ private void getLabelsFromFtlFiles() throws IOException {
}
}
private void getLabelsFromJavaFiles() throws IOException {
for (String rootFolder : this.rootFolders) {
List<File> javaFiles = FileUtil.findFiles("java", rootFolder + "src", null, null);
for (Path rootFolder : this.rootFolders) {
List<File> javaFiles = FileUtil.findFiles("java", rootFolder.resolve("src").toString(), null, null);
for (File javaFile : javaFiles) {
// do not parse this file, else issue with getResourceRegex
if ("LabelReferences.java".equals(javaFile.getName())) continue;
@@ -195,8 +192,9 @@ private void getLabelsFromJavaFiles() throws IOException {
}
}
private void getLabelsFromGroovyFiles() throws IOException {
for (String rootFolder : this.rootFolders) {
List<File> groovyFiles = FileUtil.findFiles("groovy", rootFolder + "groovyScripts", null, null);
for (Path rootFolder : this.rootFolders) {
List<File> groovyFiles =
FileUtil.findFiles("groovy", rootFolder.resolve("groovyScripts").toString(), null, null);
for (File file : groovyFiles) {
String inFile = FileUtil.readString("UTF-8", file);
findUiLabelMapInPattern(inFile, uiLabelMap, file.getPath());
@@ -285,8 +283,9 @@ protected void findLabelKeyInElement(String inFile, String filePath, String elem
}

private void getLabelsFromSimpleMethodFiles() throws IOException {
for (String rootFolder : this.rootFolders) {
List<File> simpleMethodsFiles = FileUtil.findFiles("xml", rootFolder + "minilang", null, null);
for (Path rootFolder : this.rootFolders) {
List<File> simpleMethodsFiles =
FileUtil.findFiles("xml", rootFolder.resolve("minilang").toString(), null, null);
for (File file : simpleMethodsFiles) {
String inFile = FileUtil.readString("UTF-8", file);
findUiLabelMapInFile(inFile, file.getPath());

0 comments on commit 60f7041

Please sign in to comment.
You can’t perform that action at this time.