View | Details | Raw Unified | Return to bug 42989
Collapse All | Expand All

(-)C:/lenya/lenya-1.4-dev/src/modules/notification/java/src/org/apache/lenya/notification/EmailNotifier.java (-7 / +78 lines)
Lines 24-30 Link Here
24
import org.apache.avalon.framework.configuration.Configuration;
24
import org.apache.avalon.framework.configuration.Configuration;
25
import org.apache.avalon.framework.configuration.ConfigurationException;
25
import org.apache.avalon.framework.configuration.ConfigurationException;
26
import org.apache.avalon.framework.service.ServiceException;
26
import org.apache.avalon.framework.service.ServiceException;
27
import org.apache.avalon.framework.service.ServiceSelector;
28
import org.apache.cocoon.components.modules.input.InputModule;
27
import org.apache.cocoon.mail.MailSender;
29
import org.apache.cocoon.mail.MailSender;
30
import org.apache.cocoon.sitemap.PatternException;
28
import org.apache.lenya.ac.Identifiable;
31
import org.apache.lenya.ac.Identifiable;
29
import org.apache.lenya.ac.User;
32
import org.apache.lenya.ac.User;
30
import org.apache.lenya.inbox.InboxNotifier;
33
import org.apache.lenya.inbox.InboxNotifier;
Lines 34-41 Link Here
34
 */
37
 */
35
public class EmailNotifier extends InboxNotifier implements Configurable {
38
public class EmailNotifier extends InboxNotifier implements Configurable {
36
39
37
    protected void notify(User recipient, Message translatedMessage) throws NotificationException {
40
    protected ServiceSelector selector;
38
        
41
    
42
    protected void notify(User recipient, Message translatedMessage) 
43
                          throws NotificationException {
44
        initializeAttributesIfNotDone();
39
        super.notify(recipient, translatedMessage);
45
        super.notify(recipient, translatedMessage);
40
        
46
        
41
        if (!this.manager.hasService(MailSender.ROLE)) {
47
        if (!this.manager.hasService(MailSender.ROLE)) {
Lines 77-93 Link Here
77
        }
83
        }
78
    }
84
    }
79
85
86
    private boolean init;
80
    private String smtpHost;
87
    private String smtpHost;
81
    private String username;
88
    private String username;
82
    private String password;
89
    private String password;
90
    private Configuration conf;
83
91
84
    public void configure(Configuration config) throws ConfigurationException {
92
    public void configure(Configuration config) throws ConfigurationException {
85
        Configuration smtp = config.getChild("smtp");
93
        this.conf = config;
86
        this.smtpHost = smtp.getAttribute("host");
94
        this.init = false;
87
        this.username = smtp.getAttribute("username", null);
95
    }
88
        if (this.username != null) {
96
89
            this.password = smtp.getAttribute("password");
97
    public void initializeAttributesIfNotDone() throws NotificationException {
98
        if (this.init == true) {
99
            return;
100
        }
101
        try {
102
            Configuration smtp = conf.getChild("smtp");
103
            this.smtpHost = getAttr(smtp, "host", false);
104
            this.username = getAttr(smtp, "username", true);
105
            if (this.username != null) {
106
                this.password = getAttr(smtp, "password", false);
107
            }
108
        } catch (ConfigurationException e) {
109
            getLogger().error("Sending mail failed (configuration error): ", e);
110
            throw new NotificationException(e);            
111
        } catch (PatternException e) {
112
            getLogger().error("Sending mail failed (pattern error): ", e);
113
            throw new NotificationException(e);            
114
        }
115
        this.init = true;
116
    }
117
    
118
    private String getAttr(Configuration smtp, String attName, boolean defval)
119
            throws ConfigurationException, PatternException {
120
        String s;
121
        int start;
122
        
123
        if (defval == true) {
124
            s = smtp.getAttribute(attName, null);
125
        } else {
126
            s = smtp.getAttribute(attName);
90
        }
127
        }
128
        start = s.indexOf("${");
129
        if (start != -1) {
130
            s = s.substring(start + 2, s.length() - 1);
131
            s = resolveVariable(s);
132
        }
133
        return s;
91
    }
134
    }
92
135
136
    private String resolveVariable(String variableName) throws PatternException {
137
        InputModule module = null;
138
        String moduleName = "properties";
139
        
140
        if (this.selector == null) {
141
            try {
142
                // First access to a module : lookup selector
143
                this.selector = (ServiceSelector)this.manager.lookup(InputModule.ROLE + 
144
                                                                     "Selector");
145
            } catch(ServiceException ce) {
146
                throw new PatternException("Cannot access input modules selector", ce);
147
            }
148
        }
149
        try {
150
            module = (InputModule)this.selector.select(moduleName);        
151
            Object value = module.getAttribute(variableName, null, null);        
152
            if (value != null) {
153
                return (String)value;
154
            }        
155
        } catch(ServiceException compEx) {
156
            throw new PatternException("Cannot get module '" + moduleName + "'");            
157
        } catch(ConfigurationException confEx) {
158
            throw new PatternException("Cannot get variable '" + variableName + "'");            
159
        } finally {
160
            this.selector.release(module);
161
        }
162
        return "";
163
    }
93
}
164
}

Return to bug 42989