Search in sources :

Example 11 with PlatformException

use of org.eclipse.scout.rt.platform.exception.PlatformException in project scout.rt by eclipse.

the class ClazzUtil method resolve.

/**
 * Resolves the concrete class as describes by the given <code>@Clazz</code> annotation.
 *
 * @param annotation
 *          meta-data for the class to be resolved.
 * @param expectedType
 *          the class type expected.
 * @param source
 *          used in case the resolution fails to give some more details about the origin.
 * @throws PlatformException
 *           is thrown if the class could not be resolved.
 */
public static <T> Class<T> resolve(final Clazz annotation, final Class<T> expectedType, final String source) {
    final Class<?> clazz = annotation.value();
    final String qualifiedName = annotation.qualifiedName();
    if (qualifiedName.isEmpty() && Clazz.NullClazz.class.equals(clazz)) {
        throw new PlatformException("No class specified for {}: missing 'value' or 'qualified name' attribute", source);
    }
    try {
        if (!qualifiedName.isEmpty()) {
            return ClazzUtil.assertClass(expectedType, Class.forName(qualifiedName), source);
        } else {
            return ClazzUtil.assertClass(expectedType, clazz, source);
        }
    } catch (final ReflectiveOperationException e) {
        throw new PlatformException("Failed to load class for {} [class={}, qualifiedName={}]", source, clazz, qualifiedName, e);
    }
}
Also used : PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException)

Example 12 with PlatformException

use of org.eclipse.scout.rt.platform.exception.PlatformException in project scout.rt by eclipse.

the class SandboxClassLoaderBuilder method addClasses.

public SandboxClassLoaderBuilder addClasses(String newJarFileName, String... classNames) {
    if (classNames == null || classNames.length == 0) {
        return this;
    }
    ByteArrayOutputStream jarData = new ByteArrayOutputStream();
    try {
        // create jar
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        try (JarOutputStream jar = new JarOutputStream(jarData, manifest)) {
            for (String className : classNames) {
                String classPath = (className.replace(".", "/")) + ".class";
                URL url = m_jarLocator.getResource(classPath);
                if (url == null) {
                    throw new PlatformException("Could not resolve URL for class {}", className);
                }
                // add entry
                JarEntry entry = new JarEntry(classPath);
                entry.setTime(0L);
                jar.putNextEntry(entry);
                try (InputStream in = url.openStream()) {
                    byte[] buf = new byte[ANY_SIZE];
                    int n;
                    while ((n = in.read(buf)) > 0) {
                        jar.write(buf, 0, n);
                    }
                }
                jar.closeEntry();
            }
            jar.finish();
        }
    } catch (IOException e) {
        throw new PlatformException("Cannot create jar for {}", Arrays.toString(classNames), e);
    }
    URL jarUrl = createTemporaryJar(newJarFileName, jarData.toByteArray());
    m_urls.put(jarUrl.toExternalForm(), jarUrl);
    return this;
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) URL(java.net.URL)

Example 13 with PlatformException

use of org.eclipse.scout.rt.platform.exception.PlatformException in project scout.rt by eclipse.

the class SandboxClassLoaderBuilder method createTemporaryJar.

static URL createTemporaryJar(String jarFilePath, byte[] data) {
    String jarFileName = new File(jarFilePath).getName();
    String tempDir = System.getProperty("java.io.tmpdir");
    if (!(tempDir.endsWith("/") || tempDir.endsWith("\\"))) {
        tempDir += System.getProperty("file.separator");
    }
    try {
        String sha1;
        try (InputStream inputStream = new ByteArrayInputStream(data)) {
            sha1 = readSha1(inputStream);
        }
        File targetFile = new File(tempDir, sha1 + "-" + jarFileName);
        if (targetFile.exists()) {
            String targetFileSha1;
            try (InputStream inputStream = new FileInputStream(targetFile)) {
                targetFileSha1 = readSha1(inputStream);
            }
            if (targetFileSha1.equals(sha1)) {
                return targetFile.toURI().toURL();
            }
            // sha1 hash of existing file doesn't match
            // use newly created temporary file as fallback
            File f = File.createTempFile("jar-sandbox-", jarFileName);
            LOG.error("Target file '{}' already exists but has wrong sha1 hash [{}]. Using new temporary file instead '{}' [{}].", targetFile.getAbsolutePath(), targetFileSha1, f.getAbsolutePath(), sha1);
            targetFile = f;
        }
        targetFile.deleteOnExit();
        writeContent(targetFile, data);
        return targetFile.toURI().toURL();
    } catch (NoSuchAlgorithmException e) {
        throw new PlatformException("SHA-1 algorithm is missing but required to verify jar for classloader", e);
    } catch (IOException e) {
        throw new PlatformException("JAR {} could not be extracted to temp directory", jarFilePath, e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 14 with PlatformException

use of org.eclipse.scout.rt.platform.exception.PlatformException in project scout.rt by eclipse.

the class SandboxClassLoaderBuilder method addLocalJar.

public SandboxClassLoaderBuilder addLocalJar(String path) {
    URL url = m_jarLocator.getResource(path);
    if (url == null) {
        throw new PlatformException("Could not resolve URL for path '{}'", path);
    }
    ByteArrayOutputStream jarData = new ByteArrayOutputStream();
    try {
        try (InputStream in = url.openStream()) {
            byte[] buf = new byte[ANY_SIZE];
            int n;
            while ((n = in.read(buf)) > 0) {
                jarData.write(buf, 0, n);
            }
        }
        URL jarUrl = createTemporaryJar(path, jarData.toByteArray());
        m_urls.put(jarUrl.toExternalForm(), jarUrl);
    } catch (Exception e) {
        throw new PlatformException("Cannot read content of {}", path, e);
    }
    return this;
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) IOException(java.io.IOException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 15 with PlatformException

use of org.eclipse.scout.rt.platform.exception.PlatformException in project scout.rt by eclipse.

the class JandexInventoryBuilder method scanAllModules.

public void scanAllModules() {
    try {
        for (Enumeration<URL> en = getClass().getClassLoader().getResources(SCOUT_XML_PATH); en.hasMoreElements(); ) {
            URL url = en.nextElement();
            URI indexUri = findIndexUri(url);
            scanModule(indexUri);
        }
    } catch (IOException ex) {
        throw new PlatformException("Error while reading resources '{}'", SCOUT_XML_PATH, ex);
    }
}
Also used : PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL)

Aggregations

PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)35 Test (org.junit.Test)13 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)9 IOException (java.io.IOException)8 FileInputStream (java.io.FileInputStream)4 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)4 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 TestLookupCall (org.eclipse.scout.rt.client.ui.form.fields.smartfield.fixture.TestLookupCall)3 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)3 ILookupCall (org.eclipse.scout.rt.shared.services.lookup.ILookupCall)3 File (java.io.File)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 BeanMetaData (org.eclipse.scout.rt.platform.BeanMetaData)2