|
| 1 | +package frameworks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// AspectJWeaverAgentFramework represents the AspectJ Weaver Agent framework |
| 11 | +type AspectJWeaverAgentFramework struct { |
| 12 | + context *Context |
| 13 | + aspectjJar string |
| 14 | + hasAopConfig bool |
| 15 | +} |
| 16 | + |
| 17 | +// NewAspectJWeaverAgentFramework creates a new AspectJ Weaver Agent framework instance |
| 18 | +func NewAspectJWeaverAgentFramework(ctx *Context) *AspectJWeaverAgentFramework { |
| 19 | + return &AspectJWeaverAgentFramework{context: ctx} |
| 20 | +} |
| 21 | + |
| 22 | +// Detect determines if AspectJ Weaver JAR and configuration exist in the application |
| 23 | +func (a *AspectJWeaverAgentFramework) Detect() (string, error) { |
| 24 | + // Look for aspectjweaver-*.jar in the application |
| 25 | + aspectjJar, err := a.findAspectJWeaver() |
| 26 | + if err != nil || aspectjJar == "" { |
| 27 | + return "", nil |
| 28 | + } |
| 29 | + |
| 30 | + // Check for aop.xml configuration in META-INF/aop.xml |
| 31 | + aopConfig := filepath.Join(a.context.Stager.BuildDir(), "META-INF", "aop.xml") |
| 32 | + if _, err := os.Stat(aopConfig); err == nil { |
| 33 | + a.aspectjJar = aspectjJar |
| 34 | + a.hasAopConfig = true |
| 35 | + a.context.Log.Info("AspectJ Weaver detected: %s with aop.xml", aspectjJar) |
| 36 | + return "aspectj-weaver", nil |
| 37 | + } |
| 38 | + |
| 39 | + // Also check in WEB-INF/classes/META-INF/aop.xml for web apps |
| 40 | + webInfAopConfig := filepath.Join(a.context.Stager.BuildDir(), "WEB-INF", "classes", "META-INF", "aop.xml") |
| 41 | + if _, err := os.Stat(webInfAopConfig); err == nil { |
| 42 | + a.aspectjJar = aspectjJar |
| 43 | + a.hasAopConfig = true |
| 44 | + a.context.Log.Info("AspectJ Weaver detected: %s with WEB-INF/classes/META-INF/aop.xml", aspectjJar) |
| 45 | + return "aspectj-weaver", nil |
| 46 | + } |
| 47 | + |
| 48 | + return "", nil |
| 49 | +} |
| 50 | + |
| 51 | +// Supply does nothing for AspectJ Weaver - the JAR is already in the application |
| 52 | +func (a *AspectJWeaverAgentFramework) Supply() error { |
| 53 | + a.context.Log.Info("AspectJ Weaver Agent detected (using application-provided JAR)") |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +// Finalize configures the AspectJ Weaver agent for runtime |
| 58 | +func (a *AspectJWeaverAgentFramework) Finalize() error { |
| 59 | + a.context.Log.Info("Configuring AspectJ Weaver Agent") |
| 60 | + |
| 61 | + // Re-detect JAR if not set (separate finalize instance) |
| 62 | + if a.aspectjJar == "" { |
| 63 | + jar, err := a.findAspectJWeaver() |
| 64 | + if err != nil || jar == "" { |
| 65 | + a.context.Log.Warning("AspectJ Weaver JAR not found during finalize") |
| 66 | + return nil |
| 67 | + } |
| 68 | + a.aspectjJar = jar |
| 69 | + } |
| 70 | + |
| 71 | + // Verify JAR exists |
| 72 | + if _, err := os.Stat(a.aspectjJar); err != nil { |
| 73 | + a.context.Log.Warning("AspectJ Weaver JAR not found: %s", a.aspectjJar) |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + // Add javaagent to JAVA_OPTS |
| 78 | + javaOpts := fmt.Sprintf("-javaagent:%s", a.aspectjJar) |
| 79 | + if err := a.context.Stager.WriteEnvFile("JAVA_OPTS", javaOpts); err != nil { |
| 80 | + a.context.Log.Warning("Failed to set JAVA_OPTS for AspectJ Weaver: %s", err) |
| 81 | + return nil |
| 82 | + } |
| 83 | + |
| 84 | + a.context.Log.Info("AspectJ Weaver Agent configured successfully") |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// findAspectJWeaver searches for aspectjweaver-*.jar in the application |
| 89 | +func (a *AspectJWeaverAgentFramework) findAspectJWeaver() (string, error) { |
| 90 | + buildDir := a.context.Stager.BuildDir() |
| 91 | + |
| 92 | + // Common locations to check for AspectJ Weaver JAR |
| 93 | + searchDirs := []string{ |
| 94 | + filepath.Join(buildDir, "WEB-INF", "lib"), |
| 95 | + filepath.Join(buildDir, "lib"), |
| 96 | + filepath.Join(buildDir, "BOOT-INF", "lib"), // Spring Boot |
| 97 | + buildDir, // Root directory |
| 98 | + } |
| 99 | + |
| 100 | + for _, dir := range searchDirs { |
| 101 | + if _, err := os.Stat(dir); err != nil { |
| 102 | + continue // Directory doesn't exist, skip |
| 103 | + } |
| 104 | + |
| 105 | + entries, err := os.ReadDir(dir) |
| 106 | + if err != nil { |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + for _, entry := range entries { |
| 111 | + if !entry.IsDir() && strings.HasPrefix(entry.Name(), "aspectjweaver-") && strings.HasSuffix(entry.Name(), ".jar") { |
| 112 | + jarPath := filepath.Join(dir, entry.Name()) |
| 113 | + a.context.Log.Debug("Found AspectJ Weaver JAR: %s", jarPath) |
| 114 | + return jarPath, nil |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return "", nil |
| 120 | +} |
0 commit comments