Docker - Run Jetty on HTTPS - java

I'm trying to configure my Jetty with SSL. Now I'm just stuck on why it doesn't connect on HTTPS. It works fine on HTTP though.
File: ${JETTY_HOME}/etc/jetty-https.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/config
ure_9_3.dtd">
<!-- ============================================================= -->
<!-- Configure a HTTPS connector. -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- and jetty-ssl.xml. -->
<!-- ============================================================= -->
<Configure id="sslConnector" class="org.eclipse.jetty.server.ServerConnector">
<Call name="addIfAbsentConnectionFactory">
<Arg>
<New class="org.eclipse.jetty.server.SslConnectionFactory">
<Arg name="next">http/1.1</Arg>
<Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg>
</New>
</Arg>
</Call>
<Call name="addConnectionFactory">
<Arg>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config"><Ref refid="sslHttpConfig" /></Arg>
<Arg name="compliance"><Call class="org.eclipse.jetty.http.HttpCompliance" name="
valueOf"><Arg><Property name="jetty.http.compliance" default="RFC7230"/></Arg></Call></Ar
g>
</New>
</Arg>
</Call>
</Configure>
File: ${JETTY_HOME}/etc/jetty-ssl.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<!-- ============================================================= -->
<!-- Base SSL configuration -->
<!-- This configuration needs to be used together with 1 or more -->
<!-- of jetty-https.xml or jetty-http2.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Add a SSL Connector with no protocol factories -->
<!-- =========================================================== -->
<Call name="addConnector">
<Arg>
<New id="sslConnector" class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server"><Ref refid="Server" /></Arg>
<Arg name="acceptors" type="int"><Property name="jetty.ssl.acceptors" deprecated="ssl.acceptors" default="-1"/></Arg>
<Arg name="selectors" type="int"><Property name="jetty.ssl.selectors" deprecated="ssl.selectors" default="-1"/></Arg>
<Arg name="factories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<!-- uncomment to support proxy protocol
<Item>
<New class="org.eclipse.jetty.server.ProxyConnectionFactory"/>
</Item>-->
</Array>
</Arg>
<Set name="host"><Property name="jetty.ssl.host" deprecated="jetty.host" /></Set>
<Set name="port"><Property name="jetty.ssl.port" deprecated="ssl.port" default="8443" /></Set>
<Set name="idleTimeout"><Property name="jetty.ssl.idleTimeout" deprecated="ssl.timeout" default="30000"/></Set>
<Set name="soLingerTime"><Property name="jetty.ssl.soLingerTime" deprecated="ssl.soLingerTime" default="-1"/></Set>
<Set name="acceptorPriorityDelta"><Property name="jetty.ssl.acceptorPriorityDelta" deprecated="ssl.acceptorPriorityDelta" default="0"/></Set>
<Set name="acceptQueueSize"><Property name="jetty.ssl.acceptQueueSize" deprecated="ssl.acceptQueueSize" default="0"/></Set>
</New>
</Arg>
</Call>
<!-- =========================================================== -->
<!-- Create a TLS specific HttpConfiguration based on the -->
<!-- common HttpConfiguration defined in jetty.xml -->
<!-- Add a SecureRequestCustomizer to extract certificate and -->
<!-- session information -->
<!-- =========================================================== -->
<New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Arg><Ref refid="httpConfig"/></Arg>
<Call name="addCustomizer">
<Arg>
<New class="org.eclipse.jetty.server.SecureRequestCustomizer">
<Arg name="sniHostCheck" type="boolean"><Property name="jetty.ssl.sniHostCheck" default="true"/></Arg>
<Arg name="stsMaxAgeSeconds" type="int"><Property name="jetty.ssl.stsMaxAgeSeconds" default="-1"/></Arg>
<Arg name="stsIncludeSubdomains" type="boolean"><Property name="jetty.ssl.stsIncludeSubdomains" default="false"/></Arg>
</New>
</Arg>
</Call>
</New>
</Configure>
After running docker ps -a :
root#myserver:/home/deploy/frontend/src/app/environments# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bf8f257f8ac jetty "/docker-entrypoint.s" 2 days ago Up 14 minutes 0.0.0.0:80->8080/tcp, 0.0.0.0:443->8443/tcp jetty
And finally my
File: ${JETTY_HOME}/etc/jetty.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<!-- =============================================================== -->
<!-- Documentation of this file format can be found at: -->
<!-- http://wiki.eclipse.org/Jetty/Reference/jetty.xml_syntax -->
<!-- -->
<!-- Additional configuration files are available in $JETTY_HOME/etc -->
<!-- and can be mixed in. See start.ini file for the default -->
<!-- configuration files. -->
<!-- -->
<!-- For a description of the configuration mechanism, see the -->
<!-- output of: -->
<!-- java -jar start.jar -? -->
<!-- =============================================================== -->
<!-- =============================================================== -->
<!-- Configure a Jetty Server instance with an ID "Server" -->
<!-- Other configuration files may also configure the "Server" -->
<!-- ID, in which case they are adding configuration to the same -->
<!-- instance. If other configuration have a different ID, they -->
<!-- will create and configure another instance of Jetty. -->
<!-- Consult the javadoc of o.e.j.server.Server for all -->
<!-- configuration that may be set here. -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Configure the Server Thread Pool. -->
<!-- The server holds a common thread pool which is used by -->
<!-- default as the executor used by all connectors and servlet -->
<!-- dispatches. -->
<!-- -->
<!-- Configuring a fixed thread pool is vital to controlling the -->
<!-- maximal memory footprint of the server and is a key tuning -->
<!-- parameter for tuning. In an application that rarely blocks -->
<!-- then maximal threads may be close to the number of 5*CPUs. -->
<!-- In an application that frequently blocks, then maximal -->
<!-- threads should be set as high as possible given the memory -->
<!-- available. -->
<!-- -->
<!-- Consult the javadoc of o.e.j.util.thread.QueuedThreadPool -->
<!-- for all configuration that may be set here. -->
<!-- =========================================================== -->
<!-- uncomment to change type of threadpool
<Arg name="threadpool"><New id="threadpool" class="org.eclipse.jetty.util.thread.QueuedThreadPool"/></Arg>
-->
<Get name="ThreadPool">
<Set name="minThreads" type="int"><Property name="jetty.threadPool.minThreads" deprecated="threads.min" default="10"/></Set>
<Set name="maxThreads" type="int"><Property name="jetty.threadPool.maxThreads" deprecated="threads.max" default="200"/></Set>
<Set name="idleTimeout" type="int"><Property name="jetty.threadPool.idleTimeout" deprecated="threads.timeout" default="60000"/></Set>
<Set name="detailedDump">false</Set>
</Get>
<!-- =========================================================== -->
<!-- Add shared Scheduler instance -->
<!-- =========================================================== -->
<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler"/>
</Arg>
</Call>
<!-- =========================================================== -->
<!-- Http Configuration. -->
<!-- This is a common configuration instance used by all -->
<!-- connectors that can carry HTTP semantics (HTTP, HTTPS, etc.)-->
<!-- It configures the non wire protocol aspects of the HTTP -->
<!-- semantic. -->
<!-- -->
<!-- This configuration is only defined here and is used by -->
<!-- reference from other XML files such as jetty-http.xml, -->
<!-- jetty-https.xml and other configuration files which -->
<!-- instantiate the connectors. -->
<!-- -->
<!-- Consult the javadoc of o.e.j.server.HttpConfiguration -->
<!-- for all configuration that may be set here. -->
<!-- =========================================================== -->
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Set name="secureScheme"><Property name="jetty.httpConfig.secureScheme" default="https" /></Set>
<Set name="securePort"><Property name="jetty.httpConfig.securePort" deprecated="jetty.secure.port" default="8443" /></Set>
<Set name="outputBufferSize"><Property name="jetty.httpConfig.outputBufferSize" deprecated="jetty.output.buffer.size" default="32768" /></Set>
<Set name="outputAggregationSize"><Property name="jetty.httpConfig.outputAggregationSize" deprecated="jetty.output.aggregation.size" default="8192" /></Set>
<Set name="requestHeaderSize"><Property name="jetty.httpConfig.requestHeaderSize" deprecated="jetty.request.header.size" default="8192" /></Set>
<Set name="responseHeaderSize"><Property name="jetty.httpConfig.responseHeaderSize" deprecated="jetty.response.header.size" default="8192" /></Set>
<Set name="sendServerVersion"><Property name="jetty.httpConfig.sendServerVersion" deprecated="jetty.send.server.version" default="true" /></Set>
<Set name="sendDateHeader"><Property name="jetty.httpConfig.sendDateHeader" deprecated="jetty.send.date.header" default="false" /></Set>
<Set name="headerCacheSize"><Property name="jetty.httpConfig.headerCacheSize" default="512" /></Set>
<Set name="delayDispatchUntilContent"><Property name="jetty.httpConfig.delayDispatchUntilContent" deprecated="jetty.delayDispatchUntilContent" default="true"/></Set>
<Set name="maxErrorDispatches"><Property name="jetty.httpConfig.maxErrorDispatches" default="10"/></Set>
<Set name="blockingTimeout"><Property name="jetty.httpConfig.blockingTimeout" default="-1"/></Set>
<Set name="persistentConnectionsEnabled"><Property name="jetty.httpConfig.persistentConnectionsEnabled" default="true"/></Set>
<Set name="cookieCompliance"><Call class="org.eclipse.jetty.http.CookieCompliance" name="valueOf"><Arg><Property name="jetty.httpConfig.cookieCompliance" default="RFC6265"/></Arg></Call></Set>
</New>
<!-- =========================================================== -->
<!-- Set the default handler structure for the Server -->
<!-- A handler collection is used to pass received requests to -->
<!-- both the ContextHandlerCollection, which selects the next -->
<!-- handler by context path and virtual host, and the -->
<!-- DefaultHandler, which handles any requests not handled by -->
<!-- the context handlers. -->
<!-- Other handlers may be added to the "Handlers" collection, -->
<!-- for example the jetty-requestlog.xml file adds the -->
<!-- RequestLogHandler after the default handler -->
<!-- =========================================================== -->
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
</Item>
<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
<!-- =========================================================== -->
<!-- extra server options -->
<!-- =========================================================== -->
<Set name="stopAtShutdown"><Property name="jetty.server.stopAtShutdown" default="true"/></Set>
<Set name="stopTimeout"><Property name="jetty.server.stopTimeout" default="5000"/></Set>
<Set name="dumpAfterStart"><Property name="jetty.server.dumpAfterStart" deprecated="jetty.dump.start" default="false"/></Set>
<Set name="dumpBeforeStop"><Property name="jetty.server.dumpBeforeStop" deprecated="jetty.dump.stop" default="false"/></Set>
</Configure>
I'm quite new to Jetty and can't seem to find the answer online since all these files have been auto-generated so I'm not sure what I can delete and what not.
Any ideas?
(note: in my /var/lib/jetty/start.d only the http.ini is present (not sure if there should be a https.ini))
My firewall status:
Status: active
To Action From
-- ------ ----
Anywhere ALLOW somesubnet/24
22 ALLOW Anywhere
300 ALLOW Anywhere
3000 ALLOW Anywhere
3001 ALLOW Anywhere
3002 ALLOW Anywhere
3003 ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
8443 ALLOW Anywhere
443/tcp ALLOW Anywhere
521 ALLOW Anywhere
80,443/tcp ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
300 (v6) ALLOW Anywhere (v6)
3000 (v6) ALLOW Anywhere (v6)
3001 (v6) ALLOW Anywhere (v6)
3002 (v6) ALLOW Anywhere (v6)
3003 (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
8443 (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
521 (v6) ALLOW Anywhere (v6)
80,443/tcp (v6) ALLOW Anywhere (v6)
443/tcp ALLOW OUT Anywhere
3000 ALLOW OUT Anywhere
443/tcp (v6) ALLOW OUT Anywhere (v6)
3000 (v6) ALLOW OUT Anywhere (v6)
Docker command I used:
docker run -d --name=jetty -p 80:8080 -p 443:8443 -v /home/deploy/backend/my-server/target/my-server-0.0.1-SNAPSHOT.war:/var/lib/jetty/webapps/root.war -v /home/deploy/backend/ssl:/etc/ssl/private jetty
It runs fine on http://my-server.com but doesn't load at https://my-server.com

Even if the jetty main page on Docker Hub shows that the container should be run with docker run -d -p 80:8080 -p 443:8443 jetty, it looks like the image is not configured for HTTPS by default.
As you suggest, there should be a https.ini file in /var/lib/jetty/start.d. You can generate one by running the command java -jar "$JETTY_HOME/start.jar" --add-to-startd=https in the container.
Put this command in a Dockerfile:
FROM jetty
RUN java -jar "$JETTY_HOME/start.jar" --add-to-startd=https
Build the new image:
docker build -t my-server .
And finally start your server:
docker run -d --name=jetty -p 80:8080 -p 443:8443 -v /home/deploy/backend/my-server/target/my-server-0.0.1-SNAPSHOT.war:/var/lib/jetty/webapps/root.war -v /home/deploy/backend/ssl:/etc/ssl/private my-server
This should allow you to connect to your server using HTTPS.
You can find more information on this on this Github issue.

Related

Enable SSL for specific context-path in Karaf Jetty

I have a jax-rs service deployed in Karaf container v4.2.3 with jetty v9.4.12 and the service is deployed under /services context-path as shown in the picture.
I have managed to enable ssl client auth in Karaf Jetty but the problem is that it enables it globally which causes system console to become inaccessible.
Here is the config I used in org.ops4j.pax.web.cfg
org.osgi.service.http.enabled=false
org.osgi.service.http.secure.enabled=true
org.osgi.service.http.secure.enabled=true
org.osgi.service.http.port.secure=8443
org.ops4j.pax.web.ssl.keystore=./etc/keystores/server-keystore.p12
org.ops4j.pax.web.ssl.truststore=etc/keystores/server-truststore.p12
org.ops4j.pax.web.ssl.truststore.password=secret
org.ops4j.pax.web.ssl.key.password=secret
org.ops4j.pax.web.ssl.keystore.password=secret
org.ops4j.pax.web.ssl.clientauthneeded=true
Is it possible to have SSL client auth only for the /services path and leave system console on non-ssl (http) ?
Thanks a lot
You will need 2 ports or connectors configured. (one with SSL/TLS one without)
Then set the /services/* url-pattern to have a CONFIDENTIAL (servlet) constraint.
As an alternative to the default connectors, it is possible to configure additional connectors in the etc/jetty.xml configuration file.
The etc/jetty.xml is a standard Eclipse Jetty configuration file. The default Apache Karaf WebContainer etc/jetty.xml contains:
<!-- Use this connector for many frequently idle connections and for
threadless continuations. -->
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<Set name="host">
<Property name="jetty.host" />
</Set>
<Set name="port">
<Property name="jetty.port" default="8181" />
</Set>
<Set name="maxIdleTime">300000</Set>
<Set name="Acceptors">2</Set>
<Set name="statsOn">false</Set>
<Set name="confidentialPort">8443</Set>
<Set name="lowResourcesConnections">20000</Set>
<Set name="lowResourcesMaxIdleTime">5000</Set>
</New>
</Arg>
</Call>
<!-- =========================================================== -->
<!-- Configure Authentication Realms -->
<!-- Realms may be configured for the entire server here, or -->
<!-- they can be configured for a specific web app in a context -->
<!-- =========================================================== -->
The SelectChannelConnector defines the default connector of the WebContainer.
This connector defines the 8181 port number for the HTTP protocol (port property), and the 8443 port number for the HTTPS protocol (confidentialPort property).
The following resources give you details about advanced etc/jetty.xml configurations:
http://wiki.eclipse.org/Jetty/Howto/Configure_SSL

How do I configure nginx as proxy to jetty?

I've been trying to set up nginx as proxy to jetty. I want to do something as explained in this answer but for Jetty not ring.
I've created a .war and I placed it in ~/jetty/jetty-dist/webapps/web_test-0.1.0-SNAPSHOT-standalone.war
Say, I want to use the domain example.com with ip address 198.51.100.0.
I've also copied /etc/nginx/sites-available/default into the file example.com and I have it in the same directory.
Can you help me configure nginx as proxy to jetty in my case? I know there are many references online about how to do this but they are all different and I got confused.
What specific changes do I need to make in nginx? What changes do I need to make in jetty.xml? Do I need to make any other changes? Will my app be served at example.com/index.html?
Current state of nginx is copied below:
upstream jetty {
server 127.0.0.1:8080 fail_timeout=0
}
server {
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
proxy_pass http://jetty
try_files $uri $uri/ =404;
}
EDIT
I was wondering if I need to use Jetty at all. In this setup he just uses ring, which seems super easy? What do I gain by using jetty?
How to configure nginx to work with a java server. In the example Jetty is used.
Edit /etc/nginx/sites-available/hostname:
server {
listen 80;
server_name hostname.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
Consider disabling external access to port 8080:
/sbin/iptables -A INPUT -p tcp -i eth0 --dport 8080 -j REJECT --reject-with tcp-reset
An example Jetty configuration (jetty.xml) might resemble:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!--
| http://eclipse.org/jetty/documentation/current/configuring-connectors.html
+-->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Set name="secureScheme">https</Set>
<Set name="securePort"><Property name="jetty.tls.port" default="8443" /></Set>
<Set name="outputBufferSize">65536</Set>
<Set name="requestHeaderSize">8192</Set>
<Set name="responseHeaderSize">8192</Set>
</New>
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server"><Ref refid="Server" /></Arg>
<Arg name="acceptors" type="int"><Property name="http.acceptors" default="-1"/></Arg>
<Arg name="selectors" type="int"><Property name="http.selectors" default="-1"/></Arg>
<Arg name="factories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config"><Ref refid="httpConfig" /></Arg>
</New>
</Item>
</Array>
</Arg>
<Set name="host"><Property name="jetty.host" default="localhost" /></Set>
<Set name="port"><Property name="jetty.port" default="8080" /></Set>
</New>
</Arg>
</Call>
</Configure>
This will cause Jetty to listen on localhost:8080 and nginx to redirect requests from domain.com:80 to the Jetty server.

Defining two data sources in jetty (jetty-env.xml)

I'm trying to define two data sources in my web application, using the jetty-env.xml file.
It works ok with just one data source, however I get this exception when the second data source is added:
java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default
Here's my configuration:
jetty-env.xml
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="ds" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/mybd1</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
<Set name="jdbcUrl">jdbc:jtds:sqlserver://url:1433/mybd1</Set>
<Set name="user">xx</Set>
<Set name="password">yy</Set>
</New>
</Arg>
</New>
<New id="ds2" class="org.eclipse.jetty.plus.jndi.Resource" >
<Arg>jdbc/mybd2</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
<Set name="jdbcUrl">jdbc:jtds:sqlserver://url:1433/mybd2</Set>
<Set name="user">xx</Set>
<Set name="password">yy</Set>
</New>
</Arg>
</New>
</Configure>
web.xml
<resource-ref>
<res-ref-name>jdbc/mybd1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>jdbc/mybd2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
hibernate.cfg.xml (there is another hibernate.cfb.xml to configure the second data source)
<session-factory>
<property name="connection.datasource">jdbc/mybd1</property>
<!-- ... -->
Any clue?
I haven't had a chance to test it, but it looks to me like your problem is that you're missing an <Arg /> for the scope.
Your DS should be:
<New id="ds" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/mybd1</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
etc.
That first "Arg" is the scope, and without it, the rest of your arguments are out of position, and are probably causing your issue.
The id parameter values should match in jetty-env.xml and web.xml
jetty-env.xml
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DS1" class="org.eclipse.jetty.plus.jndi.Resource">...</New>
<New id="DS2" class="org.eclipse.jetty.plus.jndi.Resource">...</New>
</Configure>
web.xml
<resource-ref id="DS1">...</resource-ref>
<resource-ref id="DS2">...</resource-ref>
Try to enable logging in Jetty.
Be carefull logger name is "jndi".
Jetty developers don't use class-name as a logger-name for JNDI.
I spent 2 days to finding difference between name defined in web.xml and jetty-env.xml.
Take a look in :
https://www.eclipse.org/jetty/documentation/9.4.x/using-jetty-jndi.html
Deciding Where to Declare Resources
You can define naming resources in three places:
jetty.xml
Naming resources defined in a jetty.xml file are scoped at either the JVM level or the Server level.
The classes for the resource must be visible at the Jetty container level. If the classes for the resource only exist inside your webapp, you must declare it in a WEB-INF/jetty-env.xml file.
WEB-INF/jetty-env.xml
Naming resources in a WEB-INF/jetty-env.xml file are scoped to the web app in which the file resides. While you can enter JVM or Server scopes if you choose, we do not recommend doing so. The resources defined here may use classes from inside your webapp. This is a Jetty-specific mechanism.
Context xml file
Entries in a context xml file should be scoped at the level of the webapp to which they apply, although you can supply a less strict scoping level of Server or JVM if you choose. As with resources declared in a jetty.xml file, classes associated with the resource must be visible on the container’s classpath.
And put a file like this :
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<!-- Add an EnvEntry only valid for this webapp -->
<New id="gargle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg>gargle</Arg>
<Arg type="java.lang.Double">100</Arg>
<Arg type="boolean">true</Arg>
</New>
<!-- Add an override for a global EnvEntry -->
<New id="wiggle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg>wiggle</Arg>
<Arg type="java.lang.Double">55.0</Arg>
<Arg type="boolean">true</Arg>
</New>
<!-- an XADataSource -->
<New id="mydatasource99" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/mydatasource99</Arg>
<Arg>
<New class="com.atomikos.jdbc.SimpleDataSourceBean">
<Set name="xaDataSourceClassName">org.apache.derby.jdbc.EmbeddedXADataSource</Set>
<Set name="xaDataSourceProperties">databaseName=testdb99;createDatabase=create</Set>
<Set name="UniqueResourceName">mydatasource99</Set>
</New>
</Arg>
</New>
</Configure>

Configuring Jetty 6 to use commons.dbcp datasource

Hi I'm trying to configure Jetty 6.1.26 to use connection pooling and it's giving me a hard time.
I put commons-dbcp-1.4.jar, commons-pool-1.5.6.jar and mysql-connector-java-5.1.16 in
my Jetty/lib/ext folder.
I also added references to those jars in my Jetty/pom.xml
<dependencies>
...
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
</dependencies>
In my web project in eclipse, my jetty-env.xml (in WEB-INF) file is like this:
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="MySQLDB" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>MySQLDB</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://host_ip</Set>
<Set name="username">user</Set>
<Set name="password">pwd</Set>
<Set name="auth">Container</Set>
<Set name="maxActive">-1</Set>
<Set name="maxIdle">30</Set>
<Set name="maxWait">10000</Set>
<Set name="minEvictableIdleTimeMillis">600000</Set>
<Set name="name">MySQLDB</Set>
<Set name="removeAbandoned">true</Set>
<Set name="removeAbandonedTimeout">5000</Set>
<Set name="timeBetweenEvictionRunsMillis">10000</Set>
<Set name="type">javax.sql.DataSource</Set>
</New>
</Arg>
</Configure>
However, when I start Jetty (using java -jar start.jar in my Jetty directory), I get this exception:
java.lang.NoSuchMethodException: class org.apache.commons.dbcp.BasicDataSource.setAuth(class java.lang.String)
How can I setup Jetty correctly?
Thanks alot!
In your code you have <Set name="auth">Container</Set> the instruction says to call the method setAuth of the class. But the class doesn't have anything like that.
Remove the lines <Set name="auth">Container</Set> and <Set name="type">javax.sql.DataSource</Set> from the configuration: the exception is telling you that those functions don't exist on the org.apache.commons.dbcp.BasicDataSource class.

How to set transaction timeout on Jetty JNDI Atomikos configuration

I am in the process of converting various Spring beans to JNDI lookups. Currently I am using Jetty to test this. I have configured the UserTransaction according to the Jetty documentation and it works:
<New id="tx" class="org.mortbay.jetty.plus.naming.Transaction">
<Arg>
<New class="com.atomikos.icatch.jta.UserTransactionImp">
</New>
</Arg>
</New>
The problem with this configuration it that it does not set the transaction timeout like my Spring config did:
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<!-- Number of seconds before transaction timesout. -->
<property name="transactionTimeout" value="30" />
</bean>
I tried the following, but it didn't work...for some reason I ended up with TWO user transactions:
<New id="tx" class="org.mortbay.jetty.plus.naming.Transaction">
<Arg>
<New class="com.atomikos.icatch.jta.UserTransactionImp">
<Set name="transactionTimeout">30</Set>
</New>
</Arg>
</New>
Any ideas?
You'll need to configure the atomikos transaction manager through the jta.properties file within your jetty context.
For example, look at the following directory within your Jetty distribution (I'm using 6.1.24):
/jetty-6.1.24/contexts/test-jndi.d/WEB-INF/classes
jta.properties
set the property called com.atomikos.icatch.max_timeout, which is commented out in the default sample file.
Then make sure that you start your jetty container using the correctly configured context.

Categories

Resources