2121import java .nio .ByteBuffer ;
2222import java .nio .channels .ServerSocketChannel ;
2323import java .nio .channels .SocketChannel ;
24- import java .nio .file .Path ;
25- import java .nio .file .Paths ;
26- import java .util .HashMap ;
27- import java .util .Map ;
28- import java .util .Objects ;
29- import java .util .StringJoiner ;
3024import java .util .concurrent .atomic .AtomicBoolean ;
3125import java .util .function .Consumer ;
3226
3327import javax .inject .Named ;
3428import javax .inject .Singleton ;
3529
30+ import org .apache .maven .AbstractMavenLifecycleParticipant ;
31+ import org .apache .maven .MavenExecutionException ;
3632import org .apache .maven .eventspy .EventSpy ;
37- import org .apache .maven .execution .ExecutionEvent ;
38- import org .apache .maven .execution .ExecutionEvent .Type ;
39- import org .apache .maven .project .MavenProject ;
33+ import org .apache .maven .execution .MavenSession ;
4034import org .slf4j .Logger ;
4135import org .slf4j .LoggerFactory ;
4236
4842 * @author Hannes Wellmann
4943 *
5044 */
51- @ Named
5245@ Singleton
53- public class M2EMavenBuildDataBridge implements EventSpy {
46+ @ Named ("m2e" )
47+ public class M2EMavenBuildDataBridge extends AbstractMavenLifecycleParticipant {
5448
5549 private static final String SOCKET_FILE_PROPERTY_NAME = "m2e.build.project.data.socket.port" ;
5650 private static final String DATA_SET_SEPARATOR = ";;" ;
@@ -60,7 +54,7 @@ public class M2EMavenBuildDataBridge implements EventSpy {
6054 private SocketChannel writeChannel ;
6155
6256 @ Override
63- public void init ( Context context ) throws IOException {
57+ public synchronized void afterSessionStart ( MavenSession session ) throws MavenExecutionException {
6458 String socketPort = System .getProperty (SOCKET_FILE_PROPERTY_NAME );
6559 if (socketPort != null ) {
6660 try {
@@ -76,79 +70,38 @@ public void init(Context context) throws IOException {
7670 }
7771
7872 @ Override
79- public void close ( ) throws IOException {
80- writeChannel . close ();
73+ public void afterSessionEnd ( MavenSession session ) throws MavenExecutionException {
74+ close ();
8175 }
8276
83- @ Override
84- public void onEvent (Object event ) throws Exception {
85- if (writeChannel != null && event instanceof ExecutionEvent
86- && ((ExecutionEvent ) event ).getType () == Type .ProjectStarted ) {
87-
88- String message = serializeProjectData (((ExecutionEvent ) event ).getProject ());
89-
90- ByteBuffer buffer = ByteBuffer .wrap (message .getBytes ());
91- synchronized (writeChannel ) {
92- while (buffer .hasRemaining ()) {
93- writeChannel .write (buffer );
94- }
77+ private synchronized void close () {
78+ if (writeChannel != null ) {
79+ try {
80+ writeChannel .close ();
81+ } catch (IOException e ) {
82+ // nothing we want to do here...
9583 }
9684 }
85+ writeChannel = null ;
9786 }
9887
99- private static String serializeProjectData (MavenProject project ) {
100- StringJoiner data = new StringJoiner ("," );
101- add (data , "groupId" , project .getGroupId ());
102- add (data , "artifactId" , project .getArtifactId ());
103- add (data , "version" , project .getVersion ());
104- add (data , "file" , project .getFile ());
105- add (data , "basedir" , project .getBasedir ());
106- add (data , "build.directory" , project .getBuild ().getDirectory ());
107- return data .toString () + DATA_SET_SEPARATOR ;
108- }
109-
110- private static void add (StringJoiner data , String key , Object value ) {
111- data .add (key + "=" + value );
88+ public synchronized boolean isActive () {
89+ return writeChannel != null ;
11290 }
11391
114- /**
115- * <p>
116- * This method is supposed to be called from M2E within the Eclipse-IDE JVM.
117- * </p>
118- *
119- * @param dataSet the data-set to parse
120- * @return the {@link MavenProjectBuildData} parsed from the given string
121- */
122- private static MavenProjectBuildData parseMavenBuildProject (String dataSet ) {
123- Map <String , String > data = new HashMap <>(8 );
124- for (String entry : dataSet .split ("," )) {
125- String [] keyValue = entry .split ("=" );
126- if (keyValue .length != 2 ) {
127- throw new IllegalStateException ("Invalid data-set format" + dataSet );
128- }
129- data .put (keyValue [0 ], keyValue [1 ]);
92+ public synchronized void sendMessage (String message ) {
93+ System .out .println ("send: " + message );
94+ if (writeChannel == null ) {
95+ return ;
13096 }
131- return new MavenProjectBuildData (data );
132- }
133-
134- public static final class MavenProjectBuildData {
135- public final String groupId ;
136- public final String artifactId ;
137- public final String version ;
138- public final Path projectBasedir ;
139- public final Path projectFile ;
140- public final Path projectBuildDirectory ;
141-
142- MavenProjectBuildData (Map <String , String > data ) {
143- if (data .size () != 6 ) {
144- throw new IllegalArgumentException ();
97+ ByteBuffer buffer = ByteBuffer .wrap ((message + DATA_SET_SEPARATOR ).getBytes ());
98+ while (buffer .hasRemaining ()) {
99+ try {
100+ writeChannel .write (buffer );
101+ } catch (IOException e ) {
102+ // channel seems dead...
103+ close ();
145104 }
146- this .groupId = Objects .requireNonNull (data .get ("groupId" ));
147- this .artifactId = Objects .requireNonNull (data .get ("artifactId" ));
148- this .version = Objects .requireNonNull (data .get ("version" ));
149- this .projectBasedir = Paths .get (data .get ("basedir" ));
150- this .projectFile = Paths .get (data .get ("file" ));
151- this .projectBuildDirectory = Paths .get (data .get ("build.directory" ));
152105 }
153106 }
154107
@@ -187,8 +140,8 @@ public static MavenBuildConnection prepareConnection(String label, Consumer<Mave
187140 for (int terminatorIndex ; (terminatorIndex = message .indexOf (DATA_SET_SEPARATOR )) >= 0 ;) {
188141 String dataSet = message .substring (0 , terminatorIndex );
189142 message .delete (0 , terminatorIndex + DATA_SET_SEPARATOR .length ());
190-
191- MavenProjectBuildData buildData = parseMavenBuildProject (dataSet );
143+ System . out . println ( "got messagE: " + dataSet );
144+ MavenProjectBuildData buildData = MavenProjectBuildData . parseMavenBuildProject (dataSet );
192145 datasetListener .accept (buildData );
193146 }
194147 // Explicit cast for compatibility with covariant return type on JDK 9's
0 commit comments