List of usage examples for com.google.common.io Resources getResource
public static URL getResource(String resourceName)
From source file:fr.da2i.lup1.security.JwtManager.java
public JwtManager() { try {//from ww w. j a va 2 s .c o m URL url = Resources.getResource("key.ser"); try (ObjectInputStream ois = new ObjectInputStream(url.openStream())) { this.key = (Key) ois.readObject(); } catch (IOException | ClassNotFoundException e) { this.key = MacProvider.generateKey(); } } catch (IllegalArgumentException e) { e.printStackTrace(); } }
From source file:uk.os.vt.mbtiles.Util.java
/** * Legacy from file./*w w w . j a v a 2s . c o m*/ * * <p>Note: please see RxJDBC DatabaseCreator:62 with: * public static void createDatabase(Connection c) { * * @param file the file * @throws IOException thrown on IO error */ private static void legacyMaker(File file) throws IOException { Connection connection = null; try { connection = getConnection(file); final URL url = Resources.getResource("mbtiles_schema.sql"); final String sql = Resources.toString(url, Charsets.UTF_8); final Statement statement = connection.createStatement(); statement.setQueryTimeout(STATEMENT_QUERY_TIMEOUT_IN_SECONDS); statement.executeUpdate(sql); // replaced statement.execute(text); because multi statement SQL statement.close(); } catch (IOException | SQLException ex) { throw new IOException("cannot produce a valid MBTiles file", ex); } finally { try { if (connection != null) { connection.close(); } } catch (final SQLException ex) { // connection close failed. LOG.error("cannot close connection", ex); } } }
From source file:org.dswarm.graph.test.Neo4jEmbeddedDBWrapper.java
public Neo4jEmbeddedDBWrapper(final String mountEndpoint) { final URL resource = Resources.getResource("dmpgraph.properties"); final Properties properties = new Properties(); try {//from w w w. j a va2 s.c o m properties.load(resource.openStream()); } catch (final IOException e) { LOG.error("Could not load dmpgraph.properties", e); } serverPort = Integer.valueOf(properties.getProperty("embedded_neo4j_server_port", "7499")); MOUNT_POINT = mountEndpoint; }
From source file:org.hlc.demo.mybatis.comment.CodetemplatesLoader.java
private void loadAndResolveDocument() throws DocumentException { URL url = Resources.getResource("codetemplates.xml"); SAXReader reader = new SAXReader(); Document document = reader.read(url.getFile()); Element root = document.getRootElement(); TemplateVisitor visitor = new TemplateVisitor(); root.accept(visitor);//from w w w . ja v a 2s. c o m templates.putAll(visitor.getTemplates()); }
From source file:org.robobninjas.riemann.spring.server.RiemannProcessConfiguration.java
private Path getConfig() { final URL resource = Resources.getResource(riemannConfigResourcePath); return Paths.get(resource.getPath()); }
From source file:com.facebook.buck.json.BuckPythonProgram.java
/** * Create a new instance by layout the files in a temporary directory. */// w w w . j a v a 2 s . c om public static BuckPythonProgram newInstance(ConstructorArgMarshaller marshaller, ImmutableSet<Description<?>> descriptions) throws IOException { Path pythonPath; try { URL url = Resources.getResource("buck_parser"); if ("jar".equals(url.getProtocol())) { // Buck is being executed from a JAR file. Extract the jar file from the resource path, and // verify it is correct. // When python attempts to import `buck_parser`, it will see the jar file, and load it via // zipimport, and look into the `buck_parser` directory in the root of the jar. JarURLConnection connection = (JarURLConnection) url.openConnection(); Preconditions.checkState(connection.getEntryName().equals("buck_parser"), "buck_parser directory should be at the root of the jar file."); URI jarFileURI = connection.getJarFileURL().toURI(); pythonPath = Paths.get(jarFileURI); } else if ("file".equals(url.getProtocol())) { // Buck is being executed from classpath on disk. Set the parent directory as the python // path. // When python attempts to import `buck_parser`, it will look for a `buck_parser` child // directory in the given path. pythonPath = Paths.get(url.toURI()).getParent(); } else { throw new IllegalStateException( "buck_python resource directory should reside in a local directory or in a jar file. " + "Got: " + url); } } catch (URISyntaxException e) { throw new IllegalStateException("Failed to determine location of buck_parser python package", e); } Path generatedRoot = Files.createTempDirectory("buck_python_program"); LOG.debug("Writing python rules stub to %s.", generatedRoot); try (Writer out = Files.newBufferedWriter(generatedRoot.resolve("generated_rules.py"), UTF_8)) { out.write("from buck_parser.buck import *\n\n"); BuckPyFunction function = new BuckPyFunction(marshaller); for (Description<?> description : descriptions) { out.write(function.toPythonFunction(Description.getBuildRuleType(description), description.createUnpopulatedConstructorArg())); out.write('\n'); } } String pathlibDir = PATH_TO_PATHLIB_PY.getParent().toString(); String watchmanDir = PATH_TO_PYWATCHMAN.toString(); try (Writer out = Files.newBufferedWriter(generatedRoot.resolve("__main__.py"), UTF_8)) { out.write(Joiner.on("\n").join("from __future__ import absolute_import", "import sys", "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(pathlibDir)) + "\")", "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(watchmanDir)) + "\")", // Path to the bundled python code. "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(pythonPath)) + "\")", // Path to the generated rules stub. "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(generatedRoot)) + "\")", "if __name__ == '__main__':", " try:", " from buck_parser import buck", " buck.main()", " except KeyboardInterrupt:", " print >> sys.stderr, 'Killed by User'", "")); } LOG.debug("Created temporary buck.py instance at %s.", generatedRoot); return new BuckPythonProgram(generatedRoot); }
From source file:name.marcelomorales.siqisiqi.examples.simple.init.InitialData.java
@Inject public InitialData(DataSource dataSource) throws SQLException, IOException { jdbcTemplate = new JdbcTemplate(dataSource); try (Connection c = dataSource.getConnection()) { URL resource = Resources.getResource("ddl.sql"); String s = Resources.toString(resource, Charsets.US_ASCII); Iterable<String> split = Splitter.on(';').omitEmptyStrings().trimResults().split(s); for (String sql : split) { try (Statement st = c.createStatement()) { st.execute(sql);//from w ww . j av a2s . c om } } c.commit(); // this is needed because we don't use @TransactionAttribute } }
From source file:com.streamsets.datacollector.vault.api.VaultTestBase.java
protected String getBody(String path) throws IOException { return Resources.toString(Resources.getResource(path), Charsets.UTF_8); }
From source file:com.stratio.deep.examples.java.extractor.GroupingCellWithES.java
private static void dataSetImport() throws IOException, ExecutionException, InterruptedException { URL url = Resources.getResource(DATA_SET_NAME); IndexResponse response = client.prepareIndex("book", "test").setCreate(true).setSource(url.getFile()) .execute().actionGet();//w w w . j a va 2 s. c o m try { CountResponse action = client.prepareCount("book").setTypes("test").execute().actionGet(); } catch (Exception e) { e.printStackTrace(); } }
From source file:iterator.Splash.java
public Splash() { super();/*ww w . j a v a 2 s. c om*/ splash = Explorer.loadImage(Resources.getResource("splash.png")); parent = new JWindow(); parent.getContentPane().setLayout(new BorderLayout()); parent.getContentPane().add(this, BorderLayout.CENTER); parent.pack(); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension size = new Dimension(splash.getWidth(), splash.getHeight()); parent.setSize(size); parent.setPreferredSize(size); parent.setMinimumSize(size); parent.setLocation((screen.width / 2) - (size.width / 2), (screen.height / 2) - (size.height / 2)); parent.setAlwaysOnTop(true); }