001: package org.esupportail.cas.server.util;
002:
003: import org.dom4j.Element;
004: import org.esupportail.cas.server.util.log.Debug;
005:
006: /**
007: * This abstract class implements a generic server class, inherited
008: * by DatabaseServer and LDAPServer.
009: *
010: * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
011: */
012:
013: public abstract class Server extends Debug {
014:
015: /**
016: * The error set by the connect() method on success.
017: */
018: protected static final int CONNECT_SUCCESS = 0;
019: /**
020: * The error code set by the connect() method when the connection
021: * with the server could not be achieved because of the bind
022: * information provided (the server responded but refused the
023: * connection). Returned by getConnectError().
024: */
025: protected static final int CONNECT_NOAUTH = 1;
026: /**
027: * The error code set by the connect() method when the connection
028: * with the server could not be achieved because of a service failure.
029: * Returned by getConnectError().
030: */
031: protected static final int CONNECT_FAILURE = 2;
032: /**
033: * an integer to store the error code of the connect() calls, set by
034: * setConnectError(), retrieved by getConnectError().
035: */
036: private int connectError = CONNECT_SUCCESS;
037: /**
038: * The result of the authenticate() method on success (when the
039: * user has been authenticated).
040: */
041: protected static final int AUTHENTICATE_SUCCESS = 0;
042: /**
043: * The result of the authenticate() method when the user could not
044: * be authenticated (but the server was accessed without any problem).
045: * When testing many servers, the next servers will not be called.
046: */
047: protected static final int AUTHENTICATE_NOAUTH = 1;
048: /**
049: * The result of the authenticate() method on failure (when the server
050: * could not be accessed for instance). When testing many servers, the
051: * next server will be called.
052: */
053: protected static final int AUTHENTICATE_FAILURE = 2;
054:
055: /**
056: * The handler the server will be used for.
057: */
058: private RedundantHandler handler;
059:
060: /**
061: * Constructor.
062: *
063: * @param handlerDebug debugging mode
064: * @param parentHandler the handler the server will be used by
065: * @param serverElement an XML element corresponding to the server configuration
066: */
067: public Server(final Boolean handlerDebug,
068: final RedundantHandler parentHandler,
069: final Element serverElement) {
070: super (elementDebugValue(serverElement, handlerDebug
071: .booleanValue()));
072: handler = parentHandler;
073: }
074:
075: /**
076: * Get the handler the server will be used by.
077: *
078: * @return a RedundantHandler object.
079: */
080: protected final RedundantHandler getHandler() {
081: return handler;
082: }
083:
084: /**
085: * Try to authenticate a user.
086: *
087: * @param username the user's name
088: * @param password the user's password
089: *
090: * @return Server.AUTHENTICATE_SUCCESS, Server.AUTHENTICATE_NOAUTH
091: * or Server.AUTHENTICATE_FAILURE.
092: */
093: protected abstract int authenticate(final String username,
094: final String password);
095:
096: /**
097: * Set the error code of the last connect() call.
098: *
099: * @param errCode an integer equal to Server.CONNECT_SUCCESS,
100: * Server.CONNECT_NOAUTH or Server.CONNECT_FAILURE.
101: */
102: protected final void setConnectError(final int errCode) {
103: connectError = errCode;
104: }
105:
106: /**
107: * Retrieve the error code of the last connect() call.
108: *
109: * @return Server.CONNECT_SUCCESS, Server.CONNECT_NOAUTH
110: * or Server.CONNECT_FAILURE.
111: */
112: protected final int getConnectError() {
113: int error = connectError;
114: connectError = CONNECT_SUCCESS;
115: return error;
116: }
117:
118: /**
119: * Retrieve the content of a server sub-element.
120: *
121: * @param serverElement the XML element representing the server in the configuration
122: * @param elementName the name of the XML element to look for
123: * @param needed false if the element can be absent or empty, true otherwise
124: * @return a String
125: * @throws Exception Exception
126: */
127: protected final String getServerSubElementContent(
128: final Element serverElement, final String elementName,
129: final boolean needed) throws Exception {
130: Element element = serverElement.element(elementName);
131: String str;
132: if (element == null) {
133: str = "";
134: } else {
135: str = element.getTextTrim();
136: }
137: if (needed && str.equals("")) {
138: traceThrow(new MisconfiguredHandlerException(
139: "A non empty nested \"" + elementName
140: + "\" element is needed to configure \""
141: + getClass().getName() + "\" servers."));
142: }
143: return str;
144: }
145:
146: }
|