On Wed, Mar 13, 2013 at 12:10 PM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Nick,
>
> On 3/12/13 11:50 PM, Nick Williams wrote:
>>
>> On Mar 12, 2013, at 10:47 PM, Nick Williams wrote:
>>
>>> The JavaDoc for o.a.c.startup.Tomcat [1] is not complete.
>>> Importantly, it is lacking complete information about all three
>>> addWebapp classes.
>
> You mean addWebapp methods? They seem fairly self-explanatory.

Yes. I meant addWebapp methods. That was a typo.

There are three of them. Only one is documented. Unfortunately, the
other two are not "self explanatory." I have no idea what the "url,"
"path," and "name" parameters are (although "host" makes sense). The
documentation for the lone method that IS documented only has
"contextPath" and "baseDir" ... that doesn't line up with the other
two methods.

>
>>> Ultimately, I want to create one giant JAR file with my classes,
>>> Tomcat classes, servlet classes, etc., with a
>>> com.mycompany.Bootstrap specified as the Main class in
>>> MANIFEST.MF.
>
> I think is called OSGi, right? I think this has been done before...

I don't know. Unfortunately, OSGi is kind of a new thing to me.

>
>>> Here's (roughly) what I expect it to look like (though, if it
>>> should be different to make something work correctly, please
>>> correct me):
>>>
>>> - MyEmbeddedWebApp.jar - com - mycompany - ... - javax - ... -
>>> META-INF - MANIFEST.MF - org - apache - ... - web - index.html -
>>> WEB-INF - web.xml
>>>
>>> How do I correctly start up Tomcat so that my (lone) web app
>>> (MyEmbeddedWebApp.jar!/web/WEB-INF/web.xml) is correctly deployed
>>> to the root context (/) with index.html and etc. resources
>>> available?
>>>
>>> [1]
>>> http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/startup/Tomcat.html
>>
>>>
>> By the way, com.mycompany.Bootstrap currently looks like the code
>> below. It's the webAppDirLocation variable and addWebapp method
>> call that I'm struggling with.
>>
>> public class Bootstrap { public static void main(String...
>> arguments) throws Exception { String webAppDirLocation = "web/";
>> Tomcat tomcat = new Tomcat(); tomcat.setPort(8973);
>> tomcat.addWebapp("/", new
>> File(webAppDirLocation).getAbsolutePath());
>
> Tomcat.addWebapp(String,String) says that the first argument is the
> context path. The context path for the ROOT webapp is "", not "/".

I didn't know this. I will change it. By the way, I got this code from
the tutorial at
https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat.

> The
> second argument is a "baseDir" which says (via Context.setDocBase) it
> can be an absolute pathname, a relative pathname (to the cwd I
> suppose, or maybe relative to the hosts's appbase), or a URL.

Well there's part of the problem with the documentation. The
documentation for the method says "Add a webapp using normal
WEB-INF/web.xml if found." and the documentation for the "baseDir"
parameter says nothing. There's no information here that would have
led me to look at the Context#setDocBase() method. Nada. I will try
out making it a URL.

> You are
> passing a relative path name which probably won't resolve to a
> resource "inside" the JAR file you are using. Try fetching a resource
> URL for the "web/" path from the ClassLoader and pass that instead of
> just "web/".

I will give this a try.

>
> You didn't say what actually happens: just stated your requirements
> and showed your code. Does Tomcat fail to start? Does it fail to
> listen on your port? Does it fail to respond to requests?

My bad. I'm always seeing y'all tell people to explain the problem,
and here I go not explaining the problem just like all the rest of
them. :-P ... When I ran the application using the batch file
generated by the mojo plugin, almost everything was good (Tomcat
started up, started listening on the right port, found all the classes
it was supposed to find, etc.). However, I got a "severe" error that
the web application directory (webAppDirLocation) did not exist and
the application could not be deployed. Understandable, since I didn't
know what to use for this.

>
>> tomcat.start();
>
> You should probably call tomcat.init() first, though some of the
> Tomcat test cases don't do it so you're probably okay.

Yea, the tutorial I was using didn't say anything about that.
Interesting that "init" and "start" are separate. If "init" was
required and "start" didn't call "init" I would think that "start"
would throw an IllegalStateException. Since it doesn't, my guess is
that calling "start" is sufficient, though I will certainly add
"init." I would love to now the semantic difference between "init" and
"start." The documentation just says "Initialize the server" and
"Start the server."

>
>> tomcat.getServer().await(); } }
>
> I don't think you configured any logging. You might want to set up
> something to at least dump to the console, and crank-up the log level
> to DEBUG or something like that. Then you might be able to see what
> Tomcat is actually doing.

It does seem to automatically dump to the console automatically. I got
plenty of messages, most of them good (listening on 8973, etc.). I
will look into logging more, of course. This was just a first pass at
proof-of-concept.

Since sending this email, I've discovered the "Executable WAR" [2]
capability of the Tomcat Maven plugin. I'm kind of confused about the
difference between Embedded Tomcat and Executable WAR. Which one do I
need? Will they both do what I need, but one might be better than the
other based on more exact requirements?

This may be premature (getting it working is my priority), but I
should mention that performance is important to what I'm doing here.
I'd like to enable the native code. Some applications and libraries
include native DLLs/SOs/JNILIBs in their JAR files, copy them to a
temporary directory at runtime and load them from the temporary
location (to avoid having to actually "install" the native libraries
in the OS or JVM directory structure). Is there a way to do this with
an embedded/executable Tomcat application so that the Tomcat classes
can take advantage of the native library?

[2] http://tomcat.apache.org/maven-plugin-2.1/executable-war-jar.html

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

Reply via email to