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: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.type;
030:
031: import com.caucho.amber.AmberException;
032: import com.caucho.java.JavaWriter;
033: import com.caucho.util.L10N;
034:
035: import java.io.IOException;
036: import java.sql.ResultSet;
037: import java.sql.SQLException;
038:
039: /**
040: * A property containing a class
041: */
042: public class ClassType extends Type {
043: private static final L10N L = new L10N(ClassType.class);
044:
045: private static final ClassType CLASS_TYPE = new ClassType();
046:
047: private ClassType() {
048: }
049:
050: /**
051: * Returns the singleton Class type.
052: */
053: public static ClassType create() {
054: return CLASS_TYPE;
055: }
056:
057: /**
058: * Returns the type name.
059: */
060: public String getName() {
061: return "java.lang.Class";
062: }
063:
064: /**
065: * Generates a string to load the property.
066: */
067: public int generateLoad(JavaWriter out, String rs, String indexVar,
068: int index) throws IOException {
069: out.print("com.caucho.amber.type.ClassType.toClass(" + rs
070: + ".getString(" + indexVar + " + " + index + "))");
071:
072: return index + 1;
073: }
074:
075: /**
076: * Generates a string to set the property.
077: */
078: public void generateSet(JavaWriter out, String pstmt, String index,
079: String value) throws IOException {
080: out.println("if (" + value + " == null)");
081: out.println(" " + pstmt + ".setNull(" + index
082: + "++, java.sql.Types.VARCHAR);");
083: out.println("else");
084: out.println(" " + pstmt + ".setString(" + index + "++, "
085: + value + ".getName());");
086: }
087:
088: /**
089: * Generates a string to set the property.
090: */
091: public void generateSetNull(JavaWriter out, String pstmt,
092: String index) throws IOException {
093: out.println(pstmt + ".setNull(" + index
094: + "++, java.sql.Types.VARCHAR);");
095: }
096:
097: /**
098: * Gets the value.
099: */
100: public static Class toClass(String name) throws SQLException {
101: if (name == null)
102: return null;
103:
104: try {
105: ClassLoader loader = Thread.currentThread()
106: .getContextClassLoader();
107:
108: return Class.forName(name, false, loader);
109: } catch (ClassNotFoundException e) {
110: throw new AmberException(e);
111: }
112: }
113:
114: /**
115: * Gets the value.
116: */
117: public Object getObject(ResultSet rs, int index)
118: throws SQLException {
119: return toClass(rs.getString(index));
120: }
121: }
|