Jetty 6.1.11 avec DataSource en hosted mode lancé par le plugin Google Eclipse

Accès à une DataSource MySql configurée sous Jetty 6.1.11 lancé par le Hosted Mode de Google Web Toolkit 1.7.1

Ajoutons la DataSource et déclarons la dans la JNDI

Pour cela nous suivrons le tutorial de Nicolas Wetzel : Setting DataSource (JNDI) in GWT 1.6 (hosted mode - Jetty)

Pour configurer la datasource, il y a plusieurs choses à faire :

1) mettre les deux jars additionnels dans votre classpath: jetty-naming-6.1.11.jar et jetty-plus-6.1.11.jar pour ajouter quelques fonctionnalités J2EE à Jetty

Vous trouverez les fichiers sur :

Ainsi nous aurons ces 2 fichiers .jar à ajouter au classpath du projet :

C:\jetty-6.1.11\lib\plus\jetty-plus-6.1.11.jar C:\jetty-6.1.11\lib\naming\jetty-naming-6.1.11.jar

2) changer le lanceur de démarrage Jetty : JettyLauncher en votre propre JettyLauncher personnalisé (fonctionne avec l'option -server) : mettre cette ligne dans les les options de l'onglet Arguments -> Program arguments:

-server com.myproject.MyCustomJettyLauncher

Vous trouverez le fichier JettyLauncher sur : Google Web Toolkit (GWT) / 1.6/ dev/ core/ src/ com/ google/ gwt/ dev/ shell/ jetty/ JettyLauncher.java ‹ r5078 r5273

3) créer votre propre MyCustomJettyLauncher : copier et coller le JettyLauncher pour ajouter quelques "Configuration"

// Initial + added modifications
    private static String[] __dftConfigurationClasses =  
    { 
 
    	"org.mortbay.jetty.webapp.WebInfConfiguration",		//	WEB-INF/classes, WEB-INF/lib/*.zip and WEB-INF/lib/*.jar
//    	"org.mortbay.jetty.webapp.WebXmlConfiguration", 	//	web.xml
        "org.mortbay.jetty.webapp.JettyWebXmlConfiguration",//	jetty6-web.xml, jetty-web.xml or web-jetty.xml
//        "org.mortbay.jetty.webapp.TagLibConfiguration"	// TagLib    	
 
//    	"org.mortbay.jetty.plus.webapp.AbstractConfiguration",	// extends WebXmlConfiguration
    	"org.mortbay.jetty.plus.webapp.EnvConfiguration",		
    	"org.mortbay.jetty.plus.webapp.Configuration",  		// extends AbstractConfiguration
 
    } ;

et à la fin de la méthode start(), appeler

wac.setConfigurationClasses(__dftConfigurationClasses); 
/

ce qui nous donne :

WebAppContext wac = new WebAppContextWithReload(logger, appRootDir.getAbsolutePath(), "/");
wac.setConfigurationClasses(__dftConfigurationClasses);
RequestLogHandler logHandler = new RequestLogHandler();
logHandler.setRequestLog(new JettyRequestLogger(logger));
logHandler.setHandler(wac);
server.setHandler(logHandler);
server.start();
server.setStopAtShutdown(true);

4) éditer /war/WEB-INF/jetty-env.xml pour spécifier une DataSource

<?xml version="1.0"  encoding="ISO-8859-1"?> 
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" " 
http://jetty.mortbay.org/configure.dtd"> 
<Configure class="org.mortbay.jetty.webapp.WebAppContext"> 
    <New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource"> 
        <Arg>jdbc/projectDS</Arg> 
        <Arg> 
            <New 
class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"> 
                <Set name="Url">jdbc:mysql://localhost:3306/database 
                </Set> 
                <Set name="User">myUserName</Set> 
                <Set name="Password">myPassword</Set> 
            </New> 
        </Arg> 
    </New> 
</Configure>

5) mettre une référence à la DataSource dans votre fichier /war/WEB-INF/web.xml

<resource-ref> 
        <description>DataSource's project</description> 
        <res-ref-name>jdbc/projectDS</res-ref-name> 
        <res-type>javax.sql.DataSource</res-type> 
        <res-auth>Container</res-auth> 
    </resource-ref>

6) ajouter un fichier jndi.properties dans vos /src/ contenant :

java.naming.factory.url.pkgs=org.mortbay.naming
java.naming.factory.initial=org.mortbay.naming.InitialContextFactory

Et voila, il ne vous reste plus qu'a lancer votre Hosted Mode GWT avec Google Eclipse Plugin, pour vérifier le bon fonctionnement de votre source de donnée. Si vous avez l'un des messages suivant c'est que vous avez oublier quelque chose :

//    "org.mortbay.jetty.webapp.WebAppClassLoader"
//    [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@19b81e{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}
//    java.lang.InstantiationException: org.mortbay.jetty.webapp.WebAppClassLoader
 
//    "org.mortbay.jetty.webapp.WebAppContext"
//    [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@11adf85{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}
//    java.lang.ClassCastException: org.mortbay.jetty.webapp.WebAppContext cannot be cast to org.mortbay.jetty.webapp.Configuration    
 
//    "try.class.not.Exist"
//    [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@e10a23{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}
//    java.lang.ClassNotFoundException: try.class.not.Exist
 
//    "org.mortbay.jetty.plus.webapp.EnvConfiguration" sans lib/plus/jetty-plus-6.1.11.jar dans les lib
//    [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@c5d6c5{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}
//    java.lang.ClassNotFoundException: org.mortbay.jetty.plus.webapp.EnvConfiguration
 
//    "org.mortbay.jetty.plus.webapp.Configuration" sans lib/naming/jetty-naming-6.1.11.jar
//    [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@c4ebd4{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}
//    javax.servlet.UnavailableException: Configuration problem    
 
//    [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@159a372{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}
//    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

Jetty

Jetty (logiciel libre) est un serveur HTTP et un moteur de servlet entièrement basé sur la technologie Java. Il est utilisé par plusieurs autres projets populaires comme GWT et les serveurs d'applications JBoss et Geronimo. En raison de sa petite taille, il convient parfaitement pour fournir des services web une fois embarqué dans une application Java.

Jetty Documentation

Documentation par Jetty

Documentation externe à Jetty

Documentation relative à la JNDI

http://www.developpez.net/forums/d670180/java/serveurs-dapplication-java-java-ee/autres/jetty-ressource-jndi/

  • javax.naming.InitialContext qui implémente l'interface javax.naming.Context
  • Locating Resources Using JNDI (Java Naming and Directory Interface) et explications sur les messages :
    • javax.naming.NoInitialContextException:Need to specify class name in environment or system property, or as an appletparameter, or in an application resource file: java.naming.factory.initial
    • javax.naming.NoInitialContextException: Cannot instantiate class: XXX
Branche Visibilité
/ Visible par tous.
java: Visible par la JVM.
java:comp Visible par chaque composant (EAR, WAR, etc.).
java:comp/env Visible par chaque thread (par chaque sous-composant)

Pour mémoire, le tableau suivant reprend les conventions JEE.

Clef JNDI Type Exemple
java:comp/env/ejb/* javax.ejb.EJBObject Les EJBs s'il y en a.
java:comp/env/jdbc/* javax.sql.DataSource Une source de données SQL avec nom d'utilisateur et mot de passe.
java:comp/env/jms/* javax.jms.ConnectionFactory, javax.jms.XAConnectionFactory, javax.jms.Queue, javax.jms.Topic Une source de file JMS avec nom d'utilisateur et mot de passe.
java:comp/env/mail/* javax.mail.Session, javax.mail.Address, javax.mail.internet.InternetAddress Un serveur SMTP, IMAP4 ou POP3 ; Une adresse courriel.
java:comp/env/url/* java.net.URL Un répertoire partagé ; Une branche FTP ; Une requête de PING vers un serveur HTTP.
java:comp/env/services/* javax.xml.ws.Service Une référence à un service Web.
java:comp/env/eis/* javax.resource.cci.ConnectionFactory, javax.resource.Referenceable Pour les ressources JCA
java:comp/UserTransaction javax.transaction.UserTransaction Clef spéciale permettant d'avoir un accès au contexte transactionnel.

Documentation relative a la configuration des DataSource

EJB3 , entity et plus ?

EasyBean

Pour monter des EJB3 tel que des stateless, statefull, entity, c'est possible c'est que fait easybeans. Une implémentation open source implementationdes EJB3 container specification par ObjectWeb

Downloads

  • EasyBeans for Apache Tomcat server
  • EasyBeans for Jetty server
  • Standalone EasyBeans

Et a chaque fois décliné en plusieurs versions :

  • Hibernate EntityManager
  • Apache OpenJPA
  • Toplink Essentials

Documentation

EJB3 Entity et JPA

Master the New Persistence Paradigm with JPA

GWT sans Google Eclipse Plugin

Lancer le hosted mode (depuis C:\gwt-windows-1.6.4\samples\Hello)

Launching an application in hosted mode http://code.google.com/intl/fr-FR/webtoolkit/doc/1.6/DevGuideCompilingAndDebugging.html#DevGuideHostedMode

"C:\Program Files (x86)\Java\jre6\bin\java" -Xmx256M -cp "src;war/WEB-INF/classes;\gwt-windows-1.6.4\gwt-user.jar;\gwt-windows-1.6.4\gwt-dev-windows.jar" com.google.gwt.dev.HostedMode -startupUrl Hello.html com.google.gwt.sample.hello.Hello

Vrac


run-jetty-run Eclipse plugin for running web applications with Jetty http://code.google.com/p/run-jetty-run/


org.mortbay.jetty.webapp.WebAppContext.setConfigurationClasses


Properties p = new Properties();
props.setProperty( "java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory" );
props.setProperty( "java.naming.provider.url", "127.0.0.1:1099" );
props.setProperty( "java.naming.factory.url.pkgs", "org.jboss.naming" );
Context ctx = new InitialContext( props );
queueConnectionFactory = (QueueConnectionFactory)
ctx.lookup( "QueueConnectionFactory" );
 
Hashtable ht=new Hashtable();
		ht.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		ht.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
		ht.put("java.naming.provider.url", "localhost");
 
		Context ctx=new InitialContext(ht);

Set the properties using a hashtable :

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
  "com.sun.enterprise.naming.SerialInitContextFactory");
env.put(Context.PROVIDER_URL,
  "localhost:1099");
Context initialContext = new InitialContext(env);
[WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@1e9e2ac{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
	at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
	at javax.naming.InitialContext.lookup(Unknown Source)
	at org.mortbay.jetty.plus.webapp.EnvConfiguration.createEnvContext(EnvConfiguration.java:51)
	at org.mortbay.jetty.plus.webapp.EnvConfiguration.configureWebApp(EnvConfiguration.java:103)
	at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1217)
	at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
	at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
	at com.myproject.MyCustomJettyLauncher$WebAppContextWithReload.doStart(MyCustomJettyLauncher.java:508)
	at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
	at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
	at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)
	at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
	at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
	at org.mortbay.jetty.Server.doStart(Server.java:222)
	at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
	at com.myproject.MyCustomJettyLauncher.start(MyCustomJettyLauncher.java:584)
	at com.google.gwt.dev.HostedMode.doStartUpServer(HostedMode.java:365)
	at com.google.gwt.dev.HostedModeBase.startUp(HostedModeBase.java:590)
	at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:397)
	at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)

Setting DataSource (JNDI) in GWT 1.6 (hosted mode - Jetty)

Setting DataSource (JNDI) in GWT 1.6 (hosted mode - Jetty) Options

	3 messages - Tout réduire  -  Translate all to Français

wiltonj Afficher le profil Translate to Français

Autres options 12 mar, 06:16

Hi, How to setting DataSource in GWT 1.6 (Hosted mode - Jetty)? Hoping for some guidance. Thanks & Regards, Wilton

   Répondre    Répondre à l'auteur    Transférer

Nicolas Wetzel Afficher le profil Translate to Français

 (1 utilisateur)  Autres options 12 mar, 09:56

Hi these are some things you have to do for setting a datasource: 1: put two additionnals jars in your classpath: jetty-naming-6.1.11.jar and jetty-plus-6.1.11.jar to add some J2EE features on Jetty 2: change the startup JettyLauncher to your own custom JettyLauncher (works with -server option ) : put this line in the tab Arguments eclipse setting run program option: -server com.myproject.MyCustomJettyLauncher 3 create your own MyCustomJettyLauncher : copy and past the JettyLauncher to add some "Configuration"

private static String[] __dftConfigurationClasses = 
    { 
        "org.mortbay.jetty.webapp.WebInfConfiguration", // 
        "org.mortbay.jetty.plus.webapp.EnvConfiguration",//jetty-env 
        "org.mortbay.jetty.plus.webapp.Configuration", //web.xml 
        "org.mortbay.jetty.webapp.JettyWebXmlConfiguration",//jettyWeb 
    } ;

and at the end of the start() method call

wac.setConfigurationClasses(__dftConfigurationClasses);
WebAppContext wac = new WebAppContextWithReload( 
        appRootDir.getAbsolutePath(), "/"); 
    wac.setConfigurationClasses(__dftConfigurationClasses); 
    server.setHandler(wac); 
    server.start(); 
    server.setStopAtShutdown(true);

4) edit a jetty-env.xml on the web-inf to specify a dataSource

<?xml version="1.0"  encoding="ISO-8859-1"?> 
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" " 
http://jetty.mortbay.org/configure.dtd"> 
<Configure class="org.mortbay.jetty.webapp.WebAppContext"> 
    <New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource"> 
        <Arg>jdbc/projectDS</Arg> 
        <Arg> 
            <New 
class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"> 
                <Set name="Url">jdbc:mysql://localhost:3306/database 
                </Set> 
                <Set name="User">myUserName</Set> 
                <Set name="Password">myPassword</Set> 
            </New> 
        </Arg> 
    </New> 
</Configure>

5) make a reference on it in your web-inf/web.xml

<resource-ref> 
        <description>DataSource's project</description> 
        <res-ref-name>jdbc/projectDS</res-ref-name> 
        <res-type>javax.sql.DataSource</res-type> 
        <res-auth>Container</res-auth> 
</resource-ref>

it works for me regards, Nicolas Wetzel

I follow the 5 steps, but when i'm running my Module GWT under Google Eclipse Plugin, i have this errors : [WARN] Configuration problem at <resource- ref><description>DataSource's test project</description><res-ref- name>jdbc/projectDS</res-ref-name><res-type>javax.sql.DataSource</res- type><res-auth>Container</res-auth></resource-ref> javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher $WebAppContextWithReload@1a663bb{/,C:\workspaceEclipse \GwtSmartGwtGoogleEclipse\war} javax.servlet.UnavailableException: Configuration problem Can you help me for working too ? Thanks.


wetzeln:  i m there what do you want
Envoyé vendredi à 11:19
moi:  ok, thanks, workgin Jetty GWT JNDI
wetzeln:  ok

you still have problem ?

moi:  [WARN] Failed startup of context com.myproject.MyCustomJettyLauncher$WebAppContextWithReload@1b3be4f{/,C:\workspaceEclipse\GwtSmartGwtGoogleEclipse\war}

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

      at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
      at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
      at javax.naming.InitialContext.lookup(Unknown Source)
      at org.mortbay.jetty.plus.webapp.EnvConfiguration.createEnvContext(EnvConfiguration.java:51)
      at org.mortbay.jetty.plus.webapp.EnvConfiguration.configureWebApp(EnvConfiguration.java:103)

yes i follow you replay about this, but no sucess

wetzeln:  ok i check
Envoyé vendredi à 11:27
moi:  Your post is here :

http://groups.google.fr/group/Google-Web-Toolkit/browse_thread/thread/3f5369b0aea1a265

I have added on my custom Jetty Launcher wac.setConfigurationClasses(__dftConfigurationClasses); with values : // Initial + added modifications

  private static String __dftConfigurationClasses =  
  { 
          
          "org.mortbay.jetty.webapp.WebInfConfiguration",                //        WEB-INF/classes, WEB-INF/lib/*.zip and WEB-INF/lib/*.jar

// "org.mortbay.jetty.webapp.WebXmlConfiguration", // web.xml

      "org.mortbay.jetty.webapp.JettyWebXmlConfiguration",//        jetty6-web.xml, jetty-web.xml or web-jetty.xml

// "org.mortbay.jetty.webapp.TagLibConfiguration" // TagLib



// "org.mortbay.jetty.plus.webapp.AbstractConfiguration", // extends WebXmlConfiguration

          "org.mortbay.jetty.plus.webapp.EnvConfiguration",                
          "org.mortbay.jetty.plus.webapp.Configuration",                  // extends AbstractConfiguration
          
  } ;
Envoyé vendredi à 11:30
wetzeln:  did you try to put them in the exact order than me
moi:  When you say "it works for me", it's with GWT 1.6 stand alone, or running by Google Eclipse Plugin ?

yes same result in your order

wetzeln:  when i 've done that the Google Eclipse Plugin was not released
moi:  ok
wetzeln:  and the 1.6 was just in RC
moi:  me 1.6.4
wetzeln:  ok but i haven't try with the lastest release of 1.6
moi:  and you haven't add file like : jndi.properties ?
wetzeln:  because i ve a lot of work

i check

moi:  time is small and lot of work ! 
Envoyé vendredi à 11:35
wetzeln:  yes i forget this tip

you have to put a jndi.properties file

moi:  where ?
wetzeln:  with this contents java.naming.factory.url.pkgs=org.mortbay.naming

java.naming.factory.initial=org.mortbay.naming.InitialContextFactory

moi:  where must i put this file ? in war/WEB-INF/ ?
wetzeln:  i've put this in a ressource path

exactely called src/main/hostedMode and i've put this directory in my eclipse project classpath just to not include it on the product environnement

moi:  ok, i w'ill try, under war/WEB-INF/ or rootProject, it doesn't work
wetzeln:  yes but don't forget to add this path on your eclipse project classpaht

after the automatic build you must see it on the web-inf/classes directory

Envoyé vendredi à 11:42
moi:  thanks, i w'ill try, i not know how tu using jndi.properties files
wetzeln:  it's a server conf

it only present in your project during de dev phase don't forget it

Envoyé vendredi à 11:44
moi:  putting  jndi.properties file at root of src/ and running GWT Hosted, i have not the error
wetzeln:  ok
moi:  my servlet not working now, but i invetigate and say you

thanks, i'have progresse with you

wetzeln:  ok you're welcome

GWT 1.6 - Using a JNDI Datasource