Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Riena Getting Started with Wiring"

m (Conclusion)
m (Getting Started with Wiring)
Line 3: Line 3:
 
Before you continue reading this please read [[Riena Getting Started with injecting services and extensions]].
 
Before you continue reading this please read [[Riena Getting Started with injecting services and extensions]].
  
=== Use case ===
+
== Use case ==
 
Within an Eclipse RCP based applications components often start their life cycle because they were contributed as an executable extension <code> org.eclipse.core.runtime.IConfigurationElement.createExecutableExtension(String)</code>.
 
Within an Eclipse RCP based applications components often start their life cycle because they were contributed as an executable extension <code> org.eclipse.core.runtime.IConfigurationElement.createExecutableExtension(String)</code>.
  
 
Who or what is responsible for injecting the needed services and/or extensions into these objects? And how does it know what to inject?
 
Who or what is responsible for injecting the needed services and/or extensions into these objects? And how does it know what to inject?
=== Wiring ===
+
== Wiring ==
 
The answer to this question is ''Wiring''. With the following ''sentence'' it is possible to initiate the wiring, i.e. perform all the necessary injections of services and extensions.
 
The answer to this question is ''Wiring''. With the following ''sentence'' it is possible to initiate the wiring, i.e. perform all the necessary injections of services and extensions.
 
<source lang=java>
 
<source lang=java>
Line 15: Line 15:
  
 
Wiring supports two options for telling what should be done.
 
Wiring supports two options for telling what should be done.
=== Wiring through a dedicated class ===
+
== Wiring through a dedicated class ==
 
This option is the most flexible but also the least convenient way. The class that needs services or extensions injected has to specify with an annotation another class which is responsible for the ''wiring''.
 
This option is the most flexible but also the least convenient way. The class that needs services or extensions injected has to specify with an annotation another class which is responsible for the ''wiring''.
 
The target class looks like that:
 
The target class looks like that:
Line 47: Line 47:
  
 
Since this seldom required there is another approach.
 
Since this seldom required there is another approach.
=== Wiring through annotations ===
+
== Wiring through annotations ==
 
The same ''wiring'' could be done with:
 
The same ''wiring'' could be done with:
 
<source lang=java>
 
<source lang=java>
Line 59: Line 59:
 
</source>
 
</source>
 
Oops, that´s all? Yes, it is. But as mentioned before less flexible but much more convenient. However, under the hood it will do exactly the same as shown in the ''less'' convenient approach.
 
Oops, that´s all? Yes, it is. But as mentioned before less flexible but much more convenient. However, under the hood it will do exactly the same as shown in the ''less'' convenient approach.
=== Back to our use case ===
+
== Back to our use case ==
 
If you define executable extensions that need to be wired, you don´t have to care. Just provide your class with the approach the suit your needs. And Riena´s extension injector will automatically do the wiring.
 
If you define executable extensions that need to be wired, you don´t have to care. Just provide your class with the approach the suit your needs. And Riena´s extension injector will automatically do the wiring.
  
 
Of course you can initiate wiring everywhere you want by explicitly calling <code>Wire...</code>
 
Of course you can initiate wiring everywhere you want by explicitly calling <code>Wire...</code>
  
=== Conclusion ===
+
== Conclusion ==
 
Riena´s wiring simplifies writing of components that need services or extensions injected. Especially when those components start their life cycle through executable extensions.
 
Riena´s wiring simplifies writing of components that need services or extensions injected. Especially when those components start their life cycle through executable extensions.

Revision as of 07:33, 30 April 2009

Getting Started with Wiring

Before you continue reading this please read Riena Getting Started with injecting services and extensions.

Use case

Within an Eclipse RCP based applications components often start their life cycle because they were contributed as an executable extension org.eclipse.core.runtime.IConfigurationElement.createExecutableExtension(String).

Who or what is responsible for injecting the needed services and/or extensions into these objects? And how does it know what to inject?

Wiring

The answer to this question is Wiring. With the following sentence it is possible to initiate the wiring, i.e. perform all the necessary injections of services and extensions.

Wire.instance(object).andStart(context);

Well, that looks really easy, but grübel grübel how does that work?

Wiring supports two options for telling what should be done.

Wiring through a dedicated class

This option is the most flexible but also the least convenient way. The class that needs services or extensions injected has to specify with an annotation another class which is responsible for the wiring. The target class looks like that:

@WireWith(SSLConfigurationWiring.class)
public class SSLConfiguration {
 
	public void configure(ISSLProperties properties) {
	..
	}
}

And the actual wiring is than performed by the class defined in the annotation:

public class SSLConfigurationWiring extends AbstractWiring {
	private ExtensionInjector sslInjector;
 
	@Override
	public void unwire(Object bean, BundleContext context) {
		sslInjector.stop();
	}
 
	@Override
	public void wire(Object bean, BundleContext context) {
		sslInjector = Inject.extension("org.eclipse.riena.communication.core.ssl").expectingMinMax(0, 1).into(bean).update(
				"configure").andStart(context); //$NON-NLS-1$
	}
}

This code uses the well known Inject. to perform the necessary injections. As mentioned before this the most flexible approach because within the wire and unwire methods you can do anything you like.

Since this seldom required there is another approach.

Wiring through annotations

The same wiring could be done with:

public class SSLConfiguration {
 
	@InjectExtension(id = "org.eclipse.riena.communication.core.ssl", min = 0, max = 1)
	public void configure(ISSLProperties properties) {
	..
	}
}

Oops, that´s all? Yes, it is. But as mentioned before less flexible but much more convenient. However, under the hood it will do exactly the same as shown in the less convenient approach.

Back to our use case

If you define executable extensions that need to be wired, you don´t have to care. Just provide your class with the approach the suit your needs. And Riena´s extension injector will automatically do the wiring.

Of course you can initiate wiring everywhere you want by explicitly calling Wire...

Conclusion

Riena´s wiring simplifies writing of components that need services or extensions injected. Especially when those components start their life cycle through executable extensions.

Back to the top