146 lines
5.0 KiB
Java
Raw Permalink Normal View History

2017-04-04 20:53:29 +02:00
/*
2019-03-12 21:19:20 +01:00
* Copyright 2017 - 2019 Volker Berlin (i-net software)
2017-04-04 20:53:29 +02:00
*
* Licensed 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.
*/
package de.inetsoftware.jwebassembly;
import java.io.DataInputStream;
2017-04-04 20:53:29 +02:00
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
2018-05-11 12:21:14 +02:00
import java.time.Instant;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
2017-04-04 20:53:29 +02:00
/**
* Download the JavaScript engine SpiderMonkey.
*
* @author Volker Berlin
*/
public class SpiderMonkey {
private String command;
/**
* Check if there is a new version of the script engine
*
* @throws IOException
* if any error occur
*/
private void download() throws IOException {
boolean is32 = "32".equals( System.getProperty( "sun.arch.data.model" ) );
String fileName;
final String os = System.getProperty( "os.name", "" ).toLowerCase();
if( os.contains( "windows" ) ) {
fileName = is32 ? "win32" : "win64";
} else if( os.contains( "mac" ) ) {
fileName = is32 ? "mac" : "mac64";
} else if( os.contains( "linux" ) ) {
fileName = is32 ? "linux-i686" : "linux-x86_64";
} else {
throw new IllegalStateException( "Unknown OS: " + os );
}
File target = new File( System.getProperty( "java.io.tmpdir" ) + "/SpiderMonkey" );
URL url = new URL( "https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-" + fileName + ".zip" );
2017-04-04 20:53:29 +02:00
System.out.println( "\tDownload: " + url );
2019-03-12 21:19:20 +01:00
command = target.getAbsolutePath() + "/js";
2017-04-04 20:53:29 +02:00
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
2019-03-12 21:19:20 +01:00
conn.setConnectTimeout( 5000 );
2017-04-04 20:53:29 +02:00
if( target.exists() ) {
conn.setIfModifiedSince( target.lastModified() );
}
2019-03-12 21:19:20 +01:00
InputStream input;
try {
input = conn.getInputStream();
} catch( IOException ex ) {
if( target.exists() ) {
System.err.println( ex );
return;
}
throw ex;
}
2017-04-04 20:53:29 +02:00
if( conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED ) {
2018-05-11 12:21:14 +02:00
System.out.println( "\tUP-TP-DATE, use version from " + Instant.ofEpochMilli( target.lastModified() ) );
2017-04-04 20:53:29 +02:00
return;
}
long lastModfied = conn.getLastModified();
extractStream( input, false, target );
target.setLastModified( lastModfied );
System.out.println( "\tUse Version from " + Instant.ofEpochMilli( lastModfied ) );
}
/**
* Get the SpeiderMonkey command. If file not exists then download it.
*
* @return the path to the executable
* @throws IOException
* if any I/O error occur
*/
public String getCommand() throws IOException {
if( command == null ) {
download();
}
return command;
}
/**
* Extract a compressed stream
*
* @param input
* the input stream
* @param tarGz
* true: *.tar.gz else *.zip
* @param target
* the location to extract
* @throws IOException
* if any I/O error occur
*/
static void extractStream( InputStream input, boolean tarGz, File target ) throws IOException {
ArchiveInputStream archiv;
if( tarGz ) {
input = new GZIPInputStream( input );
archiv = new TarArchiveInputStream( input );
} else {
archiv = new ZipArchiveInputStream( input );
}
2017-04-04 20:53:29 +02:00
do {
ArchiveEntry entry = archiv.getNextEntry();
2017-04-04 20:53:29 +02:00
if( entry == null ) {
break;
}
if( entry.isDirectory() ) {
continue;
}
File file = new File( target, entry.getName() );
file.getParentFile().mkdirs();
byte[] buffer = new byte[ (int)entry.getSize() ];
new DataInputStream( archiv ).readFully( buffer );
Files.write( file.toPath(), buffer, StandardOpenOption.CREATE );
file.setLastModified( entry.getLastModifiedDate().getTime() );
file.setExecutable( true );
2017-04-04 20:53:29 +02:00
} while( true );
archiv.close();
2017-04-04 20:53:29 +02:00
}
}