001: /*
002: * Created on Jun 22, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.xdev.base.xssl.reflection;
008:
009: import java.util.HashMap;
010:
011: import java.io.File;
012:
013: import org.apache.bcel.generic.ClassGen;
014: import org.apache.bcel.classfile.AccessFlags;
015: import org.apache.bcel.classfile.ClassParser;
016: import org.apache.bcel.classfile.JavaClass;
017:
018: import org.xdev.base.core.compiler.AXCompiler;
019: import org.xdev.base.core.manager.PluginManager;
020: import org.xdev.base.xssl.XSSLAction;
021: import org.xdev.base.xssl.XSSLReturn;
022: import org.xdev.base.xssl.XSSLActionIterator;
023: import org.xdev.base.util.ServerUtilities;
024:
025: public class RuntimeCompile extends XSSLActionIterator {
026:
027: private Object returnValue = null;
028:
029: private String className = null;
030: private String methodName = null;
031:
032: private StringBuffer innerBuffer = new StringBuffer();
033: private StringBuffer outerBuffer = new StringBuffer();
034:
035: /**
036: * @param id
037: */
038: public RuntimeCompile(String id) {
039:
040: super (id);
041: // TODO Auto-generated constructor stub
042: }
043:
044: /**
045: * @param id
046: * @param properties
047: */
048: public RuntimeCompile(String id, HashMap properties) {
049:
050: super (id, properties);
051: // TODO Auto-generated constructor stub
052: }
053:
054: /* (non-Javadoc)
055: * @see org.xdev.base.core.object.Configuration#getValue()
056: */
057: public Object getObjectValue() {
058:
059: return this .returnValue;
060: }
061:
062: /* (non-Javadoc)
063: * @see org.xdev.base.xssl.XSSLAction#set()
064: */
065: protected void set() throws Exception {
066: this .className = ServerUtilities.getAlphaGuid();
067: this .methodName = ServerUtilities.getAlphaGuid();
068: }
069:
070: protected void invoke() throws Exception {
071: super .invoke();
072:
073: String returnType = this .getProperty("type");
074:
075: if (returnType == null || "".equals(returnType)) {
076: returnType = "void";
077: }
078:
079: outerBuffer.append("public class ").append(this .className)
080: .append("{\n");
081: outerBuffer.append("\tpublic static ").append(returnType)
082: .append(" ").append(this .methodName).append("() {\n");
083: outerBuffer.append("\t\t").append(innerBuffer.toString());
084: outerBuffer.append("\t}");
085: outerBuffer.append("}");
086:
087: this .log(this .outerBuffer.toString());
088:
089: File resources = this .getFileProperty("container");
090:
091: File fileClass = new File(resources.getPath() + "/" + className
092: + ".class");
093:
094: if (!fileClass.exists()) {
095: fileClass.createNewFile();
096: }
097:
098: ClassParser parser = new ClassParser(
099: new java.io.StringBufferInputStream(this .outerBuffer
100: .toString()), className + ".class");
101:
102: //ClassGen gen = new ClassGen(className, "java.lang.Object", className+".class", 0, null);
103:
104: JavaClass cls = parser.parse();
105:
106: cls.dump(fileClass);
107:
108: PluginManager manager = PluginManager.getInstance();
109:
110: manager.addUrl(fileClass);
111:
112: Class jCls = manager.getUrlClassLoader().loadClass(className);
113:
114: jCls.getMethod(this .methodName, null).invoke(null, null);
115: }
116:
117: protected void invoke(XSSLAction c) throws Exception {
118:
119: if (c instanceof XSSLReturn) {
120:
121: Object obj = ((XSSLReturn) c).getObjectValue();
122:
123: if (obj != null) {
124:
125: String quote = c.getBooleanProperty("string") ? "\""
126: : "";
127:
128: this.innerBuffer.append(quote).append(obj)
129: .append(quote);
130: }
131: }
132: }
133:
134: }
|