Alexander Shutyaev <shuty...@gmail.com> wrote:

>Hi Mark,
>
>The problem is that I have a dynamic app where webapps come and go, so
>I
>can't call addWebapp() before calling start(). The best I can do is a
>delayed start() - in my method that calls addWebapp() I can make a
>check -
>and if this is the first time - call start() after addWebapp(). Will
>this
>help? Can I safely call addWebapp() for 2nd and all other webapps after
>start() ?

That should work. Another option that should work is (in my example below) use 
getHost(), start(), addWebapp() instead of addWebapp(), start().

The important thing is to get the Tomcat internals lined up before you call 
start() because adding them after start requires a little more work to get 
everything configured correctly. Calling getHost() automatically creates the 
parent objects (engine, service, server) and Connector as required and also 
sets up the default host. 

As I said before, patches to improve the documentation of the Tomcat class are 
welcome.

Mark


>
>2012/7/22 Mark Thomas <ma...@apache.org>
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> On 17/07/2012 19:31, Alexander Shutyaev wrote:
>> > Hi Christopher,
>> >
>> > I've found a solution to my problem although I believe it's more
>> > accurate to say I guessed it :) Maybe you'll be able to explain it
>> > to me. Here is the complete code:
>> >
>> > Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(baseDir);
>> > tomcat.getConnector().setPort(8080);
>> >
>tomcat.getServer().setParentClassLoader(getClass().getClassLoader());
>> >
>> >
>> >
>> tomcat.getEngine(); // (1)
>> > tomcat.init(); tomcat.getHost().addAlias("127.0.0.1"); // (2)
>> > tomcat.start(); tomcat.addWebapp("", path);
>> >
>> > (1) - this seems like it should be called from somewhere within
>> > Tomcat class during initialization, but somehow it isn't called,
>> > and without this line I get a NullPointerException
>> >
>> > (2) - this is my solution to the problem I've described before;
>> > without this line my app is only served on http://localhost:8080
>> > but if I include this line my app is available on
>> > http://localhost:8080, http://127.0.0.1:8080 and all other
>> > assigned addresses like http://192.168.1.1:8080 etc
>>
>> The problems you are seeing are caused by the order you are calling
>> the methods.
>>
>> 1. addWebapp() will create the necessary Engine and Host but these
>> need to be in place before you call start().
>>
>> 2. There is no need to call init(). Calling start() will call init()
>> if it hasn't already been called.
>>
>> The following should work:
>> Tomcat tomcat = new Tomcat();
>> tomcat.setBaseDir(baseDir);
>> tomcat.getConnector().setPort(8080);
>> tomcat.getServer().setParentClassLoader(getClass().getClassLoader());
>> tomcat.addWebapp("", path);
>> tomcat.start();
>>
>> Patches for improvements to the Tomcat Javadoc welcome.
>>
>> The Tomcat unit tests are a good source of examples of using the
>> Tomcat class.
>>
>> Mark
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.9 (MingW32)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>>
>> iQIcBAEBAgAGBQJQCzRTAAoJEBDAHFovYFnnpAcP/R0V7GzR7IRzBGQlbXugNkpJ
>> 7GEiEi+8RZCm5gDv8w4Ap5v/dQg23O5QeLrZaWZf+3S91omql7wpavBqeYz9H0I6
>> VjgcXk7n/iNB8qsWCtZ/42SFH61VI8vqwjWgiuCzDtfWODIkyp+uZpDJPLvUHzQj
>> BicYz6Ya1r2NUCzTRQzCB1ewcQgSJsBoiiHR6bxKZvZlW+LZKSCwnnPa0xIq9M90
>> 101Hidr3FYzvQWSNmTvWeXQd7u7q5VnPnOvKz+GdoC1T5EcZberPTXyh5bmqEzTk
>> USearjM26VoMPH8GxSmY4Sq3BZxn8cZZM9lBZ4onBaH4wM4ea8PfPuUrDn1AHqoO
>> K/g5erElXM9PGkPE8rQdLws0pU8XxMIXdbjHcC/amYjGmG1tfOd0Kxrq+xCuEes3
>> Qzl6t9g+Q2pKV/tqJSmoaZszZfXLVX4cr9KHwoU5Hq83WJSvaKsDwMvWRT8+hZAt
>> j9DGsIgurCtk3TxACgG73NdGLO1y96f1vOQNRb53zSOOjG6FhWeFxU1o+TijIJGL
>> fuAF1zSqLC9Tro+iZNKfzQvK8hTymzTLORmtlWrQmvAwaIdFzlJM4Ut4qoRyGGiL
>> g26HUjqord/nDG2xZ0CgMb/HjaZHxDnTLEh+iOGO5/pmdMciENw10GfF0snJyQht
>> Kd5VRqQ7JpAbDgHxXClR
>> =4xDL
>> -----END PGP SIGNATURE-----
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>> For additional commands, e-mail: users-h...@tomcat.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to