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    
026    /**
027     * @author William DRAI
028     */
029    public class ContextResult implements Serializable {
030    
031        private static final long serialVersionUID = 1L;
032        
033        
034        private String componentName;
035        private String componentClassName;
036        private String expression;
037        
038        
039        public ContextResult() {
040        }
041        
042        public ContextResult(String componentName, String expression) {
043            this.componentName = componentName;
044            this.expression = expression;
045        }
046    
047        public String getComponentName() {
048            return componentName;
049        }
050        public void setComponentName(String componentName) {
051            this.componentName = componentName;
052        }
053    
054        public String getComponentClassName() {
055            return componentClassName;
056        }
057        public void setComponentClassName(String componentClassName) {
058            this.componentClassName = componentClassName;
059        }
060        
061        private Class<?> componentClass;
062        
063        public Class<?> getComponentClass() {
064            if (componentClassName == null)
065                    return null;
066            
067            if (componentClass == null) {
068                    try {
069                            componentClass = Thread.currentThread().getContextClassLoader().loadClass(componentClassName);
070                    }
071                    catch (Exception e) {
072                            throw new RuntimeException("Component class not found", e);
073                    }
074            }
075            return componentClass;      
076        }
077            
078        public String getExpression() {
079            return expression;
080        }
081        public void setExpression(String expression) {
082            this.expression = expression;
083        }
084        
085        public Boolean getRestrict() {
086            return null;
087        }
088    
089        
090        @Override
091        public String toString() {
092            return (componentName != null ? componentName : "") 
093                    + (componentClassName != null ? "(" + componentClassName + ")" : "") 
094                    + (expression != null ? "." + expression : "");
095        }
096    
097        
098        @Override
099        public int hashCode() {
100            return (componentName + "(" + componentClassName + ")." + expression).hashCode();
101        }
102        
103        @Override
104        public boolean equals(Object object) {
105            if (object == null || !object.getClass().equals(getClass()))
106                return false;
107            
108            ContextResult result = (ContextResult)object;
109            if (result.getComponentName() == null && componentName == null 
110                            && (!((result.getComponentClassName() == null && componentClassName == null) || result.getComponentClassName().equals(componentClassName))))
111                    return false;
112            
113            if (result.getComponentName() != null 
114                            && !result.getComponentName().equals(componentName))
115                return false;
116            
117            if (result.getComponentClassName() != null && componentClassName != null 
118                            && !result.getComponentClassName().equals(componentClassName))
119                    return false;
120            
121            if (expression == null)
122                return result.getExpression() == null;
123            
124            return expression.equals(result.getExpression());
125        }
126    }