ClassLoading problems
ereze Mar 26, 2004 10:11 AMHi all,
In my efforts to work work with velocity to generate tempalted emails,
I got stuck with the classLoading problems, which I don't seem to pass.
I get the followig error:
" Unable to find resource 'resources.UserLoginInfo.vm' "
I tried a more general test to just load a simple properties file
and failed too...
ClassLoader classLoader = this.getClass().getClassLoader();
InputStream istream = classLoader.getResourceAsStream("resources.application.properties");
Note: the resource name is with a dot "." resource.application.propperties.
Now, it's strange cause the struts modules picks it up without any problem. Though here I called these lines from a method in one of my EJB entities. Anyway, in return I get null.
My application is contains the Logic (and EJB module) and Web (web module). the UserLoginInfo.vm is located under the resources directory.
Here is my Emailer.java:
Code:
/**
* A utility class that provides API for sending
* templated emails per application use case.
* <p>The <code>Emailer</code> uses Velocity SDK to generate
* the templated emails.
*
* @author Erez Efrati
* @version $Revision:$ $Date:$
*/
public class Emailer {
/**
* JNDI context for JavaMail sessions.
*/
private static final String JNDI_JAVA_MAIL = "java:/Mail";
/**
* The content type of the HTML email messages.
*/
private static final String HTML_CONTENT_TYPE = "text/html;charset=UTF-8";
/**
* The user-login-info email template resource name.
*/
private static final String VM_USER_LOGIN_INFO_REMINDER
= "resources.UserLoginInfo.vm";
// Velocity variable names keys
private static final String VMVAR_USER = "user";
private static final String VMVAR_SUBJECT = "subject";
/**
* the engine instance
*/
VelocityEngine m_engine = new VelocityEngine ();
// ------------------------------------------------------ Private Methods
/**
* Configures the engine to use classpath to find templates
*
* @param engine the engine to configure
* @throws Exception thrown if an errors occures
*/
private void configure (VelocityEngine engine) throws Exception
{
Properties props = new Properties ();
props.setProperty (VelocityEngine.RESOURCE_LOADER, "classpath");
props.setProperty ("classpath." + VelocityEngine.RESOURCE_LOADER + ".class",
ClasspathResourceLoader.class.getName());
m_engine.init (props);
}
/**
* Creates a Velocity context
*/
private VelocityContext createContext()
{
VelocityContext ctx = new VelocityContext ();
return ctx;
}
/**
* Retreives a new JavaMail Mime message
*
* @return
* @throws NamingException
*/
private MimeMessage createMimeMessage() throws NamingException
{
Context initCtx = new InitialContext ();
Session session = (Session)initCtx.lookup (JNDI_JAVA_MAIL);
MimeMessage message = new MimeMessage (session);
return message;
}
// ------------------------------------------------------ Public Methods
/**
* Default constructor, configures the velocity engine.
*
* @throws Exception, if an error occures
*/
public Emailer () throws Exception
{
configure (m_engine);
}
/**
* Sends an email to the specified <code>user</code> containing
* his login information which includes his username and password.
*
* @param user
* @throws Exception
*/
public void sendUserLoginInfo (User user) throws Exception
{
// retrieve the email template
Template template = m_engine.getTemplate (VM_USER_LOGIN_INFO_REMINDER);
// prepare the context and store data objects in it
VelocityContext ctx = createContext ();
ctx.put (VMVAR_USER, user);
// merge the template
StringWriter writer = new StringWriter ();
template.merge (ctx, writer);
writer.close ();
// retrieve the message subject and text. The subject
// is formulated as part of the email velocity template.
StringBuffer text = writer.getBuffer();
String subject = (String)ctx.get (VMVAR_SUBJECT);
// create a new message to work with
MimeMessage message = createMimeMessage ();
// retrieve the user's email address
String to = user.getEmail ();
String from = App.getSupportEmailAddress ();
message.setFrom (new InternetAddress (from));
message.addRecipient (Message.RecipientType.TO,
new InternetAddress (to));
// set the subject and content
message.setSubject (subject);
message.setContent (text, HTML_CONTENT_TYPE);
// Send message
Transport.send (message);
}
Why doesn't it work? What am I missing?
One more thing I maybe should point out as well - The emailer
is called from my 'UserServiceBean' which is a stateless Session bean.
I hope this is valid, is it?
I would really appreciate any help here.
Thanks in advance,
Erez