Jetty - set system property [duplicate]

[亡魂溺海] 提交于 2021-02-17 14:47:05

问题


I run webapp on Jetty. The configuration for the app come from file that lives on the same server where Jetty is running. Inside the app I rely on the system property to obtain path to the file so I can parse it. E.g.

final String loc = System.getProperty(FACTORY);

Now I can start jetty with D switch to provide $FACTORY on the command line but I rather put it in jetty.xml if I can. I know there is <SystemProperty /> tag but that seems to just provide system value that already exists for the <Set/> tag. Can someone give me example how this can be achieved? (If it can be achieved)


回答1:


To configure a web application it is better to avoid system properties and to use JNDI instead.

Recently I posted an example on how to accomplish that with Jetty.




回答2:


For the record, if you really need to do this through system properties (I did) you can do this to append for example -Drun.mode=staging to the system properties:

<Call class="java.lang.System" name="setProperties">
  <Arg>
    <New class="java.util.Properties">
      <Call name="putAll">
        <Arg><Call class="java.lang.System" name="getProperties"/></Arg>
      </Call>
      <Call name="setProperty">
        <Arg>run.mode</Arg>
        <Arg>staging</Arg>
      </Call>
    </New>
  </Arg>
</Call>

... and yes you can probably can program your application through this ;-)




回答3:


If you're starting Jetty through its Java API for a testing or 'embedded' application, the following example shows actually setting Java System properties prior to the startup of your WebAppContext.

    private void startJetty() {
    try {
        long startTime = System.currentTimeMillis();

        server = new Server();
        setUpSystemProperties(server);

        Connector connector = new SelectChannelConnector();
        connector.setPort(port);
        server.addConnector(connector);

        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setWar("src/main/webapp");
        server.setHandler(webAppContext);

        server.start();
    }
    catch (Exception e) {
        throw new RuntimeException("Failed to set-up web server fixture", e);
    }
}

private void setUpSystemProperties(Server jettyServer) {
    final Properties systemProperties = new Properties();
    // set your system properties...
    systemProperties.setProperty("yourProperty", "yourValue");
    jettyServer.addLifeCycleListener(new SystemPropertiesLifeCycleListener(systemProperties));
}

private class SystemPropertiesLifeCycleListener extends AbstractLifeCycleListener {
    private Properties toSet;

    public SystemPropertiesLifeCycleListener(Properties toSet) {
        this.toSet = toSet;
    }

    @Override
    public void lifeCycleStarting(LifeCycle anyLifeCycle) {
        // add to (don't replace) System.getProperties()
        System.getProperties().putAll(toSet);
    }
}

Unlike most of these answers, I won't lecture you about whether this is 'proper' compared to JNDI or some other technology you didn't ask about.




回答4:


I'm going to accept @vanje answer since it got me thinking into right direction. Here's what I ended up using:

  1. Create jetty-web.xml outside of your WAR distro (no you don't want to package it with WAR if you want to configure the app from "outside")
  2. Place jetty-web.xml alongside of jetty.xml
  3. I needed just a single parameter so I ended up with the following:

jetty-web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"    
     "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <New class="org.mortbay.jetty.plus.naming.EnvEntry">
        <Arg>myOwnParam</Arg>
        <Arg type="java.lang.String">//some/path/to/the/file</Arg>
    </New>
</Configure>

Java snippet

    InitialContext c = new InitialContext();
    EnvEntry env = (EnvEntry)
         c.lookup("org.mortbay.jetty.plus.naming.EnvEntry/myOwnParam");
    final String myString = (String) env.getObjectToBind();

The biggest gotcha for me here was that I was trying to get myString from the defaul env which didn't work until I realized that JNDI was using local context. This is OK for me but will break portability if you try to move WAR on say Tomcat. If someone can post an example how this can be saved into default context that would be greatOwnParam



来源:https://stackoverflow.com/questions/3895047/jetty-set-system-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!