001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.identity.services.impl;
023:
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.io.StringWriter;
029: import java.net.URL;
030: import java.util.HashMap;
031: import java.util.Locale;
032: import java.util.Map;
033: import java.util.ResourceBundle;
034:
035: import org.jboss.logging.Logger;
036: import org.jboss.portal.core.identity.services.IdentityConstants;
037: import org.jboss.portal.core.identity.services.IdentityMailService;
038: import org.jboss.portal.core.identity.services.metadata.IdentityUIConfiguration;
039: import org.jboss.portal.core.identity.services.metadata.IdentityUIConfigurationService;
040: import org.jboss.portal.core.modules.MailModule;
041: import org.jboss.portal.jems.as.JNDI;
042: import org.jboss.portal.jems.as.system.AbstractJBossService;
043:
044: import freemarker.template.Configuration;
045: import freemarker.template.Template;
046: import freemarker.template.TemplateException;
047:
048: /**
049: * @author <a href="mailto:emuckenh@redhat.com">Emanuel Muckenhuber</a>
050: * @version $Revision$
051: */
052: public class IdentityMailServiceImpl extends AbstractJBossService
053: implements IdentityMailService {
054:
055: /** Template mail data */
056: private Map mailData = new HashMap();
057:
058: /** Requested locale */
059: private Locale locale;
060:
061: /** The e-mail text */
062: private String emailText = null;
063:
064: /** The e-mail domain */
065: private String emailDomain = null;
066:
067: /** E-Mail from */
068: private String emailFrom = null;
069:
070: /** Template location */
071: private String templateLocation;
072:
073: /** The mail module */
074: private MailModule mailModule;
075:
076: /** The core-identity configuration service */
077: private IdentityUIConfigurationService identityUIConfigurationService;
078:
079: /** . */
080: private static final Logger log = Logger
081: .getLogger(IdentityMailServiceImpl.class);
082:
083: /** . */
084: private JNDI.Binding jndiBinding;
085:
086: /** . */
087: private String jndiName = null;
088:
089: public void startService() throws Exception {
090: super .startService();
091:
092: IdentityUIConfiguration cf = this .identityUIConfigurationService
093: .getConfiguration();
094: this .emailDomain = cf.getEmailDomain();
095: this .emailFrom = cf.getEmailFrom();
096:
097: if (this .jndiName != null) {
098: jndiBinding = new JNDI.Binding(jndiName, this );
099: jndiBinding.bind();
100: }
101: }
102:
103: public void stopService() throws Exception {
104: super .stopService();
105:
106: if (jndiBinding != null) {
107: jndiBinding.unbind();
108: jndiBinding = null;
109: }
110: }
111:
112: public String getEmailDomain() {
113: return emailDomain;
114: }
115:
116: public void setEmailDomain(String emailDomain) {
117: this .emailDomain = emailDomain;
118: }
119:
120: public String getEmailFrom() {
121: return emailFrom;
122: }
123:
124: public void setEmailFrom(String emailFrom) {
125: this .emailFrom = emailFrom;
126: }
127:
128: public MailModule getMailModule() {
129: return mailModule;
130: }
131:
132: public void setMailModule(MailModule mailModule) {
133: this .mailModule = mailModule;
134: }
135:
136: public IdentityUIConfigurationService getIdentityUIConfigurationService() {
137: return identityUIConfigurationService;
138: }
139:
140: public void setIdentityUIConfigurationService(
141: IdentityUIConfigurationService identityUIConfigurationService) {
142: this .identityUIConfigurationService = identityUIConfigurationService;
143: }
144:
145: public String getJNDIName() {
146: return this .jndiName;
147: }
148:
149: public void setJNDIName(String jndiName) {
150: this .jndiName = jndiName;
151: }
152:
153: public void sendMail(String templateLocation, Map mailData,
154: Locale locale) throws IOException, TemplateException {
155: this .templateLocation = templateLocation;
156: this .mailData = mailData;
157: this .locale = locale;
158: this .mailData.put(IdentityConstants.EMAIL_DOMAIN, emailDomain);
159:
160: ResourceBundle bundle = ResourceBundle.getBundle(
161: "conf.bundles.Identity", locale);
162:
163: String subject = null;
164: String to = (String) mailData.get(IdentityConstants.EMAIL_TO);
165:
166: if (IdentityConstants.ACTION_REGISTER_USER
167: .equals(templateLocation)) {
168: subject = bundle
169: .getString("IDENTITY_MAIL_SUBJECT_REGISTER");
170: } else if (IdentityConstants.ACTION_CHANGE_EMAIL
171: .equals(templateLocation)) {
172: subject = bundle
173: .getString("IDENTITY_MAIL_SUBJECT_CHANGE_EMAIL");
174: } else if (IdentityConstants.ACTION_LOST_PASSWORD
175: .equals(templateLocation)) {
176: subject = bundle
177: .getString("IDENTITY_MAIL_SUBJECT_LOST_PASSWORD");
178: } else {
179: throw new IllegalArgumentException("unknown template.");
180: }
181:
182: // Generating message
183: this .generateEmailText();
184: // log.debug(this.emailText);
185:
186: // Sending mail
187: this .getMailModule().send(emailFrom, to, subject, emailText);
188:
189: }
190:
191: private void generateEmailText() throws IOException,
192: TemplateException {
193: ClassLoader tcl = Thread.currentThread()
194: .getContextClassLoader();
195: URL config = tcl.getResource("conf/templates/"
196: + this .templateLocation + "/emailTemplate_"
197: + locale.getLanguage() + "_" + locale.getCountry()
198: + ".tpl");
199: if (config == null) {
200: config = tcl.getResource("conf/templates/"
201: + this .templateLocation + "/emailTemplate_"
202: + locale.getLanguage() + ".tpl");
203: }
204: if (config == null) {
205: config = tcl.getResource("conf/templates/"
206: + this .templateLocation + "/emailTemplate.tpl");
207: }
208: if (config == null) {
209: throw new FileNotFoundException(
210: "Cannot load a suitable emailTemplate.tpl in conf/templates/"
211: + this .templateLocation);
212: }
213: InputStream in = config.openStream();
214: Template tpl = new Template("emailTemplate",
215: new InputStreamReader(in), new Configuration());
216: StringWriter out = new StringWriter();
217: tpl.process(this.mailData, out);
218: out.close();
219:
220: this.emailText = out.toString();
221: }
222: }
|