KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > cfg > PersistenceUnitConfig


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.cfg;
31
32 import com.caucho.amber.manager.AmberContainer;
33 import com.caucho.amber.manager.AmberPersistenceUnit;
34 import com.caucho.bytecode.JClass;
35 import com.caucho.vfs.Path;
36
37 import javax.sql.DataSource JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * <persistence-unit> tag in the persistence.xml
43  */

44 public class PersistenceUnitConfig {
45   private String JavaDoc _name;
46   private String JavaDoc _provider;
47   private DataSource JavaDoc _jtaDataSource;
48   private DataSource JavaDoc _nonJtaDataSource;
49   private boolean _isExcludeUnlistedClasses;
50
51   // className -> type
52
private HashMap JavaDoc<String JavaDoc, JClass> _classMap
53     = new HashMap JavaDoc<String JavaDoc, JClass>();
54
55   /**
56    * Returns the unit name.
57    */

58   public String JavaDoc getName()
59   {
60     return _name;
61   }
62
63   /**
64    * Sets the unit name.
65    */

66   public void setName(String JavaDoc name)
67   {
68     _name = name;
69   }
70
71   /**
72    * Sets the transaction type.
73    */

74   public void setTransactionType(String JavaDoc type)
75   {
76   }
77
78   /**
79    * Sets the description.
80    */

81   public void setDescription(String JavaDoc description)
82   {
83   }
84
85   /**
86    * Sets the provider class name.
87    */

88   public void setProvider(String JavaDoc provider)
89   {
90     _provider = provider;
91   }
92
93   /**
94    * Sets the transactional data source.
95    */

96   public void setJtaDataSource(DataSource JavaDoc ds)
97   {
98     _jtaDataSource = ds;
99   }
100
101   /**
102    * Sets the non-transactional data source.
103    */

104   public void setNonJtaDataSource(DataSource JavaDoc ds)
105   {
106     _nonJtaDataSource = ds;
107   }
108
109   /**
110    * Sets the mapping file.
111    */

112   public void addMappingFile(Path file)
113   {
114   }
115
116   /**
117    * Sets the jars with classes.
118    */

119   public void addJarFile(Path file)
120   {
121   }
122
123   /**
124    * Adds a configured class.
125    */

126   public void addClass(String JavaDoc cl)
127   {
128     // null means the class is not yet verified as:
129
// Entity | Embeddable | MappedSuperclass
130

131     _classMap.put(cl, null);
132   }
133
134   /**
135    * Adds a map of configured classes.
136    */

137   public void addAllClasses(Map JavaDoc<String JavaDoc, JClass> classMap)
138   {
139     for (Map.Entry JavaDoc<String JavaDoc, JClass> entry : classMap.entrySet()) {
140       String JavaDoc k = entry.getKey();
141       JClass v = entry.getValue();
142
143       if (! _classMap.containsKey(k))
144         _classMap.put(k, v);
145     }
146   }
147
148   /**
149    * Sets true if only listed classes should be used.
150    */

151   public void setExcludeUnlistedClasses(boolean isExclude)
152   {
153     _isExcludeUnlistedClasses = isExclude;
154   }
155
156   /**
157    * Adds the properties.
158    */

159   public PropertiesConfig createProperties()
160   {
161     return new PropertiesConfig();
162   }
163
164   public AmberPersistenceUnit init(AmberContainer container,
165                                    EntityMappingsConfig entityMappings)
166     throws Exception JavaDoc
167   {
168     AmberPersistenceUnit unit
169       = new AmberPersistenceUnit(container, _name);
170
171     unit.setJPA(true);
172
173     unit.setEntityMappingsConfig(entityMappings);
174
175     unit.init();
176
177     for (Map.Entry JavaDoc<String JavaDoc, JClass> entry : _classMap.entrySet()) {
178       String JavaDoc className = entry.getKey();
179       JClass type = entry.getValue();
180
181       unit.addEntityClass(className, type);
182     }
183
184     unit.generate();
185
186     return unit;
187   }
188
189   public String JavaDoc toString()
190   {
191     return "PersistenceUnitConfig[" + _name + "]";
192   }
193
194   public class PropertiesConfig {
195     public PropertyConfig createProperty()
196     {
197       return new PropertyConfig();
198     }
199   }
200
201   public class PropertyConfig {
202     public void setName(String JavaDoc name)
203     {
204     }
205
206     public void setValue(String JavaDoc name)
207     {
208     }
209   }
210 }
211
Popular Tags