System Variables in a Jetty Application

How to configure system environment variables within a single Jetty application?

eg. For details of connecting to a database, putting it in a file and checking it on cvs is a bad idea. For this reason, using a system environment is one way. Although system environment variables are defined in the /etc/environments or .bashrc/.zshrc , executing System.getenv("variable_name") will not do anything in the Jetty application. This will result in null.

I read this question: Configuring a Jetty application with env variables , which ends up saying that Jetty does not support System.getenv() and not even in the start.ini file.

And the pier, etc. on ubuntu 12.10 which says In the jetty.sh script you can source the /etc/environment file and they will be present. I tried and did not get the value as expected, this gave me only null.

If I cannot use System.getenv() by default or in any .ini file, then how do I specify credentials such as a database connection, etc.?

+6
source share
1 answer

Doesn't support System.getenv() not a Jetty thing, but a Java thing.

There are many limitations around System.getenv() and your environment, which makes it almost useless in all but the most naive and basic test. (for example: multiline values ​​are not supported. Multiline entries can interrupt parsing. Keys without values ​​are not supported. Keys without values ​​can often merge with the next key during parsing. Records with non-US-ASCII characters are not supported. is not supported.)

A common technique for using System Environment variables with Java programs is to use special shell methods to get values ​​and enter them either on the command line or in the ini format for Jetty, which is then used.

Depending on your technology, these values ​​will appear as Jetty properties or as Java System properties.

Just created a project to demonstrate 4 ways to accomplish this with

https://github.com/jetty-project/jetty-external-config

External configuration properties with Jetty

Demonstration of how to configure simple properties that can be accessed by servlets in Jetty.

This demo shows 4 different ways to configure a property at run time, which can then be read by a servlet running inside Jetty.

Props .war

This is a simple war file with one HttpServlet and WEB-INF / web.xml

 [jetty-external-config]$ jar -tvf target/props.war 0 Mon Feb 23 09:02:14 MST 2015 META-INF/ 131 Mon Feb 23 09:02:14 MST 2015 META-INF/MANIFEST.MF 0 Mon Feb 23 09:02:14 MST 2015 WEB-INF/ 0 Mon Feb 23 09:02:14 MST 2015 WEB-INF/classes/ 0 Mon Feb 23 09:02:14 MST 2015 WEB-INF/classes/org/ 0 Mon Feb 23 09:02:14 MST 2015 WEB-INF/classes/org/eclipse/ 0 Mon Feb 23 09:02:14 MST 2015 WEB-INF/classes/org/eclipse/demo/ 2188 Mon Feb 23 09:02:12 MST 2015 WEB-INF/classes/org/eclipse/demo/PropsServlet.class 572 Mon Feb 23 08:45:22 MST 2015 WEB-INF/web.xml 

For more information about the behavior, see PropsServlet.java.

Just compile the top level and a war file will be created and placed in all places of the jetty.base demo for this project.

Example # 1: basic command line

The /base-command-line project contains a simple start.ini that launches the berth on port 9090 and deploys webapp. no additional configuration is performed using the configuration on disk.

If you run it like this ...

 [base-command-line]$ java -jar /path/to/jetty-distribution-9.2.7.v20150116/start.jar 2015-02-23 09:15:46.088:INFO::main: Logging initialized @290ms 2015-02-23 09:15:46.222:INFO:oejs.Server:main: jetty-9.2.7.v20150116 2015-02-23 09:15:46.235:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/home/joakim/code/stackoverflow/jetty-external-config/base-command-line/webapps/] at interval 1 2015-02-23 09:15:46.325:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /props, did not find org.eclipse.jetty.jsp.JettyJspServlet 2015-02-23 09:15:46.343:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@6e7f61a3 {/props,file:/tmp/jetty-0.0.0.0-9090-props.war-_props-any-27537844855769703.dir/webapp/,AVAILABLE}{/props.war} 2015-02-23 09:15:46.353:INFO:oejs.ServerConnector:main: Started ServerConnector@67cd35c5 {HTTP/1.1}{0.0.0.0:9090} 2015-02-23 09:15:46.353:INFO:oejs.Server:main: Started @555ms 

You will see that it is running and deployed to the /props .

Here you can check the properties in the servlet using tools like wget or curl .

Example:

 $ curl http://localhost:9090/props/props [java.runtime.name] = Java(TM) SE Runtime Environment [sun.boot.library.path] = /home/joakim/java/jvm/jdk-7u75-x64/jre/lib/amd64 [java.vm.version] = 24.75-b04 [java.vm.vendor] = Oracle Corporation [java.vendor.url] = http://java.oracle.com/ ... [file.separator] = / [java.vendor.url.bug] = http://bugreport.sun.com/bugreport/ [sun.io.unicode.encoding] = UnicodeLittle [sun.cpu.endian] = little [sun.desktop] = gnome [sun.cpu.isalist] = 

You can even request a specific property.

 $ curl http://localhost:9090/props/props/user.timezone [user.timezone] = America/Phoenix 

Allows you to stop the server and start it with the system property of our choice.

Pay attention to -Dfoo=bar ?

 [base-command-line]$ java -Dfoo=bar -jar /path/to/jetty-distribution-9.2.7.v20150116/start.jar 2015-02-23 09:15:46.088:INFO::main: Logging initialized @290ms 2015-02-23 09:15:46.222:INFO:oejs.Server:main: jetty-9.2.7.v20150116 2015-02-23 09:15:46.235:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/home/joakim/code/stackoverflow/jetty-external-config/base-command-line/webapps/] at interval 1 2015-02-23 09:15:46.325:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /props, did not find org.eclipse.jetty.jsp.JettyJspServlet 2015-02-23 09:15:46.343:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@6e7f61a3 {/props,file:/tmp/jetty-0.0.0.0-9090-props.war-_props-any-27537844855769703.dir/webapp/,AVAILABLE}{/props.war} 2015-02-23 09:15:46.353:INFO:oejs.ServerConnector:main: Started ServerConnector@67cd35c5 {HTTP/1.1}{0.0.0.0:9090} 2015-02-23 09:15:46.353:INFO:oejs.Server:main: Started @555ms 

and look for it through curl ...

 $ curl http://localhost:9090/props/props/foo [foo] = bar 

This demonstrates access to the properties specified on the command line, now you can see other options.

Example # 2: Using start.ini

The /base-startini contains a simple start.ini that launches the berth on port 9090 and deploys webapp.

This start.ini also contains the foo.ish property.

Lets run Jetty and try our servlet access props again ...

 [base-startini]$ java -jar /path/to/jetty-distribution-9.2.7.v20150116/start.jar 2015-02-23 09:16:46.088:INFO::main: Logging initialized @290ms 2015-02-23 09:16:46.222:INFO:oejs.Server:main: jetty-9.2.7.v20150116 

and request it through curl ...

 $ curl http://localhost:9090/props/props/foo.ish [foo.ish] = bar 

Example # 3: Using start.d optionally ini

The /base-startd contains a simple start.ini that launches the berth on port 9090 and deploys webapp.

This start.ini also does not contain additional properties that interest us.

start.d/myconf.ini contains the property foo.d interests us.

Lets run Jetty and try our servlet access props again ...

 [base-startd]$ java -jar /path/to/jetty-distribution-9.2.7.v20150116/start.jar 2015-02-23 09:19:46.088:INFO::main: Logging initialized @290ms 2015-02-23 09:19:46.222:INFO:oejs.Server:main: jetty-9.2.7.v20150116 

and request it through curl ...

 $ curl http://localhost:9090/props/props/foo.d [foo.d] = over here 

Example # 4: Using the -include-jetty-dir config option

The /base-jettyinclude contains a new start.ini that launches the jetty on port 9090 and deploys webapp.

This start.ini also does not contain additional properties that interest us.

However, start.ini uses the optional configuration --include-jetty-dir=../jettydir , which points to a completely new source for the interrim jetty.base configuration.

../jettydir/start.ini contains the property foo.jetty.dir interests us.

Lets run Jetty and try our servlet access props again ...

 [base-jettyinclude]$ java -jar /path/to/jetty-distribution-9.2.7.v20150116/start.jar 2015-02-23 09:24:46.088:INFO::main: Logging initialized @290ms 2015-02-23 09:24:46.222:INFO:oejs.Server:main: jetty-9.2.7.v20150116 

and request it through curl ...

 $ curl http://localhost:9090/props/props/foo.jetty.dir [foo.jetty.dir] = more of the same 
+7
source

Source: https://habr.com/ru/post/982886/


All Articles