/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package stockfaces; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.faces.component.UIForm; import javax.faces.component.UIGraphic; import javax.faces.component.UIInput; import javax.faces.component.UIOutput; import javax.faces.component.UIPanel; import javax.faces.component.UISelectOne; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; /** * This bean has methods to retrieve stock information from * the Yahoo quote service. */ public class Bean { private static final String SERVICE_URL = "http://quote.yahoo.com/d/quotes.csv"; /** * Action method that is used to retrieve stock information. * This method uses two helper methods - one to get the * stock information, and the other to dynamically build * the "data" components for the UI. */ public void getStockInfo(ActionEvent ae) { FacesContext context = FacesContext.getCurrentInstance(); UIForm form = (UIForm)context.getViewRoot().findComponent("form"); UISelectOne select = (UISelectOne)form.findComponent("connection"); String connection = (String)select.getValue(); if (form != null) { if (connection.equals("Remote")) { setProxyIfNeeded(form); } UIInput component = (UIInput)form.findComponent("symbol"); if (component != null) { String symbolInput = (String)component.getValue(); if (symbolInput != null) { String[] symbols = symbolInput.split("\\s"); String[] stockData = null; if (connection.equals("Local")) { stockData = getLocalStockData(symbols); buildUI(stockData); return; } try { stockData = getStockData(symbols); buildUI(stockData); } catch (Exception e) { } } } } } /** * Helper method to get the stock data (remotely). */ private String[] getStockData(String[] symbols) throws IOException, MalformedURLException { String[] data = new String[symbols.length]; for (int i=0; i 0) { imageComponent = new UIGraphic(); imageComponent.setUrl("images/up_g.gif"); dataPanel.getChildren().add(imageComponent); } else { outputComponent = new UIOutput(); outputComponent.setValue(""); dataPanel.getChildren().add(outputComponent); } // Price Change outputComponent = new UIOutput(); if (change < 0) { outputComponent.getAttributes().put("styleClass", "down-color"); } else if (change > 0) { outputComponent.getAttributes().put("styleClass", "up-color"); } outputComponent.setValue(String.valueOf(change)); dataPanel.getChildren().add(outputComponent); // Percent Change outputComponent = new UIOutput(); if (change < 0) { outputComponent.getAttributes().put("styleClass", "down-color"); } else if (change > 0) { outputComponent.getAttributes().put("styleClass", "up-color"); } outputComponent.setValue(data[5]); dataPanel.getChildren().add(outputComponent); // Volume outputComponent = new UIOutput(); outputComponent.setValue(data[6]); dataPanel.getChildren().add(outputComponent); } dataPanel.setRendered(true); } private void setProxyIfNeeded(UIForm form) { UIInput component = (UIInput)form.findComponent("proxyHost"); String proxyHost = (String)component.getValue(); component = (UIInput)form.findComponent("proxyPort"); String proxyPort = (String)component.getValue(); if ((proxyHost != null && proxyHost.length() > 0) && (proxyPort != null && proxyPort.length() > 0)) { try { System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", proxyPort); } catch (SecurityException e) { } } } private double round(double val, int places) { long factor = (long)Math.pow(10,places); // Shift the decimal the correct number of places // to the right. val = val * factor; // Round to the nearest integer. long tmp = Math.round(val); // Shift the decimal the correct number of places // back to the left. return (double)tmp / factor; } }