KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > gen > AmberGeneratorImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.gen;
31
32 import com.caucho.amber.entity.Embeddable;
33 import com.caucho.amber.entity.Entity;
34 import com.caucho.amber.entity.Listener;
35 import com.caucho.amber.manager.AmberPersistenceUnit;
36 import com.caucho.amber.type.AbstractEnhancedType;
37 import com.caucho.amber.type.EmbeddableType;
38 import com.caucho.amber.type.EntityType;
39 import com.caucho.amber.type.ListenerType;
40 import com.caucho.java.JavaCompiler;
41 import com.caucho.java.gen.DependencyComponent;
42 import com.caucho.java.gen.GenClass;
43 import com.caucho.java.gen.JavaClassGenerator;
44 import com.caucho.log.Log;
45 import com.caucho.util.L10N;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Enhancing the java objects for Amber mapping.
52  */

53 public class AmberGeneratorImpl implements AmberGenerator {
54   private static final L10N L = new L10N(AmberGeneratorImpl.class);
55   private static final Logger JavaDoc log = Log.open(AmberGeneratorImpl.class);
56
57   private AmberPersistenceUnit _amberPersistenceUnit;
58
59   private ArrayList JavaDoc<String JavaDoc> _pendingClassNames =
60     new ArrayList JavaDoc<String JavaDoc>();
61
62   public AmberGeneratorImpl(AmberPersistenceUnit manager)
63   {
64     _amberPersistenceUnit = manager;
65   }
66
67   /**
68    * Configures the type.
69    */

70   public void configure(AbstractEnhancedType type)
71     throws Exception JavaDoc
72   {
73   }
74
75   /**
76    * Generates the type.
77    */

78   public void generate(AbstractEnhancedType type)
79     throws Exception JavaDoc
80   {
81     generateJava(null, type);
82   }
83
84   /**
85    * Generates the type.
86    */

87   public void generateJava(JavaClassGenerator javaGen,
88                            AbstractEnhancedType type)
89     throws Exception JavaDoc
90   {
91     if (isPreload(javaGen, type) || type.isGenerated())
92       return;
93
94     type.setGenerated(true);
95     //type.setInstanceClassLoader(javaGen.getClassLoader());
96

97     GenClass genClass = new GenClass(type.getInstanceClassName());
98
99     genClass.setSuperClassName(type.getBeanClass().getName());
100
101     if (type instanceof EntityType) {
102       genClass.addInterfaceName("com.caucho.amber.entity.Entity");
103
104       EntityComponent entity = new EntityComponent();
105
106       entity.setEntityType((EntityType) type);
107       entity.setBaseClassName(type.getBeanClass().getName());
108       entity.setExtClassName(type.getInstanceClassName());
109
110       genClass.addComponent(entity);
111
112       DependencyComponent depend = genClass.addDependencyComponent();
113       depend.addDependencyList(entity.getDependencies());
114     }
115     else if (type instanceof EmbeddableType) {
116       genClass.addInterfaceName("com.caucho.amber.entity.Embeddable");
117
118       EmbeddableComponent embeddable = new EmbeddableComponent();
119
120       embeddable.setEmbeddableType((EmbeddableType) type);
121       embeddable.setBaseClassName(type.getBeanClass().getName());
122       embeddable.setExtClassName(type.getInstanceClassName());
123
124       genClass.addComponent(embeddable);
125     }
126     else {
127       genClass.addInterfaceName("com.caucho.amber.entity.Listener");
128
129       ListenerComponent listener = new ListenerComponent();
130
131       listener.setListenerType((ListenerType) type);
132       listener.setBaseClassName(type.getBeanClass().getName());
133       listener.setExtClassName(type.getInstanceClassName());
134
135       genClass.addComponent(listener);
136     }
137
138     javaGen.generate(genClass);
139   }
140
141   /**
142    * Generates the type.
143    */

144   public boolean isPreload(JavaClassGenerator javaGen,
145                            AbstractEnhancedType type)
146     throws Exception JavaDoc
147   {
148     Class JavaDoc cl;
149
150     if (type.isEnhanced())
151       cl = javaGen.loadClass(type.getBeanClass().getName());
152     else
153       cl = javaGen.preload(type.getInstanceClassName());
154
155     Class JavaDoc expectedClass = Listener.class;
156
157     if (type instanceof EntityType)
158       expectedClass = Entity.class;
159     else if (type instanceof EmbeddableType)
160       expectedClass = Embeddable.class;
161
162     return cl != null && expectedClass.isAssignableFrom(cl);
163   }
164
165   /**
166    * Compiles the pending classes.
167    */

168   public void compile()
169     throws Exception JavaDoc
170   {
171     if (_pendingClassNames.size() == 0)
172       return;
173
174     String JavaDoc []javaFiles = new String JavaDoc[_pendingClassNames.size()];
175
176     for (int i = 0; i < _pendingClassNames.size(); i++) {
177       String JavaDoc name = _pendingClassNames.get(i);
178       name = name.replace('.', '/') + ".java";
179
180       javaFiles[i] = name;
181     }
182     _pendingClassNames.clear();
183
184     EntityGenerator gen = new EntityGenerator();
185     JavaCompiler compiler = gen.getCompiler();
186
187     compiler.compileBatch(javaFiles);
188   }
189 }
190
Popular Tags