001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/interceptor/InterceptorService.java,v 1.12 2007/07/04 03:05:46 phuongpdd Exp $
003: * $Author: phuongpdd $
004: * $Revision: 1.12 $
005: * $Date: 2007/07/04 03:05:46 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: * @author: Minh Nguyen
032: */
033: package net.myvietnam.mvncore.interceptor;
034:
035: import net.myvietnam.mvncore.MVNCoreConfig;
036: import net.myvietnam.mvncore.exception.InterceptorException;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: public class InterceptorService {
041:
042: private static Log log = LogFactory
043: .getLog(InterceptorService.class);
044:
045: //private static final String OPTION_FILE_NAME = "mvncore.xml";
046:
047: /** Singleton instance of this class */
048: private static InterceptorService instance = new InterceptorService();
049:
050: private MailInterceptor mailInterceptor = null;
051:
052: private ContentInterceptor contentInterceptor = null;
053:
054: private LoginIDInterceptor loginIDInterceptor = null;
055:
056: private PasswordInterceptor passwordInterceptor = null;
057:
058: /**
059: * Private constructor to prevent instantiation
060: */
061: private InterceptorService() {
062: loadInterceptorInfo();
063: }
064:
065: private void loadInterceptorInfo() {
066: String mailInterceptorClassName = MVNCoreConfig
067: .getMailInterceptorClassName();
068: loadMailInterceptor(mailInterceptorClassName);
069:
070: String contentInterceptorClassName = MVNCoreConfig
071: .getContentInterceptorClassName();
072: loadContentInterceptor(contentInterceptorClassName);
073:
074: String loginIDInterceptorClassName = MVNCoreConfig
075: .getLoginIdInterceptorClassName();
076: loadLoginIDInterceptor(loginIDInterceptorClassName);
077:
078: String passwordInterceptorClassName = MVNCoreConfig
079: .getPasswordInterceptorClassName();
080: loadPasswordInterceptor(passwordInterceptorClassName);
081:
082: }
083:
084: private void loadContentInterceptor(
085: String contentInterceptorClassName) {
086:
087: try {
088: if (contentInterceptorClassName.length() > 0) {
089: Class contentInterceptorClass = Class
090: .forName(contentInterceptorClassName);
091: ContentInterceptor interceptor = (ContentInterceptor) contentInterceptorClass
092: .newInstance();
093: setContentInterceptor(interceptor);
094: }
095: } catch (Exception ex) {
096: log.error("Cannot load ContentInterceptor", ex);
097: }
098: }
099:
100: private void loadMailInterceptor(String mailInterceptorClassName) {
101:
102: try {
103: if (mailInterceptorClassName.length() > 0) {
104: Class mailInterceptorClass = Class
105: .forName(mailInterceptorClassName);
106: MailInterceptor interceptor = (MailInterceptor) mailInterceptorClass
107: .newInstance();
108: setMailInterceptor(interceptor);
109: }
110: } catch (Exception ex) {
111: log.error("Cannot load MailInterceptor", ex);
112: }
113: }
114:
115: private void loadLoginIDInterceptor(
116: String loginIDInterceptorClassName) {
117:
118: try {
119: if (loginIDInterceptorClassName.length() > 0) {
120: Class loginIDInterceptorClass = Class
121: .forName(loginIDInterceptorClassName);
122: LoginIDInterceptor interceptor = (LoginIDInterceptor) loginIDInterceptorClass
123: .newInstance();
124: setLoginIDInterceptor(interceptor);
125: }
126: } catch (Exception ex) {
127: log.error("Cannot load LoginIDInterceptor", ex);
128: }
129: }
130:
131: private void loadPasswordInterceptor(
132: String passwordInterceptorClassName) {
133:
134: try {
135: if (passwordInterceptorClassName.length() > 0) {
136: Class passwordInterceptorClass = Class
137: .forName(passwordInterceptorClassName);
138:
139: PasswordInterceptor interceptor = (PasswordInterceptor) passwordInterceptorClass
140: .newInstance();
141: setPasswordInterceptor(interceptor);
142: }
143: } catch (Exception ex) {
144: log.error("Cannot load PasswordInterceptor", ex);
145: }
146: }
147:
148: /**
149: * Return singleton instance of this class
150: *
151: * @return InterceptorService the singleton instance of this class
152: */
153: public static InterceptorService getInstance() {
154: return instance;
155: }
156:
157: /**
158: * Validate email if the MailInterceptor is present
159: *
160: * @param email String email to be validated
161: * @throws InterceptorException if email is not valid for some reason
162: */
163: public void validateMail(String email) throws InterceptorException {
164: if (mailInterceptor != null) {
165: mailInterceptor.validateEmail(email);
166: }
167: }
168:
169: public MailInterceptor getMailInterceptor() {
170: return mailInterceptor;
171: }
172:
173: public void setMailInterceptor(MailInterceptor interceptor) {
174: log.info("Use MailInterceptor = " + interceptor);
175: this .mailInterceptor = interceptor;
176: }
177:
178: /**
179: * Validate content if the ContentInterceptor is present
180: *
181: * @param content String content to be validated
182: * @return the new content
183: * @throws InterceptorException if content is not valid for some reason
184: */
185: public String validateContent(String content)
186: throws InterceptorException {
187: if (contentInterceptor != null) {
188: return contentInterceptor.validateContent(content);
189: }
190: return content;
191: }
192:
193: public ContentInterceptor getContentInterceptor() {
194: return contentInterceptor;
195: }
196:
197: public void setContentInterceptor(ContentInterceptor interceptor) {
198: log.info("Use ContentInterceptor = " + interceptor);
199: this .contentInterceptor = interceptor;
200: }
201:
202: /**
203: * Validate loginID if the LoginIDInterceptor is present
204: *
205: * @param loginID String loginID to be validated
206: * @throws InterceptorException if loginID is not valid for some reason
207: */
208: public void validateLoginID(String loginID)
209: throws InterceptorException {
210: if (loginIDInterceptor != null) {
211: loginIDInterceptor.validateLoginID(loginID);
212: }
213: }
214:
215: public LoginIDInterceptor getLoginIDInterceptor() {
216: return loginIDInterceptor;
217: }
218:
219: public void setLoginIDInterceptor(LoginIDInterceptor interceptor) {
220: log.info("Use LoginIDInterceptor = " + interceptor);
221: this .loginIDInterceptor = interceptor;
222: }
223:
224: /**
225: * Validate password if the PasswordInterceptor is present
226: *
227: * @param password String password to be validated
228: * @throws InterceptorException if password is not valid for some reason
229: */
230: public void validatePassword(String password)
231: throws InterceptorException {
232: if (passwordInterceptor != null) {
233: passwordInterceptor.validatePassword(password);
234: }
235: }
236:
237: public PasswordInterceptor getPasswordInterceptor() {
238: return passwordInterceptor;
239: }
240:
241: public void setPasswordInterceptor(PasswordInterceptor interceptor) {
242: log.info("Use PasswordInterceptor = " + interceptor);
243: this.passwordInterceptor = interceptor;
244: }
245:
246: }
|