mvn:jetty unable to load WebAppContext - maven

I have a weird situation, when trying to run jetty from eclipse. I updated my old project from jetty 7 to jetty 9.3.7.v20160115. But, now when starting the jetty:run with m2eclipse I got the following exception:
java.lang.IllegalArgumentException: Object of class 'org.eclipse.jetty.maven.plugin.JettyWebAppContext' is not of type 'org.eclipse.jetty.webapp.WebAppContext'. Object Class and type Class are from different loaders. in file:/xx/WebApp/src/main/webapp/WEB-INF/jetty-env.xml
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:295)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:245)
at org.eclipse.jetty.plus.webapp.EnvConfiguration.configure(EnvConfiguration.java:116)
From the message I can see that the two classes are not the same. But from the documentation I need to use the class org.eclipse.jetty.webapp.WebAppContext and org.eclipse.jetty.maven.plugin.JettyWebAppContext is a subclass of WebAppContext. So it is not clear for me if the condition in line 292
oClass.isInstance(obj)
is correctly used there.
I have defined in jetty-env.xml file the following entries:
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
<New id="resInspector" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid="wac"/></Arg>
<Arg>jdbc/xxx</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">org.postgresql.Driver</Set>
<Set name="url">jdbc:postgresql://xx:5432/xx</Set>
<Set name="username">xx</Set>
<Set name="password">xx</Set>
<Set name="maxActive">100</Set>
<Set name="maxIdle">30</Set>
<Set name="maxWait">-1</Set>
<Set name="defaultAutoCommit">false</Set>
</New>
</Arg>
</New>
</Configure>
Next I added in web.xml those entries
<resource-ref>
<description> JNDI resource</description>
<res-ref-name>jdbc/xxx</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
thanks for any help.
Markus

The error message says :
Object Class and type Class are from different loaders
So the object class obj.getClass() and the type Class oClass may be equal or the same, but loaded from different classloaders.
Possible reason could be that you have this class twice in your classpath. Once through dependencies of the jetty plugin and once in your pom.
Use mvn dependecy:tree to check that and exclude the dependency of your pom.
You also may try to change the class in
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
to
<Configure id='wac' class="org.eclipse.jetty.maven.plugin.JettyWebAppContext">

indeed it was related to some duplicated jar files. In my maven pom file I had unnecessary files (jetty-plus,jetty-annotations,jetty-jndi,jetty-xml).
Removing those jar file from my maven file solved the problem.
thanks for your suggestion

Related

Jetty Class-loading Issue

I have a Spring Boot Application, which I want to deploy as a war file in jetty 9.0.7.
I am getting spring jar conflict issue,
In jetty start.ini refers to a lib folder with lower version of spring jar for another project,which cant be changed.
But How I can force jetty to ignore those jar & use own WEB-INF/lib jars.
I tried to use in jetty-web.XML but it's not working.
Any other way ?
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/EmiratesIDProxy</Set>
<Set name="parentLoaderPriority">true</Set>
<Call name="prependServerClass">
<Arg>-org.springframework.org.</Arg>
</Call>
</Configure>

Configuring thread name prefix in Jetty transport

It would be great if someone was able to help me with the following.
We currently use Jetty to expose our REST interface (Which is setup with Spring) and I want to be able to set the prefix of the threads that are used to process these calls. I believe I have found the change to cxf that will enable this behaviour:
https://issues.apache.org/jira/browse/CXF-5919
It seems to change the initial "qtp" value to whatever you want. (The version we have does include these changes) The problem is that I cannot actually work out how to set it, initially I tried the following:
<Configure id="server" class="org.eclipse.jetty.server.Server">
<Set name="threadPool">
<New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<Set name="minThreads">10</Set>
<Set name="maxThreads">1000</Set>
<Set name="threadNamePrefix">myname</Set>
</New>
</Set>
</Configure>
http://wiki.eclipse.org/Jetty/Reference/jetty.xml_syntax#Creating_a_NewObject_and_Setting_It_on_the_Server
But that does not work as it's not the QueuedThreadPool that has the threadNamePrefix value.
I would be great if someone was able to give me some pointers as to how I can update my jetty.xml so that I can set this value.
Thank you
Rob
Looking at Jetty source code I see that name attribute is the one you are after. Your example should look like this:
<Configure id="server" class="org.eclipse.jetty.server.Server">
<Set name="threadPool">
<New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<Set name="minThreads">10</Set>
<Set name="maxThreads">1000</Set>
<Set name="name">myname</Set>
</New>
</Set>
</Configure>
This is the result (from VisualVM) on my setup:

Embedded databases in Jetty

What I want is when Jetty starts, it creates a database which can be used by my webapps. My goal is to be able to deploy my webapps on every computer to display my application.
I need a way to declare my HSQLDB database (I've SQL-files for all my databases to set up the structure and to fill it with datas) in the Jetty configuration. Those parameters just have to be set one time and won't change in the future.
I feel like I've looked for it everywhere and tried everything but nothing wants to work :( I'm using Jetty 9 by the way.
This is one of the option I've tried and which seems to be close to my solution to me. I added this code to jetty/etc/jetty.xml
<New id="toto" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/toto</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="DriverClassName">org.hsqldb.jdbc.jdbcDataSource</Set>
<Set name="Url">jdbc:hsqldb:hsql://localhost:9015/toto</Set>
<Set name="Username">toto</Set>
<Set name="Password">toto</Set>
</New>
</Arg>
</New>
and this one to jett/etc/webdefault.xml
<resource-ref>
<res-ref-name>jdbc/toto</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Merry Christmas to anyone who can help me :)
Edit 26/12/2013 :
Another option I tried is to configure the database through spring in Eclipse. Each webapp matches a project (maven architecture) and use its own database. Thus, for one project I did this :
*conf/common/resources/applicationContext.xml (Project)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:toto"/>
<property name="username" value="toto"/>
<property name="password" value="toto"/>
</bean>
*conf/dev/WEB-INF/web.xml (Project)
<resource-ref>
<res-ref-name>jdbc/toto</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
*conf/dev/WEB-INF/jetty-web.xml (Project)
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
<New id="square" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref id="wac" /></Arg>
<Arg>jdbc/square</Arg>
<Arg>
<New class="org.hsqldb.jdbc.jdbcDataSource">
<Set name="Database">file:square</Set>
<Set name="User">${database.connection.username}</Set>
<Set name="Password">${database.connection.password}</Set>
</New>
</Arg>
</New>
</Configure>
*jetty/start.ini (Jetty) : Uncomment these lines
OPTIONS=jndi
OPTIONS=plus
etc/jetty-plus.xml
With all that, I get this exception :
java.lang.ExceptionInInitializerError
at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:159)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:540)
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:349)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:812)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:288)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1322)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:732)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:490)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
at org.eclipse.jetty.util.component.ContainerLifeCycle.addBean(ContainerLifeCycle.java:282)
at org.eclipse.jetty.util.component.ContainerLifeCycle.addBean(ContainerLifeCycle.java:214)
at org.eclipse.jetty.util.component.ContainerLifeCycle.updateBeans(ContainerLifeCycle.java:764)
at org.eclipse.jetty.server.handler.HandlerCollection.setHandlers(HandlerCollection.java:89)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.setHandlers(ContextHandlerCollection.java:145)
at org.eclipse.jetty.server.handler.HandlerCollection.addHandler(HandlerCollection.java:155)
at org.eclipse.jetty.deploy.bindings.StandardDeployer.processBinding(StandardDeployer.java:41)
at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186)
at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:495)
at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:146)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:175)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:605)
at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:528)
at org.eclipse.jetty.util.Scanner.scan(Scanner.java:391)
at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
EDIT 07/01/2014
I gave up on that path but I succeeded to do what I want here: Connect databases to HSQLDB Server
It looks like you are using your HSQLDB as an embedded database, but are trying to connect to it in the server mode. Please check out the following documentation for the correct JDBC connection string when running HSQLDB in the embedded mode:
http://hsqldb.org/doc/guide/running-chapt.html#rgc_inprocess

Configuring a JNDI resource SQL in Jetty

I am having a lot of difficulty configuring a JNDI Resource in Jetty. I've gotten this to work in a Tomcat easily using context.xml and the Resource node. Attempts have been made to specify the resource in Jetty in both the jetty-env.xml and their version of a context.xml file but I end up getting the same exception. Here is the context.xml version of the resource definition:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/SQLDB</Arg>
<Arg>
<New class="com.microsoft.sqlserver.jdbc.SQLServerDriver">
<Set name="serverName">SQLDB.domain.com</Set>
<Set name="portNumber">1433</Set>
<Set name="databaseName">DBName</Set>
<Set name="userName">UName</Set>
<Set name="password">PWord</Set>
</New>
</Arg>
</New>
</Configure>
The exception being given when I try to start JNDI is:
java.lang.NoSuchMethodException: class com.microsoft.sqlserver.jdbc.SQLServerDriver.setServerName(class java.lang.String)
2013-02-01 16:57:39.061:WARN:oejd.DeploymentManager:Unable to reach node goal: started
java.lang.NoSuchMethodException: class com.microsoft.sqlserver.jdbc.SQLServerDriver.setServerName(class java.lang.String)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.set(XmlConfiguration.java:585)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:390)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:819)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1132)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:1035)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:783)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:398)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:349)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:302)
at org.eclipse.jetty.deploy.providers.ContextProvider.createContextHandler(ContextProvider.java:86)
at org.eclipse.jetty.deploy.App.getContextHandler(App.java:100)
at org.eclipse.jetty.deploy.bindings.StandardDeployer.processBinding(StandardDeployer.java:36)
at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186)
at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:494)
at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:141)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:145)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:56)
at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:609)
at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:540)
at org.eclipse.jetty.util.Scanner.scan(Scanner.java:403)
at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:337)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:121)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:555)
at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:230)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.util.component.AggregateLifeCycle.doStart(AggregateLifeCycle.java:81)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:96)
at org.eclipse.jetty.server.Server.doStart(Server.java:277)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1265)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1188)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:468)
at org.eclipse.jetty.start.Main.start(Main.java:616)
at org.eclipse.jetty.start.Main.main(Main.java:92)
This exception seems counter intuitive because I can see in the Microsoft JDBC Driver 4.0 for SQL Server documentation that serverName is a listed as a valid property.
Also, I've run the below command to verify that all expected jar files seem to be included. Specifically those for jndi and sqljdbc.
java -jar start.jar --list-options
Below are links to tutorials and documentation I have used to get where I am.
http://wiki.eclipse.org/Jetty/Feature/JNDI
http://www.eclipse.org/jetty/documentation/current/jndi-datasource-examples.html
http://www.eclipse.org/jetty/documentation/current/jndi.html#configuring-env-entries
Any insight the community has would be greatly appreciated!
You must follow these instructions for Jetty7.
For older Jetty.
I see that you are trying to use Sql Server, so, you need to use net.sourceforge.jtds.jdbcx.JtdsDataSource as shown in the sample:
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/DSTest</Arg>
<Arg>
<New class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
<Set name="User">user</Set>
<Set name="Password">pass</Set>
<Set name="DatabaseName">dbname</Set>
<Set name="ServerName">localhost</Set>
<Set name="PortNumber">1433</Set>
</New>
</Arg>
</New>
Also, add to [jetty_home]/lib/ext the required library jTDS or using Maven: Maven dependency:
Problem is not with serverName method. Method is not there. Neither portNumber, etc. on. You cannot use the driver directly.
If you take a look to the jetty doc, it suggests an alternative for Non-pooling DataSources. But problably you want a pool. So, this config.
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
<Set name="url">jdbc:jtds:sqlserver://<host>:<port>/<database_name></Set>
<Set name="username">jdbc.user</Set>
<Set name="password">jdbc.pass</Set>
</New>
Edited
Regarding your new problem. Not sure what it happening. It looks like jetty is facing problems creating the temporary directory. According with the doc, for Maven plugin the the temporary directory is ${basedir}/target. Review your jetty maven plugin configuration checking if temporal director is right and if jetty has permissions for that directory.

Configure Javamail JNDI for Jetty

My goal is simple. Configure the maven-jetty-plugin with a JNDI for javamail.
But after hours of googling and reading i cannot figure out exactly what to do....
Can someone please help me with a simple step-by-step instruction.
I just read the documentation at http://docs.codehaus.org/display/JETTY/JNDI, but there is absolutly no information about what file i should be editing...
For example. Where am i supposed to put this???
<Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
...
<New id="mail" class="org.mortbay.jetty.plus.naming.Resource">
<Arg><Ref id="wac"/></Arg>
<Arg>mail/Session</Arg>
<Arg>
<New class="org.mortbay.naming.factories.MailSessionReference">
<Set name="user">fred</Set>
<Set name="password">OBF:1xmk1w261z0f1w1c1xmq</Set>
<Set name="properties">
<New class="java.util.Properties">
<Put name="mail.smtp.host">XXX</Put>
<Put name="mail.from">me#me</Put>
<Put name="mail.debug">true</Put>
</New>
</Set>
</New>
</Arg>
And lastly. Since this is the maven-jetty-plugin, i do not have access to modify any core files, so should there be some kind of xml file i should create and set up to override jetty original configuration?
Add the following configuration to the maven-jetty-plugin:
<jettyEnvXml>src/jetty-env.xml</jettyEnvXml>
Then, you can place that file in that location.
Here are concrete examples:
pom.xml (line 536)
jetty-env.xml

Resources