~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/jgdi/cullconv/src/com/sun/grid/javaconv/JavaTemplateConverter.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*//*************************************************************************
 
2
 *
 
3
 *  The Contents of this file are made available subject to the terms of
 
4
 *  the Sun Industry Standards Source License Version 1.2
 
5
 *
 
6
 *  Sun Microsystems Inc., March, 2001
 
7
 *
 
8
 *
 
9
 *  Sun Industry Standards Source License Version 1.2
 
10
 *  =================================================
 
11
 *  The contents of this file are subject to the Sun Industry Standards
 
12
 *  Source License Version 1.2 (the "License"); You may not use this file
 
13
 *  except in compliance with the License. You may obtain a copy of the
 
14
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
15
 *
 
16
 *  Software provided under this License is provided on an "AS IS" basis,
 
17
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
18
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
19
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
20
 *  See the License for the specific provisions governing your rights and
 
21
 *  obligations concerning the Software.
 
22
 *
 
23
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
24
 *
 
25
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
26
 *
 
27
 *   All Rights Reserved.
 
28
 *
 
29
 ************************************************************************/
 
30
/*___INFO__MARK_END__*/
 
31
package com.sun.grid.javaconv;
 
32
 
 
33
import com.sun.grid.cull.Printer;
 
34
import com.sun.grid.cull.template.Template;
 
35
import com.sun.grid.cull.template.TemplateFactory;
 
36
import java.beans.BeanInfo;
 
37
import java.beans.IntrospectionException;
 
38
import java.io.File;
 
39
import java.io.IOException;
 
40
import java.util.HashMap;
 
41
import java.util.Map;
 
42
import java.util.logging.Logger;
 
43
 
 
44
/**
 
45
 *
 
46
 */
 
47
public class JavaTemplateConverter implements JavaToJavaConverter {
 
48
 
 
49
    private File outputFile;
 
50
    private TemplateFactory fac;
 
51
    private File templateFile;
 
52
    private String packageName;
 
53
    private String classpath;
 
54
    private Printer printer;
 
55
    private File prologFile;
 
56
    private File epilogFile;
 
57
    private Logger logger = Logger.getLogger("class");
 
58
 
 
59
    /** Creates a new instance of JavaTemplateConverter */
 
60
    public JavaTemplateConverter(File buildDir, String classpath, File templateFile, File outputFile, File prologFile, File epilogFile, String javaSourceVersion, String javaTargetVersion) {
 
61
        this.templateFile = templateFile;
 
62
        this.outputFile = outputFile;
 
63
        fac = new TemplateFactory(buildDir, classpath, javaSourceVersion, javaTargetVersion);
 
64
        this.prologFile = prologFile;
 
65
        this.epilogFile = epilogFile;
 
66
        this.printer = null;
 
67
    }
 
68
 
 
69
    public void convert(Class clazz) throws java.io.IOException {
 
70
        Template template = fac.createTemplate(templateFile);
 
71
 
 
72
        try {
 
73
            if (printer == null) {
 
74
                if (!outputFile.exists() || outputFile.lastModified() < templateFile.lastModified() || (prologFile != null && outputFile.lastModified() < prologFile.lastModified()) || (epilogFile != null && outputFile.lastModified() < epilogFile.lastModified())) {
 
75
                    printer = new Printer(outputFile);
 
76
                    if (prologFile != null) {
 
77
                        logger.fine("write prolog " + prologFile);
 
78
                        printer.printFile(prologFile);
 
79
                    }
 
80
                }
 
81
            }
 
82
            if (printer != null) {
 
83
                Map<String, Object> params = new HashMap<String, Object>();
 
84
                params.put("shortname", clazz);
 
85
                String javaClassName = clazz.getName();
 
86
                int index = javaClassName.lastIndexOf(".");
 
87
                if (index >= 0) {
 
88
                    javaClassName = javaClassName.substring(index + 1);
 
89
                }
 
90
                params.put("shortname", javaClassName);
 
91
                BeanInfo beanInfo = java.beans.Introspector.getBeanInfo(clazz);
 
92
                params.put("beanInfo", beanInfo);
 
93
                logger.fine("writer class " + clazz.getName());
 
94
                template.print(printer, params);
 
95
                printer.flush();
 
96
            }
 
97
        } catch (IntrospectionException itse) {
 
98
            IOException e = new IOException("Introspection error: " + itse.getMessage());
 
99
            e.initCause(itse);
 
100
            throw e;
 
101
        }
 
102
    }
 
103
 
 
104
    public void finish() throws IOException {
 
105
        if (printer != null) {
 
106
            if (epilogFile != null) {
 
107
                logger.fine("write epilog " + epilogFile);
 
108
                printer.printFile(epilogFile);
 
109
                printer.flush();
 
110
            }
 
111
            printer.close();
 
112
        }
 
113
    }
 
114
}