Component mailComponent = new org.apache.camel.component.mail.MailComponent();
myCamelContext.addComponent("pop3", mailComponent);
myCamelContext.addComponent("imap", mailComponent);
myCamelContext.addComponent("smtp", mailComponent);
The second (and preferred) way to populate the map of named Component objects in the
CamelContext object is to let the CamelContext object perform lazy initialization. This
approach relies on developers following a convention when they write a class that implements
the Component interface. I illustrate the convention by an example. Let's assume you write a
class called com.example.myproject.FooComponent and you want Camel to
automatically recognize this by the name "foo". To do this, you have to write a properties file
called "META-INF/services/org/apache/camel/component/foo" (without a ".properties" file
extension) that has a single entry in it called class, the value of which is the fully-scoped name
of your class. This is shown below.
Listing 1.Listing 1. META-INF/services/org/apache/camel/component/fooMETA-INF/services/org/apache/camel/component/foo
class=com.example.myproject.FooComponent
If you want Camel to also recognize the class by the name "bar" then you write another
properties file in the same directory called "bar" that has the same contents. Once you have
written the properties file(s), you create a jar file that contains the
com.example.myproject.FooComponent class and the properties file(s), and you add
this jar file to your CLASSPATH. Then, when application-level code invokes
createEndpoint("foo:...") on a CamelContext object, Camel will find the "foo""
properties file on the CLASSPATH, get the value of the class property from that properties
file, and use reflection APIs to create an instance of the specified class.
As I said in Section 4.1 ("Endpoint"), Camel provides out-of-the-box support for numerous
communication technologies. The out-of-the-box support consists of classes that implement the
Component interface plus properties files that enable a CamelContext object to populate
its map of named Component objects.
Earlier in this section I gave the following example of calling
CamelContext.getEndpoint().
myCamelContext.getEndpoint("pop3://john.smith@mailserv.example.com?password=myPassword");
When I originally gave that example, I said that the parameter to getEndpoint() was a URI.
I said that because the online Camel documentation and the Camel source code both claim the
parameter is a URI. In reality, the parameter is restricted to being a URL. This is because when
Camel extracts the component name from the parameter, it looks for the first ":", which is a
simplistic algorithm. To understand why, recall from Section 4.4 ("The Meaning of URL, URI,
URN and IRI") that a URI can be a URL or a URN. Now consider the following calls to
getEndpoint.
13 CHAPTER 3 - GETTING STARTED WITH APACHE CAMEL