Source Code Cross Referenced for CompilationUtils.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » tools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003:         * Distributed under the terms of either:
004:         * - the common development and distribution license (CDDL), v1.0; or
005:         * - the GNU Lesser General Public License, v2.1 or later
006:         * $Id: CompilationUtils.java 3634 2007-01-08 21:42:24Z gbevin $
007:         */
008:        package com.uwyn.rife.tools;
009:
010:        import com.uwyn.rife.config.RifeConfig;
011:        import com.uwyn.rife.tools.exceptions.CompilationFailedException;
012:        import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
013:        import java.io.ByteArrayOutputStream;
014:        import java.io.File;
015:        import java.io.IOException;
016:        import java.io.PrintWriter;
017:        import java.lang.reflect.InvocationTargetException;
018:        import java.lang.reflect.Method;
019:        import java.util.ArrayList;
020:        import java.util.List;
021:
022:        public abstract class CompilationUtils {
023:            public static File compile(String filenameJava, File fileClass,
024:                    String generationPath, String classpath)
025:                    throws CompilationFailedException {
026:                try {
027:                    // compile the java source file to a class file
028:                    if ((!RifeConfig.Global.isJavaCompilerPathSet() || RifeConfig.Global
029:                            .getJavaCompilerInternal())
030:                            && RifeConfig.Global
031:                                    .isInternalJavaCompilerAvailable()) {
032:                        String[] args = new String[] { "-g", "-classpath",
033:                                classpath, "-d", generationPath, filenameJava };
034:                        Method compile_method = null;
035:                        ByteArrayOutputStream errors_outputstream = new ByteArrayOutputStream();
036:                        PrintWriter errors_printwriter = new PrintWriter(
037:                                errors_outputstream);
038:
039:                        // try to compile with printwriter support for error handling
040:                        // otherwise use the regular compile method
041:                        // this is needed for ibm jdk support
042:                        try {
043:                            compile_method = com.sun.tools.javac.Main.class
044:                                    .getDeclaredMethod("compile", new Class[] {
045:                                            String[].class, PrintWriter.class });
046:                        } catch (Throwable e) {
047:                            compile_method = null;
048:                        }
049:
050:                        // use the compile method with error reporting if it exists
051:                        if (compile_method != null) {
052:                            try {
053:                                Object status = compile_method
054:                                        .invoke(null, new Object[] { args,
055:                                                errors_printwriter });
056:                                if (Integer.parseInt(String.valueOf(status)) != 0) {
057:                                    throw new CompilationFailedException(
058:                                            filenameJava, errors_outputstream
059:                                                    .toString(), null);
060:                                }
061:                            } catch (IllegalAccessException e) {
062:                                compile_method = null;
063:                            } catch (InvocationTargetException e) {
064:                                compile_method = null;
065:                            } catch (IllegalArgumentException e) {
066:                                compile_method = null;
067:                            }
068:                        }
069:
070:                        // use the regular compile method
071:                        if (null == compile_method) {
072:                            int status = com.sun.tools.javac.Main.compile(args);
073:                            if (status != 0) {
074:                                throw new CompilationFailedException(
075:                                        filenameJava, "<unknown error>", null);
076:                            }
077:                        }
078:                    } else {
079:                        // setup the command
080:                        List<String> command_elements = new ArrayList<String>();
081:                        command_elements.add(RifeConfig.Global
082:                                .getJavaCompilerPath());
083:                        if (RifeConfig.Global.areJavaCompilerArgsSet()) {
084:                            command_elements.addAll(RifeConfig.Global
085:                                    .getJavaCompilerArgs());
086:                        }
087:                        command_elements.add("-g");
088:                        command_elements.add("-d");
089:                        command_elements.add(generationPath);
090:                        command_elements.add(filenameJava);
091:
092:                        String[] commands = new String[command_elements.size()];
093:                        command_elements.toArray(commands);
094:
095:                        // execute the command
096:                        Process javac = ExecUtils.exec(commands,
097:                                new String[] { "CLASSPATH=" + classpath });
098:
099:                        if (javac.exitValue() != 0 || !fileClass.exists()) {
100:                            String error_message = null;
101:                            try {
102:                                error_message = FileUtils.readString(javac
103:                                        .getErrorStream());
104:                            } catch (FileUtilsErrorException e) {
105:                                error_message = "<unknown error>";
106:                            }
107:
108:                            if (fileClass.exists()) {
109:                                fileClass.delete();
110:                            }
111:                            throw new CompilationFailedException(filenameJava,
112:                                    error_message, null);
113:                        }
114:                    }
115:                } catch (IOException e) {
116:                    if (fileClass.exists()) {
117:                        fileClass.delete();
118:                    }
119:                    throw new CompilationFailedException(
120:                            filenameJava,
121:                            "An error occurred while launching the Java compiler.\n\nThere can be several reasons for this:\n- the JDK is maybe not installed; or\n- the compiler can't be found through your operating system's PATH environment variable; or\n- your operating system can't find the compiler path '"
122:                                    + RifeConfig.Global.getJavaCompilerPath()
123:                                    + "', try setting it up through the JAVA_COMPILER_PATH configuration parameter; or\n- the tools.jar file from the JDK isn't present in the classpath of your execution environment (servlet container).",
124:                            e);
125:                } catch (InterruptedException e) {
126:                    if (fileClass.exists()) {
127:                        fileClass.delete();
128:                    }
129:                    throw new CompilationFailedException(filenameJava, null, e);
130:                }
131:
132:                return fileClass;
133:            }
134:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.