001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ant;
031:
032: import java.io.*;
033: import java.util.*;
034:
035: import com.caucho.java.*;
036: import com.caucho.jsp.*;
037: import com.caucho.loader.*;
038: import com.caucho.xml.*;
039: import com.caucho.vfs.Vfs;
040:
041: import org.apache.tools.ant.*;
042: import org.apache.tools.ant.types.*;
043:
044: /**
045: * Command-line tool and ant task to compile jsp files
046: */
047: public class Jspc {
048: private File _rootDirectory;
049: private Vector _classpath = new Vector();
050:
051: /**
052: * For ant.
053: **/
054: public Jspc() {
055: }
056:
057: public void setRootDirectory(File root) {
058: _rootDirectory = root;
059: }
060:
061: public void addClasspath(Path path) {
062: _classpath.add(path);
063: }
064:
065: /**
066: * Executes the ant task.
067: **/
068: public void execute() throws BuildException {
069: if (_rootDirectory == null)
070: throw new BuildException(
071: "root-directory is required by jspc");
072:
073: /*
074: if (_classpath.size() == 0)
075: throw new BuildException("classpath is required by jspc");
076: */
077:
078: ClassLoader loader = JspCompiler.class.getClassLoader();
079:
080: String classPath = System.getProperty("java.class.path");
081:
082: if (loader instanceof AntClassLoader)
083: classPath = ((AntClassLoader) loader).getClasspath();
084:
085: Thread thread = Thread.currentThread();
086: ClassLoader oldLoader = thread.getContextClassLoader();
087:
088: try {
089: EnvironmentClassLoader env = new EnvironmentClassLoader();
090:
091: for (String cp : classPath.split("["
092: + File.pathSeparatorChar + "]")) {
093: com.caucho.vfs.Path path = Vfs.lookup(cp);
094:
095: env.addRoot(path);
096: }
097:
098: thread.setContextClassLoader(env);
099:
100: JspCompiler.main(new String[] { "-app-dir",
101: _rootDirectory.getAbsolutePath(), });
102: } catch (RuntimeException e) {
103: throw e;
104: } catch (Exception e) {
105: throw new BuildException(e);
106: } finally {
107: thread.setContextClassLoader(oldLoader);
108: }
109: }
110: }
|