Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2013 Ning, Inc.
   * Copyright 2014 Groupon, Inc
   * Copyright 2014 The Billing Project, LLC
   *
   * The Billing Project 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.
  */
 
 package org.killbill.billing.beatrix.integration.osgi.util;
 
 import java.io.File;
 import java.net.URL;
 
 
 
 // Install a JRuby or Java plugin programmatically
 public class SetupBundleWithAssertion {
 
     private static final String JRUBY_BUNDLE_RESOURCE = "killbill-osgi-bundles-jruby";
 
     private final String bundleName;
     private final OSGIConfig config;
     private final String killbillVersion;
     private final File rootInstallDir;
 
     public SetupBundleWithAssertion(final String bundleNamefinal OSGIConfig configfinal String killbillVersion) {
         this. = bundleName;
         this. = config;
         this. = killbillVersion;
         this. = new File(config.getRootInstallationDir());
     }
 
     public void setupJrubyBundle() {
         try {
             installJrubyJar();
 
             final URL resourceUrl = Resources.getResource();
             final File unzippedRubyPlugin = unGzip(new File(resourceUrl.getFile()), );
 
             final StringBuilder tmp = new StringBuilder(.getAbsolutePath());
             tmp.append("/plugins/")
                .append(..toString().toLowerCase());
 
             final File destination = new File(tmp.toString());
             if (!destination.exists()) {
                 Assert.assertTrue(destination.mkdirs(), "Unable to create directory " + destination.getAbsolutePath());
             }
 
             unTar(unzippedRubyPlugindestination);
         } catch (final IOException e) {
             Assert.fail(e.getMessage());
         } catch (final ArchiveException e) {
             Assert.fail(e.getMessage());
         }
     }
 
     public void setupJavaBundle() {
         try {
             // Retrieve PluginConfig info from classpath
             // test bundle should have been exported under Beatrix resource by the maven maven-dependency-plugin
             final PluginJavaConfig pluginConfig = extractJavaBundleTestResource();
             Assert.assertNotNull(pluginConfig);
 
             // Create OSGI install bundle directory
             setupDirectoryStructure(pluginConfig);
 
             // Copy the jar
             ByteStreams.copy(new FileInputStream(new File(pluginConfig.getBundleJarPath())), new FileOutputStream(new File(pluginConfig.getPluginVersionRoot().getAbsolutePath(), pluginConfig.getPluginVersionnedName() + ".jar")));
 
             // Create the osgiConfig file
            createConfigFile(pluginConfig);
        } catch (final IOException e) {
            Assert.fail(e.getMessage());
        }
    }
    public void cleanBundleInstallDir() {
        if (.exists()) {
            deleteDirectory(false);
        }
    }
    private void createConfigFile(final PluginConfig pluginConfigthrows IOException {
        PrintStream printStream = null;
        try {
            final File configFile = new File(pluginConfig.getPluginVersionRoot(), .getOSGIKillbillPropertyName());
            Assert.assertTrue(configFile.createNewFile(), "Unable to create file " + configFile.getAbsolutePath());
            printStream = new PrintStream(new FileOutputStream(configFile));
            printStream.print("pluginType=" + .);
        } finally {
            if (printStream != null) {
                printStream.close();
            }
        }
    }
    private void setupDirectoryStructure(final PluginConfig pluginConfig) {
        cleanBundleInstallDir();
        Assert.assertTrue(pluginConfig.getPluginVersionRoot().mkdirs(), "Unable to create directory " + pluginConfig.getPluginVersionRoot().getAbsolutePath());
    }
    private static void deleteDirectory(final File pathfinal boolean deleteParent) {
        if (path == null) {
            return;
        }
        if (path.exists()) {
            final File[] files = path.listFiles();
            if (files != null) {
                for (final File f : files) {
                    if (f.isDirectory()) {
                        deleteDirectory(ftrue);
                    }
                    Assert.assertTrue(f.delete(), "Unable to delete file " + f.getAbsolutePath());
                }
            }
            if (deleteParent) {
                Assert.assertTrue(path.delete(), "Unable to delete file " + path.delete());
            }
        }
    }
    private void installJrubyJar() throws IOException {
        final String resourceName =  + ".jar";
        final URL resourceUrl = Resources.getResource(resourceName);
        final File rubyJarInput = new File(resourceUrl.getFile());
        final File platform = new File("platform");
        if (!platform.exists()) {
            Assert.assertTrue(platform.mkdir(), "Unable to create directory " + platform.getAbsolutePath());
        }
        final File rubyJarDestination = new File(platform"jruby.jar");
        ByteStreams.copy(new FileInputStream(rubyJarInput), new FileOutputStream(rubyJarDestination));
    }
        final String resourceName =  + "-jar-with-dependencies.jar";
        final URL resourceUrl = Resources.getResource(resourceName);
        if (resourceUrl != null) {
            final String[] parts = resourceUrl.getPath().split("/");
            final String lastPart = parts[parts.length - 1];
            if (lastPart.startsWith()) {
                return createPluginJavaConfig(resourceUrl.getPath());
            }
        }
        return null;
    }
    private PluginJavaConfig createPluginJavaConfig(final String bundleTestResourcePath) {
        return new PluginJavaConfig() {
            @Override
            public String getBundleJarPath() {
                return bundleTestResourcePath;
            }
            @Override
            public String getPluginName() {
                return ;
            }
            @Override
            public PluginType getPluginType() {
                return .;
            }
            @Override
            public String getVersion() {
                return ;
            }
            @Override
            public String getPluginVersionnedName() {
                return  + "-" + ;
            }
            @Override
            public File getPluginVersionRoot() {
                final StringBuilder tmp = new StringBuilder(.getAbsolutePath());
                tmp.append("/plugins/")
                   .append(..toString().toLowerCase())
                   .append("/")
                   .append()
                   .append("/")
                   .append();
                return new File(tmp.toString());
            }
            @Override
            public PluginLanguage getPluginLanguage() {
                return .;
            }
        };
    }
    private static void unTar(final File inputFilefinal File outputDirthrows IOExceptionArchiveException {
        InputStream is = null;
        TarArchiveInputStream archiveInputStream = null;
        TarArchiveEntry entry;
        try {
            is = new FileInputStream(inputFile);
            archiveInputStream = (TarArchiveInputStreamnew ArchiveStreamFactory().createArchiveInputStream("tar"is);
            while ((entry = (TarArchiveEntryarchiveInputStream.getNextEntry()) != null) {
                final File outputFile = new File(outputDirentry.getName());
                if (entry.isDirectory()) {
                    if (!outputFile.exists()) {
                        if (!outputFile.mkdirs()) {
                            throw new IllegalStateException(String.format("Couldn't create directory %s."outputFile.getAbsolutePath()));
                        }
                    }
                } else {
                    final OutputStream outputFileStream = new FileOutputStream(outputFile);
                    ByteStreams.copy(archiveInputStreamoutputFileStream);
                    outputFileStream.close();
                }
            }
        } finally {
            if (archiveInputStream != null) {
                archiveInputStream.close();
            }
            if (is != null) {
                is.close();
            }
        }
    }
    private static File unGzip(final File inputFilefinal File outputDirthrows IOException {
        GZIPInputStream in = null;
        FileOutputStream out = null;
        try {
            final File outputFile = new File(outputDirinputFile.getName().substring(0, inputFile.getName().length() - 3));
            in = new GZIPInputStream(new FileInputStream(inputFile));
            out = new FileOutputStream(outputFile);
            for (int c = in.read(); c != -1; c = in.read()) {
                out.write(c);
            }
            return outputFile;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
New to GrepCode? Check out our FAQ X