KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossnet > external > server > FederatedServiceBean


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: FederatedServiceBean.java,v 1.1.1.1 2004/08/04 09:07:48 tdiesler Exp $
9

10 package org.jboss.test.jbossnet.external.server;
11
12 import java.rmi.RemoteException JavaDoc;
13
14 import javax.ejb.SessionBean JavaDoc;
15
16 import org.jboss.test.util.ejb.SessionSupport;
17 import org.jboss.test.jbossnet.external.google.GoogleSearchPort;
18 import org.jboss.test.jbossnet.external.google.GoogleSearchService;
19 import org.jboss.test.jbossnet.external.google.GoogleSearchResult;
20
21 import org.jboss.test.jbossnet.external.babelfish.BabelFishPortType;
22 import org.jboss.test.jbossnet.external.babelfish.BabelFishService;
23 import org.jboss.logging.Logger;
24
25 import javax.naming.InitialContext JavaDoc;
26
27 /**
28  * implementation of a federated ejb service making
29  * use of external web services bound in the JNDI tree.
30  * @version 1.0
31  * @author cgjung
32  * @ejb.bean name="FederatedService"
33  * display-name="Federated Service"
34  * type="Stateless"
35  * view-type="both"
36  * jndi-name="FederatedService"
37  * local-jndi-name="FederatedServiceLocal"
38  * @ejb.transaction type="Required"
39  * @jboss-net.web-service urn="FederatedService"
40  * @ejb.interface remote-class="org.jboss.test.jbossnet.external.FederatedService" local-class="org.jboss.test.jbossnet.external.server.FederatedServiceLocal" extends="javax.ejb.EJBObject" local-extends="javax.ejb.EJBLocalObject"
41  * @ejb.home remote-class="org.jboss.test.jbossnet.external.FederatedServiceHome" local-class="org.jboss.test.jbossnet.external.server.FederatedServiceHomeLocal" extends="javax.ejb.EJBHome" local-extends="javax.ejb.EJBLocalHome"
42  */

43
44 public class FederatedServiceBean extends SessionSupport implements SessionBean JavaDoc
45 {
46    private static Logger log = Logger.getLogger(FederatedServiceBean.class);
47
48    /**
49     * first accesses google to produce a set of
50     * search results and then connects to babelfish
51     * to translate the content of the title of
52     * one of them
53     * @jboss-net.web-method
54     * @ejb.interface-method view-type="both"
55     */

56
57    public String JavaDoc findAndTranslate(String JavaDoc searchTerm) throws Exception JavaDoc
58    {
59       // look into JNDI
60
InitialContext JavaDoc initContext = new InitialContext JavaDoc();
61
62       // find external references there
63
GoogleSearchService googleService =
64          (GoogleSearchService) initContext.lookup("Google");
65       GoogleSearchPort google = googleService.getGoogleSearchPort();
66
67       String JavaDoc licenseKey =
68          System.getProperty(
69             "google.license",
70             "Wr5iTf5QFHJKmmnJn+61lt9jaMuWMKCj");
71
72       GoogleSearchResult searchResult =
73          google.doGoogleSearch(
74             licenseKey,
75             searchTerm,
76             0,
77             10,
78             true,
79             "",
80             false,
81             "",
82             "latin1",
83             "latin1");
84
85       log.debug("Query for: '" + searchTerm + "' returned: " + searchResult);
86
87       BabelFishService babelFishService =
88          (BabelFishService) initContext.lookup("BabelFish");
89
90       BabelFishPortType babelFish = babelFishService.getBabelFishPort();
91
92       // and call them
93
try
94       {
95          String JavaDoc translationResult = babelFish.babelFish("en_de", searchTerm);
96          log.debug(
97             "Translation of: '"
98                + searchTerm
99                + "' returned: "
100                + translationResult);
101          return translationResult;
102       } catch (RemoteException JavaDoc e)
103       {
104          // sometimes babelfish is down
105
return "This service is currently disabled.";
106       }
107
108    }
109
110 }
111
Popular Tags