Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Enhance Wildfly port detection #219

Merged
13 changes: 13 additions & 0 deletions docs/public/port_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ exports = {
}
```

#### Wildfly

Alizer searches inside the `pom.xml` file to find any configuration inside the profiles for plugin `wildfly-maven-plugin`:
```xml
<configuration>
...
<javaOpts>-Djboss.https.port=8080</javaOpts>
# or http
<javaOpts>-Djboss.http.port=8080</javaOpts>
...
</configuration>
```

### Python Frameworks

#### Django
Expand Down
33 changes: 33 additions & 0 deletions go/pkg/apis/enricher/framework/java/java_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
package enricher

import (
"regexp"
"strings"

"github.com/redhat-developer/alizer/go/pkg/utils"
)

Expand All @@ -30,3 +33,33 @@ func hasFramework(configFile, groupId, artifactId string) (bool, error) {
return utils.IsTagInPomXMLFile(configFile, groupId)
}
}

// GetPortsForJBossFrameworks tries to detect any port information inside javaOpts of configuration
// of a given profiles plugin
func GetPortsForJBossFrameworks(pomFilePath, pluginArtifactId, pluginGroupId string) string {
portPlaceholder := ""
pom, err := utils.GetPomFileContent(pomFilePath)
if err != nil {
return portPlaceholder
}

re := regexp.MustCompile(`jboss.https?.port=\d*`)
// Check for port configuration inside profiles
for _, profile := range pom.Profiles.Profile {
for _, plugin := range profile.Build.Plugins.Plugin {
if !(strings.Contains(plugin.ArtifactId, pluginArtifactId) && strings.Contains(plugin.GroupId, pluginGroupId)) {
continue
}
matchIndexesSlice := re.FindAllStringSubmatchIndex(plugin.Configuration.JavaOpts, -1)
for _, matchIndexes := range matchIndexesSlice {
if len(matchIndexes) > 1 {
portPlaceholder = plugin.Configuration.JavaOpts[matchIndexes[0]:matchIndexes[1]]
for _, httpArg := range []string{"jboss.http.port=", "jboss.https.port="} {
portPlaceholder = strings.Replace(portPlaceholder, httpArg, "", -1)
}
}
}
}
}
return portPlaceholder
}
28 changes: 1 addition & 27 deletions go/pkg/apis/enricher/framework/java/jboss_eap_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package enricher

import (
"context"
"regexp"
"strings"

"github.com/redhat-developer/alizer/go/pkg/apis/model"
"github.com/redhat-developer/alizer/go/pkg/utils"
Expand All @@ -35,37 +33,13 @@ func (o JBossEAPDetector) DoFrameworkDetection(language *model.Language, config

func (o JBossEAPDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
ports := []int{}
portPlaceholder := ""
// Fetch the content of xml for this component
paths, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
if err != nil {
return
}
pomXML := utils.GetFile(&paths, "pom.xml")
pom, err := utils.GetPomFileContent(pomXML)
if err != nil {
return
}

re := regexp.MustCompile(`jboss.https?.port=\d*`)
// Check for port configuration inside profiles
for _, profile := range pom.Profiles.Profile {
for _, plugin := range profile.Build.Plugins.Plugin {
if !(strings.Contains(plugin.ArtifactId, "eap-maven-plugin") && strings.Contains(plugin.GroupId, "org.jboss.eap.plugins")) {
continue
}
matchIndexesSlice := re.FindAllStringSubmatchIndex(plugin.Configuration.JavaOpts, -1)
for _, matchIndexes := range matchIndexesSlice {
if len(matchIndexes) > 1 {
portPlaceholder = plugin.Configuration.JavaOpts[matchIndexes[0]:matchIndexes[1]]
for _, httpArg := range []string{"jboss.http.port=", "jboss.https.port="} {
portPlaceholder = strings.Replace(portPlaceholder, httpArg, "", -1)
}
}
}
}
}

portPlaceholder := GetPortsForJBossFrameworks(pomXML, "eap-maven-plugin", "org.jboss.eap.plugins")
if portPlaceholder == "" {
return
}
Expand Down
24 changes: 23 additions & 1 deletion go/pkg/apis/enricher/framework/java/wildfly_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"context"

"github.com/redhat-developer/alizer/go/pkg/apis/model"
"github.com/redhat-developer/alizer/go/pkg/utils"
)

type WildFlyDetector struct{}
Expand All @@ -30,6 +31,27 @@ func (o WildFlyDetector) DoFrameworkDetection(language *model.Language, config s
}
}

// DoPortsDetection for wildfly fetches the pom.xml and tries to find any javaOpts under
// the wildfly-maven-plugin profiles. If there is one it looks if jboss.http.port is defined.
func (o WildFlyDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
// Not implemented
ports := []int{}
// Fetch the content of xml for this component
paths, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
if err != nil {
return
}
pomXML := utils.GetFile(&paths, "pom.xml")
portPlaceholder := GetPortsForJBossFrameworks(pomXML, "wildfly-maven-plugin", "org.wildfly.plugins")
if portPlaceholder == "" {
return
}

if port, err := utils.GetValidPort(portPlaceholder); err == nil {
ports = append(ports, port)
}

if len(ports) > 0 {
component.Ports = ports
return
}
}
8 changes: 4 additions & 4 deletions go/pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,24 @@ func IsTagInFile(file string, tag string) (bool, error) {
}

// IsTagInPomXMLFileArtifactId checks if a pom file contains the artifactId.
func IsTagInPomXMLFileArtifactId(pomFilePath, groupdId, artifactId string) (bool, error) {
func IsTagInPomXMLFileArtifactId(pomFilePath, groupId, artifactId string) (bool, error) {
pom, err := GetPomFileContent(pomFilePath)
if err != nil {
return false, err
}
for _, dependency := range pom.Dependencies.Dependency {
if strings.Contains(dependency.ArtifactId, artifactId) && strings.Contains(dependency.GroupId, groupdId) {
if strings.Contains(dependency.ArtifactId, artifactId) && strings.Contains(dependency.GroupId, groupId) {
return true, nil
}
}
for _, plugin := range pom.Build.Plugins.Plugin {
if strings.Contains(plugin.ArtifactId, artifactId) && strings.Contains(plugin.GroupId, groupdId) {
if strings.Contains(plugin.ArtifactId, artifactId) && strings.Contains(plugin.GroupId, groupId) {
return true, nil
}
}
for _, profile := range pom.Profiles.Profile {
for _, plugin := range profile.Build.Plugins.Plugin {
if strings.Contains(plugin.ArtifactId, artifactId) && strings.Contains(plugin.GroupId, groupdId) {
if strings.Contains(plugin.ArtifactId, artifactId) && strings.Contains(plugin.GroupId, groupId) {
return true, nil
}
}
Expand Down
4 changes: 4 additions & 0 deletions go/test/apis/component_recognizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ func TestPortDetectionJavaMicronaut(t *testing.T) {
testPortDetectionInProject(t, "projectMicronaut", []int{4444})
}

func TestPortDetectionJavaWildfly(t *testing.T) {
testPortDetectionInProject(t, "projectWildfly", []int{8085})
}

func TestPortDetectionJavaQuarkus(t *testing.T) {
testPortDetectionInProject(t, "projectQuarkus", []int{9898})
}
Expand Down
Loading