Hi Chris, The switch to the Tomcat class will be made soon enough, but for now it's really a time/risk thing at this stage of the project. The Embedded class is ingrained pretty well and it isn't trivial to switch to use Tomcat.
Here is the code to set up the Embedded instance - embedded = new Embedded(); embedded.setUseNaming(false); embedded.setCatalinaHome(catalinaHome); embedded.setCatalinaBase(catalinaBase); engine = embedded.createEngine(); engine.setName(STANDARD_ENGINE_NAME); // This name MUST match the name attributes of one of the Host elements // nested immediately inside. engine.setDefaultHost(defaultHostName); engine.setParentClassLoader(classLoader); host = embedded.createHost(defaultHostName, appBase); // if not set: catalina/work/OSGiEngine/tomcatHost/_; // ow: catalina/<workDir>/_ ((StandardHost) host).setWorkDir(workDir); host.setRealm(engine.getRealm()); // http-access log ((StandardHost) host).addValve(createLoggingValve()); engine.addChild(host); File rootContext = new File(System.getProperty(SYS_PROPERTY_CATALINA_HOME) + SLASH + ROOT); String docBase = ""; if (rootContext.exists()) { docBase = rootContext.getAbsolutePath(); } else { docBase = ROOT; } tomcatDefaultCtx = embedded.createContext("", docBase); tomcatDefaultCtx.setSessionCookiePath("/"); tomcatDefaultCtx.addWelcomeFile(INDEX_JSP); host.addChild(tomcatDefaultCtx); embedded.addEngine(engine); Actually, I did just try adding the line - engine.setService(embedded); Which does move me past the NPE (and on to a different error), but I'm not sure that this is what I want to do. This code worked fine with tomcat6 and as I mentioned, the docs indicate a service need not be associated with an Engine. Is it a requirement now? > Date: Tue, 11 Dec 2012 10:30:58 -0500 > From: ch...@christopherschultz.net > To: users@tomcat.apache.org > Subject: Re: NPE when starting up Embedded tomcat7 > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA256 > > Randy, > > On 12/10/12 6:09 PM, Randy Toor wrote: > > I have an existing server which uses the Embedded class which I am > > tasked with upgrading to tomcat7 (deprecated, but we'll be keeping > > it for the time being). > > What part is deprecated? The Embedded class itself? Why not just > switch-over to using the Tomcat class? It's much easier to use and now > would be the time to make the switch. > > > There were some small changes to the code to fix API breaks but > > the startup stuff is the same. When I start up the container I am > > now seeing this - > > > > INFO: Starting tomcat server > > org.apache.catalina.startup.Embedded||Dec 7, 2012 12:09:49 PM > > org.apache.catalina.startup.Embedded startInternal INFO: Catalina > > naming disabled Dec 7, 2012 12:09:49 PM > > org.apache.catalina.core.StandardEngine startInternal INFO: > > Starting Servlet Engine: Apache Tomcat/7.0.32 Dec 7, 2012 12:09:49 > > PM org.apache.catalina.util.LifecycleBase start INFO: The start() > > method was called on component [Realm[NullRealm]] after start() had > > already been called. The second call will be ignored. > > org.apache.catalina.util.LifecycleBase||Dec 7, 2012 12:09:49 PM > > org.apache.catalina.util.LifecycleBase start Dec 7, 2012 12:09:49 > > PM org.apache.catalina.core.ContainerBase startInternal SEVERE: A > > child container failed during start > > java.util.concurrent.ExecutionException: > > org.apache.catalina.LifecycleException: Failed to start component > > [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]] > > > > > at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222) > > at java.util.concurrent.FutureTask.get(FutureTask.java:83) at > > org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123) > > > > > at > org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800) > > at > > org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) > > > > > at > org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) > > at > > org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) > > > > > at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) > > at java.util.concurrent.FutureTask.run(FutureTask.java:138) at > > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) > > > > > at > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) > > at java.lang.Thread.run(Thread.java:743) Caused by: > > org.apache.catalina.LifecycleException: Failed to start component > > [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]] > > > > > at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) > > ... 7 more Caused by: java.lang.NullPointerException: while trying > > to invoke the method org.apache.catalina.Service.findConnectors() > > of an object loaded from local variable 's' at > > org.apache.catalina.core.ApplicationContext.populateSessionTrackingModes(ApplicationContext.java:1191) > > > > > at > org.apache.catalina.core.ApplicationContext.<init>(ApplicationContext.java:125) > > at > > org.apache.catalina.core.StandardContext.getServletContext(StandardContext.java:2323) > > > > > at > org.apache.catalina.core.StandardContext.postWorkDirectory(StandardContext.java:6062) > > at > > org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5102) > > > > > at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) > > ... 7 more org.apache.catalina.core.ContainerBase||Dec 7, 2012 > > 12:09:49 PM org.apache.catalina.core.ContainerBase startInternal > > > > > > I looked into that particular line in ApplicationContext and I see > > this code is new in tomcat7 - > > > > Service s = ((Engine) > > context.getParent().getParent()).getService(); Connector[] > > connectors = s.findConnectors(); > > > > > > Looking at the API docs for Engine, it indicates that a Service > > does not necessarily need to be associated with an Engine (from the > > docs - "Set the Service with which we are associated (if any)". Am > > I missing a step now to prevent this? Or should there be a null > > check here? > > Can your post your own code that uses the Embedded class? It will > probably be easier to spot a place where you've missed something > important (or have it out of order) than to just look at the stack > trace above and try to debug Tomcat in order to determine the original > mistake. > > - -chris > -----BEGIN PGP SIGNATURE----- > Version: GnuPG/MacGPG2 v2.0.17 (Darwin) > Comment: GPGTools - http://gpgtools.org > Comment: Using GnuPG with undefined - http://www.enigmail.net/ > > iEYEAREIAAYFAlDHUbIACgkQ9CaO5/Lv0PD+kQCfQ0i2evO0hn7g0E66MlpHpy4Q > jfsAnjOqNnKIHZRlK/zuDsr4VssZQVZR > =josl > -----END PGP SIGNATURE----- > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org >