How to get path to package resource directory?

Ok, next question :slight_smile:

How do I get the path to the resource folder for my current package (i.e. the one the service belongs to)?

And I guess in general it would be handy to be able to get the name of the package of the currently executing service.

Take a look at the Java API Reference and services in the WmRoot package. You should be able to use one or the other (or a combination) to get what you want. As always, use services from WmRoot at your own risk since their use is not supported.

  • Percio

PSUtilities.misc:getServiceName service will help to achieve your requirement.

Ajay

I have services that leverage existing, non-public methods that return this info. It’s on another machine that I won’t have access to until tomorrow. I’ll post the services then.

Here’s a Java service you can use to get the pub directory of a package. Add com.wm.app.b2b.server.Server to your imports of the Java service(s).

[highlight=java]
public static final void getPackagePubDir(IData pipeline)
throws ServiceException
{
IDataCursor idc = pipeline.getCursor();
String pkg = IDataUtil.getString(idc, “packageName”);

// Note: Server & Resources classes are not public classes. Future updates from wM may break this.
IDataUtil.put(idc, "pathname", Server.getResources().getPackagePubDir(pkg).getPath());
idc.destroy();

}[/highlight]

The service can be modified/cloned to get other directories using the following methods in place of getPackagePubDir:

getPackageResourceDir
getPackageConfigDir
getPackageTemplateDir
getPackageNSDir
getPackageCodeDir

There are others but you get the idea of the available methods. The nice part about using these is you don’t have to hard-code anything in your code to get the right stuff.

For those interested in getting just the config dir of a package, there is a public API available: ServerAPI.getPackageConfigDir(pkgName)

To get the package name of a given service (PSUtilities.misc:getServiceName doesn’t return the package name) the following service can be used. In fact, this service can get the package name of any namespace object, not just services (e.g. doc types, triggers, schemas, etc.)


public static final void getPackageName(IData pipeline)
throws ServiceException
{
IDataCursor idc = pipeline.getCursor();
String nsNameStr = IDataUtil.getString(idc, “nsName”);
com.wm.lang.ns.NSName nsname = com.wm.lang.ns.NSName.create(nsNameStr);
com.wm.app.b2b.server.ns.Namespace namesp = com.wm.app.b2b.server.ns.Namespace.current();
com.wm.lang.ns.NSNode nsnode = namesp.getNode(nsname);
if(nsnode != null)
IDataUtil.put(idc, “packageName”, nsnode.getPackage().getName());
idc.destroy();
}

Hope this helps.

Great stuff! That’s exactly what I needed. This way I can have code that loads files from within the package without knowing where the package has been deployed.

Part of me thinks we should be putting all this great knowledge into a wiki. Or does one exist already?

hi,i tried the code String nsNameStr = IDataUtil.getString(idc, “nsName”);and it’s returning NULL…Is the code compatible with WM7.1 ?

You should ‘nsName’ variable with a value in the pipeline.