Search in sources :

Example 51 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project LanternServer by LanternPowered.

the class LanternClassLoader method load.

private static LanternClassLoader load() throws IOException {
    ClassLoader.registerAsParallelCapable();
    // Get the bootstrap class loader
    final ClassLoader classLoader = LanternClassLoader.class.getClassLoader();
    // Load the dependencies files
    final List<Dependencies> dependenciesEntries = new ArrayList<>();
    // Load the dependencies file within the jar, not available in the IDE
    final URL dependenciesURL = classLoader.getResource("dependencies.json");
    if (dependenciesURL != null) {
        try {
            dependenciesEntries.add(DependenciesParser.read(new BufferedReader(new InputStreamReader(dependenciesURL.openStream()))));
        } catch (ParseException e) {
            throw new IllegalStateException("Failed to parse the dependencies.json file within the jar.", e);
        }
    }
    // Try to generate or load the dependencies file
    final Path dependenciesFile = Paths.get("dependencies.json");
    if (!Files.exists(dependenciesFile)) {
        try (BufferedWriter writer = Files.newBufferedWriter(dependenciesFile)) {
            writer.write("{\n    \"repositories\": [\n    ],\n    \"dependencies\": [\n    ]\n}");
        }
    } else {
        try {
            dependenciesEntries.add(DependenciesParser.read(Files.newBufferedReader(dependenciesFile)));
        } catch (ParseException e) {
            throw new IllegalStateException("Failed to parse the dependencies.json file within the root directory.", e);
        }
    }
    // Merge the dependencies files
    final List<URL> repositoryUrls = new ArrayList<>();
    final Map<String, Dependency> dependencyMap = new HashMap<>();
    for (Dependencies dependencies : dependenciesEntries) {
        dependencies.getRepositories().stream().map(Repository::getUrl).filter(e -> !repositoryUrls.contains(e)).forEach(repositoryUrls::add);
        for (Dependency dependency : dependencies.getDependencies()) {
            dependencyMap.put(dependency.getGroup() + ':' + dependency.getName(), dependency);
        }
    }
    String localRepoPath = System.getProperty("maven.repo.local");
    if (localRepoPath == null) {
        final String mavenHome = System.getenv("M2_HOME");
        if (mavenHome != null) {
            final Path settingsPath = Paths.get(mavenHome, "conf", "setting.xml");
            if (Files.exists(settingsPath)) {
                try {
                    final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                    final Document document = documentBuilder.parse(settingsPath.toFile());
                    // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
                    document.getDocumentElement().normalize();
                    final Node node = document.getElementsByTagName("localRepository").item(0);
                    if (node != null) {
                        localRepoPath = node.getTextContent();
                    }
                } catch (ParserConfigurationException | SAXException e) {
                    throw new IllegalStateException(e);
                }
            }
        }
    }
    if (localRepoPath == null) {
        localRepoPath = "~/.m/repository";
    }
    localRepoPath = localRepoPath.trim();
    if (localRepoPath.charAt(0) == '~') {
        localRepoPath = System.getProperty("user.home") + '/' + localRepoPath.substring(2);
    }
    // Try to find the local maven repository
    repositoryUrls.add(0, new File(localRepoPath).toURL());
    final List<FileRepository> repositories = new ArrayList<>();
    for (URL repositoryUrl : repositoryUrls) {
        if (repositoryUrl.getProtocol().equals("file")) {
            final File baseFile = new File(repositoryUrl.getFile());
            repositories.add(path -> {
                final File file = new File(baseFile, path);
                try {
                    return file.exists() ? file.toURL().openStream() : null;
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
            });
        } else {
            String repositoryUrlBase = repositoryUrl.toString();
            if (repositoryUrlBase.endsWith("/")) {
                repositoryUrlBase = repositoryUrlBase.substring(0, repositoryUrlBase.length() - 1);
            }
            final String urlBase = repositoryUrlBase;
            repositories.add(path -> {
                try {
                    final URL url = new URL(urlBase + "/" + path);
                    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    final String encoding = connection.getHeaderField("Content-Encoding");
                    InputStream is = connection.getInputStream();
                    if (encoding != null) {
                        if (encoding.equals("gzip")) {
                            is = new GZIPInputStream(is);
                        } else {
                            throw new IllegalStateException("Unsupported encoding: " + encoding);
                        }
                    }
                    return is;
                } catch (IOException e) {
                    return null;
                }
            });
        }
    }
    // If we are outside development mode, the server will be packed
    // into a jar. We will also need to make sure that this one gets
    // added in this case
    final CodeSource source = LanternClassLoader.class.getProtectionDomain().getCodeSource();
    final URL location = source == null ? null : source.getLocation();
    // Setup the environment variable
    final String env = System.getProperty(ENVIRONMENT);
    final Environment environment;
    if (env != null) {
        try {
            environment = Environment.valueOf(env.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("Invalid environment type: " + env);
        }
    } else {
        environment = location == null || new File(location.getFile()).isDirectory() ? Environment.DEVELOPMENT : Environment.PRODUCTION;
        System.setProperty(ENVIRONMENT, environment.toString().toLowerCase());
    }
    Environment.set(environment);
    // Scan the jar for library jars
    if (location != null) {
        repositories.add(path -> classLoader.getResourceAsStream("dependencies/" + path));
    }
    final List<URL> libraryUrls = new ArrayList<>();
    // Download or load all the dependencies
    final Path internalLibrariesPath = Paths.get(".cached-dependencies");
    for (Dependency dependency : dependencyMap.values()) {
        final String group = dependency.getGroup();
        final String name = dependency.getName();
        final String version = dependency.getVersion();
        final Path target = internalLibrariesPath.resolve(String.format("%s/%s/%s/%s-%s.jar", group.replace('.', '/'), name, version, name, version));
        libraryUrls.add(target.toUri().toURL());
        final String id = String.format("%s:%s:%s", dependency.getGroup(), dependency.getName(), dependency.getVersion());
        if (Files.exists(target)) {
            System.out.printf("Loaded: \"%s\"\n", id);
            continue;
        }
        InputStream is = null;
        for (FileRepository repository : repositories) {
            is = repository.get(dependency);
            if (is != null) {
                break;
            }
        }
        if (is == null) {
            throw new IllegalStateException("The following dependency could not be found: " + id);
        }
        final Path parent = target.getParent();
        if (!Files.exists(parent)) {
            Files.createDirectories(parent);
        }
        System.out.printf("Downloading \"%s\"\n", id);
        try (ReadableByteChannel i = Channels.newChannel(is);
            FileOutputStream o = new FileOutputStream(target.toFile())) {
            o.getChannel().transferFrom(i, 0, Long.MAX_VALUE);
        }
    }
    // All the folders are from lantern or sponge,
    // in development mode are all the libraries on
    // the classpath, so there is no need to add them
    // to the library classloader
    final List<URL> urls = new ArrayList<>();
    final String classPath = System.getProperty("java.class.path");
    final String[] libraries = classPath.split(File.pathSeparator);
    for (String library : libraries) {
        try {
            final URL url = Paths.get(library).toUri().toURL();
            if (!library.endsWith(".jar") || url.equals(location)) {
                urls.add(url);
            }
        } catch (MalformedURLException ignored) {
            System.out.println("Invalid library found in the class path: " + library);
        }
    }
    // The server class loader will load lantern, the api and all the plugins
    final LanternClassLoader serverClassLoader = new LanternClassLoader(urls.toArray(new URL[urls.size()]), libraryUrls.toArray(new URL[libraryUrls.size()]), classLoader);
    Thread.currentThread().setContextClassLoader(serverClassLoader);
    return serverClassLoader;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Manifest(java.util.jar.Manifest) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) Enumeration(java.util.Enumeration) URL(java.net.URL) JarFile(java.util.jar.JarFile) Repository(org.lanternpowered.launch.dependencies.Repository) URLClassLoader(java.net.URLClassLoader) Document(org.w3c.dom.Document) Map(java.util.Map) Method(java.lang.reflect.Method) Path(java.nio.file.Path) Dependency(org.lanternpowered.launch.dependencies.Dependency) LanternServer(org.lanternpowered.server.LanternServer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) DependenciesParser(org.lanternpowered.launch.dependencies.DependenciesParser) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HashMap(java.util.HashMap) CodeSigner(java.security.CodeSigner) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassTransformer(org.lanternpowered.launch.transformer.ClassTransformer) ParseException(org.json.simple.parser.ParseException) Objects.requireNonNull(java.util.Objects.requireNonNull) Node(org.w3c.dom.Node) Exclusion(org.lanternpowered.launch.transformer.Exclusion) NoSuchElementException(java.util.NoSuchElementException) ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) Channels(java.nio.channels.Channels) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Dependencies(org.lanternpowered.launch.dependencies.Dependencies) File(java.io.File) Consumer(java.util.function.Consumer) Element(org.w3c.dom.Element) Paths(java.nio.file.Paths) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) BufferedReader(java.io.BufferedReader) CodeSource(java.security.CodeSource) Collections(java.util.Collections) InputStream(java.io.InputStream) ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) SAXException(org.xml.sax.SAXException) GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) URLClassLoader(java.net.URLClassLoader) Dependencies(org.lanternpowered.launch.dependencies.Dependencies) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Dependency(org.lanternpowered.launch.dependencies.Dependency) IOException(java.io.IOException) CodeSource(java.security.CodeSource) Repository(org.lanternpowered.launch.dependencies.Repository) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) ParseException(org.json.simple.parser.ParseException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 52 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project onebusaway-application-modules by camsys.

the class SyncBundleAction method syncBundle.

public String syncBundle() {
    String syncStatus = "Syncing in progress";
    String apiHost = "http://admin.staging.obast.org:9999/api/bundle/latest";
    JsonObject latestBundle = null;
    try {
        latestBundle = getJsonData(apiHost).getAsJsonObject();
    } catch (Exception e) {
        _log.error("Failed to retrieve name of the latest deployed bundle");
    }
    String datasetName = latestBundle.get("dataset").getAsString();
    String buildName = latestBundle.get("name").getAsString();
    String bundleId = latestBundle.get("id").getAsString();
    String bundleFileName = buildName + ".tar.gz";
    String tmpDir = new NYCFileUtils().createTmpDirectory();
    String bundleDir = "/var/lib/oba/bundles";
    String deployDir = "/var/lib/oba/bundles/active";
    String bundleBuildDir = "/var/lib/oba/bundles/builder";
    try {
        String bundleSourceString = "http://admin.staging.obast.org:9999/api/bundle/archive/get-by-name/" + datasetName + "/" + buildName + "/" + bundleFileName;
        URL bundleSource = new URL(bundleSourceString);
        ReadableByteChannel rbc = Channels.newChannel(bundleSource.openStream());
        FileOutputStream fos = new FileOutputStream(tmpDir + File.separator + bundleFileName);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        unzipBundle(tmpDir, bundleFileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Copy to extracted files /var/lib/oba/bundles/active
    try {
        // FileUtils.copyDirectory(new File(tmpDir + File.separator + "untarredBundle" + File.separator +  buildName), new File(deployDir));
        FileUtils.copyDirectory(new File(tmpDir + File.separator + "untarredBundle"), new File(deployDir));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Copy downloaded .tar.gz bundle to bundle builds dir
    String buildDest = bundleBuildDir + File.separator + datasetName + File.separator + "builds" + File.separator + buildName + File.separator + bundleFileName;
    try {
        FileUtils.copyFile(new File(tmpDir + File.separator + bundleFileName), new File(buildDest));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Copy inputs and outputs dirs
    File srcInputDir = new File(tmpDir + File.separator + "untarredBundle" + File.separator + buildName + File.separator + "inputs");
    File srcOutputDir = new File(tmpDir + File.separator + "untarredBundle" + File.separator + buildName + File.separator + "outputs");
    String destBuildsDir = bundleBuildDir + File.separator + datasetName + File.separator + "builds" + File.separator + buildName;
    try {
        FileUtils.copyDirectory(srcInputDir, new File(destBuildsDir + File.separator + "inputs"));
        FileUtils.copyDirectory(srcOutputDir, new File(destBuildsDir + File.separator + "outputs"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Copy gtfs and aux files to aux_latest and gtfs_latest
    String gtfsFileDest = bundleBuildDir + File.separator + datasetName + File.separator + "gtfs_latest";
    String auxFileDest = bundleBuildDir + File.separator + datasetName + File.separator + "aux_latest";
    File[] inputFiles = srcInputDir.listFiles();
    for (File inputFile : inputFiles) {
        try {
            String fileName = inputFile.getName();
            int idx = fileName.indexOf("_");
            if (idx > 0) {
                // Skip over config dir
                String agencyNum = fileName.substring(0, idx);
                String zipFileName = fileName.substring(idx + 1);
                if (agencyNum.equals("29")) {
                    // For CT aux files
                    String fileDest = auxFileDest + File.separator + agencyNum + File.separator + zipFileName;
                    FileUtils.copyFile(inputFile, new File(fileDest));
                } else {
                    String fileDest = gtfsFileDest + File.separator + agencyNum + File.separator + zipFileName;
                    FileUtils.copyFile(inputFile, new File(fileDest));
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    syncStatus = "Complete";
    return "syncStatus";
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) NYCFileUtils(org.onebusaway.admin.util.NYCFileUtils) FileOutputStream(java.io.FileOutputStream) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) File(java.io.File) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL)

Example 53 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project gemoc-studio by eclipse.

the class IFileUtils method isStreamEqual.

private static boolean isStreamEqual(InputStream i1, InputStream i2) throws IOException {
    ReadableByteChannel ch1 = Channels.newChannel(i1);
    ReadableByteChannel ch2 = Channels.newChannel(i2);
    ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);
    try {
        while (true) {
            int n1 = ch1.read(buf1);
            int n2 = ch2.read(buf2);
            if (n1 == -1 || n2 == -1)
                return n1 == n2;
            buf1.flip();
            buf2.flip();
            for (int i = 0; i < Math.min(n1, n2); i++) if (buf1.get() != buf2.get())
                return false;
            buf1.compact();
            buf2.compact();
        }
    } finally {
        if (i1 != null)
            i1.close();
        if (i2 != null)
            i2.close();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 54 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project gemoc-studio by eclipse.

the class ManifestChanger method isStreamEqual.

private static boolean isStreamEqual(InputStream i1, InputStream i2) throws IOException {
    ReadableByteChannel ch1 = Channels.newChannel(i1);
    ReadableByteChannel ch2 = Channels.newChannel(i2);
    ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);
    try {
        while (true) {
            int n1 = ch1.read(buf1);
            int n2 = ch2.read(buf2);
            if (n1 == -1 || n2 == -1)
                return n1 == n2;
            buf1.flip();
            buf2.flip();
            for (int i = 0; i < Math.min(n1, n2); i++) if (buf1.get() != buf2.get())
                return false;
            buf1.compact();
            buf2.compact();
        }
    } finally {
        if (i1 != null)
            i1.close();
        if (i2 != null)
            i2.close();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 55 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project providence by morimekta.

the class FramedBufferTest method testReadMultiple.

@Test
public void testReadMultiple() throws IOException {
    ReadableByteChannel channel = Channels.newChannel(new ByteArrayInputStream(multiData));
    FramedBufferInputStream in = new FramedBufferInputStream(channel);
    in.nextFrame();
    byte[] out = new byte[14];
    assertEquals(10, in.read(out));
    assertEquals("this is a\n", new String(out, 0, 10, UTF_8));
    in.nextFrame();
    out = new byte[14];
    assertEquals(13, in.read(out));
    assertEquals("not again...\n", new String(out, 0, 13, UTF_8));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)307 ByteBuffer (java.nio.ByteBuffer)111 IOException (java.io.IOException)84 FileOutputStream (java.io.FileOutputStream)62 WritableByteChannel (java.nio.channels.WritableByteChannel)62 Test (org.junit.Test)52 File (java.io.File)50 FileChannel (java.nio.channels.FileChannel)49 FileInputStream (java.io.FileInputStream)43 ByteArrayInputStream (java.io.ByteArrayInputStream)38 InputStream (java.io.InputStream)36 URL (java.net.URL)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Path (java.nio.file.Path)18 Test (org.testng.annotations.Test)14 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)11 MalformedURLException (java.net.MalformedURLException)11 Vector (java.util.Vector)11