KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > interceptor > InterceptorService


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/interceptor/InterceptorService.java,v 1.10 2006/04/15 02:59:19 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.10 $
5  * $Date: 2006/04/15 02:59:19 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding MyVietnam and MyVietnam CoreLib
12  * MUST remain intact in the scripts and source code.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  *
28  * Correspondence and Marketing Questions can be sent to:
29  * info at MyVietnam net
30  *
31  * @author: Minh Nguyen
32  */

33 package net.myvietnam.mvncore.interceptor;
34
35 import net.myvietnam.mvncore.MVNCoreConfig;
36 import net.myvietnam.mvncore.exception.InterceptorException;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 public class InterceptorService {
41
42     private static Log log = LogFactory.getLog(InterceptorService.class);
43
44     //private static final String OPTION_FILE_NAME = "mvncore.xml";
45

46     /** Singleton instance of this class */
47     private static InterceptorService instance = new InterceptorService();
48
49     private MailInterceptor mailInterceptor = null;
50
51     private ContentInterceptor contentInterceptor = null;
52
53     private LoginIDInterceptor loginIDInterceptor = null;
54
55     /**
56      * Private constructor to prevent instantiation
57      */

58     private InterceptorService() {
59         loadInterceptorInfo();
60     }
61
62     private void loadInterceptorInfo() {
63         String JavaDoc mailInterceptorClassName = MVNCoreConfig.getMailInterceptorClassName();
64         loadMailInterceptor(mailInterceptorClassName);
65
66         String JavaDoc contentInterceptorClassName = MVNCoreConfig.getContentInterceptorClassName();
67         loadContentInterceptor(contentInterceptorClassName);
68
69         String JavaDoc loginIDInterceptorClassName = MVNCoreConfig.getLoginIdInterceptorClassName();
70         loadLoginIDInterceptor(loginIDInterceptorClassName);
71     }
72
73     private void loadContentInterceptor(String JavaDoc contentInterceptorClassName) {
74
75         try {
76             if (contentInterceptorClassName.length() > 0) {
77                 Class JavaDoc contentInterceptorClass = Class.forName(contentInterceptorClassName);
78                 ContentInterceptor interceptor = (ContentInterceptor)contentInterceptorClass.newInstance();
79                 setContentInterceptor(interceptor);
80             }
81         } catch (Exception JavaDoc ex) {
82             log.error("Cannot load ContentInterceptor", ex);
83         }
84     }
85
86     private void loadMailInterceptor(String JavaDoc mailInterceptorClassName) {
87
88         try {
89             if (mailInterceptorClassName.length() > 0) {
90                 Class JavaDoc mailInterceptorClass = Class.forName(mailInterceptorClassName);
91                 MailInterceptor interceptor = (MailInterceptor)mailInterceptorClass.newInstance();
92                 setMailInterceptor(interceptor);
93             }
94         } catch (Exception JavaDoc ex) {
95             log.error("Cannot load MailInterceptor", ex);
96         }
97     }
98
99     private void loadLoginIDInterceptor(String JavaDoc loginIDInterceptorClassName) {
100
101         try {
102             if (loginIDInterceptorClassName.length() > 0) {
103                 Class JavaDoc loginIDInterceptorClass = Class.forName(loginIDInterceptorClassName);
104                 LoginIDInterceptor interceptor = (LoginIDInterceptor)loginIDInterceptorClass.newInstance();
105                 setLoginIDInterceptor(interceptor);
106             }
107         } catch (Exception JavaDoc ex) {
108             log.error("Cannot load LoginIDInterceptor", ex);
109         }
110     }
111
112     /**
113      * Return singleton instance of this class
114      *
115      * @return InterceptorService the singleton instance of this class
116      */

117     public static InterceptorService getInstance() {
118         return instance;
119     }
120
121     /**
122      * Validate email if the MailInterceptor is present
123      *
124      * @param email String email to be validated
125      * @throws InterceptorException if email is not valid for some reason
126      */

127     public void validateMail(String JavaDoc email) throws InterceptorException {
128         if (mailInterceptor != null) {
129             mailInterceptor.validateEmail(email);
130         }
131     }
132
133     public MailInterceptor getMailInterceptor() {
134         return mailInterceptor;
135     }
136
137     public void setMailInterceptor(MailInterceptor interceptor) {
138         log.info("Use MailInterceptor = " + interceptor);
139         this.mailInterceptor = interceptor;
140     }
141
142     /**
143      * Validate content if the ContentInterceptor is present
144      *
145      * @param content String content to be validated
146      * @return the new content
147      * @throws InterceptorException if content is not valid for some reason
148      */

149     public String JavaDoc validateContent(String JavaDoc content) throws InterceptorException {
150         if (contentInterceptor != null) {
151             return contentInterceptor.validateContent(content);
152         }
153         return content;
154     }
155
156     public ContentInterceptor getContentInterceptor() {
157         return contentInterceptor;
158     }
159
160     public void setContentInterceptor(ContentInterceptor interceptor) {
161         log.info("Use ContentInterceptor = " + interceptor);
162         this.contentInterceptor = interceptor;
163     }
164
165     /**
166      * Validate loginID if the LoginIDInterceptor is present
167      *
168      * @param loginID String loginID to be validated
169      * @throws InterceptorException if loginID is not valid for some reason
170      */

171     public void validateLoginID(String JavaDoc loginID) throws InterceptorException {
172         if (loginIDInterceptor != null) {
173             loginIDInterceptor.validateLoginID(loginID);
174         }
175     }
176
177     public LoginIDInterceptor getLoginIDInterceptor() {
178         return loginIDInterceptor;
179     }
180
181     public void setLoginIDInterceptor(LoginIDInterceptor interceptor) {
182         log.info("Use LoginIDInterceptor = " + interceptor);
183         this.loginIDInterceptor = interceptor;
184     }
185
186 }
187
Popular Tags