public void start() throws EZBComponentException {
// Create and bind factories
for (MailItf factory : getAllFactories()) {
AbsJavaMailRef javaMailRef = null;
String jndiName = factory.getJNDIName();
// Create object and bind it
if (factory instanceof Session) {
JavaMailSessionRef javamailSession = new JavaMailSessionRef();
javaMailRef = javamailSession;
} else if (factory instanceof MimePart) {
JavaMailMimePartDataSourceRef mimePartDataSourceRef = new JavaMailMimePartDataSourceRef();
javaMailRef = mimePartDataSourceRef;
// Set properties of this factory.
MimePart mimePart = (MimePart) factory;
// Set subject
mimePartDataSourceRef.setSubject(mimePart.getSubject());
// Define addresses
List<Address> toAddresses = new ArrayList<Address>();
List<Address> ccAddresses = new ArrayList<Address>();
List<Address> bccAddresses = new ArrayList<Address>();
// Get value
for (MailAddress mail : mimePart.getMailAddresses()) {
// Build address object from the string
Address address = null;
try {
address = new InternetAddress(mail.getName());
} catch (AddressException e) {
throw new EZBComponentException("Cannot build an internet address with given value '" + mail.getName()
+ "'.", e);
}
// Add address in the correct list
if ("CC".equalsIgnoreCase(mail.getType())) {
ccAddresses.add(address);
} else if ("BCC".equalsIgnoreCase(mail.getType())) {
bccAddresses.add(address);
} else {
// Default is TO
toAddresses.add(address);
}
}
// Set recipients
mimePartDataSourceRef.setToRecipients(toAddresses.toArray(new Address[toAddresses.size()]));
mimePartDataSourceRef.setCcRecipients(ccAddresses.toArray(new Address[ccAddresses.size()]));
mimePartDataSourceRef.setBccRecipients(bccAddresses.toArray(new Address[bccAddresses.size()]));
} else {
throw new EZBComponentException("Unknown factory '" + factory + "'.");
}
// Set common values
javaMailRef.setName(factory.getName());
javaMailRef.setJNDIName(jndiName);
javaMailRef.setProperties(factory.getMailSessionProperties());
// Auth set for the factory ?
Auth auth = factory.getAuth();
// No, try to see for default auth ?
if (auth == null) {
auth = getAuth();
}
// There is authentication, set it
if (auth != null) {
javaMailRef.setAuthName(auth.getUsername());
javaMailRef.setAuthName(auth.getPassword());
}
// Bind object
try {
new InitialContext().bind(jndiName, javaMailRef);
logger.info("Binding {0} Mail factory with JNDI name {1}", javaMailRef.getType(), jndiName);
} catch (NamingException e) {
throw new EZBComponentException(
"Unable to bind the factory '" + factory + "' with JNDI name '" + jndiName + "'.", e);
}