001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/services/ServiceReplyCatcher.java,v 1.1.1.1 2004/03/25 12:08:39 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:39 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.portal.controller.services;
042:
043: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
044: import java.io.IOException;
045: import java.net.URL;
046:
047: /**
048: * This class is responsible for launching the
049: * {@link ServiceReplyCatcherThread} thread, which will try to get the
050: * reply from a service at a given URL. It is also responsible for waiting
051: * until the timeout is exceeded and for killing the thread on its
052: * completion.
053: *
054: * @author Daniel Fernandez
055: * @since 1.0
056: */
057: public class ServiceReplyCatcher {
058:
059: /**
060: * Avoids the instantiation of this class.
061: */
062: private ServiceReplyCatcher() {
063: }
064:
065: /**
066: * Returns the reply got at the URL indicated. This method starts the
067: * catcher thread.
068: *
069: * @param url the URL to be requested.
070: * @param timeOut the number of seconds to wait for a reply.
071: * @throws InternalErrorException if any fatal errors happen.
072: * @throws ServiceNotAvailableException if the service is not available.
073: * @throws ServiceTimeOutException if the timeout is exceeded.
074: * @return the service reply
075: */
076: public static String getReply(URL url, int timeOut)
077: throws InternalErrorException,
078: ServiceNotAvailableException, ServiceTimeOutException {
079:
080: /* We create the thread */
081: ServiceReplyCatcherThread catcher = new ServiceReplyCatcherThread(
082: url);
083: Exception catcherException = null;
084: String catcherReply = null;
085:
086: /* We start the thread */
087: catcher.start();
088:
089: try {
090: /* We wait until the timeout is reached */
091: catcher.join(timeOut * 1000);
092:
093: } catch (InterruptedException e) {
094: throw new InternalErrorException("Interrupted during "
095: + "timeout count for " + "service reply.", e);
096: }
097:
098: try {
099: /* We kill the thread */
100: catcher.kill();
101:
102: } catch (IOException e) {
103: throw new InternalErrorException("Unable to kill the "
104: + "service reply catcher.", e);
105: }
106:
107: catcherException = catcher.getException();
108:
109: if (catcherException != null) {
110: throw new ServiceNotAvailableException(url.toString());
111: }
112:
113: catcherReply = catcher.getServiceReply();
114: if (catcherReply == null) {
115: /*
116: * If we haven't got a reply, it is because it didn't arrive
117: * in time.
118: */
119: throw new ServiceTimeOutException(url.toString(), timeOut);
120: }
121:
122: return catcherReply;
123:
124: }
125:
126: }
|