суббота, 5 ноября 2016 г.

java.net.URL as datasource based on Apache Camel, commons-vfs and maven repository

What we know about java.net.URL?
  1. It is one of the mature JVM runtime class (@since JDK1.0).
  2. URL used in lot of programs.
  3. file, jar, http is the most frequently used protocols.
  4. Any existing JVM application can use new URL protocol as datasource without recompile and reassembly.

Is it possible to use java.net.URL with CIFS/SMB, SCP, HDFS or Maven repository protocols?

Whats about capture still image from webcam by use only one expression new URL('camel:/webcam:spycam?resolution=HD720').openStream()?

My answer is "YES, you can do it"!!! Just use special Groovy assembly groovy-grape-aether-2.4.5.4.jar. By default it contains initialization of UniversalURLStreamHandlerFactory handler.


Another approach is to use com.github.igor-suhorukov:mvn-classloader:1.6 maven dependency in your JVM project and register handler before first use of URL SUPERPOWER ;-)
Just register handler in your program: java.net.URL.setURLStreamHandlerFactory(new com.github.igorsuhorukov.url.handler.UniversalURLStreamHandlerFactory());

Save image from webcam


To capture capture image from webcam and save it to file 'snap.png' just write script webcam_to_file.groovy

com.github.igorsuhorukov.codehaus.plexus.util.IOUtil.copy(new URL('camel:/webcam:spycam?resolution=HD720').openStream(), new FileOutputStream('snap.png'))

and run this example with command
java -jar groovy-grape-aether-2.4.5.4.jar webcam_to_file.groovy


View image from webcam


Groovy help us create Swing UI with it cool SwingBuilder

webcam_to_screen.groovy

import groovy.swing.SwingBuilder
import javax.imageio.ImageIO
import javax.swing.*

def swing = new SwingBuilder()
swing.edt {
    frame(title: 'Webcam protocol', defaultCloseOperation: JFrame.EXIT_ON_CLOSE, pack: true, show: true) {
        vbox {
            swing.panel() {
                def webcamStream = new URL('camel:/webcam:spycam?resolution=HD720').openStream()
                label(new JLabel(new ImageIcon(ImageIO.read(webcamStream))))
            }
        }
    }
}



java -jar groovy-grape-aether-2.4.5.4.jar webcam_to_screen.groovy

You can see snapshot image from Swing UI in the beginning of this article.

 

Groovy scripts from remote sources


from maven repository:
java -jar groovy-grape-aether-2.4.5.4.jar mvn:/groupId:artifactId[:extension[:classifier]]:version[?custom_repository_URL]


from another host by ssh:
 java -jar groovy-grape-aether-2.4.5.4.jar vfs:/sftp://myusername:mypassword@somehost/pub/downloads/my_script.groovy

 

Protocols supported by library


UniversalURLStreamHandlerFactory hide all complexity of loadable URL protocol handlers. It fetch each implementation from maven repository and place it in local .m2 cache by using eclipse aether library.

I try to support following protocols for java.net.URL
  • mvn: — allow to use artifact from maven repository with syntax groupId:artifactId[:extension[:classifier]]:version[?custom_repository_URL]
  • vfs: — 16 protocols from apache commons-vfs library
  • camel: try to use 189 components from Apache Camel. It get content from Apache Camel component as PollingConsumer under the hood.

Links on github:


This publication is based on my original Russian article on habrahabr.