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