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);
}
}
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;
}
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);
}
}
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;
}
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);
}
}
Aggregations