getCurrentTemplatePath() and inheritance

Hi,

I’m sure I’m missing something obvious here, but I hope somebody may be able to help:

I’ve got a base component base.cfc with an init() method that loads some Java classes. The JAR files for those classes are stored in a folder /lib/ that is relative to the location of that base.cfc.

So in this init-method in the base.cfc there is code like this:

partnerType = createObject( "java", "com.amazon.paapi5.v1.PartnerType", variables.oSDK.libPath ).ASSOCIATES;

variables.oSDK.libPath obviously needs to point to this /lib/ directory. So in my first attempt, I just set this with

variables.oSDK.libPath = "#getDirectoryFromPath(getCurrentTemplatePath())#/lib/";

For anything directly using an instance of base.cfc, this works.

Now this base.cfc is being extended by some other components that may reside anywhere in the file system - the extends parameter is using a mapping to point to that base.cfc. There may be child components that explicitly call SUPER.init() in their own init method.

In this case, getDirectoryFromPath(getCurrentTemplatePath()) seems to resolve to the current path of the child component, not to the path of the base.cfc. If the child component doesn’t reside directly next to base.cfc, this causes a problem, as the path to the /lib/ directory is resolved incorrectly.

The getCurrentTemplatePath() statement is not in the child component, nor does the child component override the value of variables.oSDK.libPath. Still, the path returned by getCurrentTemplatePath() always seems to resolve to the component being called instead of the component where the actual getCurrentTemplatePath() is being called.

I therefore need to hardcode the library path for it to resolve to the correct directory under any circumstance. Is there a workaround that would allow me to call the Java library under the correct path relative to the base.cfc in it’s init(), even if the base.cfc is being extended from somewhere else?

I’m on Lucee 5.4.3.15, if that matters.

Kind regards

Markus

Confirmed and ACF2023 seems to do this as well. I don’t think it’s what I would expect either although I guess the assumption is that inherited code is run as if it were written in the physical file being called. Perhaps GetCurrentTemplatePath() was intended more for included .cfm files rather than inherited components.

An obvious workaround would be to use an application mapping for your /lib directory.

2 Likes

The application mapping (or using any kind of hardcoded path) is a workaround, but not a solution. In this case, I’d like to have project repository of a Java API wrapper which includes the Java library and all of its dependencies. I would like to make sure that if another dev clones this repo and extends the parent component, the path is still resolved appropriately without a requirement to create an application mapping first, as this mapping depends on the location of the repository.

Currently getCurrentTemplatePath() does not have any arguments. It would be terrific if Lucee implemented an argument getCurrentTemplatePath(resolveChild=[true|false]) with default set to true (i.e. current behaviour), so using getCurrentTemplatePath(resolveChild=false) in case of extended CFCs would always return the path to the .cfc-file that actually features that statement in its code and never resolve to any descendant merely extending this component.

Understood. Another workaround might be to use the component metadata to determine the path:

parentDirectoryPath = GetDirectoryFromPath( GetMetaData( this ).extends.path )

This code lives in the base.cfc but only works if called from an immediate child. But if that’s your use case then it might be ok.

1 Like