Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for setting java system properties (go-tika #15) #16

Merged
merged 8 commits into from
Mar 25, 2020
49 changes: 32 additions & 17 deletions tika/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"net/url"
"os"
"os/exec"
"time"
"strconv"
"time"

"golang.org/x/net/context/ctxhttp"
)
Expand All @@ -35,21 +35,25 @@ import (
// from Start.
// There is no need to create a Server for an already running Tika Server
// since you can pass its URL directly to a Client.
// Additional Java system properties can be added to a Taka Server before
// startup by adding to the JavaProps map

type Server struct {
jar string
url string // url is derived from port.
port string
cmd *exec.Cmd
child *ChildOptions
jar string
url string // url is derived from port.
port string
cmd *exec.Cmd
child *ChildOptions
JavaProps map[string]string
}

// ChildOptions represent command line parameters that can be used when Tika is run with the -spawnChild option.
// If a field is less than or equal to 0, the associated flag is not included.
type ChildOptions struct {
MaxFiles int
TaskPulseMillis int
MaxFiles int
TaskPulseMillis int
TaskTimeoutMillis int
PingPulseMillis int
PingPulseMillis int
PingTimeoutMillis int
}

Expand Down Expand Up @@ -90,9 +94,12 @@ func NewServer(jar, port string) (*Server, error) {
if port == "" {
port = "9998"
}
javaProps := make(map[string]string)

s := &Server{
jar: jar,
port: port,
jar: jar,
port: port,
JavaProps: javaProps,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
JavaProps: javaProps,
JavaProps: map[string]string{},

}
urlString := "http://localhost:" + s.port
u, err := url.Parse(urlString)
Expand Down Expand Up @@ -121,7 +128,15 @@ var command = exec.Command
// Server. Start will wait for the server to be available or until ctx is
// cancelled.
func (s *Server) Start(ctx context.Context) error {
cmd := command("java", append([]string{"-jar", s.jar, "-p", s.port}, s.child.args()...)...)

//create a slice of Java system properties to be passed to the JVM
props := []string{}
for k, v := range s.JavaProps {
props = append(props, fmt.Sprintf("-D%s=\"%s\"", k, v))
}

args := append(append(props, "-jar", s.jar, "-p", s.port), s.child.args()...)
cmd := command("java", args...)

if err := cmd.Start(); err != nil {
return err
Expand Down Expand Up @@ -180,13 +195,13 @@ func (s *Server) Shutdown(ctx context.Context) error {
}
errChannel := make(chan error)
go func() {
select {
case errChannel <- s.cmd.Wait():
case <-ctx.Done():
}
select {
case errChannel <- s.cmd.Wait():
case <-ctx.Done():
}
}()
select {
case err := <- errChannel:
case err := <-errChannel:
if err != nil {
return fmt.Errorf("could not wait for server to finish: %v", err)
}
Expand Down
37 changes: 37 additions & 0 deletions tika/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,40 @@ func TestDownloadServerError(t *testing.T) {
}
}
}

func TestAddJavaProps(t *testing.T) {
path, err := os.Executable() // Use the text executable path as a dummy jar.
if err != nil {
t.Skip("cannot find current test executable")
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, "1.14")
}))
defer ts.Close()
tsURL, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("error creating test server: %v", err)
}

s, err := NewServer(path, tsURL.Port())
if err != nil {
t.Fatalf("NewServer got error: %v", err)
}

want := "/tmp/stuff"
want2 := "Bar"

s.JavaProps["java.io.tmpdir"] = want
s.JavaProps["Foo"] = want2

got := s.JavaProps["java.io.tmpdir"]
got2 := s.JavaProps["Foo"]

if want != got {
t.Fatalf("Wanted: %s, Got: %s", want, got)
}

if want2 != got2 {
t.Fatalf("Wanted: %s, Got: %s", want2, got2)
}
}