classpath - How to reference an included file in OSGi bundle when performing java.io.File or FileInputStream -
classpath - How to reference an included file in OSGi bundle when performing java.io.File or FileInputStream -
i using aqute bnd toolset create osgi bundle , have packaged dependant 'resource' files. includes *.css files , *.xsd files in resources directory have created.
i have included next in bundle.bnd file:
include-resource: resources/=resources/ and when build, generated *.jar file has *.css , *.xsd files in resources directory in top directory of jar bundle file.
however, in actual code having difficulty in trying refer part of class path:
i have tried following:
new file("resources/example.css"); i have tried:
url cssfile = this.getclass().getresource("resources/example.css"); seek { file = new file(cssfile.touri())); } catch(exception e) { e.printstacktrace(); } i either nullpointexception error or file cannot found ioexception error (depending 1 use). error when running in both eclipse equinox in debug configuration mode apache felix (which using our deployment). note trying in java classes outside of bundleactivator.
do need refer context of bundleactivator e.g.?
/* * (non-javadoc) * @see org.osgi.framework.bundleactivator#start(org.osgi.framework.bundlecontext) */ @override public void start(bundlecontext context) throws exception { /* service host bundle interface. */ servletcontainerservice service = new servletcontainerservice(); service.addservlet(new servletcontainer(new axisservlet(), true)); this.serverreg = context.registerservice(servletcontainerservice.class.getname(), service, null); cssfile = new file(context.getclass.getresource("resource/example.css")); } i think above work, mean have pass cssfile reference around not appear elegant.
is there way refer path of 'resources' directory included in bundle jar file in given java class part of bundle/.jar file? if involves bundlecontext, there way reference in java class?
any help much appreciated.
i have had @ , http://stackoverflow.com/questions/1411812/including-additional-resources-with-osgi-bundles appears need bundlecontext.
i might have found possible solution this: http://www.vogella.de/blog/tag/plugin/
looks vogella has illustration code this:
url url; seek { url = new url("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt"); inputstream inputstream = url.openconnection().getinputstream(); bufferedreader in = new bufferedreader(new inputstreamreader(inputstream)); string inputline; while ((inputline = in.readline()) != null) { system.out.println(inputline); } in.close(); } grab (ioexception e) { e.printstacktrace(); } does know if path same if isn't plugin , if using different osgi environments eg. equinox eclipse vs. apache felix? e.g. url = new url("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");
the bundle interface has getentry(java.lang.string path) method homecoming url , documented as:
returns url entry @ specified path in bundle. bundle's class loader not used search entry. contents of bundle searched entry. specified path relative root of bundle , may begin "/". path value of "/" indicates root of bundle.
java classpath osgi bundle
Comments
Post a Comment