RE: unable to getRealPath for servlet context
It is not a bug, ServletContext.getRealPath() never return a real path on OSGI container. For Equinox framework, use the following code fragment: import org.eclipse.core.runtime. FileLocator; ... ServletContext context = request.getSession().getServletContext(); URL url = context.getResource(/); String realPath = FileLocator.toFileURL(url).getPath(); -Original Message- From: asookazian2 [mailto:asookaz...@gmail.com] Sent: Thursday, March 06, 2014 8:12 AM To: user@karaf.apache.org Subject: unable to getRealPath for servlet context I found the following Jetty bug report: http://jira.codehaus.org/browse/JETTY-675 http://jira.codehaus.org/browse/JETTY-675 User states the following: ServletContext.getRealPath() returns null instead of returning the root dir of the webapp I am able to reproduce this behavior in my WAR bundle with MANIFEST.MF (WAB (WebApplication Bundle)) deployment to Karaf 3.0.0. The version of Jetty is the following: karaf@root() feature:list | grep jetty cxf-http-jetty| 3.0.0-milestone2 | x | cxf-3.0.0-milestone2| jetty | 8.1.9.v20130131 | x | standard-3.0.0 | pax-jetty | 8.1.14.v20131031 | x | org.ops4j.pax.web-3.0.5 | Provide Jetty engine support I have tried ServletContext.getRealPath(); I have also tried ServletContext.getRealPath(/); Both code lines return null. Is this expected for this version of Karaf/Jetty? If yes, how can I determine the real path. Our scenario involves a properties file that may be installed in karaf/config/foo directory. We don't want to load/read the file from inside the WAR (e.g. WEB-INF) b/c the customer may need to change the file. -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context- tp4032109.html Sent from the Karaf - User mailing list archive at Nabble.com.
RE: unable to getRealPath for servlet context
Thx for quick response. What is the equivalent to FileLocator in karaf? -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context-tp4032109p4032114.html Sent from the Karaf - User mailing list archive at Nabble.com.
RE: unable to getRealPath for servlet context
May change Karaf to use Equinox instead of Felix. 1. check config.properties under /etc folder, make the following change. karaf.framework=equinox 2. then locate the Equinox version. For example, Karaf v2.3.4 uses Equinox v3.8.0 # # Framework selection properties # karaf.framework=equinox # # Location of the OSGi frameworks # karaf.framework.equinox=${karaf.default.repository}/org/eclipse/osgi/3.8.0.v 20120529-1548/osgi-3.8.0.v20120529-1548.jar karaf.framework.felix=${karaf.default.repository}/org/apache/felix/org.apach e.felix.framework/4.0.3/org.apache.felix.framework-4.0.3.jar 3. shutdown and restart Karaf 4. Go to http://download.eclipse.org/equinox/ and download the corresponding Equinox SDK zip file. Inside it, you may find the corresponding file as below: org.eclipse.equinox.common-version number.jar At the end, install this bundle in your Karaf. Of course, you may try to find the corresponding org.eclipse.equinox.common file from Maven repo. -Original Message- From: asookazian2 [mailto:asookaz...@gmail.com] Sent: Thursday, March 06, 2014 12:58 PM To: user@karaf.apache.org Subject: RE: unable to getRealPath for servlet context Thx for quick response. What is the equivalent to FileLocator in karaf? -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context- tp4032109p4032114.html Sent from the Karaf - User mailing list archive at Nabble.com.
RE: unable to getRealPath for servlet context
So I tried the recommended approach in this thread and getting the following now: karaf@root() start 265 karaf@root() java.lang.NullPointerException at org.eclipse.core.internal.runtime.Activator.getURLConverter(Activator.java:313) at org.eclipse.core.runtime.FileLocator.toFileURL(FileLocator.java:205) at com.foo.bar.config.MyConfigurator.contextInitialized(MyConfigurator.java:54) The url variable is null in the Eclipse debugger. NPE is happening in this line: String realPath = FileLocator.toFileURL(url).getPath(); b/c url is null... -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context-tp4032109p4032117.html Sent from the Karaf - User mailing list archive at Nabble.com.
RE: unable to getRealPath for servlet context
Sorry i replied before reading your reply a few mins ago. I didn't change the karaf.framework to equinox but i basically added the following dependency to my pom: dependency groupIdorg.mod4j.org.eclipse.equinox/groupId artifactIdcommon/artifactId version3.5.0/version /dependency and then i added 'WEB-INF/lib/common-3.5.0.jar' to Bundle-ClassPath manifest header in manifest.mf file. maybe i still need to make the change for karaf.framework=equinox for this to work... -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context-tp4032109p4032118.html Sent from the Karaf - User mailing list archive at Nabble.com.
RE: unable to getRealPath for servlet context
OK so I made the change in config.properties to point as follows: karaf.framework=equinox saved, stopped, started karaf in debug mode i've attached a debugger session in Eclipse and see now: url = bundleentry://243.fwk42630947/ so that's not null any more. now i get the following warning in karaf.log: 2014-03-05 22:04:49,528 | WARN | pool-16-thread-1 | AbstractLifeCycle | 122 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.14.v20131031 | FAILED HttpServiceContext{httpContext=WebAppHttpContext{com.nextgate.mm.AM - 243}}: java.lang.NoClassDefFoundError: org.eclipse.osgi.service.urlconversion.URLConverter java.lang.NoClassDefFoundError: org.eclipse.osgi.service.urlconversion.URLConverter at org.eclipse.core.internal.runtime.Activator.getURLConverter(Activator.java:318) at org.eclipse.core.runtime.FileLocator.toFileURL(FileLocator.java:205) any idea how to resolve this? also it doesn't go into the catch block (I added a brkpt there) and code execution seems to suddenly stop after this line: String realPath = FileLocator.toFileURL(url).getPath(); -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context-tp4032109p4032119.html Sent from the Karaf - User mailing list archive at Nabble.com.
RE: unable to getRealPath for servlet context
It seems that org.eclipse.osgi.service.urlconversion.URLConverter cannot be found, try to install the following bundle, make sure to find a correct version for your Equinox dependency groupIdorg.eclipse.osgi/groupId artifactIdorg.eclipse.osgi/artifactId /dependency -Original Message- From: asookazian2 [mailto:asookaz...@gmail.com] Sent: Thursday, March 06, 2014 2:13 PM To: user@karaf.apache.org Subject: RE: unable to getRealPath for servlet context OK so I made the change in config.properties to point as follows: karaf.framework=equinox saved, stopped, started karaf in debug mode i've attached a debugger session in Eclipse and see now: url = bundleentry://243.fwk42630947/ so that's not null any more. now i get the following warning in karaf.log: 2014-03-05 22:04:49,528 | WARN | pool-16-thread-1 | AbstractLifeCycle | 122 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.14.v20131031 | | FAILED HttpServiceContext{httpContext=WebAppHttpContext{com.nextgate.mm.AM - 243}}: java.lang.NoClassDefFoundError: org.eclipse.osgi.service.urlconversion.URLConverter java.lang.NoClassDefFoundError: org.eclipse.osgi.service.urlconversion.URLConverter at org.eclipse.core.internal.runtime.Activator.getURLConverter(Activator.java:3 18) at org.eclipse.core.runtime.FileLocator.toFileURL(FileLocator.java:205) any idea how to resolve this? also it doesn't go into the catch block (I added a brkpt there) and code execution seems to suddenly stop after this line: String realPath = FileLocator.toFileURL(url).getPath(); -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context- tp4032109p4032119.html Sent from the Karaf - User mailing list archive at Nabble.com.
Re: unable to getRealPath for servlet context
Hi, as already stated this won't work for an OSGi Servlet Container. All of the deployment together with those information are handled by Pax Web. Jetty is just the mere container to run the stuff. To retrieve the actual ServletPath you will need to call request. getServletPath() But, this will not solve your problem. You need to be aware that since you are running in Karaf some things do work different here. So an access to the deployment directory is not possible. Since there is no such deployment directory. So for your exact use-case one possibility would be to use an extracted war in the deploy folder, which isn't nice but could work. From an OSGi perspective I'd suggest using a fragment bundle for your configuration file, that is why the fragment bundle have been invented for, and attach this to your WAB. Another possibility, use the configuration admin Service for the custom configuration, with this solution you'll need to alter your wab a bit though, to get a hold of the configuration. regards, Achim 2014-03-06 1:12 GMT+01:00 asookazian2 asookaz...@gmail.com: I found the following Jetty bug report: http://jira.codehaus.org/browse/JETTY-675 http://jira.codehaus.org/browse/JETTY-675 User states the following: ServletContext.getRealPath() returns null instead of returning the root dir of the webapp I am able to reproduce this behavior in my WAR bundle with MANIFEST.MF (WAB (WebApplication Bundle)) deployment to Karaf 3.0.0. The version of Jetty is the following: karaf@root() feature:list | grep jetty cxf-http-jetty| 3.0.0-milestone2 | x | cxf-3.0.0-milestone2| jetty | 8.1.9.v20130131 | x | standard-3.0.0 | pax-jetty | 8.1.14.v20131031 | x | org.ops4j.pax.web-3.0.5 | Provide Jetty engine support I have tried ServletContext.getRealPath(); I have also tried ServletContext.getRealPath(/); Both code lines return null. Is this expected for this version of Karaf/Jetty? If yes, how can I determine the real path. Our scenario involves a properties file that may be installed in karaf/config/foo directory. We don't want to load/read the file from inside the WAR (e.g. WEB-INF) b/c the customer may need to change the file. -- View this message in context: http://karaf.922171.n3.nabble.com/unable-to-getRealPath-for-servlet-context-tp4032109.html Sent from the Karaf - User mailing list archive at Nabble.com. -- Apache Karaf http://karaf.apache.org/ Committer PMC OPS4J Pax Web http://wiki.ops4j.org/display/paxweb/Pax+Web/ Committer Project Lead blog http://notizblog.nierbeck.de/ Software Architect / Project Manager / Scrum Master