public static String getImagePathForClass(final Class<?> cls, final ClassLoader classLoader) { final String iconPath = cls.getName().replaceAll("\\.", "/") + ".png"; final URL url = ResourceManager.get().getUrl(iconPath, classLoader); if (url == null) { return null; } return iconPath; }
public ImageIcon getImageIcon(final String imagePath, final ClassLoader... classLoaders) { if (imagePath.endsWith(".gif")) { // animated gif's will loose their animations if loaded as images final URL url = resourceManager.getUrl(imagePath, classLoaders); return new ImageIcon(url); } return new ImageIcon(getImage(imagePath, classLoaders)); }
public static String getLicense(final String licenseName) { final URL url = resourceManager.getUrl("licenses/" + licenseName + ".txt"); if (url == null) { throw new IllegalArgumentException("Could not find license file for license: " + licenseName); } BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(url.openStream(), FileHelper.UTF_8_ENCODING)); final StringBuilder sb = new StringBuilder(); for (String line = reader.readLine(); line != null; line = reader.readLine()) { if (sb.length() != 0) { sb.append('\n'); } sb.append(line); } return sb.toString(); } catch (final Exception e) { throw new IllegalStateException("Error occurred while reading license file: " + licenseName, e); } finally { if (reader != null) { try { reader.close(); } catch (final IOException e) { // do nothing } } } }
private DataCleanerConfiguration getConfigurationFromClasspath(final JaxbConfigurationReader configurationReader) { logger.info("Reading conf.xml from classpath"); try { return configurationReader.create(ResourceManager.get().getUrl("datacleaner-home/conf.xml").openStream()); } catch (final Exception e) { logger.warn("Unexpected error while reading conf.xml from classpath!", e); logger.warn("Creating a bare-minimum configuration because of previous errors!"); return new DataCleanerConfigurationImpl(); } }
@Override public Image getWelcomeImage() { if (_arguments.getJobFile() != null) { if ("Kettle".equals(_embeddedClientName)) { try { final URL url = ResourceManager.get().getUrl("images/pdi_dc_banner.png"); return ImageIO.read(url); } catch (final Exception e) { logger.warn("Could not load PDI DC banner", e); } } } return null; } }
public Image getImage(final String imagePath, final ClassLoader... classLoaders) { Image image = _cachedImageIcons.getIfPresent(imagePath); if (image == null) { final URL url = resourceManager.getUrl(imagePath, classLoaders); if (url == null && classLoaders != null && classLoaders.length > 0) { return getImage(imagePath, new ClassLoader[0]); } else if (url == null) { logger.warn("Image path ({}) could not be resolved", imagePath); throw new IllegalArgumentException( "Could not read image: '" + imagePath + "' (url could not be resolved)"); } else { logger.debug("Image path ({}) resolved to url: {}", imagePath, url); } try { image = ImageIO.read(url); } catch (final IOException e) { throw new IllegalStateException("Could not read image from url:" + url); } if (image == null) { throw new IllegalArgumentException("Could not read image: " + imagePath + " (url: " + url + ")"); } } _cachedImageIcons.put(imagePath, image); return image; }
private static Font createFont(final String path) { final URL url = ResourceManager.get().getUrl(path); if (url == null) { throw new IllegalArgumentException("Font resource not found: " + path); } InputStream in = null; try { in = url.openStream(); return Font.createFont(Font.TRUETYPE_FONT, in); } catch (final Exception e) { throw new IllegalStateException(e); } finally { FileHelper.safeClose(in); } }
private static FileObject overwriteFileWithDefaults(final FileObject targetDirectory, final String targetFilename) throws FileSystemException { final FileObject file = targetDirectory.resolveFile(targetFilename); final FileObject parentFile = file.getParent(); if (!parentFile.exists()) { parentFile.createFolder(); } final ResourceManager resourceManager = ResourceManager.get(); final URL url = resourceManager.getUrl("datacleaner-home/" + targetFilename); if (url == null) { return null; } InputStream in = null; OutputStream out = null; try { in = url.openStream(); out = file.getContent().getOutputStream(); FileHelper.copy(in, out); } catch (final IOException e) { throw new IllegalArgumentException(e); } finally { FileHelper.safeClose(in, out); } return file; }
private static FileObject copyIfNonExisting(final FileObject candidate, final FileSystemManager manager, final String filename) throws FileSystemException { final FileObject file = candidate.resolveFile(filename); if (file.exists()) { logger.info("File already exists in DATACLEANER_HOME: " + filename); return file; } final FileObject parentFile = file.getParent(); if (!parentFile.exists()) { parentFile.createFolder(); } final ResourceManager resourceManager = ResourceManager.get(); final URL url = resourceManager.getUrl("datacleaner-home/" + filename); if (url == null) { return null; } InputStream in = null; OutputStream out = null; try { in = url.openStream(); out = file.getContent().getOutputStream(); FileHelper.copy(in, out); } catch (final IOException e) { throw new IllegalArgumentException(e); } finally { FileHelper.safeClose(in, out); } return file; }
public static List<LicensedProject> getLicensedProjects() { final List<LicensedProject> result = new ArrayList<>(); final URL url = resourceManager.getUrl("licenses/dependency-licenses.csv"); if (url == null) { throw new IllegalStateException("Could not find dependencies file"); } try { final DataContext dc = DataContextFactory.createCsvDataContext(url.openStream(), ',', '"'); final Table table = dc.getDefaultSchema().getTable(0); final Column projectColumn = table.getColumnByName("Project"); final Column websiteColumn = table.getColumnByName("Website"); final Column licenseColumn = table.getColumnByName("License"); final Query q = dc.query().from(table).select(table.getColumns()).orderBy(projectColumn).asc().toQuery(); final DataSet ds = dc.executeQuery(q); while (ds.next()) { final LicensedProject licensedProject = new LicensedProject(); final Row row = ds.getRow(); final String licenseName = row.getValue(licenseColumn).toString(); licensedProject.name = row.getValue(projectColumn).toString(); licensedProject.websiteUrl = row.getValue(websiteColumn).toString(); licensedProject.license = getLicense(licenseName); result.add(licensedProject); } } catch (final IOException e) { throw new IllegalStateException("Error occurred while reading dependencies file", e); } return result; }