Repository navigation
Expand file tree
/
Copy pathframework.go
More file actions
300 lines (260 loc) · 9.79 KB
/
Copy pathframework.go
File metadata and controls
300 lines (260 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package frameworks
import (
"encoding/json"
"fmt"
"github.com/cloudfoundry/java-buildpack/src/java/common"
"os"
"path/filepath"
)
// Framework represents a cross-cutting concern (APM agents, security providers, etc.)
// DependencyIdentifierProvider optionally exposes a manifest dependency identifier
// for frameworks that install a specific dependency artifact.
type DependencyIdentifierProvider interface {
DependencyIdentifier() string
}
type Framework interface {
// Detect returns true if this framework should be included
// Returns the framework name and version if detected
Detect() (string, error)
// Supply installs the framework
Supply() error
// Finalize performs final framework configuration
Finalize() error
}
// Registry manages available frameworks
type Registry struct {
frameworks []Framework
context *common.Context
}
// NewRegistry creates a new framework registry
func NewRegistry(ctx *common.Context) *Registry {
return &Registry{
frameworks: []Framework{},
context: ctx,
}
}
// Register adds a framework to the registry
func (r *Registry) Register(f Framework) {
r.frameworks = append(r.frameworks, f)
}
// RegisterStandardFrameworks registers all standard frameworks in the correct priority order.
// This ensures Supply and Finalize phases use the same detection order.
// IMPORTANT: The order matters! Frameworks are checked in registration order.
func (r *Registry) RegisterStandardFrameworks() {
// APM Agents (Priority 1)
r.Register(NewNewRelicFramework(r.context))
r.Register(NewAppDynamicsFramework(r.context))
r.Register(NewDatadogJavaagentFramework(r.context))
r.Register(NewElasticApmAgentFramework(r.context))
// Spring Service Bindings (Priority 1)
// Note: order matters, Java Cf Env should be registered before StringAutoReconfiguration
r.Register(NewJavaCfEnvFramework(r.context))
r.Register(NewSpringAutoReconfigurationFramework(r.context))
// JDBC Drivers (Priority 1)
r.Register(NewPostgresqlJdbcFramework(r.context))
r.Register(NewMariaDBJDBCFramework(r.context))
// mTLS Support (Priority 1)
r.Register(NewClientCertificateMapperFramework(r.context))
// Security Providers (Priority 1)
r.Register(NewContainerSecurityProviderFramework(r.context))
r.Register(NewLunaSecurityProviderFramework(r.context))
r.Register(NewProtectAppSecurityProviderFramework(r.context))
r.Register(NewSeekerSecurityProviderFramework(r.context))
// Container & Runtime Support (Priority 1)
r.Register(NewContainerCustomizerFramework(r.context))
r.Register(NewJavaMemoryAssistantFramework(r.context))
// Metrics & Observability (Priority 1)
r.Register(NewMetricWriterFramework(r.context))
// Register cf-metrics-exporter agent (agent mode)
r.Register(NewCfMetricsExporterFramework(r.context))
// Development Tools (Priority 1)
r.Register(NewDebugFramework(r.context))
r.Register(NewJmxFramework(r.context))
r.Register(NewJavaOptsFramework(r.context))
// APM Agents (Priority 2)
r.Register(NewAzureApplicationInsightsAgentFramework(r.context))
r.Register(NewCheckmarxIASTAgentFramework(r.context))
// NOTE: Google Stackdriver Debugger has been removed - it's deprecated by Google
// and shares the same binary as Profiler. Use Profiler instead.
r.Register(NewGoogleStackdriverProfilerFramework(r.context))
r.Register(NewIntroscopeAgentFramework(r.context))
r.Register(NewOpenTelemetryJavaagentFramework(r.context))
r.Register(NewRiverbedAppInternalsAgentFramework(r.context))
r.Register(NewSkyWalkingAgentFramework(r.context))
r.Register(NewSplunkOtelJavaAgentFramework(r.context))
// Testing & Code Coverage (Priority 3)
r.Register(NewJacocoAgentFramework(r.context))
// Code Instrumentation & Additional Development Tools (Priority 3)
r.Register(NewJRebelAgentFramework(r.context))
r.Register(NewContrastSecurityAgentFramework(r.context))
r.Register(NewAspectJWeaverAgentFramework(r.context))
r.Register(NewYourKitProfilerFramework(r.context))
r.Register(NewJProfilerProfilerFramework(r.context))
r.Register(NewSealightsAgentFramework(r.context))
}
// DetectAll returns all frameworks that should be included
func (r *Registry) DetectAll() ([]Framework, []string, error) {
var matched []Framework
var names []string
for _, framework := range r.frameworks {
if name, err := framework.Detect(); err == nil && name != "" {
matched = append(matched, framework)
names = append(names, name)
}
}
return matched, names, nil
}
// Type aliases for backward compatibility
// All VCAP types and functions are now in common package
type VCAPServices = common.VCAPServices
type VCAPService = common.VCAPService
// GetVCAPServices is a convenience wrapper around common.GetVCAPServices
func GetVCAPServices() (common.VCAPServices, error) {
return common.GetVCAPServices()
}
// GetApplicationName returns the application name from VCAP_APPLICATION.
// If includeSpace is true, returns "space_name:application_name" format,
// falling back to just "application_name" if space is not available.
// Returns empty string if application name is not available.
func GetApplicationName(includeSpace bool) string {
vcapApp := os.Getenv("VCAP_APPLICATION")
if vcapApp == "" {
return ""
}
var appData map[string]interface{}
if err := json.Unmarshal([]byte(vcapApp), &appData); err != nil {
return ""
}
appName, hasApp := appData["application_name"].(string)
if !hasApp {
return ""
}
if includeSpace {
if spaceName, hasSpace := appData["space_name"].(string); hasSpace {
return spaceName + ":" + appName
}
}
return appName
}
// FindFileInDirectory searches for a file by name in a directory, checking common
// locations first and then recursively searching if not found.
// Returns the full path to the file or an error if not found.
//
// Parameters:
// - baseDir: The directory to search in
// - filename: The exact filename to search for (e.g., "javaagent.jar", "libjprofilerti.so")
// - commonSubdirs: Optional subdirectories to check first (e.g., ["lib", "bin/linux-x64"])
//
// Example:
//
// agentPath, err := FindFileInDirectory(installDir, "javaagent.jar", []string{"", "lib"})
func FindFileInDirectory(baseDir, filename string, commonSubdirs []string) (string, error) {
return FindFileInDirectoryWithArchFilter(baseDir, filename, commonSubdirs, nil)
}
// FindFileInDirectoryWithArchFilter searches for a file by name in a directory with optional
// architecture filtering. This is useful when archives contain multiple platform versions
// (e.g., linux-aarch64, linux-amd64, linux-x64) and you need to ensure the correct one.
//
// Parameters:
// - baseDir: The directory to search in
// - filename: The exact filename to search for
// - commonSubdirs: Optional subdirectories to check first
// - archDirs: Optional list of valid parent directory names (e.g., ["linux-x64", "linux-amd64"]).
// If nil, any parent directory is accepted.
//
// Example:
//
// agentPath, err := FindFileInDirectoryWithArchFilter(installDir, "libjprofilerti.so",
// []string{"bin/linux-x64", "bin/linux-amd64"}, []string{"linux-x64", "linux-amd64"})
func FindFileInDirectoryWithArchFilter(baseDir, filename string, commonSubdirs []string, archDirs []string) (string, error) {
// Helper to check if a path's parent dir matches archDirs filter
matchesArchFilter := func(path string) bool {
if archDirs == nil || len(archDirs) == 0 {
return true // No filter, accept all
}
parentDir := filepath.Base(filepath.Dir(path))
for _, validArch := range archDirs {
if parentDir == validArch {
return true
}
}
return false
}
// Check common locations first
for _, subdir := range commonSubdirs {
path := filepath.Join(baseDir, subdir, filename)
if _, err := os.Stat(path); err == nil && matchesArchFilter(path) {
return path, nil
}
}
// Check with glob patterns for versioned directories (e.g., "ver*/javaagent.jar")
for _, subdir := range commonSubdirs {
pattern := filepath.Join(baseDir, subdir, "*", filename)
matches, _ := filepath.Glob(pattern)
for _, match := range matches {
if _, err := os.Stat(match); err == nil && matchesArchFilter(match) {
return match, nil
}
}
}
// Search recursively as fallback
var foundPath string
filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // Continue walking on errors
}
if !info.IsDir() && info.Name() == filename && matchesArchFilter(path) {
foundPath = path
return filepath.SkipAll
}
return nil
})
if foundPath != "" {
return foundPath, nil
}
archMsg := ""
if archDirs != nil && len(archDirs) > 0 {
archMsg = fmt.Sprintf(" (matching arch: %v)", archDirs)
}
return "", fmt.Errorf("%s not found in %s%s", filename, baseDir, archMsg)
}
// FindFileByPattern searches for a file matching a glob pattern in a directory.
// Returns the first matching file or an error if not found.
//
// Parameters:
// - baseDir: The directory to search in
// - pattern: The glob pattern to match (e.g., "contrast*.jar", "sl-test-listener*.jar")
// - commonSubdirs: Optional subdirectories to check first
//
// Example:
//
// agentPath, err := FindFileByPattern(installDir, "contrast*.jar", []string{""})
func FindFileByPattern(baseDir, pattern string, commonSubdirs []string) (string, error) {
// Check common locations first
for _, subdir := range commonSubdirs {
globPattern := filepath.Join(baseDir, subdir, pattern)
matches, _ := filepath.Glob(globPattern)
if len(matches) > 0 {
return matches[0], nil
}
}
// Search recursively as fallback
var foundPath string
filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() {
matched, _ := filepath.Match(pattern, info.Name())
if matched {
foundPath = path
return filepath.SkipAll
}
}
return nil
})
if foundPath != "" {
return foundPath, nil
}
return "", fmt.Errorf("no file matching %s found in %s", pattern, baseDir)
}