1 package org.codehaus.mojo.fitnesse.runner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.net.Socket;
20 import java.net.UnknownHostException;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.logging.Log;
26 import org.codehaus.mojo.fitnesse.ClassPathSubstitution;
27
28 import fitnesse.components.FitProtocol;
29 import fitnesse.util.StreamReader;
30
31 public class ClassPathBuilder
32 {
33
34 private String mHostName;
35
36 private int mPort;
37
38 private String mPage;
39
40 private Log mLog;
41
42 ClassPathBuilder()
43 {
44 }
45
46 ClassPathBuilder( Log pLog )
47 {
48 mLog = pLog;
49 }
50
51 public ClassPathBuilder( String hostName, int port, String page, Log pLog )
52 {
53 super();
54 mHostName = hostName;
55 mPort = port;
56 mPage = page;
57 mLog = pLog;
58 }
59
60 public String getPath( List pSubstitutions, Log pLog )
61 throws MojoExecutionException
62 {
63 String tOriginalPath;
64 String tUrl = "GET /" + mPage + "?responder=fitClient&includePaths=yes HTTP/1.1\r\n\r\n";
65 pLog.debug( "Use URL for classPath download [" + tUrl + "]" );
66 try
67 {
68 StreamReader tSocketReader = establishConnection( tUrl );
69 int tNbBytes = FitProtocol.readSize( tSocketReader );
70 if ( tNbBytes != 0 )
71 {
72 throw new MojoExecutionException( "Unable to connect to server." );
73 }
74 tNbBytes = FitProtocol.readSize( tSocketReader );
75 tOriginalPath = FitProtocol.readDocument( tSocketReader, tNbBytes );
76 pLog.debug( "Download classpath is [" + tOriginalPath + "]" );
77 String tPath = transformPath( tOriginalPath, pSubstitutions );
78 pLog.info( "Use path [" + tPath + "]" );
79 return tPath;
80 }
81 catch ( Exception e )
82 {
83 throw new MojoExecutionException( "Unable to download path from FitNesse Server", e );
84 }
85 }
86
87 private StreamReader establishConnection( String pUrl )
88 throws UnknownHostException, IOException
89 {
90 Socket socket = new Socket( mHostName, mPort );
91 OutputStream socketOutput = socket.getOutputStream();
92 StreamReader socketReader = new StreamReader( socket.getInputStream() );
93 byte[] tBytes = pUrl.getBytes( "UTF-8" );
94 socketOutput.write( tBytes );
95 socketOutput.flush();
96 return socketReader;
97 }
98
99 String transformPath( String pOriginalPath, List pSubstitutions )
100 {
101 String curPath = pOriginalPath;
102 ClassPathSubstitution curSub;
103 for ( Iterator tIt = pSubstitutions.iterator(); tIt.hasNext(); )
104 {
105 curSub = (ClassPathSubstitution) tIt.next();
106 mLog.debug( "use subtitution [" + curSub.getSearch() + "=" + curSub.getReplaceWith() );
107 curPath = replaceAll( curPath, curSub.getSearch(), curSub.getReplaceWith() );
108 }
109 curPath = curPath.replaceAll( " +\";", "\";" );
110 curPath = curPath.replaceAll( ";\" +", ";\"" );
111 curPath = curPath.replaceAll( "\"", "" );
112
113
114
115
116
117
118 return curPath;
119 }
120
121 String replaceAll( String pPath, String pKey, String pValue )
122 {
123 StringBuffer tempNewPath = new StringBuffer();
124 int curStart = 0;
125 int curEnd = pPath.indexOf( pKey );
126 while ( curEnd != -1 )
127 {
128 tempNewPath.append( pPath.substring( curStart, curEnd ) );
129 tempNewPath.append( pValue );
130 curStart = curEnd + pKey.length();
131 curEnd = pPath.indexOf( pKey, curStart );
132 }
133 tempNewPath.append( pPath.substring( curStart, pPath.length() ) );
134 return tempNewPath.toString();
135 }
136 }