Search in sources :

Example 6 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class FilePath method createTextTempFile.

/**
     * Creates a temporary file in this directory and set the contents by the
     * given text (encoded in the platform default encoding)
     */
public FilePath createTextTempFile(final String prefix, final String suffix, final String contents, final boolean inThisDirectory) throws IOException, InterruptedException {
    try {
        return new FilePath(channel, act(new FileCallable<String>() {

            public String invoke(File dir, VirtualChannel channel) throws IOException {
                if (!inThisDirectory)
                    dir = new File(System.getProperty("java.io.tmpdir"));
                else
                    dir.mkdirs();
                File f;
                try {
                    f = File.createTempFile(prefix, suffix, dir);
                } catch (IOException e) {
                    throw new IOException2("Failed to create a temporary directory in " + dir, e);
                }
                Writer w = new FileWriter(f);
                w.write(contents);
                w.close();
                return f.getAbsolutePath();
            }
        }));
    } catch (IOException e) {
        throw new IOException2("Failed to create a temp file on " + remote, e);
    }
}
Also used : VirtualChannel(hudson.remoting.VirtualChannel) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) IOException2(hudson.util.IOException2) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Example 7 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class FilePath method createTempDir.

/**
     * Creates a temporary directory inside the directory represented by 'this'
     * @since 1.311
     */
public FilePath createTempDir(final String prefix, final String suffix) throws IOException, InterruptedException {
    try {
        return new FilePath(this, act(new FileCallable<String>() {

            public String invoke(File dir, VirtualChannel channel) throws IOException {
                File f = File.createTempFile(prefix, suffix, dir);
                f.delete();
                f.mkdir();
                return f.getName();
            }
        }));
    } catch (IOException e) {
        throw new IOException2("Failed to create a temp directory on " + remote, e);
    }
}
Also used : VirtualChannel(hudson.remoting.VirtualChannel) IOException(java.io.IOException) File(java.io.File) IOException2(hudson.util.IOException2)

Example 8 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class AnnotatedLargeText method createAnnotator.

private ConsoleAnnotator createAnnotator(StaplerRequest req) throws IOException {
    try {
        String base64 = req != null ? req.getHeader("X-ConsoleAnnotator") : null;
        if (base64 != null) {
            Cipher sym = Secret.getCipher("AES");
            sym.init(Cipher.DECRYPT_MODE, Hudson.getInstance().getSecretKeyAsAES128());
            ObjectInputStream ois = new ObjectInputStreamEx(new GZIPInputStream(new CipherInputStream(new ByteArrayInputStream(Base64.decode(base64.toCharArray())), sym)), Hudson.getInstance().pluginManager.uberClassLoader);
            long timestamp = ois.readLong();
            if (TimeUnit2.HOURS.toMillis(1) > abs(System.currentTimeMillis() - timestamp))
                // don't deserialize something too old to prevent a replay attack
                return (ConsoleAnnotator) ois.readObject();
        }
    } catch (GeneralSecurityException e) {
        throw new IOException2(e);
    } catch (ClassNotFoundException e) {
        throw new IOException2(e);
    }
    // start from scratch
    return ConsoleAnnotator.initial(context == null ? null : context.getClass());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) ObjectInputStreamEx(hudson.remoting.ObjectInputStreamEx) IOException2(hudson.util.IOException2) ObjectInputStream(java.io.ObjectInputStream)

Example 9 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class ClassicPluginStrategy method load.

public void load(PluginWrapper wrapper) throws IOException {
    // override the context classloader so that XStream activity in plugin.start()
    // will be able to resolve classes in this plugin
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(wrapper.classLoader);
    try {
        String className = wrapper.getPluginClass();
        if (className == null) {
            // use the default dummy instance
            wrapper.setPlugin(new DummyImpl());
        } else {
            try {
                Class clazz = wrapper.classLoader.loadClass(className);
                Object o = clazz.newInstance();
                if (!(o instanceof Plugin)) {
                    throw new IOException(className + " doesn't extend from hudson.Plugin");
                }
                wrapper.setPlugin((Plugin) o);
            } catch (LinkageError e) {
                throw new IOException2("Unable to load " + className + " from " + wrapper.getShortName(), e);
            } catch (ClassNotFoundException e) {
                throw new IOException2("Unable to load " + className + " from " + wrapper.getShortName(), e);
            } catch (IllegalAccessException e) {
                throw new IOException2("Unable to create instance of " + className + " from " + wrapper.getShortName(), e);
            } catch (InstantiationException e) {
                throw new IOException2("Unable to create instance of " + className + " from " + wrapper.getShortName(), e);
            }
        }
        // initialize plugin
        try {
            Plugin plugin = wrapper.getPlugin();
            plugin.setServletContext(pluginManager.context);
            startPlugin(wrapper);
        } catch (Throwable t) {
            // gracefully handle any error in plugin.
            throw new IOException2("Failed to initialize", t);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : DummyImpl(hudson.Plugin.DummyImpl) IOException(java.io.IOException) MaskingClassLoader(hudson.util.MaskingClassLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) URLClassLoader(java.net.URLClassLoader) IOException2(hudson.util.IOException2)

Example 10 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class ClassicPluginStrategy method explode.

/**
     * Explodes the plugin into a directory, if necessary.
     */
private static void explode(File archive, File destDir) throws IOException {
    if (!destDir.exists())
        destDir.mkdirs();
    // timestamp check
    File explodeTime = new File(destDir, ".timestamp");
    if (explodeTime.exists() && explodeTime.lastModified() == archive.lastModified())
        // no need to expand
        return;
    // delete the contents so that old files won't interfere with new files
    Util.deleteContentsRecursive(destDir);
    try {
        Expand e = new Expand();
        e.setProject(new Project());
        e.setTaskType("unzip");
        e.setSrc(archive);
        e.setDest(destDir);
        e.execute();
    } catch (BuildException x) {
        throw new IOException2("Failed to expand " + archive, x);
    }
    try {
        new FilePath(explodeTime).touch(archive.lastModified());
    } catch (InterruptedException e) {
        // impossible
        throw new AssertionError(e);
    }
}
Also used : Project(org.apache.tools.ant.Project) Expand(org.apache.tools.ant.taskdefs.Expand) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) IOException2(hudson.util.IOException2)

Aggregations

IOException2 (hudson.util.IOException2)37 IOException (java.io.IOException)20 File (java.io.File)16 FileInputStream (java.io.FileInputStream)10 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 InputStream (java.io.InputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 TarInputStream (hudson.org.apache.tools.tar.TarInputStream)3 VirtualChannel (hudson.remoting.VirtualChannel)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 Document (org.w3c.dom.Document)3 Element (org.w3c.dom.Element)3 NodeList (org.w3c.dom.NodeList)3 SAXException (org.xml.sax.SAXException)3 XmlPullParser (org.xmlpull.v1.XmlPullParser)3 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)3 StreamException (com.thoughtworks.xstream.io.StreamException)2 ObjectInputStreamEx (hudson.remoting.ObjectInputStreamEx)2