1 | |
package org.apache.maven.scm.provider.perforce.command; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.scm.CommandParameters; |
23 | |
import org.apache.maven.scm.ScmException; |
24 | |
import org.apache.maven.scm.ScmFileSet; |
25 | |
import org.apache.maven.scm.ScmResult; |
26 | |
import org.apache.maven.scm.command.AbstractCommand; |
27 | |
import org.apache.maven.scm.log.ScmLogger; |
28 | |
import org.apache.maven.scm.provider.ScmProviderRepository; |
29 | |
import org.apache.maven.scm.provider.perforce.PerforceScmProvider; |
30 | |
import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository; |
31 | |
import org.codehaus.plexus.util.IOUtil; |
32 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
33 | |
import org.codehaus.plexus.util.cli.Commandline; |
34 | |
|
35 | |
import java.io.BufferedReader; |
36 | |
import java.io.IOException; |
37 | |
import java.io.InputStreamReader; |
38 | |
import java.util.HashMap; |
39 | |
import java.util.Map; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | public class PerforceInfoCommand |
65 | |
extends AbstractCommand |
66 | |
implements PerforceCommand |
67 | |
{ |
68 | 0 | private static PerforceInfoCommand singleton = null; |
69 | |
|
70 | 0 | private Map<String, String> entries = null; |
71 | |
|
72 | |
public static PerforceInfoCommand getInfo( ScmLogger logger, PerforceScmProviderRepository repo ) |
73 | |
{ |
74 | 0 | return getSingleton( logger, repo ); |
75 | |
} |
76 | |
|
77 | |
public String getEntry( String key ) |
78 | |
{ |
79 | 0 | return (String) entries.get( key ); |
80 | |
} |
81 | |
|
82 | |
private static synchronized PerforceInfoCommand getSingleton( ScmLogger logger, PerforceScmProviderRepository repo ) |
83 | |
{ |
84 | 0 | if ( singleton == null ) |
85 | |
{ |
86 | 0 | PerforceInfoCommand pic = new PerforceInfoCommand(); |
87 | 0 | if ( logger != null ) |
88 | |
{ |
89 | 0 | pic.setLogger( logger ); |
90 | |
} |
91 | |
try |
92 | |
{ |
93 | 0 | pic.executeCommand( repo, null, null ); |
94 | 0 | singleton = pic; |
95 | |
} |
96 | 0 | catch ( ScmException e ) |
97 | |
{ |
98 | 0 | if ( pic.getLogger().isErrorEnabled() ) |
99 | |
{ |
100 | 0 | pic.getLogger().error( "ScmException " + e.getMessage(), e ); |
101 | |
} |
102 | 0 | } |
103 | |
} |
104 | |
|
105 | 0 | return singleton; |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
protected ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet scmFileSet, |
112 | |
CommandParameters commandParameters ) |
113 | |
throws ScmException |
114 | |
{ |
115 | 0 | if ( !PerforceScmProvider.isLive() ) |
116 | |
{ |
117 | 0 | return null; |
118 | |
} |
119 | 0 | InputStreamReader isReader = null; |
120 | |
try |
121 | |
{ |
122 | 0 | Commandline command = PerforceScmProvider.createP4Command( (PerforceScmProviderRepository) repo, null ); |
123 | 0 | command.createArg().setValue( "info" ); |
124 | 0 | if ( getLogger().isDebugEnabled() ) |
125 | |
{ |
126 | 0 | getLogger().debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) ); |
127 | |
} |
128 | 0 | Process proc = command.execute(); |
129 | 0 | isReader = new InputStreamReader( proc.getInputStream() ); |
130 | 0 | BufferedReader br = new BufferedReader( isReader ); |
131 | |
String line; |
132 | 0 | entries = new HashMap<String, String>(); |
133 | 0 | while ( ( line = br.readLine() ) != null ) |
134 | |
{ |
135 | 0 | int idx = line.indexOf( ':' ); |
136 | 0 | if ( idx == -1 ) |
137 | |
{ |
138 | 0 | if ( line.indexOf( "Client unknown." ) == -1 ) |
139 | |
{ |
140 | 0 | throw new IllegalStateException( "Unexpected results from 'p4 info' command: " + line ); |
141 | |
} |
142 | |
|
143 | 0 | if ( getLogger().isDebugEnabled() ) |
144 | |
{ |
145 | 0 | getLogger().debug( "Cannot find client." ); |
146 | |
} |
147 | 0 | entries.put( "Client root", "" ); |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 0 | String key = line.substring( 0, idx ); |
152 | 0 | String value = line.substring( idx + 1 ).trim(); |
153 | 0 | entries.put( key, value ); |
154 | |
} |
155 | 0 | } |
156 | 0 | } |
157 | 0 | catch ( CommandLineException e ) |
158 | |
{ |
159 | 0 | throw new ScmException( e.getLocalizedMessage() ); |
160 | |
} |
161 | 0 | catch ( IOException e ) |
162 | |
{ |
163 | 0 | throw new ScmException( e.getLocalizedMessage() ); |
164 | |
} |
165 | |
finally |
166 | |
{ |
167 | 0 | IOUtil.close( isReader ); |
168 | 0 | } |
169 | 0 | return null; |
170 | |
} |
171 | |
} |