简体   繁体   English

Jetty应用程序中的系统环境变量

[英]System environment variables in Jetty application

How to configure system environment variables inside one Jetty application? 如何在一个Jetty应用程序中配置系统环境变量?

eg For database connection details, putting it in file and checking it into cvs is bad idea. 例如,对于数据库连接细节,将其放入文件并将其检入cvs是个坏主意。 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. 虽然系统环境变量是在/etc/environments文件或.bashrc/.zshrc文件中定义的,但在Jetty应用程序中,执行System.getenv("variable_name")时不会提供任何内容。 It will result in null. 它将导致null。

I have read this question: Configuring a Jetty application with env variables which concludes that which tells that Jetty doesn't support System.getenv() and not even in start.ini file. 我已经读过这个问题: 使用env变量配置一个Jetty应用程序 ,该变量结束告诉Jetty不支持System.getenv() ,甚至不支持start.ini文件。

And jetty and etc environment on ubuntu 12.10 which says In the jetty.sh script you can source the /etc/environment file and they will be present. ubuntu 12.10上的jetty和etc环境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. 我试过并没有得到预期的值意味着它只给了我null。

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文件,那么如何指定数据库连接等凭据?

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. System.getenv()和你的环境有很多限制,除了最天真和最基本的测试用例外几乎没用。 (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.) (例如:不支持多行值。多行条目可能会破坏解析。不支持没有值的键。在解析过程中,没有值的键通常会与下一个键合并。不支持非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 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文件将被构建并放置在该项目的所有demo 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. /base-command-line项目包含一个简单的start.ini ,它在端口9090上启动jetty,并部署webapp。 no extra configuration is done by the on-disk configuration. 磁盘配置不会进行额外配置。

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 . 从这里,您可以通过wgetcurl等工具测试servlet中的属性。

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 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并再次尝试我们的道具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 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并再次尝试我们的道具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 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使用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并再次尝试我们的道具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 http://localhost:9090/props/props/foo.jetty.dir
[foo.jetty.dir] = more of the same

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM