import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.ResourceBundle; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javazoom.jl.player.Player; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; public class Monitor { private static ResourceBundle p = ResourceBundle.getBundle("Config"); public static void main(String[] args) { int interval = Integer.valueOf(p.getString("interval")); String do_url = p.getString("do_url"); String pharmweb_url = p.getString("pharmweb_url"); String do_login_url = p.getString("do_login_url"); String pharmweb_login_url = p.getString("pharmweb_login_url"); int timeout = Integer.valueOf(p.getString("timeout")); String proxyAddress = p.getString("proxy"); int port = Integer.valueOf(p.getString("port")); String musicPath = p.getString("music_path"); int sendmailCount = Integer.valueOf(p .getString("sendmail_interrupt_count")); int existCount = Integer.valueOf(p.getString("exit_interrupt_count")); ScheduledExecutorService service = Executors .newSingleThreadScheduledExecutor(); Task task = new Task(do_url, do_login_url, pharmweb_url, pharmweb_login_url, timeout, proxyAddress, port, musicPath, sendmailCount, existCount); service.scheduleWithFixedDelay(task, 0, interval, TimeUnit.SECONDS); } } class Task implements Runnable { private static final SimpleDateFormat sf = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss"); private String do_url ; private String pharmweb_url ; private String do_login_url ; private String pharmweb_login_url ; private int timeout = 30; private String proxyAddress = "proxy1.scig.gov.hk"; private int port = 8080; private String music; private int sendmailCount; private int existCount; private static HashMap<String, Integer> interruptCounter = new HashMap<String, Integer>(); private static int totalErrorConnection = 0; public Task(String do_url, String do_login_url, String pharmweb_url, String pharmweb_login_url, int timeout, String proxyAddress, int port, String music, int sendmailCount, int existCount) { this.do_url = do_url; this.do_login_url = do_login_url; this.pharmweb_url = pharmweb_url; this.pharmweb_login_url = pharmweb_login_url; this.timeout = timeout; this.proxyAddress = proxyAddress; this.port = port; this.music = music; this.sendmailCount = sendmailCount; this.existCount = existCount; } @Override public void run() { System.out.printf("=============== %s =====================\n", sf .format(new Date())); boolean useProxy = false; if (!"".equals(this.proxyAddress)) { useProxy = true; } System.out.printf("connect: %s\n", do_url); if (connectionChecking(do_url, useProxy)) { System.out.printf("connect: %s\n", do_login_url); checkDoLogin(do_login_url, useProxy); } System.out.printf("connect: %s\n", pharmweb_url); if (connectionChecking(pharmweb_url, false)) { System.out.printf("connect: %s\n", pharmweb_login_url); checkPharmwebLogin(pharmweb_login_url); } if (totalErrorConnection > 0) { System.out.printf("total error connection: [%s]\n", totalErrorConnection); if (totalErrorConnection > this.existCount) { System.exit(0); } } System.out.println("\n"); notifyUser(); } private void notifyUser() { for (String url : interruptCounter.keySet()) { int connectErrors = interruptCounter.get(url); if (connectErrors > this.sendmailCount) { System.out.println("send mail to notify users. error url: " + url); interruptCounter.put(url, 0); } } } private boolean connectionChecking(String url, boolean useProxy) { DefaultHttpClient client = new DefaultHttpClient(); if (useProxy) { HttpHost proxy = new HttpHost(proxyAddress, port); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } client.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); HttpGet httpget = new HttpGet(url); try { HttpResponse response = client.execute(httpget); System.out.println(response.getStatusLine()); return true; } catch (ClientProtocolException e) { processException(url, e); } catch (IOException e) { processException(url, e); } finally { postProcess(client, url); } return false; } private void checkPharmwebLogin(String url) { final DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); HttpPost httpget = new HttpPost(url); try { HttpResponse response = client.execute(httpget); System.out.println(response.getStatusLine()); } catch (ClientProtocolException e) { processException(url, e); } catch (IOException e) { processException(url, e); } finally { postProcess(client, url); } } private void checkDoLogin(String url, boolean useProxy) { HttpClient client = WebClientDevWrapper .wrapClient(new DefaultHttpClient()); HttpPost httpget = new HttpPost(url); try { if (useProxy) { HttpHost proxy = new HttpHost(proxyAddress, port); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } client.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); HttpResponse response = client.execute(httpget); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } catch (ClientProtocolException e) { processException(url, e); } catch (IOException e) { processException(url, e); } finally { postProcess(client, url); } } private void processException(String url, Exception e) { e.printStackTrace(); System.out.printf("connect error:[%s]\n ", url); try { InputStream in = new FileInputStream(music); Player player = new Player(in); player.play(); } catch (Exception e1) { e1.printStackTrace(); } int num = 1; if (interruptCounter.containsKey(url)) { num = interruptCounter.get(url); num++; interruptCounter.put(url, num); } else { interruptCounter.put(url, num); } System.out.printf("url [%s] interrupt:[%d] \n", url, num); totalErrorConnection++; } private void postProcess(HttpClient client, String url) { client.getConnectionManager().shutdown(); /* * if (interruptCounter.containsKey(url)) { interruptCounter.put(url, * 0); } */ } } class WebClientWrapper { public static HttpClient wrapClient(HttpClient base) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); return new DefaultHttpClient(ccm, base.getParams()); } catch (Exception e) { e.printStackTrace(); return null; } } }