private String readBom(URL bom) { try (final InputStream ins = bom.openStream()) { return Streams.readFullyString(ins); } catch (IOException e) { throw Exceptions.propagateAnnotated("Error loading Catalog BOM from " + bom, e); } }
/** returns the manifest in a JAR file, or null if no manifest contained therein */ public Manifest getManifest(File f) { JarFile jf = null; try { jf = new JarFile(f); return jf.getManifest(); } catch (IOException e) { throw Exceptions.propagateAnnotated("Unable to read "+f+" when looking for manifest", e); } finally { Streams.closeQuietly(jf); } }
private <T> T constructWithSpecial(Class<? extends SpecialBrooklynObjectConstructor> special, Class<? extends T> clazz, AbstractBrooklynObjectSpec<?, ?> spec) { try { return special.newInstance().create(managementContext, clazz, spec); } catch (Exception e) { Exceptions.propagateIfFatal(e); throw Exceptions.propagateAnnotated("Unable to create "+clazz+" "+spec+" using special "+special, e); } }
/** installs the given JAR file as an OSGi bundle; all manifest info should be already set up. * bundle-start semantics are TBD. * * @deprecated since 0.12.0, use {@link OsgiManager#installUploadedBundle(org.apache.brooklyn.api.typereg.ManagedBundle, InputStream)}*/ @Deprecated public Bundle installBundle(File f, boolean start) { try { Bundle b = Osgis.install( framework, "file://"+f.getAbsolutePath() ); if (start) { // benefits of start: // a) we get wiring issues thrown here, and // b) catalog.bom in root will be scanned synchronously here // however drawbacks: // c) other code doesn't always do it (method above) // d) heavier-weight earlier // e) tests in IDE break (but mvn fine) b.start(); } return b; } catch (Exception e) { throw Exceptions.propagateAnnotated("Error starting bundle from "+f, e); } }
@Override public void append(String val) { try { if (val==null) val = ""; FileUtil.setFilePermissionsTo600(file); Files.append(val, file, Charsets.UTF_8); } catch (IOException e) { throw Exceptions.propagateAnnotated("Problem appending to file "+file, e); } }
/** create a copy of the given ZIP as a JAR with the given manifest, returning the new temp file */ public File copyAddingManifest(File f, Manifest mf) { File f2 = Os.newTempFile(f.getName(), "zip"); ZipOutputStream zout = null; ZipFile zf = null; try { zout = new JarOutputStream(new FileOutputStream(f2), mf); writeZipEntriesFromFile(zout, f, Predicates.not(Predicates.equalTo(MANIFEST_PATH))); } catch (IOException e) { throw Exceptions.propagateAnnotated("Unable to read "+f+" when looking for manifest", e); } finally { Streams.closeQuietly(zf); Streams.closeQuietly(zout); } return f2; }
protected Maybe<String> transformUri(Entity source, MachineLocation machine, String sensorVal) { URI uri = URI.create(sensorVal); int port = uri.getPort(); if (port == -1 && "http".equalsIgnoreCase(uri.getScheme())) port = 80; if (port == -1 && "https".equalsIgnoreCase(uri.getScheme())) port = 443; if (port != -1) { Optional<HostAndPort> mappedEndpoint = getMappedEndpoint(source, machine, port); if (!mappedEndpoint.isPresent()) { LOG.trace("network-facing enricher not transforming {} URI {}, because no port-mapping for {}", new Object[] {source, sensorVal, machine}); return Maybe.absent(); } if (!mappedEndpoint.get().hasPort()) { LOG.debug("network-facing enricher not transforming {} URI {}, because no port in target {} for {}", new Object[] {source, sensorVal, mappedEndpoint, machine}); return Maybe.absent(); } URI result; try { result = new URI(uri.getScheme(), uri.getUserInfo(), mappedEndpoint.get().getHostText(), mappedEndpoint.get().getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); } catch (URISyntaxException e) { LOG.debug("Error transforming URI "+uri+", using target "+mappedEndpoint+"; rethrowing"); throw Exceptions.propagateAnnotated("Error transforming URI "+uri+", using target "+mappedEndpoint, e); } return Maybe.of(result.toString()); } else { LOG.debug("sensor mapper not transforming URI "+uri+" because no port defined"); return Maybe.absent(); } }
private Response buildCreateResponse(Iterable<RegisteredType> catalogItems) { log.info("REST created catalog items: "+catalogItems); Map<String,Object> result = MutableMap.of(); for (RegisteredType catalogItem: catalogItems) { try { result.put( catalogItem.getId(), CatalogTransformer.catalogItemSummary(brooklyn(), catalogItem, ui.getBaseUriBuilder())); } catch (Throwable t) { log.warn("Error loading catalog item '"+catalogItem+"' (rethrowing): "+t); // unfortunately items are already added to the catalog and hard to remove, // but at least let the user know; // happens eg if a class refers to a missing class, like // loading nosql items including mongo without the mongo bson class on the classpath throw Exceptions.propagateAnnotated("At least one unusable item was added ("+catalogItem.getId()+")", t); } } return Response.status(Status.CREATED).entity(result).build(); }
@Test public void testPropagateWithAnnotationExplicitNotSuppressed() throws Exception { Throwable t = new RuntimeException("test"); try { Exceptions.propagateAnnotated("important", t); } catch (Throwable t2) { t = t2; } Assert.assertEquals(Exceptions.collapseText(t), "important: test"); }
@Override public String get() { try { if (!exists()) return null; return Files.asCharSource(file, Charsets.UTF_8).read(); } catch (IOException e) { throw Exceptions.propagateAnnotated("Problem reading String contents of file "+file, e); } }
@Override public byte[] getBytes() { try { if (!exists()) return null; return Files.asByteSource(file).read(); } catch (IOException e) { throw Exceptions.propagateAnnotated("Problem reading bytes of file "+file, e); } }
private void loadCatalogItems(boolean failOnLoadError) { Iterable<CatalogItemDtoAbstract<?, ?>> entries = dto.getUniqueEntries(); if (entries!=null) { for (CatalogItemDtoAbstract<?,?> entry : entries) { try { CatalogUtils.installLibraries(mgmt, entry.getLibraries()); } catch (Exception e) { Exceptions.propagateIfFatal(e); if (failOnLoadError) { Exceptions.propagateAnnotated("Loading bundles for catalog item " + entry.getCatalogItemId() + " failed", e); } else { log.error("Loading bundles for catalog item " + entry + " failed: " + e.getMessage(), e); } } } } }
throw Exceptions.propagateAnnotated("Unable to read "+f+" when looking for manifest", e); } finally { Streams.closeQuietly(zf);
throw Exceptions.propagateAnnotated("Error creating ZIP from classpath spec "+path, e);
new Object[] { libraries.size(), libraries.size() - errors.size(), errors.keySet() }); if (errors.size()==1) { throw Exceptions.propagateAnnotated("Error starting referenced library in Brooklyn bundle "+Iterables.getOnlyElement(errors.keySet()), Iterables.getOnlyElement(errors.values())); } else {
} catch (Exception e) { if (context!=null) { throw Exceptions.propagateAnnotated("Error getting resource '"+url+"' for "+context, e); } else { throw Exceptions.propagate(e);
@Override public void put(ByteSource bytes) { try { FileUtil.setFilePermissionsTo600(tmpFile); final FileOutputStream tempStream = new FileOutputStream(tmpFile); final InputStream byteStream = bytes.openStream(); try { Streams.copy(byteStream, tempStream); syncFileSystem(tempStream.getFD()); } finally { Streams.closeQuietly(byteStream); Streams.closeQuietly(tempStream); } FileBasedObjectStore.moveFile(tmpFile, file); } catch (IOException e) { throw Exceptions.propagateAnnotated("Problem writing data to file "+file+" (via temporary file "+tmpFile+")", e); } catch (InterruptedException e) { throw Exceptions.propagate(e); } }
return result; } catch (FileNotFoundException e) { throw Exceptions.propagateAnnotated("Error extracting "+url+" to scan "+containingBundle.getVersionedName(), e); } finally { fJar.delete();