スキップしてメイン コンテンツに移動

投稿

Java: Get Exchange Rate From Yahoo Finance Api

In this post, I will show you how to retrieve currency exchange rate from yahoo finance api by Java program. There are some apis for getting exchange rate - reuters, bloomberg, yahoo finance etc. If you don't have to get high-ratency realtime exchange rate, the most simple and free api - yahoo finance api - is enough. Okay, here is the actual code. The api returns exchange rate as a Rate object. package com.dukesoftware.exchangerate.api; import static java.lang.String.format; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; public class YahooExchangeRateApi implements ExchangeRateApi { // url for yahoo finance api private final static String URL_FORMAT = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X"; public Rate getRate(String ccy1, String ccy2) { assertNotNull(ccy1, "

Get Root Cause of Java Exception

I think sometimes you would like to simply extract root cause of exception. The following method will help you. public final static Throwable getRootCause(Throwable t){ for(Throwable s = t.getCause() ;s != null;){ s = t.getCause(); t = s; } return t; }

Create Stack Trace String from Java Exception

Maybe many times you would like to generate stack trace string from Exception thrown. The code below will help you! public final static String createStackTraceString(Exception e){ StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw, false); e.printStackTrace(writer); return sw.toString(); }

File Clean Up and Archiving Linux Command Tips

If you would like to delete files whose name ends with "batch.log" older than 30 days, Use the following command. find /var/log/ -name "*batch.log" -mtime +30 -type -f -delete If you would like to archive files whose name ends with "batch.log" older than 30 days, Use the following command. The archived file will have timestamp at the end of the filename. find /var/log/ -name "*batch.log" -mtime +30 -type -f -pront0 | tar -czvf /var/log/batch.log.tar.gz.`date +\%Y%m%d%H%M%S` -T - --null --remove-files

Solve Local Capistrano Deployment Issue

Local Capistrano Deployment Issue I have an inssue when deploying PHP application on "local" machine (e.g. deploying app to same machine where executing capistrano deploy.) using Capistrano. The cause is when capistrano creates temporary directory for destination (remote) directory and directory for source (local) directory point to same directory. This problem does not happen when you deploy to different machine. Solution Add the below code to your deploy.rb!! Just change the name for temoprary directory used during deployment name. # put this line at the top of app.yml in order to use SecureRandom methods require "securerandom" # set flag true when local deployment set :deploy_to_self, "true" # hook tasks before :deploy, "local:create_dir" after :deploy, "local:clean_dir" #################################### # setup copy_dir, copy_remote_dir # when deploying to machine which this script is running on to avoid copy issue # na

Java: Extract img src from HTML

Here is an code snippet for extracting image src from html. private static final Pattern IMG_SRC_PATTERN = Pattern.compile("<img\\s+.*src\\s*=\\s*('|\")(.+?)\\1.+?>"); public static List<String> extractImgSrces(final String content) { List<String> list = new ArrayList<>(); final Matcher matcher = IMG_SRC_PATTERN.matcher(content); while(matcher.find()){ list.add(matcher.group(2)); } return list; } The example usage is below. In this example, you should only prepare HttpUtils.getStringContentsFromURL method, which is getting html from given url, for your self. public static void main(String[] args) throws URISyntaxException, IOException { extractImgSrces(HttpUtils.getStringContentsFromURL("http://www.google.com/", "utf-8")).stream().forEach(System.out::println); }

Set Specific Revision Number for SVN External

Sometimes, You might want to set revision number for svn-external repository explicitly. In that case, you should just set svn-external with "-rXXXX" on your svn property (XXXX is the revision number you want to set)! I confimred it worked perfectly on windows, linux svn client. lib -rXXXX [svn-external target url]