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.amber.gen;
031:
032: import com.caucho.amber.entity.Embeddable;
033: import com.caucho.amber.entity.Entity;
034: import com.caucho.amber.entity.Listener;
035: import com.caucho.amber.manager.AmberPersistenceUnit;
036: import com.caucho.amber.type.*;
037: import com.caucho.java.JavaCompiler;
038: import com.caucho.java.gen.DependencyComponent;
039: import com.caucho.java.gen.GenClass;
040: import com.caucho.java.gen.JavaClassGenerator;
041: import com.caucho.log.Log;
042: import com.caucho.util.L10N;
043:
044: import java.util.ArrayList;
045: import java.util.logging.Logger;
046:
047: /**
048: * Enhancing the java objects for Amber mapping.
049: */
050: public class AmberGeneratorImpl implements AmberGenerator {
051: private static final L10N L = new L10N(AmberGeneratorImpl.class);
052: private static final Logger log = Log
053: .open(AmberGeneratorImpl.class);
054:
055: private AmberPersistenceUnit _amberPersistenceUnit;
056:
057: private ArrayList<String> _pendingClassNames = new ArrayList<String>();
058:
059: public AmberGeneratorImpl(AmberPersistenceUnit manager) {
060: _amberPersistenceUnit = manager;
061: }
062:
063: /**
064: * Configures the type.
065: */
066: public void configure(AbstractEnhancedType type) throws Exception {
067: }
068:
069: /**
070: * Generates the type.
071: */
072: public void generate(AbstractEnhancedType type) throws Exception {
073: generateJava(null, type);
074: }
075:
076: /**
077: * Generates the type.
078: */
079: public void generateJava(JavaClassGenerator javaGen,
080: AbstractEnhancedType type) throws Exception {
081: if (isPreload(javaGen, type) || type.isGenerated())
082: return;
083:
084: type.setGenerated(true);
085: //type.setInstanceClassLoader(javaGen.getClassLoader());
086:
087: GenClass genClass = new GenClass(type.getInstanceClassName());
088:
089: genClass.setSuperClassName(type.getBeanClass().getName());
090:
091: genClass.addImport("com.caucho.amber.manager.*");
092: genClass.addImport("com.caucho.amber.entity.*");
093: genClass.addImport("com.caucho.amber.type.*");
094:
095: AmberMappedComponent componentGenerator = type
096: .getComponentGenerator();
097:
098: if (componentGenerator != null) {
099: // type is EntityType or MappedSuperclassType
100:
101: genClass.addInterfaceName(type.getComponentInterfaceName());
102:
103: componentGenerator.setRelatedType((RelatedType) type);
104: componentGenerator.setBaseClassName(type.getBeanClass()
105: .getName());
106: componentGenerator.setExtClassName(type
107: .getInstanceClassName());
108:
109: genClass.addComponent(componentGenerator);
110:
111: DependencyComponent depend = genClass
112: .addDependencyComponent();
113: depend.addDependencyList(componentGenerator
114: .getDependencies());
115: } else if (type instanceof EmbeddableType) {
116: genClass
117: .addInterfaceName("com.caucho.amber.entity.Embeddable");
118:
119: EmbeddableComponent embeddable = new EmbeddableComponent();
120:
121: embeddable.setEmbeddableType((EmbeddableType) type);
122: embeddable.setBaseClassName(type.getBeanClass().getName());
123: embeddable.setExtClassName(type.getInstanceClassName());
124:
125: genClass.addComponent(embeddable);
126: } else {
127: genClass
128: .addInterfaceName("com.caucho.amber.entity.Listener");
129:
130: ListenerComponent listener = new ListenerComponent();
131:
132: listener.setListenerType((ListenerType) type);
133: listener.setBaseClassName(type.getBeanClass().getName());
134: listener.setExtClassName(type.getInstanceClassName());
135:
136: genClass.addComponent(listener);
137: }
138:
139: javaGen.generate(genClass);
140: }
141:
142: /**
143: * Generates the type.
144: */
145: public boolean isPreload(JavaClassGenerator javaGen,
146: AbstractEnhancedType type) throws Exception {
147: Class cl;
148:
149: if (type.isEnhanced())
150: cl = javaGen.loadClass(type.getBeanClass().getName());
151: else
152: cl = javaGen.preload(type.getInstanceClassName());
153:
154: Class expectedClass = Listener.class;
155:
156: if (type instanceof EntityType)
157: expectedClass = Entity.class;
158: else if (type instanceof EmbeddableType)
159: expectedClass = Embeddable.class;
160:
161: return cl != null && expectedClass.isAssignableFrom(cl);
162: }
163:
164: /**
165: * Compiles the pending classes.
166: */
167: public void compile() throws Exception {
168: if (_pendingClassNames.size() == 0)
169: return;
170:
171: String[] javaFiles = new String[_pendingClassNames.size()];
172:
173: for (int i = 0; i < _pendingClassNames.size(); i++) {
174: String name = _pendingClassNames.get(i);
175: name = name.replace('.', '/') + ".java";
176:
177: javaFiles[i] = name;
178: }
179: _pendingClassNames.clear();
180:
181: EntityGenerator gen = new EntityGenerator();
182: JavaCompiler compiler = gen.getCompiler();
183:
184: compiler.compileBatch(javaFiles);
185: }
186: }
|