├── ch-10 └── account-parent │ ├── .classpath │ ├── .project │ ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs │ ├── account-captcha │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── captcha │ │ │ │ ├── AccountCaptchaException.java │ │ │ │ ├── AccountCaptchaService.java │ │ │ │ ├── AccountCaptchaServiceImpl.java │ │ │ │ └── RandomGenerator.java │ │ └── resources │ │ │ └── account-captcha.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── juvenxu │ │ └── mvnbook │ │ └── account │ │ └── captcha │ │ ├── AccountCaptchaServiceTest.java │ │ └── RandomGeneratorTest.java │ ├── account-email │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── email │ │ │ │ ├── AccountEmailException.java │ │ │ │ ├── AccountEmailService.java │ │ │ │ └── AccountEmailServiceImpl.java │ │ └── resources │ │ │ └── account-email.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── email │ │ │ └── AccountEmailServiceTest.java │ │ └── resources │ │ └── service.properties │ ├── account-persist │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── persist │ │ │ │ ├── Account.java │ │ │ │ ├── AccountPersistException.java │ │ │ │ ├── AccountPersistService.java │ │ │ │ └── AccountPersistServiceImpl.java │ │ └── resources │ │ │ └── account-persist.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── persist │ │ │ └── AccountPersistServiceTest.java │ │ └── resources │ │ └── account-service.properties │ └── pom.xml ├── ch-12 └── account-parent │ ├── .classpath │ ├── .project │ ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs │ ├── account-captcha │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── captcha │ │ │ │ ├── AccountCaptchaException.java │ │ │ │ ├── AccountCaptchaService.java │ │ │ │ ├── AccountCaptchaServiceImpl.java │ │ │ │ └── RandomGenerator.java │ │ └── resources │ │ │ └── account-captcha.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── juvenxu │ │ └── mvnbook │ │ └── account │ │ └── captcha │ │ ├── AccountCaptchaServiceTest.java │ │ └── RandomGeneratorTest.java │ ├── account-email │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── email │ │ │ │ ├── AccountEmailException.java │ │ │ │ ├── AccountEmailService.java │ │ │ │ └── AccountEmailServiceImpl.java │ │ └── resources │ │ │ └── account-email.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── email │ │ │ └── AccountEmailServiceTest.java │ │ └── resources │ │ └── account-service.properties │ ├── account-persist │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── persist │ │ │ │ ├── Account.java │ │ │ │ ├── AccountPersistException.java │ │ │ │ ├── AccountPersistService.java │ │ │ │ └── AccountPersistServiceImpl.java │ │ └── resources │ │ │ └── account-persist.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── persist │ │ │ └── AccountPersistServiceTest.java │ │ └── resources │ │ └── account-service.properties │ ├── account-service │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── service │ │ │ │ ├── AccountService.java │ │ │ │ ├── AccountServiceException.java │ │ │ │ ├── AccountServiceImpl.java │ │ │ │ └── SignUpRequest.java │ │ └── resources │ │ │ └── account-service.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── service │ │ │ └── AccountServiceTest.java │ │ └── resources │ │ └── account-service.properties │ ├── account-web │ ├── .classpath │ ├── .project │ ├── .settings │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── web │ │ │ ├── ActivateServlet.java │ │ │ ├── CaptchaImageServlet.java │ │ │ ├── LoginServlet.java │ │ │ └── SignUpServlet.java │ │ ├── resources │ │ └── account-service.properties │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ ├── login.jsp │ │ └── signup.jsp │ └── pom.xml ├── ch-17 └── maven-loc-plugin │ ├── .classpath │ ├── .project │ ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ ├── it │ └── app │ │ ├── pom.xml │ │ ├── src │ │ ├── main │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── App.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── juvenxu │ │ │ └── AppTest.java │ │ └── validate.groovy │ └── main │ └── java │ └── com │ └── juvenxu │ └── mvnbook │ └── loc │ └── CountMojo.java ├── ch-18 └── mvnbook-archetype-sample │ ├── .classpath │ ├── .project │ ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ └── main │ └── resources │ ├── META-INF │ └── maven │ │ └── archetype-metadata.xml │ └── archetype-resources │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ ├── App.java │ │ ├── dao │ │ │ └── Dao.java │ │ └── service │ │ │ └── Service.java │ └── resources │ │ └── config.properties │ └── test │ └── java │ └── AppTest.java ├── ch-3 └── hello-world │ ├── .classpath │ ├── .project │ ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── com │ │ └── juvenxu │ │ └── mvnbook │ │ └── helloworld │ │ └── HelloWorld.java │ └── test │ └── java │ └── com │ └── juvenxu │ └── mvnbook │ └── helloworld │ └── HelloWorldTest.java ├── ch-5 └── account-email │ ├── .classpath │ ├── .project │ ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── email │ │ │ ├── AccountEmailException.java │ │ │ ├── AccountEmailService.java │ │ │ └── AccountEmailServiceImpl.java │ └── resources │ │ └── account-email.xml │ └── test │ ├── java │ └── com │ │ └── juvenxu │ │ └── mvnbook │ │ └── account │ │ └── email │ │ └── AccountEmailServiceTest.java │ └── resources │ └── service.properties ├── ch-7 ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── juvenxu │ └── mvnbook │ └── template │ └── method │ └── AbstractBuild.java └── ch-8 ├── account-aggregator ├── account-email │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── email │ │ │ │ ├── AccountEmailException.java │ │ │ │ ├── AccountEmailService.java │ │ │ │ └── AccountEmailServiceImpl.java │ │ └── resources │ │ │ └── account-email.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── email │ │ │ └── AccountEmailServiceTest.java │ │ └── resources │ │ └── service.properties ├── account-parent │ └── pom.xml ├── account-persist │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── juvenxu │ │ │ │ └── mvnbook │ │ │ │ └── account │ │ │ │ └── persist │ │ │ │ ├── Account.java │ │ │ │ ├── AccountPersistException.java │ │ │ │ ├── AccountPersistService.java │ │ │ │ └── AccountPersistServiceImpl.java │ │ └── resources │ │ │ └── account-persist.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── persist │ │ │ └── AccountPersistServiceTest.java │ │ └── resources │ │ └── account-service.properties └── pom.xml └── account-parent ├── .classpath ├── .project ├── .settings ├── org.eclipse.jdt.core.prefs └── org.maven.ide.eclipse.prefs ├── account-email ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── email │ │ │ ├── AccountEmailException.java │ │ │ ├── AccountEmailService.java │ │ │ └── AccountEmailServiceImpl.java │ └── resources │ │ └── account-email.xml │ └── test │ ├── java │ └── com │ │ └── juvenxu │ │ └── mvnbook │ │ └── account │ │ └── email │ │ └── AccountEmailServiceTest.java │ └── resources │ └── service.properties ├── account-persist ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.maven.ide.eclipse.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── juvenxu │ │ │ └── mvnbook │ │ │ └── account │ │ │ └── persist │ │ │ ├── Account.java │ │ │ ├── AccountPersistException.java │ │ │ ├── AccountPersistService.java │ │ │ └── AccountPersistServiceImpl.java │ └── resources │ │ └── account-persist.xml │ └── test │ ├── java │ └── com │ │ └── juvenxu │ │ └── mvnbook │ │ └── account │ │ └── persist │ │ └── AccountPersistServiceTest.java │ └── resources │ └── account-service.properties └── pom.xml /ch-10/account-parent/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ch-10/account-parent/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-aggregator 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-10/account-parent/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Sun Feb 14 15:24:57 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 4 | org.eclipse.jdt.core.compiler.compliance=1.4 5 | org.eclipse.jdt.core.compiler.source=1.3 6 | -------------------------------------------------------------------------------- /ch-10/account-parent/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Sun Feb 14 15:24:57 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-captcha 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Wed Mar 24 17:07:29 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Wed Mar 24 16:30:18 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.juvenxu.mvnbook.account 6 | account-parent 7 | 1.0.0-SNAPSHOT 8 | 9 | 10 | account-captcha 11 | Account Captcha 12 | 13 | 14 | 2.3 15 | 16 | 17 | 18 | 19 | com.google.code.kaptcha 20 | kaptcha 21 | ${kaptcha.version} 22 | jdk15 23 | 24 | 25 | org.springframework 26 | spring-core 27 | 28 | 29 | org.springframework 30 | spring-beans 31 | 32 | 33 | org.springframework 34 | spring-context 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | 42 | 43 | 44 | sonatype-forge 45 | Sonatype Forge 46 | http://repository.sonatype.org/content/groups/forge/ 47 | 48 | true 49 | 50 | 51 | false 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | public class AccountCaptchaException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = 1339439433313285858L; 7 | 8 | public AccountCaptchaException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountCaptchaException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import java.util.List; 4 | 5 | public interface AccountCaptchaService 6 | { 7 | String generateCaptchaKey() 8 | throws AccountCaptchaException; 9 | 10 | byte[] generateCaptchaImage( String captchaKey ) 11 | throws AccountCaptchaException; 12 | 13 | boolean validateCaptcha( String captchaKey, String captchaValue ) 14 | throws AccountCaptchaException; 15 | 16 | List getPreDefinedTexts(); 17 | 18 | void setPreDefinedTexts( List preDefinedTexts ); 19 | } 20 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import java.awt.image.BufferedImage; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Properties; 10 | 11 | import javax.imageio.ImageIO; 12 | 13 | import org.springframework.beans.factory.InitializingBean; 14 | 15 | import com.google.code.kaptcha.impl.DefaultKaptcha; 16 | import com.google.code.kaptcha.util.Config; 17 | 18 | public class AccountCaptchaServiceImpl 19 | implements AccountCaptchaService, InitializingBean 20 | { 21 | private DefaultKaptcha producer; 22 | 23 | private Map captchaMap = new HashMap(); 24 | 25 | private List preDefinedTexts; 26 | 27 | private int textCount = 0; 28 | 29 | public void afterPropertiesSet() 30 | throws Exception 31 | { 32 | producer = new DefaultKaptcha(); 33 | 34 | producer.setConfig( new Config( new Properties() ) ); 35 | } 36 | 37 | public String generateCaptchaKey() 38 | { 39 | String key = RandomGenerator.getRandomString(); 40 | 41 | String value = getCaptchaText(); 42 | 43 | captchaMap.put( key, value ); 44 | 45 | return key; 46 | } 47 | 48 | public List getPreDefinedTexts() 49 | { 50 | return preDefinedTexts; 51 | } 52 | 53 | public void setPreDefinedTexts( List preDefinedTexts ) 54 | { 55 | this.preDefinedTexts = preDefinedTexts; 56 | } 57 | 58 | private String getCaptchaText() 59 | { 60 | if ( preDefinedTexts != null && !preDefinedTexts.isEmpty() ) 61 | { 62 | String text = preDefinedTexts.get( textCount ); 63 | 64 | textCount = ( textCount + 1 ) % preDefinedTexts.size(); 65 | 66 | return text; 67 | } 68 | else 69 | { 70 | return producer.createText(); 71 | } 72 | } 73 | 74 | public byte[] generateCaptchaImage( String captchaKey ) 75 | throws AccountCaptchaException 76 | { 77 | String text = captchaMap.get( captchaKey ); 78 | 79 | if ( text == null ) 80 | { 81 | throw new AccountCaptchaException( "Captch key '" + captchaKey + "' not found!" ); 82 | } 83 | 84 | BufferedImage image = producer.createImage( text ); 85 | 86 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 87 | 88 | try 89 | { 90 | ImageIO.write( image, "jpg", out ); 91 | } 92 | catch ( IOException e ) 93 | { 94 | throw new AccountCaptchaException( "Failed to write captcha stream!", e ); 95 | } 96 | 97 | return out.toByteArray(); 98 | } 99 | 100 | public boolean validateCaptcha( String captchaKey, String captchaValue ) 101 | throws AccountCaptchaException 102 | { 103 | String text = captchaMap.get( captchaKey ); 104 | 105 | if ( text == null ) 106 | { 107 | throw new AccountCaptchaException( "Captch key '" + captchaKey + "' not found!" ); 108 | } 109 | 110 | if ( text.equals( captchaValue ) ) 111 | { 112 | captchaMap.remove( captchaKey ); 113 | 114 | return true; 115 | } 116 | else 117 | { 118 | return false; 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/RandomGenerator.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import java.util.Random; 4 | 5 | public class RandomGenerator 6 | { 7 | private static String range = "0123456789abcdefghijklmnopqrstuvwxyz"; 8 | 9 | public static synchronized String getRandomString() 10 | { 11 | Random random = new Random(); 12 | 13 | StringBuffer result = new StringBuffer(); 14 | 15 | for ( int i = 0; i < 8; i++ ) 16 | { 17 | result.append( range.charAt( random.nextInt( range.length() ) ) ); 18 | } 19 | 20 | return result.toString(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/src/main/resources/account-captcha.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/src/test/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | import java.io.FileOutputStream; 7 | import java.io.OutputStream; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.springframework.context.ApplicationContext; 14 | import org.springframework.context.support.ClassPathXmlApplicationContext; 15 | 16 | public class AccountCaptchaServiceTest 17 | { 18 | private AccountCaptchaService service; 19 | 20 | @Before 21 | public void prepare() 22 | throws Exception 23 | { 24 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-captcha.xml" ); 25 | service = (AccountCaptchaService) ctx.getBean( "accountCaptchaService" ); 26 | } 27 | 28 | @Test 29 | public void testGenerateCaptcha() 30 | throws Exception 31 | { 32 | String captchaKey = service.generateCaptchaKey(); 33 | assertNotNull( captchaKey ); 34 | 35 | byte[] captchaImage = service.generateCaptchaImage( captchaKey ); 36 | assertTrue( captchaImage.length > 0 ); 37 | 38 | File image = new File( "target/" + captchaKey + ".jpg" ); 39 | OutputStream output = null; 40 | try 41 | { 42 | output = new FileOutputStream( image ); 43 | output.write( captchaImage ); 44 | } 45 | finally 46 | { 47 | if ( output != null ) 48 | { 49 | output.close(); 50 | } 51 | } 52 | assertTrue( image.exists() && image.length() > 0 ); 53 | } 54 | 55 | @Test 56 | public void testValidateCaptchaCorrect() 57 | throws Exception 58 | { 59 | List preDefinedTexts = new ArrayList(); 60 | preDefinedTexts.add( "12345" ); 61 | preDefinedTexts.add( "abcde" ); 62 | service.setPreDefinedTexts( preDefinedTexts ); 63 | 64 | String captchaKey = service.generateCaptchaKey(); 65 | service.generateCaptchaImage( captchaKey ); 66 | assertTrue( service.validateCaptcha( captchaKey, "12345" ) ); 67 | 68 | captchaKey = service.generateCaptchaKey(); 69 | service.generateCaptchaImage( captchaKey ); 70 | assertTrue( service.validateCaptcha( captchaKey, "abcde" ) ); 71 | } 72 | 73 | @Test 74 | public void testValidateCaptchaIncorrect() 75 | throws Exception 76 | { 77 | List preDefinedTexts = new ArrayList(); 78 | preDefinedTexts.add( "12345" ); 79 | service.setPreDefinedTexts( preDefinedTexts ); 80 | 81 | String captchaKey = service.generateCaptchaKey(); 82 | service.generateCaptchaImage( captchaKey ); 83 | assertFalse( service.validateCaptcha( captchaKey, "67890" ) ); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-captcha/src/test/java/com/juvenxu/mvnbook/account/captcha/RandomGeneratorTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import static org.junit.Assert.assertFalse; 4 | 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | 8 | import org.junit.Test; 9 | 10 | public class RandomGeneratorTest 11 | { 12 | @Test 13 | public void testGetRandomString() 14 | throws Exception 15 | { 16 | Set randoms = new HashSet( 100 ); 17 | 18 | for ( int i = 0; i < 100; i++ ) 19 | { 20 | String random = RandomGenerator.getRandomString(); 21 | 22 | assertFalse( randoms.contains( random ) ); 23 | 24 | randoms.add( random ); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-email 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Wed Mar 24 16:29:49 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:48 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | 10 | 11 | account-email 12 | Account Email 13 | 14 | 15 | 1.4.1 16 | 1.3.1b 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-core 23 | 24 | 25 | org.springframework 26 | spring-beans 27 | 28 | 29 | org.springframework 30 | spring-context 31 | 32 | 33 | org.springframework 34 | spring-context-support 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | javax.mail 42 | mail 43 | ${javax.mail.version} 44 | 45 | 46 | com.icegreen 47 | greenmail 48 | ${greenmail.version} 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public class AccountEmailException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -4817386460334501672L; 7 | 8 | public AccountEmailException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountEmailException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public interface AccountEmailService 4 | { 5 | void sendMail( String to, String subject, String htmlText ) 6 | throws AccountEmailException; 7 | } 8 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import javax.mail.MessagingException; 4 | import javax.mail.internet.MimeMessage; 5 | 6 | import org.springframework.mail.javamail.JavaMailSender; 7 | import org.springframework.mail.javamail.MimeMessageHelper; 8 | 9 | public class AccountEmailServiceImpl 10 | implements AccountEmailService 11 | { 12 | private JavaMailSender javaMailSender; 13 | 14 | private String systemEmail; 15 | 16 | public void sendMail( String to, String subject, String htmlText ) 17 | throws AccountEmailException 18 | { 19 | try 20 | { 21 | MimeMessage msg = javaMailSender.createMimeMessage(); 22 | MimeMessageHelper msgHelper = new MimeMessageHelper( msg ); 23 | 24 | msgHelper.setFrom( systemEmail ); 25 | msgHelper.setTo( to ); 26 | msgHelper.setSubject( subject ); 27 | msgHelper.setText( htmlText, true ); 28 | 29 | javaMailSender.send( msg ); 30 | } 31 | catch ( MessagingException e ) 32 | { 33 | throw new AccountEmailException( "Faild to send mail.", e ); 34 | } 35 | } 36 | 37 | public JavaMailSender getJavaMailSender() 38 | { 39 | return javaMailSender; 40 | } 41 | 42 | public void setJavaMailSender( JavaMailSender javaMailSender ) 43 | { 44 | this.javaMailSender = javaMailSender; 45 | } 46 | 47 | public String getSystemEmail() 48 | { 49 | return systemEmail; 50 | } 51 | 52 | public void setSystemEmail( String systemEmail ) 53 | { 54 | this.systemEmail = systemEmail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/src/main/resources/account-email.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ${email.auth} 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/src/test/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import static junit.framework.Assert.assertEquals; 4 | 5 | import javax.mail.Message; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.context.support.ClassPathXmlApplicationContext; 12 | 13 | import com.icegreen.greenmail.util.GreenMail; 14 | import com.icegreen.greenmail.util.GreenMailUtil; 15 | import com.icegreen.greenmail.util.ServerSetup; 16 | 17 | public class AccountEmailServiceTest 18 | { 19 | private GreenMail greenMail; 20 | 21 | @Before 22 | public void startMailServer() 23 | throws Exception 24 | { 25 | greenMail = new GreenMail( ServerSetup.SMTP ); 26 | greenMail.setUser( "test@juvenxu.com", "123456" ); 27 | greenMail.start(); 28 | } 29 | 30 | @Test 31 | public void testSendMail() 32 | throws Exception 33 | { 34 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" ); 35 | AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean( "accountEmailService" ); 36 | 37 | String subject = "Test Subject"; 38 | String htmlText = "

Test

"; 39 | accountEmailService.sendMail( "test2@juvenxu.com", subject, htmlText ); 40 | 41 | greenMail.waitForIncomingEmail( 2000, 1 ); 42 | 43 | Message[] msgs = greenMail.getReceivedMessages(); 44 | assertEquals( 1, msgs.length ); 45 | assertEquals( subject, msgs[0].getSubject() ); 46 | assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() ); 47 | } 48 | 49 | @After 50 | public void stopMailServer() 51 | throws Exception 52 | { 53 | greenMail.stop(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-email/src/test/resources/service.properties: -------------------------------------------------------------------------------- 1 | #email.protocol=smtps 2 | #email.host=smtp.gmail.com 3 | #email.port=465 4 | #email.username=your-id@gmail.com 5 | #email.password=your-password 6 | #email.auth=true 7 | #email.systemEmail=your-id@gmail.com 8 | 9 | email.protocol=smtp 10 | email.host=localhost 11 | email.port=25 12 | email.username=test@juvenxu.com 13 | email.password=123456 14 | email.auth=true 15 | email.systemEmail=test1@juvenxu.com -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-persist 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Wed Mar 24 16:29:49 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:48 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | 10 | 11 | account-persist 12 | Account Persist 13 | 14 | 15 | 1.6.1 16 | 17 | 18 | 19 | 20 | dom4j 21 | dom4j 22 | ${dom4j.version} 23 | 24 | 25 | org.springframework 26 | spring-core 27 | 28 | 29 | org.springframework 30 | spring-beans 31 | 32 | 33 | org.springframework 34 | spring-context 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | 42 | 43 | 44 | 45 | src/test/resources 46 | true 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/Account.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class Account 4 | { 5 | private String id; 6 | 7 | private String name; 8 | 9 | private String email; 10 | 11 | private String password; 12 | 13 | private boolean activated; 14 | 15 | public String getId() 16 | { 17 | return id; 18 | } 19 | 20 | public void setId( String id ) 21 | { 22 | this.id = id; 23 | } 24 | 25 | public String getName() 26 | { 27 | return name; 28 | } 29 | 30 | public void setName( String name ) 31 | { 32 | this.name = name; 33 | } 34 | 35 | public String getEmail() 36 | { 37 | return email; 38 | } 39 | 40 | public void setEmail( String email ) 41 | { 42 | this.email = email; 43 | } 44 | 45 | public String getPassword() 46 | { 47 | return password; 48 | } 49 | 50 | public void setPassword( String password ) 51 | { 52 | this.password = password; 53 | } 54 | 55 | public boolean isActivated() 56 | { 57 | return activated; 58 | } 59 | 60 | public void setActivated( boolean activated ) 61 | { 62 | this.activated = activated; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class AccountPersistException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -8334988626147289624L; 7 | 8 | public AccountPersistException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountPersistException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public interface AccountPersistService 4 | { 5 | Account createAccount( Account account ) 6 | throws AccountPersistException; 7 | 8 | Account readAccount( String id ) 9 | throws AccountPersistException; 10 | 11 | Account updateAccount( Account account ) 12 | throws AccountPersistException; 13 | 14 | void deleteAccount( String id ) 15 | throws AccountPersistException; 16 | } 17 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.OutputStreamWriter; 7 | import java.io.Writer; 8 | import java.util.List; 9 | 10 | import org.dom4j.Document; 11 | import org.dom4j.DocumentException; 12 | import org.dom4j.DocumentFactory; 13 | import org.dom4j.Element; 14 | import org.dom4j.io.OutputFormat; 15 | import org.dom4j.io.SAXReader; 16 | import org.dom4j.io.XMLWriter; 17 | 18 | public class AccountPersistServiceImpl 19 | implements AccountPersistService 20 | { 21 | private static final String ELEMENT_ROOT = "account-persist"; 22 | private static final String ELEMENT_ACCOUNTS = "accounts"; 23 | private static final String ELEMENT_ACCOUNT = "account"; 24 | private static final String ELEMENT_ACCOUNT_ID = "id"; 25 | private static final String ELEMENT_ACCOUNT_NAME = "name"; 26 | private static final String ELEMENT_ACCOUNT_EMAIL = "email"; 27 | private static final String ELEMENT_ACCOUNT_PASSWORD = "password"; 28 | private static final String ELEMENT_ACCOUNT_ACTIVATED = "activated"; 29 | 30 | private String file; 31 | 32 | private SAXReader reader = new SAXReader(); 33 | 34 | public String getFile() 35 | { 36 | return file; 37 | } 38 | 39 | public void setFile( String file ) 40 | { 41 | this.file = file; 42 | } 43 | 44 | public Account createAccount( Account account ) 45 | throws AccountPersistException 46 | { 47 | Document doc = readDocument(); 48 | 49 | Element accountsEle = doc.getRootElement().element(ELEMENT_ACCOUNTS); 50 | 51 | accountsEle.add( buildAccountElement( account ) ); 52 | 53 | writeDocument( doc ); 54 | 55 | return account; 56 | } 57 | 58 | @SuppressWarnings("unchecked") 59 | public void deleteAccount( String id ) 60 | throws AccountPersistException 61 | { 62 | Document doc = readDocument(); 63 | 64 | Element accountsEle = doc.getRootElement().element( ELEMENT_ACCOUNTS ); 65 | 66 | for ( Element accountEle : (List) accountsEle.elements() ) 67 | { 68 | if ( accountEle.elementText( ELEMENT_ACCOUNT_ID ).equals( id ) ) 69 | { 70 | accountEle.detach(); 71 | 72 | writeDocument( doc ); 73 | 74 | return; 75 | } 76 | } 77 | } 78 | 79 | @SuppressWarnings("unchecked") 80 | public Account readAccount( String id ) 81 | throws AccountPersistException 82 | { 83 | Document doc = readDocument(); 84 | 85 | Element accountsEle = doc.getRootElement().element( ELEMENT_ACCOUNTS ); 86 | 87 | for ( Element accountEle : (List) accountsEle.elements() ) 88 | { 89 | if ( accountEle.elementText( ELEMENT_ACCOUNT_ID ).equals( id ) ) 90 | { 91 | return buildAccount( accountEle ); 92 | } 93 | } 94 | 95 | return null; 96 | } 97 | 98 | public Account updateAccount( Account account ) 99 | throws AccountPersistException 100 | { 101 | if ( readAccount( account.getId() ) != null ) 102 | { 103 | deleteAccount( account.getId() ); 104 | 105 | return createAccount ( account ); 106 | } 107 | 108 | return null; 109 | } 110 | 111 | private Account buildAccount( Element element ) 112 | { 113 | Account account = new Account(); 114 | 115 | account.setId( element.elementText( ELEMENT_ACCOUNT_ID ) ); 116 | account.setName( element.elementText( ELEMENT_ACCOUNT_NAME ) ); 117 | account.setEmail( element.elementText( ELEMENT_ACCOUNT_EMAIL ) ); 118 | account.setPassword( element.elementText( ELEMENT_ACCOUNT_PASSWORD ) ); 119 | account.setActivated( ( "true".equals( element.elementText( ELEMENT_ACCOUNT_ACTIVATED ) ) ? true : false ) ); 120 | 121 | return account; 122 | } 123 | 124 | private Element buildAccountElement( Account account ) 125 | { 126 | Element element = DocumentFactory.getInstance().createElement( ELEMENT_ACCOUNT ); 127 | 128 | element.addElement( ELEMENT_ACCOUNT_ID ).setText( account.getId() ); 129 | element.addElement( ELEMENT_ACCOUNT_NAME ).setText( account.getName() ); 130 | element.addElement( ELEMENT_ACCOUNT_EMAIL ).setText( account.getEmail() ); 131 | element.addElement( ELEMENT_ACCOUNT_PASSWORD ).setText( account.getPassword() ); 132 | element.addElement( ELEMENT_ACCOUNT_ACTIVATED ).setText( account.isActivated() ? "true" : "false" ); 133 | 134 | return element; 135 | } 136 | 137 | private Document readDocument() 138 | throws AccountPersistException 139 | { 140 | File dataFile = new File( file ); 141 | 142 | if( !dataFile.exists() ) 143 | { 144 | dataFile.getParentFile().mkdirs(); 145 | 146 | Document doc = DocumentFactory.getInstance().createDocument(); 147 | 148 | Element rootEle = doc.addElement( ELEMENT_ROOT ); 149 | 150 | rootEle.addElement( ELEMENT_ACCOUNTS ); 151 | 152 | writeDocument( doc ); 153 | } 154 | 155 | try 156 | { 157 | return reader.read( new File( file ) ); 158 | } 159 | catch ( DocumentException e ) 160 | { 161 | throw new AccountPersistException( "Unable to read persist data xml", e ); 162 | } 163 | } 164 | 165 | private void writeDocument( Document doc ) 166 | throws AccountPersistException 167 | { 168 | Writer out = null; 169 | 170 | try 171 | { 172 | out = new OutputStreamWriter( new FileOutputStream( file ), "utf-8" ); 173 | 174 | XMLWriter writer = new XMLWriter( out, OutputFormat.createPrettyPrint() ); 175 | 176 | writer.write( doc ); 177 | } 178 | catch ( IOException e ) 179 | { 180 | throw new AccountPersistException( "Unable to write persist data xml", e ); 181 | } 182 | finally 183 | { 184 | try 185 | { 186 | if ( out != null) 187 | { 188 | out.close(); 189 | } 190 | } 191 | catch ( IOException e ) 192 | { 193 | throw new AccountPersistException( "Unable to close persist data xml writer", e ); 194 | } 195 | } 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/src/main/resources/account-persist.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/src/test/java/com/juvenxu/mvnbook/account/persist/AccountPersistServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.springframework.context.ApplicationContext; 10 | import org.springframework.context.support.ClassPathXmlApplicationContext; 11 | 12 | public class AccountPersistServiceTest 13 | { 14 | private AccountPersistService service; 15 | 16 | @Before 17 | public void prepare() 18 | throws Exception 19 | { 20 | File persistDataFile = new File ( "target/test-classes/persist-data.xml" ); 21 | 22 | if ( persistDataFile.exists() ) 23 | { 24 | persistDataFile.delete(); 25 | } 26 | 27 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-persist.xml" ); 28 | 29 | service = (AccountPersistService) ctx.getBean( "accountPersistService" ); 30 | 31 | Account account = new Account(); 32 | account.setId("juven"); 33 | account.setName("Juven Xu"); 34 | account.setEmail("juven@changeme.com"); 35 | account.setPassword("this_should_be_encrypted"); 36 | account.setActivated(true); 37 | 38 | service.createAccount(account); 39 | } 40 | 41 | @Test 42 | public void testReadAccount() 43 | throws Exception 44 | { 45 | Account account = service.readAccount( "juven" ); 46 | 47 | assertNotNull( account ); 48 | assertEquals( "juven", account.getId() ); 49 | assertEquals( "Juven Xu", account.getName() ); 50 | assertEquals( "juven@changeme.com", account.getEmail() ); 51 | assertEquals( "this_should_be_encrypted", account.getPassword() ); 52 | assertTrue( account.isActivated() ); 53 | } 54 | 55 | @Test 56 | public void testDeleteAccount() 57 | throws Exception 58 | { 59 | assertNotNull( service.readAccount( "juven" ) ); 60 | service.deleteAccount( "juven" ); 61 | assertNull( service.readAccount( "juven" ) ); 62 | } 63 | 64 | @Test 65 | public void testCreateAccount() 66 | throws Exception 67 | { 68 | assertNull( service.readAccount( "mike" ) ); 69 | 70 | Account account = new Account(); 71 | account.setId("mike"); 72 | account.setName("Mike"); 73 | account.setEmail("mike@changeme.com"); 74 | account.setPassword("this_should_be_encrypted"); 75 | account.setActivated(true); 76 | 77 | service.createAccount(account); 78 | 79 | assertNotNull( service.readAccount( "mike" )); 80 | } 81 | 82 | @Test 83 | public void testUpdateAccount() 84 | throws Exception 85 | { 86 | Account account = service.readAccount( "juven" ); 87 | 88 | account.setName("Juven Xu 1"); 89 | account.setEmail("juven1@changeme.com"); 90 | account.setPassword("this_still_should_be_encrypted"); 91 | account.setActivated(false); 92 | 93 | service.updateAccount( account ); 94 | 95 | account = service.readAccount( "juven" ); 96 | 97 | assertEquals( "Juven Xu 1", account.getName() ); 98 | assertEquals( "juven1@changeme.com", account.getEmail() ); 99 | assertEquals( "this_still_should_be_encrypted", account.getPassword() ); 100 | assertFalse( account.isActivated() ); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /ch-10/account-parent/account-persist/src/test/resources/account-service.properties: -------------------------------------------------------------------------------- 1 | persist.file=${project.build.testOutputDirectory}/persist-data.xml -------------------------------------------------------------------------------- /ch-10/account-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.juvenxu.mvnbook.account 5 | account-parent 6 | 1.0.0-SNAPSHOT 7 | pom 8 | Account Parent 9 | 10 | account-email 11 | account-persist 12 | account-captcha 13 | 14 | 15 | 2.5.6 16 | 4.7 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-core 23 | ${springframework.version} 24 | 25 | 26 | org.springframework 27 | spring-beans 28 | ${springframework.version} 29 | 30 | 31 | org.springframework 32 | spring-context 33 | ${springframework.version} 34 | 35 | 36 | org.springframework 37 | spring-context-support 38 | ${springframework.version} 39 | 40 | 41 | junit 42 | junit 43 | ${junit.version} 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.apache.maven.plugins 53 | maven-compiler-plugin 54 | 55 | 1.5 56 | 1.5 57 | 58 | 59 | 60 | org.apache.maven.plugins 61 | maven-resources-plugin 62 | 63 | UTF-8 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /ch-12/account-parent/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ch-12/account-parent/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-aggregator 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-12/account-parent/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Sun Feb 14 15:24:57 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 4 | org.eclipse.jdt.core.compiler.compliance=1.4 5 | org.eclipse.jdt.core.compiler.source=1.3 6 | -------------------------------------------------------------------------------- /ch-12/account-parent/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Sun Feb 14 15:24:57 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-captcha 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Thu Jul 01 16:54:16 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Wed Mar 24 16:30:18 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.juvenxu.mvnbook.account 6 | account-parent 7 | 1.0.0-SNAPSHOT 8 | 9 | 10 | account-captcha 11 | Account Captcha 12 | 13 | 14 | 2.3 15 | 16 | 17 | 18 | 19 | com.google.code.kaptcha 20 | kaptcha 21 | ${kaptcha.version} 22 | jdk15 23 | 24 | 25 | org.springframework 26 | spring-core 27 | 28 | 29 | org.springframework 30 | spring-beans 31 | 32 | 33 | org.springframework 34 | spring-context 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | 42 | 43 | 44 | sonatype-forge 45 | Sonatype Forge 46 | http://repository.sonatype.org/content/groups/forge/ 47 | 48 | true 49 | 50 | 51 | false 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | public class AccountCaptchaException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = 1339439433313285858L; 7 | 8 | public AccountCaptchaException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountCaptchaException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import java.util.List; 4 | 5 | public interface AccountCaptchaService 6 | { 7 | String generateCaptchaKey() 8 | throws AccountCaptchaException; 9 | 10 | byte[] generateCaptchaImage( String captchaKey ) 11 | throws AccountCaptchaException; 12 | 13 | boolean validateCaptcha( String captchaKey, String captchaValue ) 14 | throws AccountCaptchaException; 15 | 16 | List getPreDefinedTexts(); 17 | 18 | void setPreDefinedTexts( List preDefinedTexts ); 19 | } 20 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import java.awt.image.BufferedImage; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Properties; 10 | 11 | import javax.imageio.ImageIO; 12 | 13 | import org.springframework.beans.factory.InitializingBean; 14 | 15 | import com.google.code.kaptcha.impl.DefaultKaptcha; 16 | import com.google.code.kaptcha.util.Config; 17 | 18 | public class AccountCaptchaServiceImpl 19 | implements AccountCaptchaService, InitializingBean 20 | { 21 | private DefaultKaptcha producer; 22 | 23 | private Map captchaMap = new HashMap(); 24 | 25 | private List preDefinedTexts; 26 | 27 | private int textCount = 0; 28 | 29 | public void afterPropertiesSet() 30 | throws Exception 31 | { 32 | producer = new DefaultKaptcha(); 33 | 34 | producer.setConfig( new Config( new Properties() ) ); 35 | } 36 | 37 | public String generateCaptchaKey() 38 | { 39 | String key = RandomGenerator.getRandomString(); 40 | 41 | String value = getCaptchaText(); 42 | 43 | captchaMap.put( key, value ); 44 | 45 | return key; 46 | } 47 | 48 | public List getPreDefinedTexts() 49 | { 50 | return preDefinedTexts; 51 | } 52 | 53 | public void setPreDefinedTexts( List preDefinedTexts ) 54 | { 55 | this.preDefinedTexts = preDefinedTexts; 56 | } 57 | 58 | private String getCaptchaText() 59 | { 60 | if ( preDefinedTexts != null && !preDefinedTexts.isEmpty() ) 61 | { 62 | String text = preDefinedTexts.get( textCount ); 63 | 64 | textCount = ( textCount + 1 ) % preDefinedTexts.size(); 65 | 66 | return text; 67 | } 68 | else 69 | { 70 | return producer.createText(); 71 | } 72 | } 73 | 74 | public byte[] generateCaptchaImage( String captchaKey ) 75 | throws AccountCaptchaException 76 | { 77 | String text = captchaMap.get( captchaKey ); 78 | 79 | if ( text == null ) 80 | { 81 | throw new AccountCaptchaException( "Captch key '" + captchaKey + "' not found!" ); 82 | } 83 | 84 | BufferedImage image = producer.createImage( text ); 85 | 86 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 87 | 88 | try 89 | { 90 | ImageIO.write( image, "jpg", out ); 91 | } 92 | catch ( IOException e ) 93 | { 94 | throw new AccountCaptchaException( "Failed to write captcha stream!", e ); 95 | } 96 | 97 | return out.toByteArray(); 98 | } 99 | 100 | public boolean validateCaptcha( String captchaKey, String captchaValue ) 101 | throws AccountCaptchaException 102 | { 103 | String text = captchaMap.get( captchaKey ); 104 | 105 | if ( text == null ) 106 | { 107 | throw new AccountCaptchaException( "Captch key '" + captchaKey + "' not found!" ); 108 | } 109 | 110 | if ( text.equals( captchaValue ) ) 111 | { 112 | captchaMap.remove( captchaKey ); 113 | 114 | return true; 115 | } 116 | else 117 | { 118 | return false; 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/src/main/java/com/juvenxu/mvnbook/account/captcha/RandomGenerator.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import java.util.Random; 4 | 5 | public class RandomGenerator 6 | { 7 | private static String range = "0123456789abcdefghijklmnopqrstuvwxyz"; 8 | 9 | public static synchronized String getRandomString() 10 | { 11 | Random random = new Random(); 12 | 13 | StringBuffer result = new StringBuffer(); 14 | 15 | for ( int i = 0; i < 8; i++ ) 16 | { 17 | result.append( range.charAt( random.nextInt( range.length() ) ) ); 18 | } 19 | 20 | return result.toString(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/src/main/resources/account-captcha.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/src/test/java/com/juvenxu/mvnbook/account/captcha/AccountCaptchaServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | import java.io.FileOutputStream; 7 | import java.io.OutputStream; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.springframework.context.ApplicationContext; 14 | import org.springframework.context.support.ClassPathXmlApplicationContext; 15 | 16 | public class AccountCaptchaServiceTest 17 | { 18 | private AccountCaptchaService service; 19 | 20 | @Before 21 | public void prepare() 22 | throws Exception 23 | { 24 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-captcha.xml" ); 25 | service = (AccountCaptchaService) ctx.getBean( "accountCaptchaService" ); 26 | } 27 | 28 | @Test 29 | public void testGenerateCaptcha() 30 | throws Exception 31 | { 32 | String captchaKey = service.generateCaptchaKey(); 33 | assertNotNull( captchaKey ); 34 | 35 | byte[] captchaImage = service.generateCaptchaImage( captchaKey ); 36 | assertTrue( captchaImage.length > 0 ); 37 | 38 | File image = new File( "target/" + captchaKey + ".jpg" ); 39 | OutputStream output = null; 40 | try 41 | { 42 | output = new FileOutputStream( image ); 43 | output.write( captchaImage ); 44 | } 45 | finally 46 | { 47 | if ( output != null ) 48 | { 49 | output.close(); 50 | } 51 | } 52 | assertTrue( image.exists() && image.length() > 0 ); 53 | } 54 | 55 | @Test 56 | public void testValidateCaptchaCorrect() 57 | throws Exception 58 | { 59 | List preDefinedTexts = new ArrayList(); 60 | preDefinedTexts.add( "12345" ); 61 | preDefinedTexts.add( "abcde" ); 62 | service.setPreDefinedTexts( preDefinedTexts ); 63 | 64 | String captchaKey = service.generateCaptchaKey(); 65 | service.generateCaptchaImage( captchaKey ); 66 | assertTrue( service.validateCaptcha( captchaKey, "12345" ) ); 67 | 68 | captchaKey = service.generateCaptchaKey(); 69 | service.generateCaptchaImage( captchaKey ); 70 | assertTrue( service.validateCaptcha( captchaKey, "abcde" ) ); 71 | } 72 | 73 | @Test 74 | public void testValidateCaptchaIncorrect() 75 | throws Exception 76 | { 77 | List preDefinedTexts = new ArrayList(); 78 | preDefinedTexts.add( "12345" ); 79 | service.setPreDefinedTexts( preDefinedTexts ); 80 | 81 | String captchaKey = service.generateCaptchaKey(); 82 | service.generateCaptchaImage( captchaKey ); 83 | assertFalse( service.validateCaptcha( captchaKey, "67890" ) ); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-captcha/src/test/java/com/juvenxu/mvnbook/account/captcha/RandomGeneratorTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.captcha; 2 | 3 | import static org.junit.Assert.assertFalse; 4 | 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | 8 | import org.junit.Test; 9 | 10 | public class RandomGeneratorTest 11 | { 12 | @Test 13 | public void testGetRandomString() 14 | throws Exception 15 | { 16 | Set randoms = new HashSet( 100 ); 17 | 18 | for ( int i = 0; i < 100; i++ ) 19 | { 20 | String random = RandomGenerator.getRandomString(); 21 | 22 | assertFalse( randoms.contains( random ) ); 23 | 24 | randoms.add( random ); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-email 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Fri Sep 03 14:42:20 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:48 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | 10 | 11 | account-email 12 | Account Email 13 | 14 | 15 | 1.4.1 16 | 1.3.1b 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-core 23 | 24 | 25 | org.springframework 26 | spring-beans 27 | 28 | 29 | org.springframework 30 | spring-context 31 | 32 | 33 | org.springframework 34 | spring-context-support 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | javax.mail 42 | mail 43 | ${javax.mail.version} 44 | 45 | 46 | com.icegreen 47 | greenmail 48 | ${greenmail.version} 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public class AccountEmailException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -4817386460334501672L; 7 | 8 | public AccountEmailException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountEmailException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public interface AccountEmailService 4 | { 5 | void sendMail( String to, String subject, String htmlText ) 6 | throws AccountEmailException; 7 | } 8 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import javax.mail.MessagingException; 4 | import javax.mail.internet.MimeMessage; 5 | 6 | import org.springframework.mail.javamail.JavaMailSender; 7 | import org.springframework.mail.javamail.MimeMessageHelper; 8 | 9 | public class AccountEmailServiceImpl 10 | implements AccountEmailService 11 | { 12 | private JavaMailSender javaMailSender; 13 | 14 | private String systemEmail; 15 | 16 | public void sendMail( String to, String subject, String htmlText ) 17 | throws AccountEmailException 18 | { 19 | try 20 | { 21 | MimeMessage msg = javaMailSender.createMimeMessage(); 22 | MimeMessageHelper msgHelper = new MimeMessageHelper( msg ); 23 | 24 | msgHelper.setFrom( systemEmail ); 25 | msgHelper.setTo( to ); 26 | msgHelper.setSubject( subject ); 27 | msgHelper.setText( htmlText, true ); 28 | 29 | javaMailSender.send( msg ); 30 | } 31 | catch ( MessagingException e ) 32 | { 33 | throw new AccountEmailException( "Faild to send mail.", e ); 34 | } 35 | } 36 | 37 | public JavaMailSender getJavaMailSender() 38 | { 39 | return javaMailSender; 40 | } 41 | 42 | public void setJavaMailSender( JavaMailSender javaMailSender ) 43 | { 44 | this.javaMailSender = javaMailSender; 45 | } 46 | 47 | public String getSystemEmail() 48 | { 49 | return systemEmail; 50 | } 51 | 52 | public void setSystemEmail( String systemEmail ) 53 | { 54 | this.systemEmail = systemEmail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/src/main/resources/account-email.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ${email.auth} 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/src/test/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import static junit.framework.Assert.assertEquals; 4 | 5 | import javax.mail.Message; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.context.support.ClassPathXmlApplicationContext; 12 | 13 | import com.icegreen.greenmail.util.GreenMail; 14 | import com.icegreen.greenmail.util.GreenMailUtil; 15 | import com.icegreen.greenmail.util.ServerSetup; 16 | 17 | public class AccountEmailServiceTest 18 | { 19 | private GreenMail greenMail; 20 | 21 | @Before 22 | public void startMailServer() 23 | throws Exception 24 | { 25 | greenMail = new GreenMail( ServerSetup.SMTP ); 26 | greenMail.setUser( "test@juvenxu.com", "123456" ); 27 | greenMail.start(); 28 | } 29 | 30 | @Test 31 | public void testSendMail() 32 | throws Exception 33 | { 34 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" ); 35 | AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean( "accountEmailService" ); 36 | 37 | String subject = "Test Subject"; 38 | String htmlText = "

Test

"; 39 | accountEmailService.sendMail( "test2@juvenxu.com", subject, htmlText ); 40 | 41 | greenMail.waitForIncomingEmail( 2000, 1 ); 42 | 43 | Message[] msgs = greenMail.getReceivedMessages(); 44 | assertEquals( 1, msgs.length ); 45 | assertEquals( "admin@juvenxu.com", msgs[0].getFrom()[0].toString() ); 46 | assertEquals( subject, msgs[0].getSubject() ); 47 | assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() ); 48 | } 49 | 50 | @After 51 | public void stopMailServer() 52 | throws Exception 53 | { 54 | greenMail.stop(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-email/src/test/resources/account-service.properties: -------------------------------------------------------------------------------- 1 | #email.protocol=smtps 2 | #email.host=smtp.gmail.com 3 | #email.port=465 4 | #email.username=juvenshun@gmail.com 5 | #email.password=googleh8z5v0 6 | #email.auth=true 7 | 8 | email.protocol=smtp 9 | email.host=localhost 10 | email.port=25 11 | email.username=test@juvenxu.com 12 | email.password=123456 13 | email.auth=true 14 | email.systemEmail=admin@juvenxu.com -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-persist 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Thu Jul 01 16:54:16 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:48 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | 10 | 11 | account-persist 12 | Account Persist 13 | 14 | 15 | 1.6.1 16 | 17 | 18 | 19 | 20 | dom4j 21 | dom4j 22 | ${dom4j.version} 23 | 24 | 25 | org.springframework 26 | spring-core 27 | 28 | 29 | org.springframework 30 | spring-beans 31 | 32 | 33 | org.springframework 34 | spring-context 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | 42 | 43 | 44 | 45 | src/test/resources 46 | true 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/Account.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class Account 4 | { 5 | private String id; 6 | 7 | private String name; 8 | 9 | private String email; 10 | 11 | private String password; 12 | 13 | private boolean activated; 14 | 15 | public String getId() 16 | { 17 | return id; 18 | } 19 | 20 | public void setId( String id ) 21 | { 22 | this.id = id; 23 | } 24 | 25 | public String getName() 26 | { 27 | return name; 28 | } 29 | 30 | public void setName( String name ) 31 | { 32 | this.name = name; 33 | } 34 | 35 | public String getEmail() 36 | { 37 | return email; 38 | } 39 | 40 | public void setEmail( String email ) 41 | { 42 | this.email = email; 43 | } 44 | 45 | public String getPassword() 46 | { 47 | return password; 48 | } 49 | 50 | public void setPassword( String password ) 51 | { 52 | this.password = password; 53 | } 54 | 55 | public boolean isActivated() 56 | { 57 | return activated; 58 | } 59 | 60 | public void setActivated( boolean activated ) 61 | { 62 | this.activated = activated; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class AccountPersistException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -8334988626147289624L; 7 | 8 | public AccountPersistException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountPersistException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public interface AccountPersistService 4 | { 5 | Account createAccount( Account account ) 6 | throws AccountPersistException; 7 | 8 | Account readAccount( String id ) 9 | throws AccountPersistException; 10 | 11 | Account updateAccount( Account account ) 12 | throws AccountPersistException; 13 | 14 | void deleteAccount( String id ) 15 | throws AccountPersistException; 16 | } 17 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.OutputStreamWriter; 7 | import java.io.Writer; 8 | import java.util.List; 9 | 10 | import org.dom4j.Document; 11 | import org.dom4j.DocumentException; 12 | import org.dom4j.DocumentFactory; 13 | import org.dom4j.Element; 14 | import org.dom4j.io.OutputFormat; 15 | import org.dom4j.io.SAXReader; 16 | import org.dom4j.io.XMLWriter; 17 | 18 | public class AccountPersistServiceImpl 19 | implements AccountPersistService 20 | { 21 | private static final String ELEMENT_ROOT = "account-persist"; 22 | private static final String ELEMENT_ACCOUNTS = "accounts"; 23 | private static final String ELEMENT_ACCOUNT = "account"; 24 | private static final String ELEMENT_ACCOUNT_ID = "id"; 25 | private static final String ELEMENT_ACCOUNT_NAME = "name"; 26 | private static final String ELEMENT_ACCOUNT_EMAIL = "email"; 27 | private static final String ELEMENT_ACCOUNT_PASSWORD = "password"; 28 | private static final String ELEMENT_ACCOUNT_ACTIVATED = "activated"; 29 | 30 | private String file; 31 | 32 | private SAXReader reader = new SAXReader(); 33 | 34 | public String getFile() 35 | { 36 | return file; 37 | } 38 | 39 | public void setFile( String file ) 40 | { 41 | this.file = file; 42 | } 43 | 44 | public Account createAccount( Account account ) 45 | throws AccountPersistException 46 | { 47 | Document doc = readDocument(); 48 | 49 | Element accountsEle = doc.getRootElement().element(ELEMENT_ACCOUNTS); 50 | 51 | accountsEle.add( buildAccountElement( account ) ); 52 | 53 | writeDocument( doc ); 54 | 55 | return account; 56 | } 57 | 58 | @SuppressWarnings("unchecked") 59 | public void deleteAccount( String id ) 60 | throws AccountPersistException 61 | { 62 | Document doc = readDocument(); 63 | 64 | Element accountsEle = doc.getRootElement().element( ELEMENT_ACCOUNTS ); 65 | 66 | for ( Element accountEle : (List) accountsEle.elements() ) 67 | { 68 | if ( accountEle.elementText( ELEMENT_ACCOUNT_ID ).equals( id ) ) 69 | { 70 | accountEle.detach(); 71 | 72 | writeDocument( doc ); 73 | 74 | return; 75 | } 76 | } 77 | } 78 | 79 | @SuppressWarnings("unchecked") 80 | public Account readAccount( String id ) 81 | throws AccountPersistException 82 | { 83 | Document doc = readDocument(); 84 | 85 | Element accountsEle = doc.getRootElement().element( ELEMENT_ACCOUNTS ); 86 | 87 | for ( Element accountEle : (List) accountsEle.elements() ) 88 | { 89 | if ( accountEle.elementText( ELEMENT_ACCOUNT_ID ).equals( id ) ) 90 | { 91 | return buildAccount( accountEle ); 92 | } 93 | } 94 | 95 | return null; 96 | } 97 | 98 | public Account updateAccount( Account account ) 99 | throws AccountPersistException 100 | { 101 | if ( readAccount( account.getId() ) != null ) 102 | { 103 | deleteAccount( account.getId() ); 104 | 105 | return createAccount ( account ); 106 | } 107 | 108 | return null; 109 | } 110 | 111 | private Account buildAccount( Element element ) 112 | { 113 | Account account = new Account(); 114 | 115 | account.setId( element.elementText( ELEMENT_ACCOUNT_ID ) ); 116 | account.setName( element.elementText( ELEMENT_ACCOUNT_NAME ) ); 117 | account.setEmail( element.elementText( ELEMENT_ACCOUNT_EMAIL ) ); 118 | account.setPassword( element.elementText( ELEMENT_ACCOUNT_PASSWORD ) ); 119 | account.setActivated( ( "true".equals( element.elementText( ELEMENT_ACCOUNT_ACTIVATED ) ) ? true : false ) ); 120 | 121 | return account; 122 | } 123 | 124 | private Element buildAccountElement( Account account ) 125 | { 126 | Element element = DocumentFactory.getInstance().createElement( ELEMENT_ACCOUNT ); 127 | 128 | element.addElement( ELEMENT_ACCOUNT_ID ).setText( account.getId() ); 129 | element.addElement( ELEMENT_ACCOUNT_NAME ).setText( account.getName() ); 130 | element.addElement( ELEMENT_ACCOUNT_EMAIL ).setText( account.getEmail() ); 131 | element.addElement( ELEMENT_ACCOUNT_PASSWORD ).setText( account.getPassword() ); 132 | element.addElement( ELEMENT_ACCOUNT_ACTIVATED ).setText( account.isActivated() ? "true" : "false" ); 133 | 134 | return element; 135 | } 136 | 137 | private Document readDocument() 138 | throws AccountPersistException 139 | { 140 | File dataFile = new File( file ); 141 | 142 | if( !dataFile.exists() ) 143 | { 144 | dataFile.getParentFile().mkdirs(); 145 | 146 | Document doc = DocumentFactory.getInstance().createDocument(); 147 | 148 | Element rootEle = doc.addElement( ELEMENT_ROOT ); 149 | 150 | rootEle.addElement( ELEMENT_ACCOUNTS ); 151 | 152 | writeDocument( doc ); 153 | } 154 | 155 | try 156 | { 157 | return reader.read( new File( file ) ); 158 | } 159 | catch ( DocumentException e ) 160 | { 161 | throw new AccountPersistException( "Unable to read persist data xml", e ); 162 | } 163 | } 164 | 165 | private void writeDocument( Document doc ) 166 | throws AccountPersistException 167 | { 168 | Writer out = null; 169 | 170 | try 171 | { 172 | out = new OutputStreamWriter( new FileOutputStream( file ), "utf-8" ); 173 | 174 | XMLWriter writer = new XMLWriter( out, OutputFormat.createPrettyPrint() ); 175 | 176 | writer.write( doc ); 177 | } 178 | catch ( IOException e ) 179 | { 180 | throw new AccountPersistException( "Unable to write persist data xml", e ); 181 | } 182 | finally 183 | { 184 | try 185 | { 186 | if ( out != null) 187 | { 188 | out.close(); 189 | } 190 | } 191 | catch ( IOException e ) 192 | { 193 | throw new AccountPersistException( "Unable to close persist data xml writer", e ); 194 | } 195 | } 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/src/main/resources/account-persist.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/src/test/java/com/juvenxu/mvnbook/account/persist/AccountPersistServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.springframework.context.ApplicationContext; 10 | import org.springframework.context.support.ClassPathXmlApplicationContext; 11 | 12 | public class AccountPersistServiceTest 13 | { 14 | private AccountPersistService service; 15 | 16 | @Before 17 | public void prepare() 18 | throws Exception 19 | { 20 | File persistDataFile = new File ( "target/test-classes/persist-data.xml" ); 21 | 22 | if ( persistDataFile.exists() ) 23 | { 24 | persistDataFile.delete(); 25 | } 26 | 27 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-persist.xml" ); 28 | 29 | service = (AccountPersistService) ctx.getBean( "accountPersistService" ); 30 | 31 | Account account = new Account(); 32 | account.setId("juven"); 33 | account.setName("Juven Xu"); 34 | account.setEmail("juven@changeme.com"); 35 | account.setPassword("this_should_be_encrypted"); 36 | account.setActivated(true); 37 | 38 | service.createAccount(account); 39 | } 40 | 41 | @Test 42 | public void testReadAccount() 43 | throws Exception 44 | { 45 | Account account = service.readAccount( "juven" ); 46 | 47 | assertNotNull( account ); 48 | assertEquals( "juven", account.getId() ); 49 | assertEquals( "Juven Xu", account.getName() ); 50 | assertEquals( "juven@changeme.com", account.getEmail() ); 51 | assertEquals( "this_should_be_encrypted", account.getPassword() ); 52 | assertTrue( account.isActivated() ); 53 | } 54 | 55 | @Test 56 | public void testDeleteAccount() 57 | throws Exception 58 | { 59 | assertNotNull( service.readAccount( "juven" ) ); 60 | service.deleteAccount( "juven" ); 61 | assertNull( service.readAccount( "juven" ) ); 62 | } 63 | 64 | @Test 65 | public void testCreateAccount() 66 | throws Exception 67 | { 68 | assertNull( service.readAccount( "mike" ) ); 69 | 70 | Account account = new Account(); 71 | account.setId("mike"); 72 | account.setName("Mike"); 73 | account.setEmail("mike@changeme.com"); 74 | account.setPassword("this_should_be_encrypted"); 75 | account.setActivated(true); 76 | 77 | service.createAccount(account); 78 | 79 | assertNotNull( service.readAccount( "mike" )); 80 | } 81 | 82 | @Test 83 | public void testUpdateAccount() 84 | throws Exception 85 | { 86 | Account account = service.readAccount( "juven" ); 87 | 88 | account.setName("Juven Xu 1"); 89 | account.setEmail("juven1@changeme.com"); 90 | account.setPassword("this_still_should_be_encrypted"); 91 | account.setActivated(false); 92 | 93 | service.updateAccount( account ); 94 | 95 | account = service.readAccount( "juven" ); 96 | 97 | assertEquals( "Juven Xu 1", account.getName() ); 98 | assertEquals( "juven1@changeme.com", account.getEmail() ); 99 | assertEquals( "this_still_should_be_encrypted", account.getPassword() ); 100 | assertFalse( account.isActivated() ); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-persist/src/test/resources/account-service.properties: -------------------------------------------------------------------------------- 1 | persist.file=${project.build.testOutputDirectory}/persist-data.xml -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-service 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Thu Jul 01 16:54:17 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Sun May 09 19:38:56 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.juvenxu.mvnbook.account 6 | account-parent 7 | 1.0.0-SNAPSHOT 8 | 9 | 10 | account-service 11 | Account Service 12 | 13 | 14 | 1.3.1b 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | account-email 21 | ${project.version} 22 | 23 | 24 | ${project.groupId} 25 | account-persist 26 | ${project.version} 27 | 28 | 29 | ${project.groupId} 30 | account-captcha 31 | ${project.version} 32 | 33 | 34 | junit 35 | junit 36 | 37 | 38 | com.icegreen 39 | greenmail 40 | ${greenmail.version} 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | src/test/resources 49 | true 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/src/main/java/com/juvenxu/mvnbook/account/service/AccountService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.service; 2 | 3 | public interface AccountService 4 | { 5 | String generateCaptchaKey() 6 | throws AccountServiceException; 7 | 8 | byte[] generateCaptchaImage( String captchaKey ) 9 | throws AccountServiceException; 10 | 11 | void signUp( SignUpRequest signUpRequest ) 12 | throws AccountServiceException; 13 | 14 | void activate( String activationNumber ) 15 | throws AccountServiceException; 16 | 17 | void login( String id, String password ) 18 | throws AccountServiceException; 19 | } 20 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/src/main/java/com/juvenxu/mvnbook/account/service/AccountServiceException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.service; 2 | 3 | public class AccountServiceException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = 949282286716874926L; 7 | 8 | public AccountServiceException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountServiceException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/src/main/java/com/juvenxu/mvnbook/account/service/SignUpRequest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.service; 2 | 3 | public class SignUpRequest 4 | { 5 | private String id; 6 | 7 | private String email; 8 | 9 | private String name; 10 | 11 | private String password; 12 | 13 | private String confirmPassword; 14 | 15 | private String captchaKey; 16 | 17 | private String captchaValue; 18 | 19 | private String activateServiceUrl; 20 | 21 | public String getId() 22 | { 23 | return id; 24 | } 25 | 26 | public void setId( String id ) 27 | { 28 | this.id = id; 29 | } 30 | 31 | public String getEmail() 32 | { 33 | return email; 34 | } 35 | 36 | public void setEmail( String email ) 37 | { 38 | this.email = email; 39 | } 40 | 41 | public String getName() 42 | { 43 | return name; 44 | } 45 | 46 | public void setName( String name ) 47 | { 48 | this.name = name; 49 | } 50 | 51 | public String getPassword() 52 | { 53 | return password; 54 | } 55 | 56 | public void setPassword( String password ) 57 | { 58 | this.password = password; 59 | } 60 | 61 | public String getConfirmPassword() 62 | { 63 | return confirmPassword; 64 | } 65 | 66 | public void setConfirmPassword( String confirmPassword ) 67 | { 68 | this.confirmPassword = confirmPassword; 69 | } 70 | 71 | public String getCaptchaKey() 72 | { 73 | return captchaKey; 74 | } 75 | 76 | public void setCaptchaKey( String captchaKey ) 77 | { 78 | this.captchaKey = captchaKey; 79 | } 80 | 81 | public String getCaptchaValue() 82 | { 83 | return captchaValue; 84 | } 85 | 86 | public void setCaptchaValue( String captchaValue ) 87 | { 88 | this.captchaValue = captchaValue; 89 | } 90 | 91 | public String getActivateServiceUrl() 92 | { 93 | return activateServiceUrl; 94 | } 95 | 96 | public void setActivateServiceUrl( String activateServiceUrl ) 97 | { 98 | this.activateServiceUrl = activateServiceUrl; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/src/main/resources/account-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/src/test/java/com/juvenxu/mvnbook/account/service/AccountServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.service; 2 | 3 | import static junit.framework.Assert.*; 4 | 5 | import java.io.File; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import javax.mail.Message; 10 | 11 | import org.junit.After; 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.springframework.context.ApplicationContext; 15 | import org.springframework.context.support.ClassPathXmlApplicationContext; 16 | 17 | import com.icegreen.greenmail.util.GreenMail; 18 | import com.icegreen.greenmail.util.GreenMailUtil; 19 | import com.icegreen.greenmail.util.ServerSetup; 20 | import com.juvenxu.mvnbook.account.captcha.AccountCaptchaService; 21 | 22 | public class AccountServiceTest 23 | { 24 | private GreenMail greenMail; 25 | 26 | private AccountService accountService; 27 | 28 | @Before 29 | public void prepare() 30 | throws Exception 31 | { 32 | String[] springConfigFiles = { 33 | "account-email.xml", 34 | "account-persist.xml", 35 | "account-captcha.xml", 36 | "account-service.xml" }; 37 | 38 | ApplicationContext ctx = new ClassPathXmlApplicationContext( springConfigFiles ); 39 | 40 | AccountCaptchaService accountCaptchaService = (AccountCaptchaService) ctx.getBean( "accountCaptchaService" ); 41 | 42 | List preDefinedTexts = new ArrayList(); 43 | preDefinedTexts.add( "12345" ); 44 | preDefinedTexts.add( "abcde" ); 45 | accountCaptchaService.setPreDefinedTexts( preDefinedTexts ); 46 | 47 | accountService = (AccountService) ctx.getBean( "accountService" ); 48 | 49 | greenMail = new GreenMail( ServerSetup.SMTP ); 50 | greenMail.setUser( "test@juvenxu.com", "123456" ); 51 | greenMail.start(); 52 | 53 | File persistDataFile = new File( "target/test-classes/persist-data.xml" ); 54 | if ( persistDataFile.exists() ) 55 | { 56 | persistDataFile.delete(); 57 | } 58 | } 59 | 60 | @Test 61 | public void testAccountService() 62 | throws Exception 63 | { 64 | // 1. Get captcha 65 | String captchaKey = accountService.generateCaptchaKey(); 66 | accountService.generateCaptchaImage( captchaKey ); 67 | String captchaValue = "12345"; 68 | 69 | // 2. Submit sign up Request 70 | SignUpRequest signUpRequest = new SignUpRequest(); 71 | signUpRequest.setCaptchaKey( captchaKey ); 72 | signUpRequest.setCaptchaValue( captchaValue ); 73 | signUpRequest.setId( "juven" ); 74 | signUpRequest.setEmail( "test@juvenxu.com" ); 75 | signUpRequest.setName( "Juven Xu" ); 76 | signUpRequest.setPassword( "admin123" ); 77 | signUpRequest.setConfirmPassword( "admin123" ); 78 | signUpRequest.setActivateServiceUrl( "http://localhost:8080/account/activate" ); 79 | accountService.signUp( signUpRequest ); 80 | 81 | // 3. Read activation link 82 | greenMail.waitForIncomingEmail( 2000, 1 ); 83 | Message[] msgs = greenMail.getReceivedMessages(); 84 | assertEquals( 1, msgs.length ); 85 | assertEquals( "Please Activate Your Account", msgs[0].getSubject() ); 86 | String activationLink = GreenMailUtil.getBody( msgs[0] ).trim(); 87 | 88 | // 3a. Try login but not activated 89 | try 90 | { 91 | accountService.login( "juven", "admin123" ); 92 | fail( "Disabled account shouldn't be able to log in." ); 93 | } 94 | catch ( AccountServiceException e ) 95 | { 96 | } 97 | 98 | // 4. Activate account 99 | String activationCode = activationLink.substring( activationLink.lastIndexOf( "=" ) + 1 ); 100 | accountService.activate( activationCode ); 101 | 102 | // 5. Login with correct id and password 103 | accountService.login( "juven", "admin123" ); 104 | 105 | // 5a. Login with incorrect password 106 | try 107 | { 108 | accountService.login( "juven", "admin456" ); 109 | fail( "Password is incorrect, shouldn't be able to login." ); 110 | } 111 | catch ( AccountServiceException e ) 112 | { 113 | } 114 | 115 | } 116 | 117 | @After 118 | public void stopMailServer() 119 | throws Exception 120 | { 121 | greenMail.stop(); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-service/src/test/resources/account-service.properties: -------------------------------------------------------------------------------- 1 | email.protocol=smtp 2 | email.host=localhost 3 | email.port=25 4 | email.username=test@juvenxu.com 5 | email.password=123456 6 | email.auth=true 7 | email.systemEmail=admin@juvenxu.com 8 | 9 | persist.file=${project.build.testOutputDirectory}/persist-data.xml -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-web 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Thu Jul 01 16:54:17 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Sun May 09 19:55:57 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.juvenxu.mvnbook.account 8 | account-parent 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | account-web 13 | war 14 | Account Web 15 | 16 | 17 | 18 | ${project.groupId} 19 | account-service 20 | ${project.version} 21 | 22 | 23 | javax.servlet 24 | servlet-api 25 | 2.4 26 | provided 27 | 28 | 29 | javax.servlet.jsp 30 | jsp-api 31 | 2.0 32 | provided 33 | 34 | 35 | org.springframework 36 | spring-web 37 | 38 | 39 | 40 | 41 | 42 | 43 | src/main/resources 44 | true 45 | 46 | 47 | 48 | 49 | 50 | org.mortbay.jetty 51 | jetty-maven-plugin 52 | 7.1.0.RC1 53 | 54 | 10 55 | 56 | /account 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/java/com/juvenxu/mvnbook/account/web/ActivateServlet.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.web; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServlet; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.web.context.support.WebApplicationContextUtils; 12 | 13 | import com.juvenxu.mvnbook.account.service.AccountService; 14 | import com.juvenxu.mvnbook.account.service.AccountServiceException; 15 | 16 | public class ActivateServlet 17 | extends HttpServlet 18 | { 19 | private static final long serialVersionUID = 3668445055149826106L; 20 | 21 | private ApplicationContext context; 22 | 23 | @Override 24 | public void init() 25 | throws ServletException 26 | { 27 | super.init(); 28 | context = WebApplicationContextUtils.getWebApplicationContext( getServletContext() ); 29 | } 30 | 31 | @Override 32 | protected void doGet( HttpServletRequest req, HttpServletResponse resp ) 33 | throws ServletException, 34 | IOException 35 | { 36 | String key = req.getParameter( "key" ); 37 | 38 | if ( key == null || key.length() == 0 ) 39 | { 40 | resp.sendError( 400, "No activation key provided." ); 41 | return; 42 | } 43 | 44 | AccountService service = (AccountService) context.getBean( "accountService" ); 45 | 46 | try 47 | { 48 | service.activate( key ); 49 | resp.getWriter().write( "Account is activated, now you can login." ); 50 | } 51 | catch ( AccountServiceException e ) 52 | { 53 | resp.sendError( 400, "Unable to activate account" ); 54 | return; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/java/com/juvenxu/mvnbook/account/web/CaptchaImageServlet.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.web; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | import org.springframework.context.ApplicationContext; 12 | import org.springframework.web.context.support.WebApplicationContextUtils; 13 | 14 | import com.juvenxu.mvnbook.account.service.AccountService; 15 | import com.juvenxu.mvnbook.account.service.AccountServiceException; 16 | 17 | public class CaptchaImageServlet 18 | extends HttpServlet 19 | { 20 | private ApplicationContext context; 21 | 22 | private static final long serialVersionUID = 5274323889605521606L; 23 | 24 | @Override 25 | public void init() 26 | throws ServletException 27 | { 28 | super.init(); 29 | context = WebApplicationContextUtils.getWebApplicationContext( getServletContext() ); 30 | } 31 | 32 | public void doGet( HttpServletRequest request, HttpServletResponse response ) 33 | throws ServletException, 34 | IOException 35 | { 36 | String key = request.getParameter( "key" ); 37 | 38 | if ( key == null || key.length() == 0 ) 39 | { 40 | response.sendError( 400, "No Captcha Key Found" ); 41 | } 42 | else 43 | { 44 | AccountService service = (AccountService) context.getBean( "accountService" ); 45 | 46 | try 47 | { 48 | response.setContentType( "image/jpeg" ); 49 | OutputStream out = response.getOutputStream(); 50 | out.write( service.generateCaptchaImage( key ) ); 51 | out.close(); 52 | } 53 | catch ( AccountServiceException e ) 54 | { 55 | response.sendError( 400, e.getMessage() ); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/java/com/juvenxu/mvnbook/account/web/LoginServlet.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.web; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServlet; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.web.context.support.WebApplicationContextUtils; 12 | 13 | import com.juvenxu.mvnbook.account.service.AccountService; 14 | import com.juvenxu.mvnbook.account.service.AccountServiceException; 15 | 16 | public class LoginServlet 17 | extends HttpServlet 18 | { 19 | private static final long serialVersionUID = 929160785365121624L; 20 | 21 | private ApplicationContext context; 22 | 23 | @Override 24 | public void init() 25 | throws ServletException 26 | { 27 | super.init(); 28 | context = WebApplicationContextUtils.getWebApplicationContext( getServletContext() ); 29 | } 30 | 31 | @Override 32 | protected void doPost( HttpServletRequest req, HttpServletResponse resp ) 33 | throws ServletException, 34 | IOException 35 | { 36 | String id = req.getParameter( "id" ); 37 | String password = req.getParameter( "password" ); 38 | 39 | if ( id == null || id.length() == 0 || password == null || password.length() == 0 ) 40 | { 41 | resp.sendError( 400, "incomplete parameter" ); 42 | return; 43 | } 44 | 45 | AccountService service = (AccountService) context.getBean( "accountService" ); 46 | 47 | try 48 | { 49 | service.login( id, password ); 50 | resp.getWriter().print( "Login Successful!" ); 51 | } 52 | catch ( AccountServiceException e ) 53 | { 54 | resp.sendError( 400, e.getMessage() ); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/java/com/juvenxu/mvnbook/account/web/SignUpServlet.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.web; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServlet; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.web.context.support.WebApplicationContextUtils; 12 | 13 | import com.juvenxu.mvnbook.account.service.AccountService; 14 | import com.juvenxu.mvnbook.account.service.AccountServiceException; 15 | import com.juvenxu.mvnbook.account.service.SignUpRequest; 16 | 17 | public class SignUpServlet 18 | extends HttpServlet 19 | { 20 | private static final long serialVersionUID = 4784742296013868199L; 21 | 22 | private ApplicationContext context; 23 | 24 | @Override 25 | public void init() 26 | throws ServletException 27 | { 28 | super.init(); 29 | context = WebApplicationContextUtils.getWebApplicationContext( getServletContext() ); 30 | } 31 | 32 | @Override 33 | protected void doPost( HttpServletRequest req, HttpServletResponse resp ) 34 | throws ServletException, 35 | IOException 36 | { 37 | String id = req.getParameter( "id" ); 38 | String email = req.getParameter( "email" ); 39 | String name = req.getParameter( "name" ); 40 | String password = req.getParameter( "password" ); 41 | String confirmPassword = req.getParameter( "confirm_password" ); 42 | String captchaKey = req.getParameter( "captcha_key" ); 43 | String captchaValue = req.getParameter( "captcha_value" ); 44 | 45 | if ( id == null || id.length() == 0 || email == null || email.length() == 0 || name == null 46 | || name.length() == 0 || password == null || password.length() == 0 || confirmPassword == null 47 | || confirmPassword.length() == 0 || captchaKey == null || captchaKey.length() == 0 || captchaValue == null 48 | || captchaValue.length() == 0 ) 49 | { 50 | resp.sendError( 400, "Parameter Incomplete." ); 51 | return; 52 | } 53 | 54 | AccountService service = (AccountService) context.getBean( "accountService" ); 55 | 56 | SignUpRequest request = new SignUpRequest(); 57 | 58 | request.setId( id ); 59 | request.setEmail( email ); 60 | request.setName( name ); 61 | request.setPassword( password ); 62 | request.setConfirmPassword( confirmPassword ); 63 | request.setCaptchaKey( captchaKey ); 64 | request.setCaptchaValue( captchaValue ); 65 | 66 | request.setActivateServiceUrl( getServletContext().getRealPath( "/" ) + "activate" ); 67 | 68 | try 69 | { 70 | service.signUp( request ); 71 | resp.getWriter().print( "Account is created, please check your mail box for activation link." ); 72 | } 73 | catch ( AccountServiceException e ) 74 | { 75 | resp.sendError( 400, e.getMessage() ); 76 | return; 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/resources/account-service.properties: -------------------------------------------------------------------------------- 1 | email.protocol=smtps 2 | email.host=smtp.gmail.com 3 | email.port=465 4 | email.username=juvenshun@gmail.com 5 | email.password=googleh8z5v0 6 | email.auth=true 7 | email.systemEmail=admin@juvenxu.com 8 | persist.file=C:/persist-data.xml -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Sample Maven Project: Account Service 7 | 8 | org.springframework.web.context.ContextLoaderListener 9 | 10 | 11 | contextConfigLocation 12 | 13 | classpath:/account-persist.xml 14 | classpath:/account-captcha.xml 15 | classpath:/account-email.xml 16 | classpath:/account-service.xml 17 | 18 | 19 | 20 | CaptchaImageServlet 21 | com.juvenxu.mvnbook.account.web.CaptchaImageServlet 22 | 23 | 24 | SignUpServlet 25 | com.juvenxu.mvnbook.account.web.SignUpServlet 26 | 27 | 28 | ActivateServlet 29 | com.juvenxu.mvnbook.account.web.ActivateServlet 30 | 31 | 32 | LoginServlet 33 | com.juvenxu.mvnbook.account.web.LoginServlet 34 | 35 | 36 | CaptchaImageServlet 37 | /captcha_image 38 | 39 | 40 | SignUpServlet 41 | /signup 42 | 43 | 44 | ActivateServlet 45 | /activate 46 | 47 | 48 | LoginServlet 49 | /login 50 | 51 | 52 | -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/webapp/login.jsp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juven/mvn_in_action_code/983ebe069cf9d7d1885807613aa4bec8facaf6c5/ch-12/account-parent/account-web/src/main/webapp/login.jsp -------------------------------------------------------------------------------- /ch-12/account-parent/account-web/src/main/webapp/signup.jsp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juven/mvn_in_action_code/983ebe069cf9d7d1885807613aa4bec8facaf6c5/ch-12/account-parent/account-web/src/main/webapp/signup.jsp -------------------------------------------------------------------------------- /ch-12/account-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.juvenxu.mvnbook.account 5 | account-parent 6 | 1.0.0-SNAPSHOT 7 | pom 8 | Account Parent 9 | 10 | account-email 11 | account-persist 12 | account-captcha 13 | account-service 14 | account-web 15 | 16 | 17 | 2.5.6 18 | 4.7 19 | 20 | 21 | 22 | 23 | org.springframework 24 | spring-core 25 | ${springframework.version} 26 | 27 | 28 | org.springframework 29 | spring-beans 30 | ${springframework.version} 31 | 32 | 33 | org.springframework 34 | spring-context 35 | ${springframework.version} 36 | 37 | 38 | org.springframework 39 | spring-context-support 40 | ${springframework.version} 41 | 42 | 43 | org.springframework 44 | spring-web 45 | ${springframework.version} 46 | 47 | 48 | junit 49 | junit 50 | ${junit.version} 51 | test 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.apache.maven.plugins 60 | maven-compiler-plugin 61 | 62 | 1.5 63 | 1.5 64 | 65 | 66 | 67 | org.apache.maven.plugins 68 | maven-resources-plugin 69 | 70 | UTF-8 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | maven-loc-plugin 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Thu Jun 03 13:42:45 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Mon May 31 00:38:13 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.juvenxu.mvnbook 5 | maven-loc-plugin 6 | maven-plugin 7 | 0.0.1-SNAPSHOT 8 | Maven LOC Plugin 9 | http://www.juvenxu.com/ 10 | 11 | 12 | 3.0-beta-1 13 | UTF-8 14 | 15 | 16 | 17 | 18 | org.apache.maven 19 | maven-plugin-api 20 | ${maven.version} 21 | 22 | 23 | 24 | org.apache.maven 25 | maven-model 26 | ${maven.version} 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | maven-compiler-plugin 35 | 36 | 1.5 37 | 1.5 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-invoker-plugin 44 | 1.5 45 | 46 | src/it 47 | 48 | install 49 | 50 | validate.groovy 51 | 52 | 53 | 54 | integration-test 55 | 56 | install 57 | run 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/src/it/app/pom.xml: -------------------------------------------------------------------------------- 1 | 5 | 4.0.0 6 | com.juvenxu 7 | app 8 | jar 9 | 1.0-SNAPSHOT 10 | app 11 | http://maven.apache.org 12 | 13 | 14 | junit 15 | junit 16 | 3.8.1 17 | test 18 | 19 | 20 | 21 | 22 | 23 | com.juvenxu.mvnbook 24 | maven-loc-plugin 25 | 0.0.1-SNAPSHOT 26 | 27 | 28 | 29 | count 30 | 31 | verify 32 | 33 | 34 | 35 | 36 | org.apache.maven.plugins 37 | maven-resources-plugin 38 | 39 | UTF-8 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/src/it/app/src/main/java/com/juvenxu/App.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/src/it/app/src/test/java/com/juvenxu/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/src/it/app/validate.groovy: -------------------------------------------------------------------------------- 1 | def file = new File(basedir, 'build.log') 2 | 3 | def countMain = false 4 | def countTest = false 5 | 6 | file.eachLine { 7 | if ( it =~ /src.main.java: 13 lines of code in 1 files/ ) 8 | countMain = true 9 | if ( it =~ /src.test.java: 38 lines of code in 1 files/ ) 10 | countTest = true 11 | } 12 | 13 | if ( !countMain ) 14 | throw new RuntimeException( "incorrect src/main/java count info" ); 15 | 16 | if ( !countTest ) 17 | throw new RuntimeException( "incorrect src/test/java count info" ); -------------------------------------------------------------------------------- /ch-17/maven-loc-plugin/src/main/java/com/juvenxu/mvnbook/loc/CountMojo.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.loc; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | import org.apache.maven.model.Resource; 11 | import org.apache.maven.plugin.AbstractMojo; 12 | import org.apache.maven.plugin.MojoExecutionException; 13 | 14 | /** 15 | * Goal which counts lines of code of a project 16 | * 17 | * @goal count 18 | */ 19 | public class CountMojo 20 | extends AbstractMojo 21 | { 22 | 23 | private static final String[] INCLUDES_DEFAULT = { "java", "xml", "properties" }; 24 | 25 | /** 26 | * @parameter expression="${project.basedir}" 27 | * @required 28 | * @readonly 29 | */ 30 | private File basedir; 31 | 32 | /** 33 | * @parameter expression="${project.build.sourceDirectory}" 34 | * @required 35 | * @readonly 36 | */ 37 | private File sourceDirectory; 38 | 39 | /** 40 | * @parameter expression="${project.build.testSourceDirectory}" 41 | * @required 42 | * @readonly 43 | */ 44 | private File testSourceDirectory; 45 | 46 | /** 47 | * @parameter expression="${project.build.resources}" 48 | * @required 49 | * @readonly 50 | */ 51 | private List resources; 52 | 53 | /** 54 | * @parameter expression="${project.build.testResources}" 55 | * @required 56 | * @readonly 57 | */ 58 | private List testResources; 59 | 60 | /** 61 | * The file types which will be included for counting 62 | * 63 | * @parameter 64 | */ 65 | private String[] includes; 66 | 67 | public void execute() 68 | throws MojoExecutionException 69 | { 70 | if ( includes == null || includes.length == 0 ) 71 | { 72 | includes = INCLUDES_DEFAULT; 73 | } 74 | 75 | try 76 | { 77 | countDir( sourceDirectory ); 78 | 79 | countDir( testSourceDirectory ); 80 | 81 | for ( Resource resource : resources ) 82 | { 83 | countDir( new File( resource.getDirectory() ) ); 84 | } 85 | 86 | for ( Resource resource : testResources ) 87 | { 88 | countDir( new File( resource.getDirectory() ) ); 89 | } 90 | } 91 | catch ( IOException e ) 92 | { 93 | throw new MojoExecutionException( "Unable to count lines of code.", e ); 94 | } 95 | } 96 | 97 | private void countDir( File dir ) 98 | throws IOException 99 | { 100 | if ( !dir.exists() ) 101 | { 102 | return; 103 | } 104 | 105 | List collected = new ArrayList(); 106 | 107 | collectFiles( collected, dir ); 108 | 109 | int lines = 0; 110 | 111 | for ( File sourceFile : collected ) 112 | { 113 | lines += countLine( sourceFile ); 114 | } 115 | 116 | String path = dir.getAbsolutePath().substring( basedir.getAbsolutePath().length() ); 117 | 118 | getLog().info( path + ": " + lines + " lines of code in " + collected.size() + " files" ); 119 | 120 | } 121 | 122 | private void collectFiles( List collected, File file ) 123 | { 124 | if ( file.isFile() ) 125 | { 126 | for ( String include : includes ) 127 | { 128 | if ( file.getName().endsWith( "." + include ) ) 129 | { 130 | collected.add( file ); 131 | 132 | break; 133 | } 134 | } 135 | } 136 | else 137 | { 138 | for ( File sub : file.listFiles() ) 139 | { 140 | collectFiles( collected, sub ); 141 | } 142 | } 143 | } 144 | 145 | private int countLine( File file ) 146 | throws IOException 147 | { 148 | BufferedReader reader = new BufferedReader( new FileReader( file ) ); 149 | 150 | int line = 0; 151 | 152 | try 153 | { 154 | while ( reader.ready() ) 155 | { 156 | reader.readLine(); 157 | 158 | line++; 159 | } 160 | } 161 | finally 162 | { 163 | reader.close(); 164 | } 165 | 166 | return line; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | mvnbook-archetype-sample 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Fri Apr 30 15:52:23 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.4 4 | org.eclipse.jdt.core.compiler.compliance=1.4 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.4 7 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Fri Apr 30 15:52:19 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/pom.xml: -------------------------------------------------------------------------------- 1 | 5 | 4.0.0 6 | com.juvenxu.mvnbook.archetypes 7 | mvnbook-archetype-sample 8 | 1.0-SNAPSHOT 9 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/src/main/resources/META-INF/maven/archetype-metadata.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src/main/java 6 | 7 | **/*.java 8 | 9 | 10 | 11 | src/test/java 12 | 13 | **/*.java 14 | 15 | 16 | 17 | src/main/resources 18 | 19 | **/*.properties 20 | 21 | 22 | 23 | 24 | 25 | 26 | com.juvenxu.mvnbook 27 | 28 | 29 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/src/main/resources/archetype-resources/pom.xml: -------------------------------------------------------------------------------- 1 | 5 | 4.0.0 6 | ${groupId} 7 | ${artifactId} 8 | ${version} 9 | ${artifactId} 10 | http://www.juvenxu.com 11 | 12 | 13 | 14 | junit 15 | junit 16 | 4.8.1 17 | test 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-compiler-plugin 27 | 28 | 1.5 29 | 1.5 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-resources-plugin 35 | 36 | UTF-8 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/src/main/resources/archetype-resources/src/main/java/App.java: -------------------------------------------------------------------------------- 1 | package ${package}; 2 | 3 | public class App 4 | { 5 | public static void main( String[] args ) 6 | { 7 | System.out.println( "Hello World!" ); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/src/main/resources/archetype-resources/src/main/java/dao/Dao.java: -------------------------------------------------------------------------------- 1 | package ${package}.dao; 2 | 3 | public class Dao 4 | { 5 | } -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/src/main/resources/archetype-resources/src/main/java/service/Service.java: -------------------------------------------------------------------------------- 1 | package ${package}.service; 2 | 3 | public class Service 4 | { 5 | } -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/src/main/resources/archetype-resources/src/main/resources/config.properties: -------------------------------------------------------------------------------- 1 | port=${port} -------------------------------------------------------------------------------- /ch-18/mvnbook-archetype-sample/src/main/resources/archetype-resources/src/test/java/AppTest.java: -------------------------------------------------------------------------------- 1 | package ${package}; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | import org.junit.Test; 5 | 6 | public class AppTest 7 | { 8 | @Test 9 | public void testApp() 10 | { 11 | assertTrue( true ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ch-3/hello-world/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ch-3/hello-world/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ch-hello-world 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-3/hello-world/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Thu Aug 26 16:45:34 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-3/hello-world/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Fri Oct 09 01:46:44 CST 2009 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-3/hello-world/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | com.juvenxu.mvnbook 8 | hello-world 9 | 1.0-SNAPSHOT 10 | Maven Hello World Project 11 | 12 | 13 | junit 14 | junit 15 | 4.7 16 | test 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-compiler-plugin 24 | 25 | 1.5 26 | 1.5 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-shade-plugin 32 | 1.2.1 33 | 34 | 35 | package 36 | 37 | shade 38 | 39 | 40 | 41 | 42 | com.juvenxu.mvnbook.helloworld.HelloWorld 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /ch-3/hello-world/src/main/java/com/juvenxu/mvnbook/helloworld/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.helloworld; 2 | 3 | public class HelloWorld { 4 | 5 | public String sayHello() 6 | { 7 | return "Hello Maven"; 8 | } 9 | 10 | public static void main(String[] args) 11 | { 12 | System.out.print( new HelloWorld().sayHello() ); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ch-3/hello-world/src/test/java/com/juvenxu/mvnbook/helloworld/HelloWorldTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.helloworld; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import org.junit.Test; 5 | 6 | import com.juvenxu.mvnbook.helloworld.HelloWorld; 7 | 8 | public class HelloWorldTest 9 | { 10 | @Test 11 | public void testSayHello() 12 | { 13 | HelloWorld helloWorld = new HelloWorld(); 14 | 15 | String result = helloWorld.sayHello(); 16 | 17 | assertEquals( "Hello Maven", result ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ch-5/account-email/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-5/account-email/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-email 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-5/account-email/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Fri Sep 03 14:47:29 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-5/account-email/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Sat Nov 21 22:42:31 CST 2009 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-5/account-email/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.juven.mvnbook.account 5 | account-email 6 | Account Email 7 | 1.0.0-SNAPSHOT 8 | 9 | 10 | 11 | org.springframework 12 | spring-core 13 | 2.5.6 14 | 15 | 16 | org.springframework 17 | spring-beans 18 | 2.5.6 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 2.5.6 24 | 25 | 26 | org.springframework 27 | spring-context-support 28 | 2.5.6 29 | 30 | 31 | javax.mail 32 | mail 33 | 1.4.1 34 | 35 | 36 | junit 37 | junit 38 | 4.7 39 | test 40 | 41 | 42 | com.icegreen 43 | greenmail 44 | 1.3.1b 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.apache.maven.plugins 53 | maven-compiler-plugin 54 | 55 | 1.5 56 | 1.5 57 | 58 | 59 | 60 | org.apache.maven.plugins 61 | maven-resources-plugin 62 | 63 | UTF-8 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /ch-5/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public class AccountEmailException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -4817386460334501672L; 7 | 8 | public AccountEmailException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountEmailException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-5/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public interface AccountEmailService 4 | { 5 | void sendMail( String to, String subject, String htmlText ) 6 | throws AccountEmailException; 7 | } 8 | -------------------------------------------------------------------------------- /ch-5/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import javax.mail.MessagingException; 4 | import javax.mail.internet.MimeMessage; 5 | 6 | import org.springframework.mail.javamail.JavaMailSender; 7 | import org.springframework.mail.javamail.MimeMessageHelper; 8 | 9 | public class AccountEmailServiceImpl 10 | implements AccountEmailService 11 | { 12 | private JavaMailSender javaMailSender; 13 | 14 | private String systemEmail; 15 | 16 | public void sendMail( String to, String subject, String htmlText ) 17 | throws AccountEmailException 18 | { 19 | try 20 | { 21 | MimeMessage msg = javaMailSender.createMimeMessage(); 22 | MimeMessageHelper msgHelper = new MimeMessageHelper( msg ); 23 | 24 | msgHelper.setFrom( systemEmail ); 25 | msgHelper.setTo( to ); 26 | msgHelper.setSubject( subject ); 27 | msgHelper.setText( htmlText, true ); 28 | 29 | javaMailSender.send( msg ); 30 | } 31 | catch ( MessagingException e ) 32 | { 33 | throw new AccountEmailException( "Faild to send mail.", e ); 34 | } 35 | } 36 | 37 | public JavaMailSender getJavaMailSender() 38 | { 39 | return javaMailSender; 40 | } 41 | 42 | public void setJavaMailSender( JavaMailSender javaMailSender ) 43 | { 44 | this.javaMailSender = javaMailSender; 45 | } 46 | 47 | public String getSystemEmail() 48 | { 49 | return systemEmail; 50 | } 51 | 52 | public void setSystemEmail( String systemEmail ) 53 | { 54 | this.systemEmail = systemEmail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ch-5/account-email/src/main/resources/account-email.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ${email.auth} 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /ch-5/account-email/src/test/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import static junit.framework.Assert.assertEquals; 4 | 5 | import javax.mail.Message; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.context.support.ClassPathXmlApplicationContext; 12 | 13 | import com.icegreen.greenmail.util.GreenMail; 14 | import com.icegreen.greenmail.util.GreenMailUtil; 15 | import com.icegreen.greenmail.util.ServerSetup; 16 | 17 | public class AccountEmailServiceTest 18 | { 19 | private GreenMail greenMail; 20 | 21 | @Before 22 | public void startMailServer() 23 | throws Exception 24 | { 25 | greenMail = new GreenMail( ServerSetup.SMTP ); 26 | greenMail.setUser( "test@juvenxu.com", "123456" ); 27 | greenMail.start(); 28 | } 29 | 30 | @Test 31 | public void testSendMail() 32 | throws Exception 33 | { 34 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" ); 35 | AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean( "accountEmailService" ); 36 | 37 | String subject = "Test Subject"; 38 | String htmlText = "

Test

"; 39 | accountEmailService.sendMail( "test2@juvenxu.com", subject, htmlText ); 40 | 41 | greenMail.waitForIncomingEmail( 2000, 1 ); 42 | 43 | Message[] msgs = greenMail.getReceivedMessages(); 44 | assertEquals( 1, msgs.length ); 45 | assertEquals( subject, msgs[0].getSubject() ); 46 | assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() ); 47 | } 48 | 49 | @After 50 | public void stopMailServer() 51 | throws Exception 52 | { 53 | greenMail.stop(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ch-5/account-email/src/test/resources/service.properties: -------------------------------------------------------------------------------- 1 | #email.protocol=smtps 2 | #email.host=smtp.gmail.com 3 | #email.port=465 4 | #email.username=your-id@gmail.com 5 | #email.password=your-password 6 | #email.auth=true 7 | #email.systemEmail=your-id@gmail.com 8 | 9 | email.protocol=smtp 10 | email.host=localhost 11 | email.port=25 12 | email.username=test@juvenxu.com 13 | email.password=123456 14 | email.auth=true 15 | email.systemEmail=test1@juvenxu.com -------------------------------------------------------------------------------- /ch-7/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-7/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | template-method 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-7/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Sun Jan 17 22:44:08 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 4 | org.eclipse.jdt.core.compiler.compliance=1.4 5 | org.eclipse.jdt.core.compiler.source=1.3 6 | -------------------------------------------------------------------------------- /ch-7/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Sun Jan 17 22:44:07 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-7/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.juvenxu.mvnbook 4 | template-method 5 | 0.0.1-SNAPSHOT 6 | -------------------------------------------------------------------------------- /ch-7/src/main/java/com/juvenxu/mvnbook/template/method/AbstractBuild.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.template.method; 2 | 3 | public abstract class AbstractBuild 4 | { 5 | public void build() 6 | { 7 | initialize(); 8 | compile(); 9 | test(); 10 | packagee(); 11 | integrationTest(); 12 | deploy(); 13 | } 14 | 15 | protected abstract void initialize(); 16 | 17 | protected abstract void compile(); 18 | 19 | protected abstract void test(); 20 | 21 | protected abstract void packagee(); 22 | 23 | protected abstract void integrationTest(); 24 | 25 | protected abstract void deploy(); 26 | } 27 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-email/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | ../account-parent/pom.xml 10 | 11 | 12 | account-email 13 | Account Email 14 | 15 | 16 | 1.4.1 17 | 1.3.1b 18 | 19 | 20 | 21 | 22 | org.springframework 23 | spring-core 24 | 25 | 26 | org.springframework 27 | spring-beans 28 | 29 | 30 | org.springframework 31 | spring-context 32 | 33 | 34 | org.springframework 35 | spring-context-support 36 | 37 | 38 | junit 39 | junit 40 | 41 | 42 | javax.mail 43 | mail 44 | ${javax.mail.version} 45 | 46 | 47 | com.icegreen 48 | greenmail 49 | ${greenmail.version} 50 | test 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public class AccountEmailException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -4817386460334501672L; 7 | 8 | public AccountEmailException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountEmailException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public interface AccountEmailService 4 | { 5 | void sendMail( String to, String subject, String htmlText ) 6 | throws AccountEmailException; 7 | } 8 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import javax.mail.MessagingException; 4 | import javax.mail.internet.MimeMessage; 5 | 6 | import org.springframework.mail.javamail.JavaMailSender; 7 | import org.springframework.mail.javamail.MimeMessageHelper; 8 | 9 | public class AccountEmailServiceImpl 10 | implements AccountEmailService 11 | { 12 | private JavaMailSender javaMailSender; 13 | 14 | private String systemEmail; 15 | 16 | public void sendMail( String to, String subject, String htmlText ) 17 | throws AccountEmailException 18 | { 19 | try 20 | { 21 | MimeMessage msg = javaMailSender.createMimeMessage(); 22 | MimeMessageHelper msgHelper = new MimeMessageHelper( msg ); 23 | 24 | msgHelper.setFrom( systemEmail ); 25 | msgHelper.setTo( to ); 26 | msgHelper.setSubject( subject ); 27 | msgHelper.setText( htmlText, true ); 28 | 29 | javaMailSender.send( msg ); 30 | } 31 | catch ( MessagingException e ) 32 | { 33 | throw new AccountEmailException( "Faild to send mail.", e ); 34 | } 35 | } 36 | 37 | public JavaMailSender getJavaMailSender() 38 | { 39 | return javaMailSender; 40 | } 41 | 42 | public void setJavaMailSender( JavaMailSender javaMailSender ) 43 | { 44 | this.javaMailSender = javaMailSender; 45 | } 46 | 47 | public String getSystemEmail() 48 | { 49 | return systemEmail; 50 | } 51 | 52 | public void setSystemEmail( String systemEmail ) 53 | { 54 | this.systemEmail = systemEmail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-email/src/main/resources/account-email.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ${email.auth} 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-email/src/test/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import static junit.framework.Assert.assertEquals; 4 | 5 | import javax.mail.Message; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.context.support.ClassPathXmlApplicationContext; 12 | 13 | import com.icegreen.greenmail.util.GreenMail; 14 | import com.icegreen.greenmail.util.GreenMailUtil; 15 | import com.icegreen.greenmail.util.ServerSetup; 16 | 17 | public class AccountEmailServiceTest 18 | { 19 | private GreenMail greenMail; 20 | 21 | @Before 22 | public void startMailServer() 23 | throws Exception 24 | { 25 | greenMail = new GreenMail( ServerSetup.SMTP ); 26 | greenMail.setUser( "test@juvenxu.com", "123456" ); 27 | greenMail.start(); 28 | } 29 | 30 | @Test 31 | public void testSendMail() 32 | throws Exception 33 | { 34 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" ); 35 | AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean( "accountEmailService" ); 36 | 37 | String subject = "Test Subject"; 38 | String htmlText = "

Test

"; 39 | accountEmailService.sendMail( "test2@juvenxu.com", subject, htmlText ); 40 | 41 | greenMail.waitForIncomingEmail( 2000, 1 ); 42 | 43 | Message[] msgs = greenMail.getReceivedMessages(); 44 | assertEquals( 1, msgs.length ); 45 | assertEquals( subject, msgs[0].getSubject() ); 46 | assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() ); 47 | } 48 | 49 | @After 50 | public void stopMailServer() 51 | throws Exception 52 | { 53 | greenMail.stop(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-email/src/test/resources/service.properties: -------------------------------------------------------------------------------- 1 | #email.protocol=smtps 2 | #email.host=smtp.gmail.com 3 | #email.port=465 4 | #email.username=your-id@gmail.com 5 | #email.password=your-password 6 | #email.auth=true 7 | #email.systemEmail=your-id@gmail.com 8 | 9 | email.protocol=smtp 10 | email.host=localhost 11 | email.port=25 12 | email.username=test@juvenxu.com 13 | email.password=123456 14 | email.auth=true 15 | email.systemEmail=test1@juvenxu.com -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.juvenxu.mvnbook.account 5 | account-parent 6 | 1.0.0-SNAPSHOT 7 | pom 8 | Account Parent 9 | 10 | 2.5.6 11 | 4.7 12 | 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | ${springframework.version} 19 | 20 | 21 | org.springframework 22 | spring-beans 23 | ${springframework.version} 24 | 25 | 26 | org.springframework 27 | spring-context 28 | ${springframework.version} 29 | 30 | 31 | org.springframework 32 | spring-context-support 33 | ${springframework.version} 34 | 35 | 36 | junit 37 | junit 38 | ${junit.version} 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-compiler-plugin 49 | 50 | 1.5 51 | 1.5 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-resources-plugin 57 | 58 | UTF-8 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-persist/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | ../account-parent/pom.xml 10 | 11 | 12 | account-persist 13 | Account Persist 14 | 15 | 16 | 1.6.1 17 | 18 | 19 | 20 | 21 | dom4j 22 | dom4j 23 | ${dom4j.version} 24 | 25 | 26 | org.springframework 27 | spring-core 28 | 29 | 30 | org.springframework 31 | spring-beans 32 | 33 | 34 | org.springframework 35 | spring-context 36 | 37 | 38 | junit 39 | junit 40 | 41 | 42 | 43 | 44 | 45 | 46 | src/test/resources 47 | true 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/Account.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class Account 4 | { 5 | private String id; 6 | 7 | private String name; 8 | 9 | private String email; 10 | 11 | private String password; 12 | 13 | private boolean activated; 14 | 15 | public String getId() 16 | { 17 | return id; 18 | } 19 | 20 | public void setId( String id ) 21 | { 22 | this.id = id; 23 | } 24 | 25 | public String getName() 26 | { 27 | return name; 28 | } 29 | 30 | public void setName( String name ) 31 | { 32 | this.name = name; 33 | } 34 | 35 | public String getEmail() 36 | { 37 | return email; 38 | } 39 | 40 | public void setEmail( String email ) 41 | { 42 | this.email = email; 43 | } 44 | 45 | public String getPassword() 46 | { 47 | return password; 48 | } 49 | 50 | public void setPassword( String password ) 51 | { 52 | this.password = password; 53 | } 54 | 55 | public boolean isActivated() 56 | { 57 | return activated; 58 | } 59 | 60 | public void setActivated( boolean activated ) 61 | { 62 | this.activated = activated; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class AccountPersistException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -8334988626147289624L; 7 | 8 | public AccountPersistException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountPersistException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public interface AccountPersistService 4 | { 5 | Account createAccount( Account account ) 6 | throws AccountPersistException; 7 | 8 | Account readAccount( String id ) 9 | throws AccountPersistException; 10 | 11 | Account updateAccount( Account account ) 12 | throws AccountPersistException; 13 | 14 | void deleteAccount( String id ) 15 | throws AccountPersistException; 16 | } 17 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-persist/src/main/resources/account-persist.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-persist/src/test/java/com/juvenxu/mvnbook/account/persist/AccountPersistServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.springframework.context.ApplicationContext; 10 | import org.springframework.context.support.ClassPathXmlApplicationContext; 11 | 12 | public class AccountPersistServiceTest 13 | { 14 | private AccountPersistService service; 15 | 16 | @Before 17 | public void prepare() 18 | throws Exception 19 | { 20 | File persistDataFile = new File ( "target/test-classes/persist-data.xml" ); 21 | 22 | if ( persistDataFile.exists() ) 23 | { 24 | persistDataFile.delete(); 25 | } 26 | 27 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-persist.xml" ); 28 | 29 | service = (AccountPersistService) ctx.getBean( "accountPersistService" ); 30 | 31 | Account account = new Account(); 32 | account.setId("juven"); 33 | account.setName("Juven Xu"); 34 | account.setEmail("juven@changeme.com"); 35 | account.setPassword("this_should_be_encrypted"); 36 | account.setActivated(true); 37 | 38 | service.createAccount(account); 39 | } 40 | 41 | @Test 42 | public void testReadAccount() 43 | throws Exception 44 | { 45 | Account account = service.readAccount( "juven" ); 46 | 47 | assertNotNull( account ); 48 | assertEquals( "juven", account.getId() ); 49 | assertEquals( "Juven Xu", account.getName() ); 50 | assertEquals( "juven@changeme.com", account.getEmail() ); 51 | assertEquals( "this_should_be_encrypted", account.getPassword() ); 52 | assertTrue( account.isActivated() ); 53 | } 54 | 55 | @Test 56 | public void testDeleteAccount() 57 | throws Exception 58 | { 59 | assertNotNull( service.readAccount( "juven" ) ); 60 | service.deleteAccount( "juven" ); 61 | assertNull( service.readAccount( "juven" ) ); 62 | } 63 | 64 | @Test 65 | public void testCreateAccount() 66 | throws Exception 67 | { 68 | assertNull( service.readAccount( "mike" ) ); 69 | 70 | Account account = new Account(); 71 | account.setId("mike"); 72 | account.setName("Mike"); 73 | account.setEmail("mike@changeme.com"); 74 | account.setPassword("this_should_be_encrypted"); 75 | account.setActivated(true); 76 | 77 | service.createAccount(account); 78 | 79 | assertNotNull( service.readAccount( "mike" )); 80 | } 81 | 82 | @Test 83 | public void testUpdateAccount() 84 | throws Exception 85 | { 86 | Account account = service.readAccount( "juven" ); 87 | 88 | account.setName("Juven Xu 1"); 89 | account.setEmail("juven1@changeme.com"); 90 | account.setPassword("this_still_should_be_encrypted"); 91 | account.setActivated(false); 92 | 93 | service.updateAccount( account ); 94 | 95 | account = service.readAccount( "juven" ); 96 | 97 | assertEquals( "Juven Xu 1", account.getName() ); 98 | assertEquals( "juven1@changeme.com", account.getEmail() ); 99 | assertEquals( "this_still_should_be_encrypted", account.getPassword() ); 100 | assertFalse( account.isActivated() ); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /ch-8/account-aggregator/account-persist/src/test/resources/account-service.properties: -------------------------------------------------------------------------------- 1 | persist.file=${project.build.testOutputDirectory}/persist-data.xml -------------------------------------------------------------------------------- /ch-8/account-aggregator/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.juvenxu.mvnbook.account 5 | account-aggregator 6 | 1.0.0-SNAPSHOT 7 | pom 8 | Account Aggregator 9 | 10 | account-email 11 | account-persist 12 | account-parent 13 | 14 | -------------------------------------------------------------------------------- /ch-8/account-parent/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ch-8/account-parent/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-aggregator 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-8/account-parent/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Sun Feb 14 15:24:57 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 4 | org.eclipse.jdt.core.compiler.compliance=1.4 5 | org.eclipse.jdt.core.compiler.source=1.3 6 | -------------------------------------------------------------------------------- /ch-8/account-parent/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Sun Feb 14 15:24:57 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-email 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:51 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:48 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | 10 | 11 | account-email 12 | Account Email 13 | 14 | 15 | 1.4.1 16 | 1.3.1b 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-core 23 | 24 | 25 | org.springframework 26 | spring-beans 27 | 28 | 29 | org.springframework 30 | spring-context 31 | 32 | 33 | org.springframework 34 | spring-context-support 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | javax.mail 42 | mail 43 | ${javax.mail.version} 44 | 45 | 46 | com.icegreen 47 | greenmail 48 | ${greenmail.version} 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public class AccountEmailException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -4817386460334501672L; 7 | 8 | public AccountEmailException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountEmailException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | public interface AccountEmailService 4 | { 5 | void sendMail( String to, String subject, String htmlText ) 6 | throws AccountEmailException; 7 | } 8 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/src/main/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import javax.mail.MessagingException; 4 | import javax.mail.internet.MimeMessage; 5 | 6 | import org.springframework.mail.javamail.JavaMailSender; 7 | import org.springframework.mail.javamail.MimeMessageHelper; 8 | 9 | public class AccountEmailServiceImpl 10 | implements AccountEmailService 11 | { 12 | private JavaMailSender javaMailSender; 13 | 14 | private String systemEmail; 15 | 16 | public void sendMail( String to, String subject, String htmlText ) 17 | throws AccountEmailException 18 | { 19 | try 20 | { 21 | MimeMessage msg = javaMailSender.createMimeMessage(); 22 | MimeMessageHelper msgHelper = new MimeMessageHelper( msg ); 23 | 24 | msgHelper.setFrom( systemEmail ); 25 | msgHelper.setTo( to ); 26 | msgHelper.setSubject( subject ); 27 | msgHelper.setText( htmlText, true ); 28 | 29 | javaMailSender.send( msg ); 30 | } 31 | catch ( MessagingException e ) 32 | { 33 | throw new AccountEmailException( "Faild to send mail.", e ); 34 | } 35 | } 36 | 37 | public JavaMailSender getJavaMailSender() 38 | { 39 | return javaMailSender; 40 | } 41 | 42 | public void setJavaMailSender( JavaMailSender javaMailSender ) 43 | { 44 | this.javaMailSender = javaMailSender; 45 | } 46 | 47 | public String getSystemEmail() 48 | { 49 | return systemEmail; 50 | } 51 | 52 | public void setSystemEmail( String systemEmail ) 53 | { 54 | this.systemEmail = systemEmail; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/src/main/resources/account-email.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ${email.auth} 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/src/test/java/com/juvenxu/mvnbook/account/email/AccountEmailServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.email; 2 | 3 | import static junit.framework.Assert.assertEquals; 4 | 5 | import javax.mail.Message; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.context.support.ClassPathXmlApplicationContext; 12 | 13 | import com.icegreen.greenmail.util.GreenMail; 14 | import com.icegreen.greenmail.util.GreenMailUtil; 15 | import com.icegreen.greenmail.util.ServerSetup; 16 | 17 | public class AccountEmailServiceTest 18 | { 19 | private GreenMail greenMail; 20 | 21 | @Before 22 | public void startMailServer() 23 | throws Exception 24 | { 25 | greenMail = new GreenMail( ServerSetup.SMTP ); 26 | greenMail.setUser( "test@juvenxu.com", "123456" ); 27 | greenMail.start(); 28 | } 29 | 30 | @Test 31 | public void testSendMail() 32 | throws Exception 33 | { 34 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" ); 35 | AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean( "accountEmailService" ); 36 | 37 | String subject = "Test Subject"; 38 | String htmlText = "

Test

"; 39 | accountEmailService.sendMail( "test2@juvenxu.com", subject, htmlText ); 40 | 41 | greenMail.waitForIncomingEmail( 2000, 1 ); 42 | 43 | Message[] msgs = greenMail.getReceivedMessages(); 44 | assertEquals( 1, msgs.length ); 45 | assertEquals( subject, msgs[0].getSubject() ); 46 | assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() ); 47 | } 48 | 49 | @After 50 | public void stopMailServer() 51 | throws Exception 52 | { 53 | greenMail.stop(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-email/src/test/resources/service.properties: -------------------------------------------------------------------------------- 1 | #email.protocol=smtps 2 | #email.host=smtp.gmail.com 3 | #email.port=465 4 | #email.username=your-id@gmail.com 5 | #email.password=your-password 6 | #email.auth=true 7 | #email.systemEmail=your-id@gmail.com 8 | 9 | email.protocol=smtp 10 | email.host=localhost 11 | email.port=25 12 | email.username=test@juvenxu.com 13 | email.password=123456 14 | email.auth=true 15 | email.systemEmail=test1@juvenxu.com -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | account-persist 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:51 CST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 15 22:20:48 CST 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | includeModules=false 6 | resolveWorkspaceProjects=true 7 | resourceFilterGoals=process-resources resources\:testResources 8 | skipCompilerPlugin=true 9 | version=1 10 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | 10 | 11 | account-persist 12 | Account Persist 13 | 14 | 15 | 1.6.1 16 | 17 | 18 | 19 | 20 | dom4j 21 | dom4j 22 | ${dom4j.version} 23 | 24 | 25 | org.springframework 26 | spring-core 27 | 28 | 29 | org.springframework 30 | spring-beans 31 | 32 | 33 | org.springframework 34 | spring-context 35 | 36 | 37 | junit 38 | junit 39 | 40 | 41 | 42 | 43 | 44 | 45 | src/test/resources 46 | true 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/Account.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class Account 4 | { 5 | private String id; 6 | 7 | private String name; 8 | 9 | private String email; 10 | 11 | private String password; 12 | 13 | private boolean activated; 14 | 15 | public String getId() 16 | { 17 | return id; 18 | } 19 | 20 | public void setId( String id ) 21 | { 22 | this.id = id; 23 | } 24 | 25 | public String getName() 26 | { 27 | return name; 28 | } 29 | 30 | public void setName( String name ) 31 | { 32 | this.name = name; 33 | } 34 | 35 | public String getEmail() 36 | { 37 | return email; 38 | } 39 | 40 | public void setEmail( String email ) 41 | { 42 | this.email = email; 43 | } 44 | 45 | public String getPassword() 46 | { 47 | return password; 48 | } 49 | 50 | public void setPassword( String password ) 51 | { 52 | this.password = password; 53 | } 54 | 55 | public boolean isActivated() 56 | { 57 | return activated; 58 | } 59 | 60 | public void setActivated( boolean activated ) 61 | { 62 | this.activated = activated; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistException.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public class AccountPersistException 4 | extends Exception 5 | { 6 | private static final long serialVersionUID = -8334988626147289624L; 7 | 8 | public AccountPersistException( String message ) 9 | { 10 | super( message ); 11 | } 12 | 13 | public AccountPersistException( String message, Throwable throwable ) 14 | { 15 | super( message, throwable ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/src/main/java/com/juvenxu/mvnbook/account/persist/AccountPersistService.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | public interface AccountPersistService 4 | { 5 | Account createAccount( Account account ) 6 | throws AccountPersistException; 7 | 8 | Account readAccount( String id ) 9 | throws AccountPersistException; 10 | 11 | Account updateAccount( Account account ) 12 | throws AccountPersistException; 13 | 14 | void deleteAccount( String id ) 15 | throws AccountPersistException; 16 | } 17 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/src/main/resources/account-persist.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/src/test/java/com/juvenxu/mvnbook/account/persist/AccountPersistServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.juvenxu.mvnbook.account.persist; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.springframework.context.ApplicationContext; 10 | import org.springframework.context.support.ClassPathXmlApplicationContext; 11 | 12 | public class AccountPersistServiceTest 13 | { 14 | private AccountPersistService service; 15 | 16 | @Before 17 | public void prepare() 18 | throws Exception 19 | { 20 | File persistDataFile = new File ( "target/test-classes/persist-data.xml" ); 21 | 22 | if ( persistDataFile.exists() ) 23 | { 24 | persistDataFile.delete(); 25 | } 26 | 27 | ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-persist.xml" ); 28 | 29 | service = (AccountPersistService) ctx.getBean( "accountPersistService" ); 30 | 31 | Account account = new Account(); 32 | account.setId("juven"); 33 | account.setName("Juven Xu"); 34 | account.setEmail("juven@changeme.com"); 35 | account.setPassword("this_should_be_encrypted"); 36 | account.setActivated(true); 37 | 38 | service.createAccount(account); 39 | } 40 | 41 | @Test 42 | public void testReadAccount() 43 | throws Exception 44 | { 45 | Account account = service.readAccount( "juven" ); 46 | 47 | assertNotNull( account ); 48 | assertEquals( "juven", account.getId() ); 49 | assertEquals( "Juven Xu", account.getName() ); 50 | assertEquals( "juven@changeme.com", account.getEmail() ); 51 | assertEquals( "this_should_be_encrypted", account.getPassword() ); 52 | assertTrue( account.isActivated() ); 53 | } 54 | 55 | @Test 56 | public void testDeleteAccount() 57 | throws Exception 58 | { 59 | assertNotNull( service.readAccount( "juven" ) ); 60 | service.deleteAccount( "juven" ); 61 | assertNull( service.readAccount( "juven" ) ); 62 | } 63 | 64 | @Test 65 | public void testCreateAccount() 66 | throws Exception 67 | { 68 | assertNull( service.readAccount( "mike" ) ); 69 | 70 | Account account = new Account(); 71 | account.setId("mike"); 72 | account.setName("Mike"); 73 | account.setEmail("mike@changeme.com"); 74 | account.setPassword("this_should_be_encrypted"); 75 | account.setActivated(true); 76 | 77 | service.createAccount(account); 78 | 79 | assertNotNull( service.readAccount( "mike" )); 80 | } 81 | 82 | @Test 83 | public void testUpdateAccount() 84 | throws Exception 85 | { 86 | Account account = service.readAccount( "juven" ); 87 | 88 | account.setName("Juven Xu 1"); 89 | account.setEmail("juven1@changeme.com"); 90 | account.setPassword("this_still_should_be_encrypted"); 91 | account.setActivated(false); 92 | 93 | service.updateAccount( account ); 94 | 95 | account = service.readAccount( "juven" ); 96 | 97 | assertEquals( "Juven Xu 1", account.getName() ); 98 | assertEquals( "juven1@changeme.com", account.getEmail() ); 99 | assertEquals( "this_still_should_be_encrypted", account.getPassword() ); 100 | assertFalse( account.isActivated() ); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /ch-8/account-parent/account-persist/src/test/resources/account-service.properties: -------------------------------------------------------------------------------- 1 | persist.file=${project.build.testOutputDirectory}/persist-data.xml -------------------------------------------------------------------------------- /ch-8/account-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 5 | 4.0.0 6 | com.juvenxu.mvnbook.account 7 | account-parent 8 | 1.0.0-SNAPSHOT 9 | pom 10 | Account Parent 11 | 12 | account-email 13 | account-persist 14 | 15 | 16 | 2.5.6 17 | 4.7 18 | 19 | 20 | 21 | 22 | org.springframework 23 | spring-core 24 | ${springframework.version} 25 | 26 | 27 | org.springframework 28 | spring-beans 29 | ${springframework.version} 30 | 31 | 32 | org.springframework 33 | spring-context 34 | ${springframework.version} 35 | 36 | 37 | org.springframework 38 | spring-context-support 39 | ${springframework.version} 40 | 41 | 42 | junit 43 | junit 44 | ${junit.version} 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-compiler-plugin 55 | 56 | 1.5 57 | 1.5 58 | 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-resources-plugin 63 | 64 | UTF-8 65 | 66 | 67 | 68 | 69 | 70 | --------------------------------------------------------------------------------