| Line | Hits | Note | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * $Id: HttpConnectionUtil.java 1011 2008-06-16 17:57:36Z amandel $ | ||
| 3 | * | ||
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above | ||
| 13 | * copyright notice, this list of conditions and the following | ||
| 14 | * disclaimer in the documentation and/or other materials | ||
| 15 | * provided with the distribution. | ||
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of | ||
| 17 | * its contributors may be used to endorse or promote products | ||
| 18 | * derived from this software without specific prior written | ||
| 19 | * permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS | ||
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | ||
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | package org.jcoderz.commons.connector.http; | ||
| 34 | |||
| 35 | import java.util.logging.Level; | ||
| 36 | import java.util.logging.Logger; | ||
| 37 | |||
| 38 | import javax.naming.InitialContext; | ||
| 39 | import javax.naming.NamingException; | ||
| 40 | |||
| 41 | import org.jcoderz.commons.InternalErrorException; | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | /** | ||
| 46 | * This class provides utility functions for the http connector. | ||
| 47 | * | ||
| 48 | * @author Michael Griffel | ||
| 49 | */ | ||
| 50 | public final class HttpConnectionUtil | ||
| 51 | { | ||
| 52 | /** The JNDI name of the http connector. */ | ||
| 53 | public static final String HTTP_CONNECTOR_JNDI_NAME | ||
| 54 | = "HttpConnector"; | ||
| 55 | /** The JNDI name of the http connector. */ | ||
| 56 | public static final String HTTP_CONNECTOR_EIS_NAME | ||
| 57 | = "java:comp/env/eis/HttpConnector"; | ||
| 58 | |||
| 59 | /** The full qualified name of this class. */ | ||
| 60 | 0 | private static final String CLASSNAME | |
| 61 | = HttpConnectionUtil.class.getName(); | ||
| 62 | |||
| 63 | /** The logger to use. */ | ||
| 64 | 0 | private static final Logger logger = Logger.getLogger(CLASSNAME); | |
| 65 | |||
| 66 | private HttpConnectionUtil () | ||
| 67 | 0 | { | |
| 68 | // no instances allowed - provides only static helper methods. | ||
| 69 | 0 | } | |
| 70 | |||
| 71 | /** | ||
| 72 | * Returns a http connection. | ||
| 73 | * | ||
| 74 | * @param spec the http connection spec identifying the target system | ||
| 75 | * @return a http connection. | ||
| 76 | */ | ||
| 77 | public static HttpConnection getHttpConnection (HttpConnectionSpec spec) | ||
| 78 | { | ||
| 79 | 0 | InitialContext context = null; | |
| 80 | final HttpConnection connection; | ||
| 81 | try | ||
| 82 | { | ||
| 83 | 0 | context = new InitialContext(); | |
| 84 | 0 | final HttpConnectionFactory factory | |
| 85 | = (HttpConnectionFactory) context.lookup( | ||
| 86 | HTTP_CONNECTOR_EIS_NAME); | ||
| 87 | 0 | connection = factory.getConnection(spec); | |
| 88 | } | ||
| 89 | // possible exceptions: ResourceException, NamingException | ||
| 90 | 0 | catch (Exception e) | |
| 91 | { | ||
| 92 | 0 | (1) | throw new InternalErrorException( |
| 93 | "Failed to create http connection", e); | ||
| 94 | } | ||
| 95 | finally | ||
| 96 | { | ||
| 97 | 0 | if (context != null) | |
| 98 | { | ||
| 99 | try | ||
| 100 | { | ||
| 101 | 0 | context.close(); | |
| 102 | } | ||
| 103 | 0 | catch (NamingException e) | |
| 104 | { | ||
| 105 | 0 | logger.log(Level.FINER, | |
| 106 | "Failed to close InitialContext: " + e, e); | ||
| 107 | 0 | } | |
| 108 | } | ||
| 109 | } | ||
| 110 | 0 | return connection; | |
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Closes the HTTP connection (safe). | ||
| 115 | * | ||
| 116 | * This method tries to close the given HTTP connection and | ||
| 117 | * if an ResourceException occurs a message with the level | ||
| 118 | * {@link Level#FINE} is logged. It's safe to pass a | ||
| 119 | * <code>null</code> reference for the argument. | ||
| 120 | * | ||
| 121 | * @param in the HTTP connection that should be closed. | ||
| 122 | */ | ||
| 123 | public static void close (HttpConnection in) | ||
| 124 | { | ||
| 125 | 0 | if (in != null) | |
| 126 | { | ||
| 127 | try | ||
| 128 | { | ||
| 129 | 0 | in.close(); | |
| 130 | } | ||
| 131 | 0 | catch (Exception x) | |
| 132 | { | ||
| 133 | 0 | logger.log(Level.FINE, "Error while closing HTTP " | |
| 134 | + "connection: HttpConnection.close()", x); | ||
| 135 | 0 | } | |
| 136 | } | ||
| 137 | 0 | } | |
| 138 | |||
| 139 | } |
|
|
(1) | 92 | : | 0 | method org.jcoderz.commons.connector.http.HttpConnectionUtil.getHttpConnection(HttpConnectionSpec) throws exception with static message string |