001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sqlc;
005    
006    import org.apache.tools.ant.AntClassLoader;
007    import org.apache.tools.ant.BuildException;
008    import org.apache.tools.ant.Task;
009    import org.apache.tools.ant.types.Path;
010    
011    /**
012     * Interface to extend
013     * @ant.element name="interface"
014     * @author Pavel Vlasov
015     * @version $Revision: 1.3 $
016     */
017    public class InterfaceEntry extends Task {
018            private ClassLoader classLoader;
019            
020            void setClassLoader(ClassLoader classLoader) {
021                    this.classLoader=classLoader;
022            }
023            
024            /**
025             * Interface name.
026             * @ant.required
027             * @param name
028             */
029            public void setName(String name) {
030                    this.interfaceName = name;
031            }
032            
033            /**
034             * Maybe creates a nested classpath element.
035             * @ant :non-required
036             */
037            public Path createClasspath() {
038                    if (classPath == null) {
039                            classPath = new Path(getProject());
040                    }
041                    return classPath.createPath();
042            }
043    
044            public void setClassPath(Path classPath) {
045                    if (this.classPath == null) {
046                            this.classPath = classPath;
047                    } else {
048                            this.classPath.append(classPath);
049                    }
050            }
051    
052            /**
053             * Classpath for loading classes.
054             * @ant :non-required 
055             */
056            protected Path classPath;       
057            
058            private String interfaceName;   
059            
060            Class getInterface() {
061                    try {
062                        Class ret;
063                        if (classPath==null) {
064                                    ret = Class.forName(interfaceName);
065                        } else if (classLoader==null) {
066                            ret = new AntClassLoader(getProject(), classPath).loadClass(interfaceName);
067                        } else {
068                            ret = new AntClassLoader(classLoader, getProject(), classPath, true).loadClass(interfaceName);                  
069                        }
070                        
071                            if (!ret.isInterface()) {
072                                    throw new BuildException(ret.getName()+" is not an interface");
073                            }
074                            return ret;
075                    } catch (ClassNotFoundException e) {
076                            throw new BuildException("Class not found: "+e, e);
077                    }
078            }
079    }