001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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:
019: /* $Id: CredentialImpl.java 565827 2007-08-14 16:34:12Z rfrovarp $ */
020:
021: package org.apache.lenya.ac.impl;
022:
023: import org.apache.lenya.ac.Accreditable;
024: import org.apache.lenya.ac.Credential;
025: import org.apache.lenya.ac.Role;
026: import org.apache.lenya.util.Assert;
027:
028: /**
029: * Credential implementation.
030: */
031: public class CredentialImpl implements Credential {
032: private Accreditable accreditable;
033: private Role role;
034: private String method = DENY;
035:
036: /**
037: * Creates a new credential object.
038: *
039: * @param accreditable The accreditable.
040: * @param role The role.
041: */
042: public CredentialImpl(Accreditable accreditable, Role role) {
043: Assert.notNull("accreditable", accreditable);
044: this .accreditable = accreditable;
045: Assert.notNull("role", role);
046: this .role = role;
047: }
048:
049: /**
050: * Returns the role of this credential.
051: *
052: * @return A role.
053: */
054: public Role getRole() {
055: return this .role;
056: }
057:
058: /**
059: * Returns the accreditable of this credential.
060: *
061: * @return An accreditable.
062: */
063: public Accreditable getAccreditable() {
064: return this .accreditable;
065: }
066:
067: /**
068: * @see java.lang.Object#toString()
069: */
070: public String toString() {
071: return "[" + getAccreditable() + ":" + getRole() + " ("
072: + getMethod() + ")]";
073: }
074:
075: /**
076: * Set the method of the credential, grant or deny
077: *
078: * @param method A string grant or deny
079: */
080: public void setMethod(String method) {
081: this .method = method;
082: }
083:
084: public String getMethod() {
085: return method;
086: }
087:
088: /**
089: * @return if the method is {@link Credential#GRANT}.
090: */
091: public boolean isGranted() {
092: return this .method.equals(GRANT);
093: }
094:
095: /**
096: * @return if the method is {@link Credential#DENY}.
097: */
098: public boolean isDenied() {
099: return this .method.equals(DENY);
100: }
101:
102: public boolean equals(Object obj) {
103: if (!getClass().isInstance(obj)) {
104: return false;
105: }
106: Credential cred = (Credential) obj;
107: return cred.getAccreditable().equals(getAccreditable())
108: && cred.getRole().equals(getRole());
109: }
110:
111: public int hashCode() {
112: return Integer.valueOf(
113: getAccreditable().hashCode() + getRole().hashCode())
114: .hashCode();
115: }
116: }
|