001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.invocation;
022    
023    import java.io.Serializable;
024    
025    import org.granite.tide.Expression;
026    
027    
028    /**
029     * @author William DRAI
030     */
031    public class ContextResult implements Serializable, Expression {
032    
033        private static final long serialVersionUID = 1L;
034        
035        
036        private String componentName;
037        private String componentClassName;
038        private String expression;
039        
040        
041        public ContextResult() {
042        }
043        
044        public ContextResult(String componentName, String expression) {
045            this.componentName = componentName;
046            this.expression = expression;
047        }
048    
049        public String getComponentName() {
050            return componentName;
051        }
052        public void setComponentName(String componentName) {
053            this.componentName = componentName;
054        }
055    
056        public String getComponentClassName() {
057            return componentClassName;
058        }
059        public void setComponentClassName(String componentClassName) {
060            this.componentClassName = componentClassName;
061        }
062        
063        private Class<?> componentClass;
064        
065        public Class<?> getComponentClass() {
066            if (componentClassName == null)
067                    return null;
068            
069            if (componentClass == null) {
070                    try {
071                            componentClass = Thread.currentThread().getContextClassLoader().loadClass(componentClassName);
072                    }
073                    catch (Exception e) {
074                            throw new RuntimeException("Component class not found", e);
075                    }
076            }
077            return componentClass;      
078        }
079            
080        public String getExpression() {
081            return expression;
082        }
083        public void setExpression(String expression) {
084            this.expression = expression;
085        }
086        
087        public Boolean getRestrict() {
088            return null;
089        }
090        
091        public String getPath() {
092            return componentName + (expression != null ? "." + expression : "");
093        }
094        
095        public boolean matches(String componentName, String componentClassName, String expr) {
096            if (this.componentClassName != null && componentClassName != null 
097                && (this.componentClassName + (this.expression != null ? "." + this.expression : "")).indexOf(componentClassName + (expr != null ? "." + expr : "")) == 0) {
098                return true;
099            }
100            return getPath().indexOf(componentName + (expr != null ? "." + expr : "")) == 0; 
101        }
102    
103        
104        @Override
105        public String toString() {
106            return (componentName != null ? componentName : "") 
107                    + (componentClassName != null ? "(" + componentClassName + ")" : "") 
108                    + (expression != null ? "." + expression : "");
109        }
110    
111        
112        @Override
113        public int hashCode() {
114            return (componentName + "(" + componentClassName + ")." + expression).hashCode();
115        }
116        
117        @Override
118        public boolean equals(Object object) {
119            if (object == null || !object.getClass().equals(getClass()))
120                return false;
121            
122            ContextResult result = (ContextResult)object;
123            if (result.getComponentName() == null && componentName == null 
124                            && (!((result.getComponentClassName() == null && componentClassName == null) || result.getComponentClassName().equals(componentClassName))))
125                    return false;
126            
127            if (result.getComponentName() != null 
128                            && !result.getComponentName().equals(componentName))
129                return false;
130            
131            if (result.getComponentClassName() != null && componentClassName != null 
132                            && !result.getComponentClassName().equals(componentClassName))
133                    return false;
134            
135            if (expression == null)
136                return result.getExpression() == null;
137            
138            return expression.equals(result.getExpression());
139        }
140    }