-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#25582] Use a Pathing Jar instead of long command line class paths i…
…n Java Boot loader. (#26087)
- Loading branch information
Showing
5 changed files
with
235 additions
and
46 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You 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 | ||
// | ||
// http://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 main | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// makePathingJar produces a context or 'pathing' jar which only contains the relative | ||
// classpaths in its META-INF/MANIFEST.MF. | ||
// | ||
// Since we build with Java 8 as a minimum, this is the only supported way of reducing | ||
// command line length, since argsfile support wasn't added until Java 9. | ||
// | ||
// https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html is the spec. | ||
// | ||
// In particular, we only need to populate the Jar with a Manifest-Version and Class-Path | ||
// attributes. | ||
// Per https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#classpath | ||
// the classpath URLs must be relative for security reasons. | ||
func makePathingJar(classpaths []string) (string, error) { | ||
f, err := os.Create("pathing.jar") | ||
if err != nil { | ||
return "", fmt.Errorf("unable to create pathing.jar: %w", err) | ||
} | ||
defer f.Close() | ||
if err := writePathingJar(classpaths, f); err != nil { | ||
return "", fmt.Errorf("unable to write pathing.jar: %w", err) | ||
} | ||
return f.Name(), nil | ||
} | ||
|
||
var lineBreak = []byte{'\r', '\n'} | ||
var continuation = []byte{' '} | ||
|
||
func writePathingJar(classpaths []string, w io.Writer) error { | ||
jar := zip.NewWriter(w) | ||
defer jar.Close() | ||
|
||
if _, err := jar.Create("META-INF/"); err != nil { | ||
return fmt.Errorf("unable to create META-INF/ directory: %w", err) | ||
} | ||
|
||
zf, err := jar.Create("META-INF/MANIFEST.MF") | ||
if err != nil { | ||
return fmt.Errorf("unable to create META-INF/MANIFEST.MF: %w", err) | ||
} | ||
|
||
zf.Write([]byte("Manifest-Version: 1.0")) | ||
zf.Write(lineBreak) | ||
zf.Write([]byte("Created-By: sdks/java/container/pathingjar.go")) | ||
zf.Write(lineBreak) | ||
// Class-Path: must have a sequence of relative URIs for the paths | ||
// which we assume outright in this case. | ||
|
||
// We could do this memory efficiently, but it's not worthwhile compared to the complexity | ||
// at this stage. | ||
allCPs := "Class-Path: file:" + strings.Join(classpaths, " file:") | ||
|
||
const lineLenMax = 71 // it's actually 72, but we remove 1 to account for the continuation line prefix. | ||
buf := make([]byte, lineLenMax) | ||
cur := 0 | ||
for cur+lineLenMax < len(allCPs) { | ||
next := cur + lineLenMax | ||
copy(buf, allCPs[cur:next]) | ||
zf.Write(buf) | ||
zf.Write(lineBreak) | ||
zf.Write(continuation) | ||
cur = next | ||
} | ||
zf.Write([]byte(allCPs[cur:])) | ||
zf.Write(lineBreak) | ||
return nil | ||
} |
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,88 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You 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 | ||
// | ||
// http://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 main | ||
|
||
import ( | ||
"archive/zip" | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestWritePathingJar(t *testing.T) { | ||
var buf bytes.Buffer | ||
input := []string{"a.jar", "b.jar", "c.jar", "thisVeryLongJarNameIsOverSeventyTwoCharactersLongAndNeedsToBeSplitCorrectly.jar"} | ||
err := writePathingJar(input, &buf) | ||
if err != nil { | ||
t.Errorf("writePathingJar(%v) = %v, want nil", input, err) | ||
} | ||
|
||
zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) | ||
if err != nil { | ||
t.Errorf("zip.NewReader() = %v, want nil", err) | ||
} | ||
|
||
f := getManifest(zr) | ||
if f == nil { | ||
t.Fatalf("Jar didn't contain manifest") | ||
} | ||
|
||
{ | ||
fr, err := f.Open() | ||
if err != nil { | ||
t.Errorf("(%v).Open() = %v, want nil", f.Name, err) | ||
} | ||
all, err := io.ReadAll(fr) | ||
if err != nil { | ||
t.Errorf("(%v).Open() = %v, want nil", f.Name, err) | ||
} | ||
fr.Close() | ||
want := "\nClass-Path: file:a.jar file:b.jar file:c.jar" | ||
if !strings.Contains(string(all), want) { | ||
t.Errorf("%v = %v, want nil", f.Name, err) | ||
} | ||
} | ||
|
||
{ | ||
fr, err := f.Open() | ||
if err != nil { | ||
t.Errorf("(%v).Open() = %v, want nil", f.Name, err) | ||
} | ||
defer fr.Close() | ||
fs := bufio.NewScanner(fr) | ||
fs.Split(bufio.ScanLines) | ||
|
||
for fs.Scan() { | ||
if got, want := len(fs.Bytes()), 72; got > want { | ||
t.Errorf("Manifest line exceeds limit got %v:, want %v line: %q", got, want, fs.Text()) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
// getManifest extracts the java manifest from the zip file. | ||
func getManifest(zr *zip.Reader) *zip.File { | ||
for _, f := range zr.File { | ||
if f.Name != "META-INF/MANIFEST.MF" { | ||
continue | ||
} | ||
return f | ||
} | ||
return nil | ||
} |