001: /*
002:
003: Derby - Class org.apache.derby.impl.jdbc.authentication.JNDIAuthenticationSchemeBase
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.jdbc.authentication;
023:
024: import org.apache.derby.iapi.services.context.ContextService;
025: import org.apache.derby.iapi.error.StandardException;
026:
027: import org.apache.derby.iapi.store.access.AccessFactory;
028: import org.apache.derby.iapi.store.access.TransactionController;
029:
030: import org.apache.derby.iapi.jdbc.AuthenticationService;
031: import org.apache.derby.authentication.UserAuthenticator;
032:
033: import org.apache.derby.iapi.services.sanity.SanityManager;
034: import org.apache.derby.iapi.reference.SQLState;
035: import org.apache.derby.iapi.error.ExceptionSeverity;
036: import org.apache.derby.iapi.reference.MessageId;
037: import org.apache.derby.iapi.services.i18n.MessageService;
038:
039: import java.util.Properties;
040: import java.util.Enumeration;
041: import java.sql.SQLException;
042:
043: /**
044: * This is the base JNDI authentication scheme class.
045: *
046: * The generic environment JNDI properties for the selected JNDI
047: * scheme are retrieved here so that the user can set JNDI properties
048: * at the database or system level.
049: *
050: * @see org.apache.derby.authentication.UserAuthenticator
051: *
052: */
053:
054: public abstract class JNDIAuthenticationSchemeBase implements
055: UserAuthenticator {
056: protected final JNDIAuthenticationService authenticationService;
057: protected String providerURL;
058:
059: private AccessFactory store;
060: protected Properties initDirContextEnv;
061:
062: //
063: // Constructor
064: //
065: // We get passed some Users properties if the authentication service
066: // could not set them as part of System properties.
067: //
068: public JNDIAuthenticationSchemeBase(JNDIAuthenticationService as,
069: Properties dbProperties) {
070:
071: this .authenticationService = as;
072:
073: //
074: // Let's initialize the Directory Context environment based on
075: // generic JNDI properties. Each JNDI scheme can then add its
076: // specific scheme properties on top of it.
077: //
078: setInitDirContextEnv(dbProperties);
079:
080: // Specify the ones for this scheme if not already specified
081: this .setJNDIProviderProperties();
082: }
083:
084: /**
085: * To be OVERRIDEN by subclasses. This basically tests and sets
086: * default/expected JNDI properties for the JNDI provider scheme.
087: *
088: **/
089: abstract protected void setJNDIProviderProperties();
090:
091: /**
092: * Construct the initial JNDI directory context environment Properties
093: * object. We retrieve JNDI environment properties that the user may
094: * have set at the database level.
095: *
096: **/
097: private void setInitDirContextEnv(Properties dbProps) {
098:
099: //
100: // We retrieve JNDI properties set at the database level
101: // if any. If dbProps == null, there are obviously no database
102: // properties to retrieve.
103: //
104: initDirContextEnv = new Properties();
105:
106: if (dbProps != null) {
107: for (Enumeration keys = dbProps.propertyNames(); keys
108: .hasMoreElements();) {
109:
110: String key = (String) keys.nextElement();
111:
112: if (key.startsWith("java.naming.")) {
113: initDirContextEnv
114: .put(key, dbProps.getProperty(key));
115: }
116: }
117: }
118: }
119:
120: protected static final SQLException getLoginSQLException(Exception e) {
121:
122: String text = MessageService.getTextMessage(
123: SQLState.LOGIN_FAILED, e);
124:
125: SQLException sqle = new SQLException(text,
126: SQLState.LOGIN_FAILED,
127: ExceptionSeverity.SESSION_SEVERITY);
128:
129: return sqle;
130: }
131:
132: }
|