View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.innergrid.api;
18  
19  import java.util.regex.Pattern;
20  
21  import com.gridsystems.innergrid.kernel.KernelException;
22  
23  /**
24   * Connects to a single server.
25   * <p>
26   * Used when connecting to a Server that has no backup Server (redundant).
27   *
28   * @author Rodrigo Ruiz
29   * @author Xmas
30   * @version 1.0
31   */
32  public class DirectConnection extends AbstractConnection {
33  
34    /**
35     * Pattern used to identify those exception codes that must launch
36     * a connection retry.
37     */
38    private static final Pattern RETRY_PATTERN = Pattern.compile("CLT000|CLT001|CLT002");
39  
40    /**
41     * Creates an instance of the connection to the specified <code>host:port</code>,
42     * using the specified credentials.
43     *
44     * @param h a string with the remote Server host name or address
45     * @param p the remote Server port
46     * @param secure a boolean set to <code>true</code> if a secure protocol(HTTPS)
47     *        must be used; <code>false</code> otherwise
48     * @param credentials the credentials to authenticate the user
49     */
50    public DirectConnection(String h, int p, boolean secure, Credentials credentials) {
51      this(h, p, secure, null, credentials);
52    }
53  
54    /**
55     * Creates an instance of the connection to the specified <code>host:port</code>,
56     * using the specified credentials. The port must accept SSL connections.
57     *
58     * @param h a string with the remote Server host name or address
59     * @param p the remote Server port
60     * @param sslinfo additional info to establish SSL connections
61     * @param credentials the credentials to authenticate the user
62     *
63     * @see AcceptAllCertificates AcceptAllCertificates
64     * @see AcceptCertificate AcceptCertificate
65     * @see AcceptCertificateOfKeyStore AcceptCertificateOfKeyStore
66     * @see AcceptCertificateOfSystemKeyStore AcceptCertificateOfSystemKeyStore
67     */
68    public DirectConnection(String h, int p, SSLConnectionInfo sslinfo,
69      Credentials credentials) {
70      this(h, p, true, sslinfo, credentials);
71    }
72  
73    /**
74     * Creates an instance of the connector to the specified <code>host:port</code>,
75     * using the specified credentials.
76     *
77     * @param h a string with the remote Server host name or address
78     * @param p the remote Server port
79     * @param secure a boolean set to <code>true</code> if a secure protocol(HTTPS)
80     *        must be used; <code>false</code> otherwise
81     * @param sslInfo additional info to establish SSL connection
82     * @param credentials the credentials to authenticate the user
83     *
84     * @see AcceptAllCertificates AcceptAllCertificates
85     * @see AcceptCertificate AcceptCertificate
86     * @see AcceptCertificateOfKeyStore AcceptCertificateOfKeyStore
87     * @see AcceptCertificateOfSystemKeyStore AcceptCertificateOfSystemKeyStore
88     */
89    public DirectConnection(String h, int p, boolean secure,
90      SSLConnectionInfo sslInfo, Credentials credentials) {
91      super(h, p, secure, sslInfo, credentials);
92    }
93  
94    /**
95     * Creates an instance of the connector to the specified <code>host:port</code>,
96     * using the specified credentials.
97     *
98     * @param h a string with the remote Server host name or address
99     * @param p the remote Server port
100    * @param secure a boolean set to <code>true</code> if a secure protocol(HTTPS)
101    *        must be used; <code>false</code> otherwise
102    * @param contextPath Service context path
103    * @param sslInfo additional info to establish SSL connection
104    * @param credentials the credentials to authenticate the user
105    *
106    * @see AcceptAllCertificates AcceptAllCertificates
107    * @see AcceptCertificate AcceptCertificate
108    * @see AcceptCertificateOfKeyStore AcceptCertificateOfKeyStore
109    * @see AcceptCertificateOfSystemKeyStore AcceptCertificateOfSystemKeyStore
110    */
111   public DirectConnection(String h, int p, boolean secure, String contextPath,
112     SSLConnectionInfo sslInfo, Credentials credentials) {
113     super(h, p, contextPath, secure, sslInfo, credentials);
114   }
115 
116   /**
117    * {@inheritDoc}
118    */
119   public void close() {
120     // This implementation does nothing
121   }
122 
123   /**
124    * Gets the base URL for all API URLs (Internal use only).
125    *
126    * @return the base URL
127    * @throws KernelException <code>CTL008</code> if the URL is not well formed
128    */
129   @Override public String getBaseUrl() throws KernelException {
130     checkConnection();
131 
132     return (secured ? "https" : "http") + "://" + host + ":" + port + contextPath;
133   }
134 
135   /**
136    * Manages exceptions (Internal use only).
137    *
138    * @param ke KernelException captured
139    * @param retry number of retries
140    * @throws KernelException the <code>ke</code> exception if it passes along the
141    *                         exception without managing
142    *
143    */
144   @Override
145   public void manageException(KernelException ke, int retry) throws KernelException {
146     if (mustRetry(ke)) {
147       if (retry < retries) {
148         return;
149       }
150     }
151     throw ke;
152   }
153 
154   /**
155    * Notifies to the Connection instance that the target API has been reached.
156    */
157   public void success() {
158   }
159 
160   /**
161    * Checks if the request must be retried, depending on the error code.
162    *
163    * @param ke exception produced
164    * @return <code>true</code> if exception must be retried; <code>false</code> otherwise.
165    */
166   private boolean mustRetry(KernelException ke) {
167     String code = ke.getCode();
168     return RETRY_PATTERN.matcher(code).matches();
169   }
170 }