-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Glassfish service name detector (#579)
This is a continuation of #562 that introduces the first of several web app servers for which we will detect the service name. Because this is the first (GlassFish), it includes some of the helper/plumbing utilities. Subsequent additions will mostly be the `AppServer` implementations (and adding them to the delegate list).
- Loading branch information
1 parent
40fb370
commit 2e8edf3
Showing
9 changed files
with
658 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
95 changes: 95 additions & 0 deletions
95
...viders/src/main/java/io/opentelemetry/resourceproviders/AppServerServiceNameDetector.java
This file contains 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,95 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.resourceproviders; | ||
|
||
import static java.util.logging.Level.FINE; | ||
|
||
import com.google.errorprone.annotations.MustBeClosed; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Stream; | ||
import javax.annotation.Nullable; | ||
|
||
final class AppServerServiceNameDetector implements ServiceNameDetector { | ||
|
||
private static final Logger logger = | ||
Logger.getLogger(AppServerServiceNameDetector.class.getName()); | ||
|
||
private final AppServer appServer; | ||
private final ParseBuddy parseBuddy; | ||
private final DirectoryTool dirTool; | ||
|
||
AppServerServiceNameDetector(AppServer appServer) { | ||
this(appServer, new ParseBuddy(appServer), new DirectoryTool()); | ||
} | ||
|
||
// Exists for testing | ||
AppServerServiceNameDetector(AppServer appServer, ParseBuddy parseBuddy, DirectoryTool dirTool) { | ||
this.appServer = appServer; | ||
this.parseBuddy = parseBuddy; | ||
this.dirTool = dirTool; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String detect() throws Exception { | ||
if (appServer.getServerClass() == null) { | ||
return null; | ||
} | ||
|
||
Path deploymentDir = appServer.getDeploymentDir(); | ||
if (deploymentDir == null) { | ||
return null; | ||
} | ||
|
||
if (!dirTool.isDirectory(deploymentDir)) { | ||
logger.log(FINE, "Deployment dir '{0}' doesn't exist.", deploymentDir); | ||
return null; | ||
} | ||
|
||
logger.log(FINE, "Looking for deployments in '{0}'.", deploymentDir); | ||
try (Stream<Path> stream = dirTool.list(deploymentDir)) { | ||
return stream.map(this::detectName).filter(Objects::nonNull).findFirst().orElse(null); | ||
} | ||
} | ||
|
||
@Nullable | ||
private String detectName(Path path) { | ||
if (!appServer.isValidAppName(path)) { | ||
logger.log(FINE, "Skipping '{0}'.", path); | ||
return null; | ||
} | ||
|
||
logger.log(FINE, "Attempting service name detection in '{0}'.", path); | ||
String name = path.getFileName().toString(); | ||
if (dirTool.isDirectory(path)) { | ||
return parseBuddy.handleExplodedApp(path); | ||
} | ||
if (name.endsWith(".war")) { | ||
return parseBuddy.handlePackagedWar(path); | ||
} | ||
if (appServer.supportsEar() && name.endsWith(".ear")) { | ||
return parseBuddy.handlePackagedEar(path); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// Exists for testing | ||
static class DirectoryTool { | ||
boolean isDirectory(Path path) { | ||
return Files.isDirectory(path); | ||
} | ||
|
||
@MustBeClosed | ||
Stream<Path> list(Path path) throws IOException { | ||
return Files.list(path); | ||
} | ||
} | ||
} |
This file contains 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
39 changes: 39 additions & 0 deletions
39
resource-providers/src/main/java/io/opentelemetry/resourceproviders/GlassfishAppServer.java
This file contains 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,39 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.resourceproviders; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import javax.annotation.Nullable; | ||
|
||
class GlassfishAppServer implements AppServer { | ||
|
||
private static final String SERVICE_CLASS_NAME = "com.sun.enterprise.glassfish.bootstrap.ASMain"; | ||
private final ResourceLocator locator; | ||
|
||
GlassfishAppServer(ResourceLocator locator) { | ||
this.locator = locator; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Path getDeploymentDir() { | ||
String instanceRoot = System.getProperty("com.sun.aas.instanceRoot"); | ||
if (instanceRoot == null) { | ||
return null; | ||
} | ||
|
||
// besides autodeploy directory it is possible to deploy applications through admin console and | ||
// asadmin script, to detect those we would need to parse config/domain.xml | ||
return Paths.get(instanceRoot, "autodeploy"); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Class<?> getServerClass() { | ||
return locator.findClass(SERVICE_CLASS_NAME); | ||
} | ||
} |
Oops, something went wrong.