java Jetty 应用程序中的系统环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28674008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System environment variables in Jetty application
提问by Pranaya Behera
How to configure system environment variables inside one Jetty application?
如何在一个 Jetty 应用程序中配置系统环境变量?
e.g. For database connection details, putting it in file and checking it into cvs is bad idea. For that reason using system environment is one way to go. While the system environment variables are defined in /etc/environments
file or .bashrc/.zshrc
file , in Jetty application, doing System.getenv("variable_name")
won't give anything. It will result in null.
例如,对于数据库连接的详细信息,将其放入文件并将其检入 cvs 是个坏主意。因此,使用系统环境是一种方法。虽然系统环境变量是在/etc/environments
file 或.bashrc/.zshrc
file中定义的,但在 Jetty 应用程序中,这样做System.getenv("variable_name")
不会给出任何东西。它将导致空值。
I have read this question: Configuring a Jetty application with env variableswhich concludes that which tells that Jetty doesn't support System.getenv()
and not even in start.ini
file.
我读过这个问题:Configuring a Jetty application with env variables得出的结论是,Jetty 不支持System.getenv()
,甚至在start.ini
文件中也不支持。
And jetty and etc environment on ubuntu 12.10which says In the jetty.sh script you can source the /etc/environment file and they will be present.
which I tried and didn't get the values as expected meaning it gave me only null.
和码头,在Ubuntu 12.10等环境它说In the jetty.sh script you can source the /etc/environment file and they will be present.
这是我尝试并没有获得预期意味着它给了我唯一的空值。
If I can't use the default System.getenv()
or in any .ini
file then how to specify credentials such as Database connection etc ?
如果我不能使用默认值System.getenv()
或任何.ini
文件,那么如何指定凭据,例如数据库连接等?
回答by Joakim Erdfelt
Not supporting System.getenv()
is not a Jetty thing, but a Java thing.
不支持System.getenv()
不是 Jetty 的事情,而是 Java 的事情。
There are ton of restrictions around System.getenv()
and your environment, making it nearly useless in all but the most naive and basic test case. (eg: multiline values are not supported. multiline entries can break parsing. keys without values are not supported. keys without values can often merge with next key during parsing. entries with non US-ASCII characters are not supported. entries with control characters are not supported.)
周围System.getenv()
和您的环境有很多限制,除了最幼稚和基本的测试用例外,它几乎没有用处。(例如:不支持多行值。多行条目可能会破坏解析。不支持没有值的键。在解析过程中,没有值的键通常可以与下一个键合并。不支持具有非 US-ASCII 字符的条目。具有控制字符的条目是不支持。)
The common technique when using System Environment variables with Java programs is to use the shell specific techniques to obtain the values and inject them either on the command line, or into a ini file format for Jetty to then use.
在 Java 程序中使用系统环境变量时的常用技术是使用特定于 shell 的技术来获取值并将它们注入命令行,或注入到 Jetty 然后使用的 ini 文件格式中。
Depending on your technique, these values would either show up as Jetty properties, or as Java System Properties.
根据您的技术,这些值要么显示为 Jetty 属性,要么显示为 Java 系统属性。
Just created a project to demonstrate 4 ways to accomplish this at
刚刚创建了一个项目来演示 4 种方法来实现这一点
https://github.com/jetty-project/jetty-external-config
https://github.com/jetty-project/jetty-external-config
External Configuration Properties with Jetty
Jetty 的外部配置属性
Demonstration of how to configure simple properties that can be accessed by Servlets within Jetty.
演示如何配置可由 Jetty 中的 Servlet 访问的简单属性。
This demonstration shows 4 different ways to configure a property at runtime, that can then be read by the Servlet running within Jetty.
该演示展示了在运行时配置属性的 4 种不同方法,然后可以由在 Jetty 中运行的 Servlet 读取。
The props.war
道具战
This is a simple war file with a single HttpServlet and a WEB-INF/web.xml
这是一个带有单个 HttpServlet 和 WEB-INF/web.xml 的简单War文件
[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
See PropsServlet.java for details of behavior.
有关行为的详细信息,请参阅 PropsServlet.java。
Just compile the top level and the war file will be built and placed in all of the demo jetty.base locations for this project.
只需编译顶层,war 文件将被构建并放置在该项目的所有演示 jetty.base 位置。
Example #1: Basic Command Line
示例 #1:基本命令行
The /base-command-line
project contains a simple start.ini
which starts jetty on port 9090, and deploys the webapp. no extra configuration is done by the on-disk configuration.
该/base-command-line
项目包含一个简单的start.ini
,它在端口 9090 上启动 jetty,并部署 webapp。磁盘配置没有进行额外的配置。
If you start it up like so ...
如果你像这样启动它......
[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'll see that it has started up and deployed to the /props
context path.
您将看到它已启动并部署到/props
上下文路径。
From here you can test for properties in the servlet via tooling like wget
or curl
.
从这里,您可以通过工具(如wget
或 )测试 servlet 中的属性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
Lets stop the server and run it with a system property of our choice.
让我们停止服务器并使用我们选择的系统属性运行它。
Notice the -Dfoo=bar
?
注意-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 via curl ...
并通过 curl 寻找它......
$ curl http://localhost:9090/props/props/foo
[foo] = bar
That demonstrates access of a property that was specified via the command line, now lets look at the other choices.
这演示了对通过命令行指定的属性的访问,现在让我们看看其他选择。
Example #2: Using start.ini
示例 2:使用 start.ini
The /base-startini
project contains a simple start.ini
which starts jetty on port 9090, and deploys the webapp.
该/base-startini
项目包含一个简单的start.ini
,它在端口 9090 上启动 jetty,并部署 webapp。
This start.ini
also contains a foo.ish
property.
这start.ini
也包含一个foo.ish
属性。
Lets start up Jetty and try our props servlet access again ...
让我们启动 Jetty 并再次尝试我们的 props servlet 访问......
[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 via curl ...
并通过 curl 请求它...
$ curl http://localhost:9090/props/props/foo.ish
[foo.ish] = bar
Example #3: Using start.d optional ini
示例 #3:使用 start.d 可选的 ini
The /base-startd
project contains a simple start.ini
which starts jetty on port 9090, and deploys the webapp.
该/base-startd
项目包含一个简单的start.ini
,它在端口 9090 上启动 jetty,并部署 webapp。
This start.ini
also contains no extra properties that we are interested in.
这start.ini
也不包含我们感兴趣的额外属性。
The start.d/myconf.ini
contains a property called foo.d
that we are interested in.
该start.d/myconf.ini
包含一个名为属性foo.d
,我们感兴趣的内容。
Lets start up Jetty and try our props servlet access again ...
让我们启动 Jetty 并再次尝试我们的 props servlet 访问......
[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 via curl ...
并通过 curl 请求它...
$ curl http://localhost:9090/props/props/foo.d
[foo.d] = over here
Example #4: Using --include-jetty-dir optional config
示例 #4:使用 --include-jetty-dir 可选配置
The /base-jettyinclude
project contains a new start.ini
which starts jetty on port 9090, and deploys the webapp.
该/base-jettyinclude
项目包含一个新的start.ini
,它在端口 9090 上启动 jetty,并部署 webapp。
This start.ini
also contains no extra properties that we are interested in.
这start.ini
也不包含我们感兴趣的额外属性。
However, the start.ini
uses the --include-jetty-dir=../jettydir
optional configuration that points to an entirely new interrim jetty.base configuration source.
但是,start.ini
使用--include-jetty-dir=../jettydir
指向全新 interrim jetty.base 配置源的可选配置。
The ../jettydir/start.ini
contains a property called foo.jetty.dir
that we are interested in.
该../jettydir/start.ini
包含一个名为属性foo.jetty.dir
,我们感兴趣的内容。
Lets start up Jetty and try our props servlet access again ...
让我们启动 Jetty 并再次尝试我们的 props servlet 访问......
[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 via curl ...
并通过 curl 请求它...
$ curl http://localhost:9090/props/props/foo.jetty.dir
[foo.jetty.dir] = more of the same