001    package org.apache.ojb.broker;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import java.io.Serializable;
019    
020    /**
021     * A immutable key to identify PB instances in pools, ...
022     * <br>
023     * The used <i>jcdAlias</i> name represents an alias for a connection
024     * defined in the repository file.
025     *
026     * @author Armin Waibel
027     * @version $Id: PBKey.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
028     */
029    public class PBKey implements Cloneable, Serializable
030    {
031            private static final long serialVersionUID = -8858811398162391578L;
032        private final String jcdAlias;
033        private final String user;
034        private final String password;
035        private int hashCode;
036    
037        /**
038         * Creates a new PBKey.
039         * 
040         * @param jcdAlias The jdbc connection descriptor name as defined in the repository file
041         * @param user     The username
042         * @param password The password
043         */
044        public PBKey(final String jcdAlias, final String user, final String password)
045        {
046            this.jcdAlias = jcdAlias;
047            this.user = user;
048            this.password = password;
049        }
050    
051        /**
052         * Convenience constructor for <code>PBKey(jcdAlias, null, null)</code>.
053         * 
054         * @param jcdAlias The jdbc connection descriptor name as defined in the repository file
055         */
056        public PBKey(final String jcdAlias)
057        {
058            this(jcdAlias, null, null);
059        }
060    
061        /**
062         * {@inheritDoc}
063         */
064        public boolean equals(Object obj)
065        {
066            if (obj == this)
067            {
068                return true;
069            }
070            if (!(obj instanceof PBKey))
071            {
072                return false;
073            }
074            PBKey other = (PBKey) obj;
075            return this.jcdAlias.equals(other.getAlias())
076                    && (user != null ? user.equals(other.user) : null == other.user)
077                    && (password != null ? password.equals(other.password) : null == other.password);
078        }
079    
080        /**
081         * {@inheritDoc}
082         */
083        protected Object clone() throws CloneNotSupportedException
084        {
085            return new PBKey(this.jcdAlias, this.user, this.password);
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        public int hashCode()
092        {
093            if(hashCode == 0)
094            {
095                hashCode = jcdAlias.hashCode()
096                        + (user != null ? user.hashCode() : 0)
097                        + (password != null ? password.hashCode() : 0);
098            }
099            return hashCode;
100        }
101    
102        /**
103         * {@inheritDoc}
104         */
105        public String toString()
106        {
107            return this.getClass().getName() + ": jcdAlias="+jcdAlias+", user="+user+
108                    (user != null ? ", password=*****" : ", password="+password);
109        }
110    
111        /**
112         * Returns the jdbc connection descriptor name as defined in the repository file.
113         * 
114         * @return The JCD alias
115         */
116        public String getAlias()
117        {
118            return jcdAlias;
119        }
120    
121        /**
122         * Returns the jdbc connection descriptor name as defined in the repository file.
123         * 
124         * @return The JCD alias
125         * @deprecated use {@link #getAlias} instead.
126         */
127        public String getRepositoryFile()
128        {
129            return jcdAlias;
130        }
131    
132        /**
133         * Returns the username.
134         * 
135         * @return The username
136         */
137        public String getUser()
138        {
139            return user;
140        }
141    
142        /**
143         * Returns the password.
144         * 
145         * @return The password
146         */
147        public String getPassword()
148        {
149            return password;
150        }
151    }