b42f5d0ea114d382677f3bade9420573f7faf327
[utils] / security / impl / src / main / java / org / wamblee / security / authorization / IsaOperationCondition.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.security.authorization;
17
18 import javax.persistence.Access;
19 import javax.persistence.AccessType;
20 import javax.persistence.Column;
21 import javax.persistence.DiscriminatorValue;
22 import javax.persistence.Entity;
23
24 /**
25  * Determiens if an operation is a subclass of a specified operation.
26  */
27 @Entity
28 @DiscriminatorValue("ISA")
29 @Access(AccessType.PROPERTY)
30 public class IsaOperationCondition extends AbstractOperationCondition {
31     /**
32      * Operation that the other operation must be a subclass of.
33      */
34     private Class<? extends Operation> operation;
35
36     /**
37      * Constructs the condition.
38      * 
39      * @param aOperation
40      *            Operation that an operation must be an instance of.
41      */
42     public IsaOperationCondition(Class<? extends Operation> aOperation) {
43         operation = aOperation;
44     }
45
46     /**
47      * For OR mapping.
48      * 
49      */
50     public IsaOperationCondition() {
51         operation = null;
52     }
53
54     /*
55      * (non-Javadoc)
56      * 
57      * @see
58      * org.wamblee.security.authorization.OperationCondition#matches(org.wamblee
59      * .security.authorization.Operation)
60      */
61     public boolean matches(Operation aOperation) {
62         return operation.isInstance(aOperation);
63     }
64
65     /**
66      * Gets the operation as a string. For OR mapping only.
67      * 
68      * @return Operation string.
69      */
70     @Column(name = "CLASSNAME")
71     protected String getOperationString() {
72         if (operation == null) {
73             return null;
74         }
75
76         return operation.getName();
77     }
78
79     /**
80      * Sets the operation as a string. For OR mapping only.
81      * 
82      * @param aOperation
83      *            Operation string.
84      * 
85      */
86     protected void setOperationString(String aOperation) {
87         if (aOperation == null) {
88             return;
89         }
90
91         try {
92             operation = (Class<? extends Operation>) Class.forName(aOperation);
93         } catch (Exception e) {
94             throw new IllegalArgumentException("Unknown class '" + aOperation +
95                 "'");
96         }
97     }
98
99     /*
100      * (non-Javadoc)
101      * 
102      * @see java.lang.Object#toString()
103      */
104     @Override
105     public String toString() {
106         return "IsaOperationCondition(operation=" + operation.getName() + ")";
107     }
108 }