Injecting values into custom plugin on init

Hi,

I have a custom plugin which provides access to a web service API. Different applications using this plugin might have different API keys for the service, so I’d like to be able to send the key value into the plugin as it is instantiated.

The plugin is not used within a handler, but within a service which provides application-specific methods, which in turn call the more generic plugin methods.

Within the service, I’m currently using the property:inject syntax to create the plugin.

Is there a way to send that API key into the plugin as it’s instantiated?

If not, assuming the plugin is injected as a property in the service’s pseudo-constructor, could I call a method setting the property in the service’s init() method? I’m unsure of the lifecycle in this case.

Thanks in advance for any help.

i would not use the DSL and init it in the method that is using it.

if the plugin does stuff specific to a ID, i would not go the route of treating it like a singleton and create a new instance of it every time it is used.

Only through wirebox bind map and aspects.

Hi Tom McNeer,

Just make sure that plugin is “not” a signleton then init() method you can override/set API-Keys values accordingly.

Documenation how you can create your own custom plugins and its internal workings.
http://wiki.coldbox.org/wiki/Plugins.cfm

re-read your post. if its app specific, like andrew said, use the binder in your wirebox config.

Sorry I missed everyone’s replies yesterday. I was out of the office all day.

Thanks for all the suggestions. The binder is what I was looking for. The key is app-specific, so there’s no reason not to make the plugin a singleton within the individual application. I just needed a way to configure it for each application.

If it was me, I would look into maybe the configure() method, which fires when ti is being configured or maybe an interception on the loading of a model, then you could do what you need to get the value you need and inject it back in or the case of the config() method on the model to store it in the component.

Hi Andrew,

I’m not quite following what you’re suggesting. I assume you mean the configure() method for the application itself, in Coldbox.cfc. If so, that makes sense as a place to store the API key for the application. But I’m not sure what you’re suggesting from there.

There are, as you say, various ways to move the value into the model - in this case, into the application-specific service into which the plugin is injected.

But how are you suggesting the value then be passed from that service to the injected plugin? Or am I missing something entirely?

No actually I mean on your injected component, I am not sure haven’t done it for awhile but anything that is injected will run the configure when it is being instantiated. So when it gets instantiated, it will look for that method and then call it. What you could do is put some logic in there to grab the value you need and then setup the value for that plugin.

I assume you are using getModel() to load your plugin?

Hi Andrew,

Thanks for the reply. No, I’m not using getModel().

The application-specific service is injected into my handler: property name=“variables.edmundsService” inject=“model:EdmundsService”;

And in my service, the plugin is injected with: property name=“variables.edmundsAPIService” inject=“coldbox:myPlugIn:EdmundsAPIService”;

Tom,

Yeah that is fine, WireBox is injecting it anyway so you can still use what was suggested.

Got it. Thanks for the help.