KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > metadata > JDBCOptimisticLockingMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.metadata;
23
24 import org.jboss.deployment.DeploymentException;
25 import org.jboss.metadata.MetaData;
26 import org.jboss.logging.Logger;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * Optimistick locking metadata
31  *
32  * @author <a HREF="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
33  * @version $Revision: 37459 $
34  */

35 public final class JDBCOptimisticLockingMetaData
36 {
37    // Constants ---------------------------------------
38
public static final Integer JavaDoc FIELD_GROUP_STRATEGY = new Integer JavaDoc(1);
39    public static final Integer JavaDoc MODIFIED_STRATEGY = new Integer JavaDoc(2);
40    public static final Integer JavaDoc READ_STRATEGY = new Integer JavaDoc(4);
41    public static final Integer JavaDoc VERSION_COLUMN_STRATEGY = new Integer JavaDoc(8);
42    public static final Integer JavaDoc TIMESTAMP_COLUMN_STRATEGY = new Integer JavaDoc(16);
43    public static final Integer JavaDoc KEYGENERATOR_COLUMN_STRATEGY = new Integer JavaDoc(32);
44
45    // Attributes --------------------------------------
46
/** locking strategy */
47    final private Integer JavaDoc lockingStrategy;
48
49    /** group name for field group strategy */
50    final private String JavaDoc groupName;
51
52    /** locking field for verion- or timestamp-column strategy */
53    final private JDBCCMPFieldMetaData lockingField;
54
55    /** key generator factory */
56    final private String JavaDoc keyGeneratorFactory;
57
58    /** logger */
59    final private Logger log;
60
61    // Constructors ------------------------------------
62
/**
63     * Constructs optimistic locking metadata reading
64     * optimistic-locking XML element
65     */

66    public JDBCOptimisticLockingMetaData(JDBCEntityMetaData entityMetaData,
67                                         Element JavaDoc element)
68       throws DeploymentException
69    {
70       log = Logger.getLogger(entityMetaData.getName());
71
72       Element JavaDoc strategyEl;
73       if((strategyEl = MetaData.getOptionalChild(element, "group-name")) != null)
74       {
75          lockingStrategy = FIELD_GROUP_STRATEGY;
76          groupName = MetaData.getElementContent(strategyEl);
77          lockingField = null;
78          keyGeneratorFactory = null;
79
80          log.debug("optimistic locking: group=" + groupName);
81       }
82       else if((strategyEl = MetaData.getOptionalChild(element, "modified-strategy")) != null)
83       {
84          lockingStrategy = MODIFIED_STRATEGY;
85          groupName = null;
86          lockingField = null;
87          keyGeneratorFactory = null;
88
89          log.debug("optimistic locking: modified strategy");
90       }
91       else if((strategyEl = MetaData.getOptionalChild(element, "read-strategy")) != null)
92       {
93          lockingStrategy = READ_STRATEGY;
94          groupName = null;
95          lockingField = null;
96          keyGeneratorFactory = null;
97
98          log.debug("optimistic locking: read strategy");
99       }
100       else if((strategyEl = MetaData.getOptionalChild(element, "version-column")) != null)
101       {
102          String JavaDoc fieldType = MetaData.getOptionalChildContent(element, "field-type");
103          if(fieldType != null)
104             throw new DeploymentException(
105                "field-type is not allowed for version column. It is implicitly set to java.lang.Long."
106             );
107
108          lockingStrategy = VERSION_COLUMN_STRATEGY;
109          lockingField = constructLockingField(entityMetaData, element);
110          groupName = null;
111          keyGeneratorFactory = null;
112
113          log.debug("optimistic locking: version-column=" + lockingField.getFieldName());
114       }
115       else if((strategyEl = MetaData.getOptionalChild(element, "timestamp-column")) != null)
116       {
117          String JavaDoc fieldType = MetaData.getOptionalChildContent(element, "field-type");
118          if(fieldType != null)
119             throw new DeploymentException(
120                "field-type is not allowed for timestamp column. It is implicitly set to java.util.Date."
121             );
122
123          lockingStrategy = TIMESTAMP_COLUMN_STRATEGY;
124          lockingField = constructLockingField(entityMetaData, element);
125          groupName = null;
126          keyGeneratorFactory = null;
127
128          log.debug("optimistic locking: timestamp-column=" + lockingField.getFieldName());
129       }
130       else if((keyGeneratorFactory =
131          MetaData.getOptionalChildContent(element, "key-generator-factory")) != null)
132       {
133          lockingStrategy = KEYGENERATOR_COLUMN_STRATEGY;
134          lockingField = constructLockingField(entityMetaData, element);
135          groupName = null;
136
137          log.debug("optimistic locking: key-generator-factory=" + keyGeneratorFactory);
138       }
139       else
140       {
141          throw new DeploymentException("Unexpected error: entity "
142             + entityMetaData.getName()
143             + " has unkown/incorrect optimistic locking configuration.");
144       }
145    }
146
147    // Public ------------------------------------------
148
public Integer JavaDoc getLockingStrategy()
149    {
150       return lockingStrategy;
151    }
152
153    public String JavaDoc getGroupName()
154    {
155       return groupName;
156    }
157
158    public JDBCCMPFieldMetaData getLockingField()
159    {
160       return lockingField;
161    }
162
163    public String JavaDoc getKeyGeneratorFactory()
164    {
165       return keyGeneratorFactory;
166    }
167
168    // Private -----------------------------------------
169
/**
170     * Constructs a locking field metadata from
171     * XML element
172     */

173    private JDBCCMPFieldMetaData constructLockingField(
174       JDBCEntityMetaData entity,
175       Element JavaDoc element)
176       throws DeploymentException
177    {
178       // field name
179
String JavaDoc fieldName = MetaData.getOptionalChildContent(element, "field-name");
180       if(fieldName == null || fieldName.trim().length() < 1)
181          fieldName = (lockingStrategy == VERSION_COLUMN_STRATEGY ? "version_lock" :
182             (lockingStrategy == TIMESTAMP_COLUMN_STRATEGY ? "timestamp_lock" : "generated_lock"));
183
184       // column name
185
String JavaDoc columnName = MetaData.getOptionalChildContent(element, "column-name");
186       if(columnName == null || columnName.trim().length() < 1)
187          columnName = (lockingStrategy == VERSION_COLUMN_STRATEGY ? "version_lock" :
188             (lockingStrategy == TIMESTAMP_COLUMN_STRATEGY ? "timestamp_lock" : "generated_lock"));
189
190       // field type
191
Class JavaDoc fieldType = null;
192       if(lockingStrategy == VERSION_COLUMN_STRATEGY)
193          fieldType = java.lang.Long JavaDoc.class;
194       else if(lockingStrategy == TIMESTAMP_COLUMN_STRATEGY)
195          fieldType = java.util.Date JavaDoc.class;
196       String JavaDoc fieldTypeStr = MetaData.getOptionalChildContent(element, "field-type");
197       if(fieldTypeStr != null)
198       {
199          try
200          {
201             fieldType = GetTCLAction.
202                getContextClassLoader().loadClass(fieldTypeStr);
203          }
204          catch(ClassNotFoundException JavaDoc e)
205          {
206             throw new DeploymentException(
207                "Could not load field type for optimistic locking field "
208                + fieldName + ": " + fieldTypeStr);
209          }
210       }
211
212       // JDBC/SQL Type
213
int jdbcType;
214       String JavaDoc sqlType;
215       String JavaDoc jdbcTypeName = MetaData.getOptionalChildContent(element, "jdbc-type");
216       if(jdbcTypeName != null)
217       {
218          jdbcType = JDBCMappingMetaData.getJdbcTypeFromName(jdbcTypeName);
219          sqlType = MetaData.getUniqueChildContent(element, "sql-type");
220       }
221       else
222       {
223          jdbcType = Integer.MIN_VALUE;
224          sqlType = null;
225       }
226
227       return new JDBCCMPFieldMetaData(
228          entity, fieldName, fieldType, columnName, jdbcType, sqlType
229       );
230    }
231 }
232
Popular Tags