001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.cfx.customtag;
020
021import lucee.commons.lang.ExceptionUtil;
022import lucee.runtime.cfx.CFXTagException;
023import lucee.runtime.reflection.Reflector;
024
025import com.allaire.cfx.CustomTag;
026
027/**
028 * 
029 */
030public final class JavaCFXTagClass implements CFXTagClass {
031        
032        private String name;
033        private String strClass;
034        private Class clazz;
035        private boolean readOnly=false;
036
037    
038        public JavaCFXTagClass(String name, String strClass) {
039                name=name.toLowerCase();
040                if(name.startsWith("cfx_"))name=name.substring(4);
041                this.name=name;
042                this.strClass=strClass;
043        }
044        private JavaCFXTagClass(String name, String strClass, Class clazz,boolean readOnly) {
045                
046                this.name=name;
047                this.strClass=strClass;
048                this.clazz=clazz;
049                this.readOnly=readOnly;
050        }
051        
052        @Override
053        public CustomTag newInstance() throws CFXTagException {
054                try {
055                        return _newInstance();
056                } catch (Throwable e) {
057                ExceptionUtil.rethrowIfNecessary(e);
058                        throw new CFXTagException(e);
059                }
060        }
061
062        public CustomTag _newInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException  {
063                
064                Object o=getClazz().newInstance();
065                return (CustomTag)o;
066        }
067    /**
068     * @return Returns the clazz.
069     * @throws ClassNotFoundException 
070     */
071    public Class<CustomTag> getClazz() throws ClassNotFoundException {
072        if(clazz==null) {
073            clazz=this.getClass().getClassLoader().loadClass(strClass);
074                }
075        return clazz;
076    }
077    
078    /**
079     * @return Returns the name.
080     */
081    public String getName() {
082        return name;
083    }
084    /**
085     * @return Returns the strClass.
086     */
087    public String getStrClass() {
088        return strClass;
089    }
090
091    @Override
092    public boolean isReadOnly() {
093        return readOnly;
094    }
095
096    @Override
097    public CFXTagClass cloneReadOnly() {
098        return new JavaCFXTagClass(name,strClass,clazz,true);
099    }
100    @Override
101    public String getDisplayType() {
102        return "Java";
103    }
104    @Override
105    public String getSourceName() {
106        return strClass;
107    }
108    
109    @Override
110    public boolean isValid() {
111        try {
112            return Reflector.isInstaneOf(getClazz(),CustomTag.class);
113        } 
114        catch (ClassNotFoundException e) {
115            return false;
116        }
117    }
118}