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 public class PerforceInfoCommand
65 extends AbstractCommand
66 implements PerforceCommand
67 {
68 private static PerforceInfoCommand singleton = null;
69
70 private Map<String, String> entries = null;
71
72 public static PerforceInfoCommand getInfo( ScmLogger logger, PerforceScmProviderRepository repo )
73 {
74 return getSingleton( logger, repo );
75 }
76
77 public String getEntry( String key )
78 {
79 return (String) entries.get( key );
80 }
81
82 private static synchronized PerforceInfoCommand getSingleton( ScmLogger logger, PerforceScmProviderRepository repo )
83 {
84 if ( singleton == null )
85 {
86 PerforceInfoCommand pic = new PerforceInfoCommand();
87 if ( logger != null )
88 {
89 pic.setLogger( logger );
90 }
91 try
92 {
93 pic.executeCommand( repo, null, null );
94 singleton = pic;
95 }
96 catch ( ScmException e )
97 {
98 if ( pic.getLogger().isErrorEnabled() )
99 {
100 pic.getLogger().error( "ScmException " + e.getMessage(), e );
101 }
102 }
103 }
104
105 return singleton;
106 }
107
108
109
110
111 protected ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet scmFileSet,
112 CommandParameters commandParameters )
113 throws ScmException
114 {
115 if ( !PerforceScmProvider.isLive() )
116 {
117 return null;
118 }
119 InputStreamReader isReader = null;
120 try
121 {
122 Commandline command = PerforceScmProvider.createP4Command( (PerforceScmProviderRepository) repo, null );
123 command.createArg().setValue( "info" );
124 if ( getLogger().isDebugEnabled() )
125 {
126 getLogger().debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
127 }
128 Process proc = command.execute();
129 isReader = new InputStreamReader( proc.getInputStream() );
130 BufferedReader br = new BufferedReader( isReader );
131 String line;
132 entries = new HashMap<String, String>();
133 while ( ( line = br.readLine() ) != null )
134 {
135 int idx = line.indexOf( ':' );
136 if ( idx == -1 )
137 {
138 if ( line.indexOf( "Client unknown." ) == -1 )
139 {
140 throw new IllegalStateException( "Unexpected results from 'p4 info' command: " + line );
141 }
142
143 if ( getLogger().isDebugEnabled() )
144 {
145 getLogger().debug( "Cannot find client." );
146 }
147 entries.put( "Client root", "" );
148 }
149 else
150 {
151 String key = line.substring( 0, idx );
152 String value = line.substring( idx + 1 ).trim();
153 entries.put( key, value );
154 }
155 }
156 }
157 catch ( CommandLineException e )
158 {
159 throw new ScmException( e.getLocalizedMessage() );
160 }
161 catch ( IOException e )
162 {
163 throw new ScmException( e.getLocalizedMessage() );
164 }
165 finally
166 {
167 IOUtil.close( isReader );
168 }
169 return null;
170 }
171 }