-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjre.go
149 lines (125 loc) · 5.24 KB
/
jre.go
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
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libjvm
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/buildpacks/libcnb"
"github.com/magiconair/properties"
"github.com/paketo-buildpacks/libpak"
"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/crush"
"github.com/paketo-buildpacks/libjvm/count"
)
type JRE struct {
ApplicationPath string
CertificateLoader CertificateLoader
DistributionType DistributionType
LayerContributor libpak.DependencyLayerContributor
Logger bard.Logger
Metadata map[string]interface{}
}
func NewJRE(applicationPath string, dependency libpak.BuildpackDependency, cache libpak.DependencyCache, distributionType DistributionType, certificateLoader CertificateLoader, metadata map[string]interface{}) (JRE, libcnb.BOMEntry, error) {
expected := map[string]interface{}{"dependency": dependency}
if md, err := certificateLoader.Metadata(); err != nil {
return JRE{}, libcnb.BOMEntry{}, fmt.Errorf("unable to generate certificate loader metadata\n%w", err)
} else {
for k, v := range md {
expected[k] = v
}
}
contributor, be := libpak.NewDependencyLayer(dependency, cache, libcnb.LayerTypes{
Build: IsBuildContribution(metadata),
Cache: IsBuildContribution(metadata),
Launch: IsLaunchContribution(metadata),
})
contributor.ExpectedMetadata = expected
return JRE{
ApplicationPath: applicationPath,
CertificateLoader: certificateLoader,
DistributionType: distributionType,
LayerContributor: contributor,
Metadata: metadata,
}, be, nil
}
func (j JRE) Contribute(layer libcnb.Layer) (libcnb.Layer, error) {
j.LayerContributor.Logger = j.Logger
return j.LayerContributor.Contribute(layer, func(artifact *os.File) (libcnb.Layer, error) {
j.Logger.Bodyf("Expanding to %s", layer.Path)
if err := crush.Extract(artifact, layer.Path, 1); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to expand JRE\n%w", err)
}
var cacertsPath string
if IsBeforeJava9(j.LayerContributor.Dependency.Version) && j.DistributionType == JDKType {
cacertsPath = filepath.Join(layer.Path, "jre", "lib", "security", "cacerts")
} else {
cacertsPath = filepath.Join(layer.Path, "lib", "security", "cacerts")
}
if err := os.Chmod(cacertsPath, 0664); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to set keystore file permissions\n%w", err)
}
if err := j.CertificateLoader.Load(cacertsPath, "changeit"); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to load certificates\n%w", err)
}
if IsBuildContribution(j.Metadata) {
layer.BuildEnvironment.Default("JAVA_HOME", layer.Path)
layer.BuildEnvironment.Default("JRE_HOME", layer.Path)
}
if IsLaunchContribution(j.Metadata) {
layer.LaunchEnvironment.Default("BPI_APPLICATION_PATH", j.ApplicationPath)
layer.LaunchEnvironment.Default("BPI_JVM_CACERTS", cacertsPath)
if c, err := count.Classes(layer.Path); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to count JVM classes\n%w", err)
} else {
layer.LaunchEnvironment.Default("BPI_JVM_CLASS_COUNT", c)
}
if IsBeforeJava9(j.LayerContributor.Dependency.Version) && j.DistributionType == JDKType {
layer.LaunchEnvironment.Default("BPI_JVM_EXT_DIR", filepath.Join(layer.Path, "jre", "lib", "ext"))
} else if IsBeforeJava9(j.LayerContributor.Dependency.Version) && j.DistributionType == JREType {
layer.LaunchEnvironment.Default("BPI_JVM_EXT_DIR", filepath.Join(layer.Path, "lib", "ext"))
}
var file string
if IsBeforeJava9(j.LayerContributor.Dependency.Version) && j.DistributionType == JDKType {
file = filepath.Join(layer.Path, "jre", "lib", "security", "java.security")
} else if IsBeforeJava9(j.LayerContributor.Dependency.Version) && j.DistributionType == JREType {
file = filepath.Join(layer.Path, "lib", "security", "java.security")
} else {
file = filepath.Join(layer.Path, "conf", "security", "java.security")
}
p, err := properties.LoadFile(file, properties.UTF8)
if err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to read properties file %s\n%w", file, err)
}
p = p.FilterStripPrefix("security.provider.")
var providers []string
for k, v := range p.Map() {
providers = append(providers, fmt.Sprintf("%s|%s", k, v))
}
sort.Strings(providers)
layer.LaunchEnvironment.Default("BPI_JVM_SECURITY_PROVIDERS", strings.Join(providers, " "))
layer.LaunchEnvironment.Default("JAVA_HOME", layer.Path)
layer.LaunchEnvironment.Default("MALLOC_ARENA_MAX", "2")
layer.LaunchEnvironment.Append("JAVA_TOOL_OPTIONS", " ", "-XX:+ExitOnOutOfMemoryError")
}
return layer, nil
})
}
func (j JRE) Name() string {
return j.LayerContributor.LayerName()
}