📜 ⬆️ ⬇️

java.net.URL or the old horse will not spoil the furrow

The first thing we remember right away when we see the java.net.URL class is that it is just as old as the first version of java (@since JDK1.0). Secondly, it is found in almost all java programs. Third - most often it uses the file, jar and http protocols.



Is it possible to get data using java.net.URL via CIFS / SMB, SCP, from HDFS - the hadup file system or from the maven repository? And get the image from the webcam in one line new URL (' camel: / webcam: spycam? Resolution = HD720 ') .openStream ()?

For those who are particularly curious, I’ll say at once that this is easy to do with the help of the UniversalURLStreamHandlerFactory. It is only necessary to use groovy-grape-aether-2.4.5.4.jar to launch the grooves, where it is already enabled by default.
')
In java, you’ll have to add the com.github.igor-suhorukov: mvn-classloader: 1.6 dependency to the project and write a little more code for initialization.

Let's start with simple examples, and leave trash and waste to the final publication.

It is very easy to capture a frame from a webcam using the camel webcam component and save it to the snap.png file.

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


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

It is a little harder to capture a frame from a webcam and show it in a window. You have already seen the result of this script in the screenshot for the article.

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

So, all this magic is available thanks to the UniversalURLStreamHandlerFactory, a URL handler for loadable protocol implementations. It loads them from the maven repository or uses the local repository cache.

Now for the URL protocols are supported:


It is easy to register a universal handler in a java program — just add initialization before using such exotic addresses in the URL:
 java.net.URL.setURLStreamHandlerFactory(new com.github.igorsuhorukov.url.handler.UniversalURLStreamHandlerFactory()); 

But it is necessary to remember about the limitation of the standard java library, that you can call java.net.URL.setURLStreamHandlerFactory only once for the duration of the program.

The UniversalURLStreamHandlerFactory was inspired by an idea from the paxurl project and its predecessor hansa . PaxUrl - works fine in OSGI containers, and the solution from the article in standalone jvm applications.

Yes, by the way, the syntax works for getting groovy scripts using hundreds of protocols!
java -jar groovy-grape-aether-2.4.5.4.jar mvn: / groupId: artifactId [: extension [: classifier]]: version [? custom_repository_URL]

java -jar groovy-grape-aether-2.4.5.4.jar vfs: / sftp: // myusername: mypassword@somehost/pub/downloads/my_script.groovy (or with ssh authentication).

Projects are available on github: camel-url-handler , vfs-url-handler , mvn-classloader , groovy-grape-aether .

Good luck with the new use of a long time familiar technology!

Source: https://habr.com/ru/post/276719/


All Articles