[maven-studies] branch maven-eventsound-extension created (now ef93d3d)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[maven-studies] branch maven-eventsound-extension created (now ef93d3d)

rfscholte
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a change to branch maven-eventsound-extension
in repository https://gitbox.apache.org/repos/asf/maven-studies.git.


      at ef93d3d  Initial commit

This branch includes the following new commits:

     new ef93d3d  Initial commit

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Reply | Threaded
Open this post in threaded view
|

[maven-studies] 01/01: Initial commit

rfscholte
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch maven-eventsound-extension
in repository https://gitbox.apache.org/repos/asf/maven-studies.git

commit ef93d3d88c7fe6bcd8443f92333a60346eb75a47
Author: rfscholte <[hidden email]>
AuthorDate: Fri Sep 13 20:42:19 2019 +0200

    Initial commit
---
 .gitignore                                         |   4 +
 pom.xml                                            |  88 +++++++++++
 .../eventsound/SoundPlayingListener.java           | 167 +++++++++++++++++++++
 3 files changed, 259 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..06a84e9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+/target/
+/.classpath
+/.project
+/.settings
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..0042ead
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.maven.plugins</groupId>
+    <artifactId>maven-plugins</artifactId>
+    <version>33</version>
+    <relativePath />
+  </parent>
+  <groupId>org.apache.maven.extensions</groupId>
+  <artifactId>maven-eventsound-extension</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+
+  <prerequisites>
+    <maven>3.2.1</maven>
+  </prerequisites>
+
+  <properties>
+    <javaVersion>8</javaVersion>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-component-metadata</artifactId>
+        <version>1.7.1</version>
+        <executions>
+          <execution>
+            <goals>
+              <goal>generate-metadata</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-core</artifactId>
+      <version>3.2.1</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter</artifactId>
+      <version>5.5.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>3.0.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-junit-jupiter</artifactId>
+      <version>3.0.0</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/src/main/java/org/apache/maven/extensions/eventsound/SoundPlayingListener.java b/src/main/java/org/apache/maven/extensions/eventsound/SoundPlayingListener.java
new file mode 100644
index 0000000..bf85cf8
--- /dev/null
+++ b/src/main/java/org/apache/maven/extensions/eventsound/SoundPlayingListener.java
@@ -0,0 +1,167 @@
+package org.apache.maven.extensions.eventsound;
+
+import java.io.File;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Properties;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+import javax.sound.sampled.AudioInputStream;
+import javax.sound.sampled.AudioSystem;
+import javax.sound.sampled.Clip;
+import javax.sound.sampled.LineUnavailableException;
+import javax.sound.sampled.UnsupportedAudioFileException;
+
+import org.apache.maven.eventspy.AbstractEventSpy;
+import org.apache.maven.execution.ExecutionEvent;
+import org.apache.maven.execution.ExecutionEvent.Type;
+import org.codehaus.plexus.logging.LogEnabled;
+import org.codehaus.plexus.logging.Logger;
+
+@Named
+@Singleton
+public class SoundPlayingListener extends AbstractEventSpy implements LogEnabled
+{
+    private Logger logger;
+    
+    private Properties properties = new Properties();
+
+    private Path config;
+
+    @Override
+    public void enableLogging( Logger logger )
+    {
+        this.logger = logger;
+    }
+    
+    @Override
+    public void init( Context context )
+        throws Exception
+    {
+        String mavenHome = System.getProperty( "maven.home" );
+        
+        config = Paths.get( mavenHome, "conf/ext/eventsound.properties").normalize();
+        
+        if ( !Files.isRegularFile( config ) )
+        {
+            // TODO write properties file template with all available properties
+            logger.error( "MISSING " + config.toString() );
+        }
+        
+        try ( InputStream is = new FileInputStream( config.toFile() ) )
+        {
+            properties.load( is );
+        }
+        
+    }
+
+    @Override
+    public void onEvent( Object event )
+        throws Exception
+    {
+        if ( event instanceof ExecutionEvent )
+        {
+            ExecutionEvent executionEvent = (ExecutionEvent) event;
+            
+            if ( executionEvent.getType() == Type.SessionEnded )
+            {
+                
+                String key;
+                if ( executionEvent.getSession().getResult().hasExceptions() )
+                {
+                    key = "executionEvent.session.failure.audiofile";
+                }
+                else
+                {
+                    key = "executionEvent.session.success.audiofile";
+                }
+                
+                String value = properties.getProperty( key );
+                if ( value != null )
+                {
+                    Path mediafile = config.getParent().resolve( "eventsound" ).resolve( value );
+                    
+                    Thread t = new Thread( new Player( mediafile ) );
+                    t.run();
+                }
+            }
+        }
+    }
+    
+    private class Player
+        implements Runnable
+    {
+        private final Path mediafile;
+
+        Player( Path mediafile )
+        {
+            this.mediafile = mediafile;
+        }
+
+        public void run()
+        {
+            try (Clip clip = AudioSystem.getClip();
+                            AudioInputStream ais = AudioSystem.getAudioInputStream( mediafile.toFile() ) )
+            {
+                clip.open( ais );
+                clip.start();
+
+                // AudioSystem uses a separate Thread.
+                // If the active thread is closed, you might hear just a fragment or nothing at all
+
+                // Warmup Time
+                while ( !clip.isRunning() )
+                {
+                    try
+                    {
+                        Thread.sleep( 10 );
+                    }
+                    catch ( InterruptedException e )
+                    {
+                    }
+                }
+
+                // Running time
+                while ( clip.isRunning() )
+                {
+                    try
+                    {
+                        Thread.sleep( 10 );
+                    }
+                    catch ( InterruptedException e )
+                    {
+                    }
+                }
+            }
+            catch ( IOException | UnsupportedAudioFileException | LineUnavailableException e )
+            {
+                e.printStackTrace();
+            }
+        }
+    }
+}