The feature set currently contains:
org.mortbay.jetty.plus.Server
class in your xml configuration file rather than the standard
org.mortbay.jetty.Server
class, eg:
<Configure class="org.mortbay.jetty.plus.Server">
java -Djetty.server=org.mortbay.jetty.plus.Server -Dlog4j.configuration=log4j.xml -jar start.jar config.xml
If you prefer, instead of specifying the class on the runline,
you can edit the start.config
file inside
start.jar
and change the line:
# The main class to run
org.mortbay.jetty.Server.class
to
org.mortbay.jetty.plus.Server.class
extra/etc/jettyplus.mlet
.
To run with JMX do:
java -Dstart.class=org.mortbay.util.jmx.Main -Dserver.class=org.mortbay.jetty.plus.Server -Dlog4j.configuration=log4j.xml -jar start.jar extra/etc/jettyplus.mlet
JettyPlus consists of
org.mortbay.jetty.plus.jar
, ancillary file
org.mortbay.jetty.plus.resource.jar
, and
optionally org.mortbay.jaas.jar
as well
as a number of libraries found in extra/ext
. If you use the convenient
start.jar method of running Jetty (highly recommended), all necessary jar files will
automatically be placed on the JVM classpath for you. Otherwise,
you will need to place the following on the Java classpath in
addition to the usual Jetty jars:
The extra/plus/README.TXT
file in the source distribution
contains more information on the JettyPlus package, including
instructions on how to run the JettyPlus demonstration webapps.
javax.transaction.UserTransaction
objects.
For example: code to obtain, start and commit a UserTransaction
is as follows:
try
{
UserTransaction usertransaction =
(UserTransaction)context.lookup("java:comp/UserTransaction");
//do some stuff
usertransaction.begin();
//access some resources and make changes
usertransaction.commit();
}
catch (Exception e)
{
usertransaction.rollback();
}
JettyPlus also provides support for
javax.sql.DataSource
and javax.sql.XADataSource
objects referenced in web.xml
<resource-ref>
and <resource-env-ref>
elements.
To use DataSources in your webapp, you need to:
web.xml
descriptor. Eg.
<resource-env-ref>
<description>
DB Connection
</description>
<resource-env-ref-name>
jdbc/myDB
</resource-env-ref-name>
<resource-env-ref-type>
javax.sql.DataSource
</resource-env-ref-type>
</resource-env-ref>
<Call name="addService">
<Arg>
<New class="org.mortbay.jetty.plus.JotmService">
<Set name="Name">TransactionMgr</Set>
<!-- set up a pooled DataSource -->
<Call name="addPooledDataSource">
<Arg>jdbc/myDB</Arg>
<!-- set up the datasource -->
<Arg>
<!-- Uncomment one of the following types of XADataSource -->
<!-- according to your type of database: -->
<!-- New class="org.enhydra.jdbc.sybase.SybaseXADataSource" -->
<!-- New class="org.enhydra.jdbc.informix.InformixXADataSource" -->
<!-- New class="org.enhydra.jdbc.oracle.OracleXADataSource" -->
<New class="org.enhydra.jdbc.standard.StandardXADataSource">
<Set name="DriverName">org.hsqldb.jdbcDriver</Set>
<Set name="Url">jdbc:hsqldb:extra/etc/tmtest</Set>
<Set name="User">sa</Set>
<Set name="Password"></Set>
<!-- Uncomment to setup isolation level as required -->
<!--
<Set name="TransactionIsolation"><Get class="java.sql.Connection" name="TRANSACTION_SERIALIZABLE"/></Set>
-->
</New>
</Arg>
<!-- set up a pool for the datasource -->
<Arg>
<New class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
<Arg type="Integer">4</Arg> <!-- initial size of pool -->
<Set name="MinSize">4</Set>
<Set name="MaxSize">15</Set>
<!-- Uncomment to setup other pool params as required -->
<!--
<Set name="SleepTime">10</Set>
<Set name="LifeTime">10</Set>
<Set name="DeadLockMaxWait">10</Set>
<Set name="DeadLockRetryWait">10</Set>
<Set name="LoginTimeout">10</Set>
<Set name="Debug" type="boolean">true</Set>
-->
</New>
</Arg>
</Call>
Note: if your JDBC driver does connection pooling for you, then instead of
configuring both a StandardXADataSource
and a StandardXAPoolDataSource
,
you can just configure the StandardXADataSource
instead:
<!-- set up a DataSource where the driver does the pooling -->
<Call name="addDataSource">
<Arg>jdbc/otherDB</Arg> <!-- client lookup jndi name of datasource -->
<Arg>
<New class="org.enhydra.jdbc.standard.StandardXADataSource">
<Set name="DriverName">com.mysql.jdbc.Driver</Set>
<Set name="Url">jdbc:mysql://localhost:3306/oln </Set>
<Set name="User"></Set>
<Set name="Password"></Set>
</New>
</Arg>
</Call>
</New>
</Arg>
</Call>
DataSource datasource =
(DataSource)context.lookup("java:comp/env/jdbc/myDB");
Connection connection = datasource.getConnection();
To use the mail service, you need to:
web.xml
descriptor as a resource. Eg.
<resource-env-ref>
<description>
Mail Service
</description>
<resource-env-ref-name>
mail/Session <!-- must be same as JNDI name in xml config file (see step 2) -->
</resource-env-ref-name>
<resource-env-ref-type>
javax.mail.Session
</resource-env-ref-type>
</resource-env-ref>
<Call name="addService">
<Arg>
<New class="org.mortbay.jetty.plus.MailService">
<Set name="Name">MailService</Set>
<Set name="JNDI">mail/Session</Set>
<!-- set up the id of the smtp user -->
<Set name="User">XXX</Set>
<!-- set up the password of the smtp user -->
<Set name="Password">XXX</Set>
<!-- set up the hostname/ip addr of the smtp server -->
<Put name="mail.smtp.host">XXX</Put>
<!-- setup any javax.mail.smtp provider variables -->
<Put name="mail.pop3.user">zzz</Put>
<Put name="mail.pop3.host">vvv</Put>
<Put name="mail.from">me@me</Put>
<Put name="mail.debug">false</Put>
</New>
</Arg>
</Call>
- Step 3: access it in your code. Eg:
javax.mail.Session session =
(javax.mail.Session)context.lookup("java:comp/env/mail/Session");
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("me@me.com"));
msg.addRecipient(new InternetAddress("you@you.com"));
msg.setContent("Hi");
Transport.send(msg);
<env-entry>
<env-entry-name>
someEnv
</env-entry-name>
<env-entry-value>
A long time ago in a galaxy far far away
</env-entry-value>
<env-entry-type>
java.lang.String
</env-entry-type>
</env-entry>
InitialContext context = new InitialContext();
String starwars = (String)context.lookup("java:comp/env/someEnv");
"/"
elements of JNDI names. This avoids a lot of errors caused by mis-typings. For example, the following are all now equivalent:
java:comp/env/mything (strictly the correct form according to the spec)
java:/comp/env/mything
java:/comp/env/mything/
org.mortbay.util.LogSink
to
enable Jetty log messages to appear in Log4J style logs. The
org.mortbay.jetty.plus.resource.jar
file contains a
log4j.xml
configuration file which may be modified
as needed.
To use Log4J:
<Call name="instance" class="org.mortbay.util.Log">
<Call name="disableLog"/>
<Call name="add">
<Arg>
<New class="org.mortbay.util.log4j.Log4jSink">
<Call name="start"/>
</New>
</Arg>
</Call>
</Call>
java -Dlog4j.configuration=log4j.xml -jar start.jar config.xml
The Jetty JAAS classes are org.mortbay.jetty.plus.jar
). Instead, it is built
as org.mortbay.jaas.jar
to enable it to be used
either with standard Jetty, or with JettyPlus.
As JAAS is a pluggable framework, the Jetty JAAS integration
aims to dictate as little as possible whilst providing
a sufficiently flexible infrastructure to allow users to drop in
their own custom LoginModules. An example LoginModule (org.mortbay.jaas.spi.JDBCLoginModule
),
interacting with a database to store user names, passwords and
roles, is included with the release to illustrate what to
implement.
Some important classes are:
To use JAAS:
<Call name="addWebApplication">
<Arg>/jaas/*</Arg>
<Arg><SystemProperty name="jetty.home"/>/extra/plus/demo/webapps/jaas</Arg>
<Set name="Realm">
<New class="org.mortbay.jaas.JAASUserRealm">
<Set name="Name">Test JAAS Realm</Set>
<Set name="LoginModuleName">jdbc</Set>
<Set name="RoleCheckPolicy">
<New class="org.mortbay.jaas.StrictRoleCheckPolicy"/>
</Set>
<Set name="CallbackHandler">
<New class="org.mortbay.jaas.callback.DefaultCallbackHandler"/>
</Set>
</New>
</Set>
</Call>
Note that it is only actually necessary to specify the
RoleCheckPolicy
or
CallbackHandler
if you have provided custom
implementations of them.
web.xml
file, eg:
<security-constraint>
<web-resource-collection>
<web-resource-name>JAAS Role</web-resource-name>
<url-pattern>/doit/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>roleA</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Jetty Demo Realm</realm-name>
</login-config>
extra/etc/login.conf
):
// sample login config file for the Jetty JDBCLoginModule
// if you change the database and need to specify a password, set the property dbPassword
jdbc {
org.mortbay.jaas.spi.JDBCLoginModule required
debug="true"
dbUrl="jdbc:hsqldb:."
dbUserName="sa"
dbDriver="org.hsqldb.jdbcDriver"
userTable="myusers"
userField="myuser"
credentialField="mypassword"
userRoleTable="myuserroles"
userRoleUserField="myuser"
userRoleRoleField="myrole";
};
Note that the name of the module ("jdbc") must be the same as
that specified as the LoginModuleName
in the Jetty
xml config file in Step 1.
java.security.auth.login.config
property:
-Djava.security.auth.login.config=mylogin.conf
The above describes how to use the Jetty JAAS integration to
authenticate web users and authorize them against webapp security
constraints. It can also be used for authorization with a Java
security manager and permission policy file. For information on
how to accomplish this, build and run the JAAS demo in extra/plus
as instructed in the README.TXT file in the source distribution.