Java tutorial
/* * Copyright (c) 2004-2007 Marco Maccaferri and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Marco Maccaferri - initial API and implementation */ package net.sourceforge.eclipsetrader.yahoo; import java.net.URL; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import net.sourceforge.eclipsetrader.core.CorePlugin; import net.sourceforge.eclipsetrader.core.INewsProvider; import net.sourceforge.eclipsetrader.core.db.NewsItem; import net.sourceforge.eclipsetrader.core.db.Security; import net.sourceforge.eclipsetrader.news.NewsPlugin; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.eclipse.core.net.proxy.IProxyData; import org.eclipse.core.net.proxy.IProxyService; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.preference.IPreferenceStore; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.fetcher.impl.FeedFetcherCache; import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache; import com.sun.syndication.fetcher.impl.HttpClientFeedFetcher; public class FrenchNewsProvider implements Runnable, INewsProvider { private final static String NEWS_NAME = Messages.FrenchNewsProvider_Name; private final static String NEWS_CONFIG_FILE = "categories.fr.xml"; //$NON-NLS-1$ private final static String NEWS_SECURITY_URL_BASE = "http://fr.search.news.yahoo.com/search/compnews_fr?p=ytic:"; //$NON-NLS-1$ private Thread thread; private boolean stopping = false; private FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); private HttpClientFeedFetcher fetcher = new HttpClientFeedFetcher(feedInfoCache); public FrenchNewsProvider() { } /* (non-Javadoc) * @see net.sourceforge.eclipsetrader.news.INewsProvider#start() */ public void start() { if (thread == null) { stopping = false; thread = new Thread(this); thread.start(); } } /* (non-Javadoc) * @see net.sourceforge.eclipsetrader.news.INewsProvider#stop() */ public void stop() { stopping = true; if (thread != null) { try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } thread = null; } } /* (non-Javadoc) * @see net.sourceforge.eclipsetrader.news.INewsProvider#snapshot() */ public void snapshot() { update(); } /* (non-Javadoc) * @see net.sourceforge.eclipsetrader.news.INewsProvider#snapshot(net.sourceforge.eclipsetrader.core.db.Security) */ public void snapshot(Security security) { try { update(new URL(NEWS_SECURITY_URL_BASE + security.getCode().toLowerCase()), security); } catch (Exception e) { CorePlugin.logException(e); } } /* (non-Javadoc) * @see java.lang.Runnable#run() */ public void run() { long nextRun = System.currentTimeMillis() + 2 * 1000; while (!stopping) { if (System.currentTimeMillis() >= nextRun) { update(); int interval = NewsPlugin.getDefault().getPreferenceStore() .getInt(NewsPlugin.PREFS_UPDATE_INTERVAL); nextRun = System.currentTimeMillis() + interval * 60 * 1000; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); break; } } thread = null; } private void update() { Job job = new Job(NEWS_NAME) { @Override protected IStatus run(IProgressMonitor monitor) { IPreferenceStore store = YahooPlugin.getDefault().getPreferenceStore(); List urls = new ArrayList(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(FileLocator.openStream(YahooPlugin.getDefault().getBundle(), new Path(NEWS_CONFIG_FILE), false)); NodeList childNodes = document.getFirstChild().getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); String nodeName = node.getNodeName(); if (nodeName.equalsIgnoreCase("category")) //$NON-NLS-1$ { String id = (node).getAttributes().getNamedItem("id").getNodeValue(); //$NON-NLS-1$ NodeList list = node.getChildNodes(); for (int x = 0; x < list.getLength(); x++) { Node item = list.item(x); nodeName = item.getNodeName(); Node value = item.getFirstChild(); if (value != null) { if (nodeName.equalsIgnoreCase("url")) //$NON-NLS-1$ { if (store.getBoolean(id)) urls.add(value.getNodeValue()); } } } } } } catch (Exception ex) { ex.printStackTrace(); } // List securities = CorePlugin.getRepository().allSecurities(); // monitor.beginTask("Fetching" + NEWS_NAME, securities.size() + urls.size()); // // for (Iterator iter = securities.iterator(); iter.hasNext(); ) // { // Security security = (Security) iter.next(); // try { // String url = NEWS_SECURITY_URL_BASE + security.getCode().toLowerCase(); // monitor.subTask(url); // update(new URL(url), security); // } catch(Exception e) { // CorePlugin.logException(e); // } // monitor.worked(1); // } for (Iterator iter = urls.iterator(); iter.hasNext();) { String url = (String) iter.next(); try { monitor.subTask(url); update(new URL(url)); } catch (Exception e) { CorePlugin.logException(e); } monitor.worked(1); } monitor.done(); return Status.OK_STATUS; } }; job.setUser(false); job.schedule(); } private void update(URL feedUrl) { update(feedUrl, null); } private void update(URL feedUrl, Security security) { System.out.println("Fetching " + feedUrl.toExternalForm()); //$NON-NLS-1$ boolean subscribersOnly = YahooPlugin.getDefault().getPreferenceStore() .getBoolean(YahooPlugin.PREFS_SHOW_SUBSCRIBERS_ONLY); try { HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); BundleContext context = YahooPlugin.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxy = (IProxyService) context.getService(reference); IProxyData data = proxy.getProxyDataForHost(feedUrl.getHost(), IProxyData.HTTP_PROXY_TYPE); if (data != null) { if (data.getHost() != null) client.getHostConfiguration().setProxy(data.getHost(), data.getPort()); if (data.isRequiresAuthentication()) client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(data.getUserId(), data.getPassword())); } } SyndFeed feed = fetcher.retrieveFeed(feedUrl, client); for (Iterator iter = feed.getEntries().iterator(); iter.hasNext();) { SyndEntry entry = (SyndEntry) iter.next(); if (!subscribersOnly && entry.getTitle().indexOf("[$$]") != -1) //$NON-NLS-1$ continue; NewsItem news = new NewsItem(); news.setRecent(true); Date entryDate = entry.getPublishedDate(); if (entry.getUpdatedDate() != null) entryDate = entry.getUpdatedDate(); if (entryDate != null) { Calendar date = Calendar.getInstance(); date.setTime(entryDate); date.set(Calendar.SECOND, 0); news.setDate(date.getTime()); } String title = entry.getTitle(); if (title.endsWith(")")) //$NON-NLS-1$ { int s = title.lastIndexOf('('); if (s != -1) { news.setSource(title.substring(s + 1, title.length() - 1)); title = title.substring(0, s - 1).trim(); } } news.setTitle(title); news.setUrl(entry.getLink()); if (security != null) news.addSecurity(security); // System.out.println("** News found:"); // System.out.println(news.getTitle()); // System.out.println(news.getSource()); // System.out.println(news.getUrl()); // System.out.println("--"); CorePlugin.getRepository().save(news); } } catch (Exception e) { CorePlugin.logException(e); } } }