KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.jboss.deployment.DeploymentException;
30 import org.jboss.metadata.MetaData;
31
32 import org.w3c.dom.Element JavaDoc;
33
34 /**
35  * Imutable class which holds all the information about read-ahead settings.
36  * It loads its data from standardjbosscmp-jdbc.xml and jbosscmp-jdbc.xml
37  *
38  * @author <a HREF="mailto:on@ibis.odessa.ua">Oleg Nitz</a>
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version $Revision: 37459 $
41  */

42 public class JDBCReadAheadMetaData
43 {
44    public static final JDBCReadAheadMetaData DEFAULT = new JDBCReadAheadMetaData();
45
46    /** Don't read ahead. */
47    private static final byte NONE = 0;
48
49    /** Read ahead when some entity is being loaded (lazily, good for all queries). */
50    private static final byte ON_LOAD = 1;
51
52    /** Read ahead during "find" (not lazily, the best for queries with small result set). */
53    private static final byte ON_FIND = 2;
54
55    private static final List JavaDoc STRATEGIES = Arrays.asList(new String JavaDoc[]{"none", "on-load", "on-find"});
56
57    /** The strategy of reading ahead, one of {@link #NONE}, {@link #ON_LOAD}, {@link #ON_FIND}. */
58    private final byte strategy;
59
60    /** The page size of the read ahead buffer */
61    private final int pageSize;
62
63    /** The name of the load group to eager load. */
64    private final String JavaDoc eagerLoadGroup;
65
66    /** a list of left-join */
67    private final List JavaDoc leftJoinList;
68
69    /**
70     * Constructs default read ahead meta data: no read ahead.
71     */

72    private JDBCReadAheadMetaData()
73    {
74       strategy = ON_LOAD;
75       pageSize = 255;
76       eagerLoadGroup = "*";
77       leftJoinList = Collections.EMPTY_LIST;
78    }
79
80    /**
81     * Constructs read ahead meta data with specified strategy, pageSize and
82     * eagerLoadGroup.
83     * NOTE: used only in tests.
84     */

85    public JDBCReadAheadMetaData(String JavaDoc strategy, int pageSize, String JavaDoc eagerLoadGroup)
86    {
87       this(strategy, pageSize, eagerLoadGroup, Collections.EMPTY_LIST);
88    }
89
90    public JDBCReadAheadMetaData(String JavaDoc strategy, int pageSize, String JavaDoc eagerLoadGroup, List JavaDoc leftJoins)
91    {
92       this.strategy = (byte)STRATEGIES.indexOf(strategy);
93       if(this.strategy < 0)
94       {
95          throw new IllegalArgumentException JavaDoc("Unknown read ahead strategy '" + strategy + "'.");
96       }
97       this.pageSize = pageSize;
98       this.eagerLoadGroup = eagerLoadGroup;
99       leftJoinList = leftJoins;
100    }
101
102    /**
103     * Constructs read ahead meta data with the data contained in the read-ahead
104     * xml element from a jbosscmp-jdbc xml file. Optional values of the xml
105     * element that are not present are instead loaded from the defalutValues
106     * parameter.
107     *
108     * @param element the xml Element which contains the read-ahead metadata
109     * @throws DeploymentException if the xml element is invalid
110     */

111    public JDBCReadAheadMetaData(Element JavaDoc element, JDBCReadAheadMetaData defaultValue)
112       throws DeploymentException
113    {
114       // Strategy
115
String JavaDoc strategyStr = MetaData.getUniqueChildContent(element, "strategy");
116       strategy = (byte)STRATEGIES.indexOf(strategyStr);
117       if(strategy < 0)
118       {
119          throw new DeploymentException("Unknown read ahead strategy '" + strategyStr + "'.");
120       }
121
122       // page-size
123
String JavaDoc pageSizeStr = MetaData.getOptionalChildContent(element, "page-size");
124       if(pageSizeStr != null)
125       {
126          try
127          {
128             pageSize = Integer.parseInt(pageSizeStr);
129          }
130          catch(NumberFormatException JavaDoc ex)
131          {
132             throw new DeploymentException("Invalid number format in read-ahead page-size '" + pageSizeStr + "': " + ex);
133          }
134          if(pageSize < 0)
135          {
136             throw new DeploymentException("Negative value for read ahead page-size '" + pageSizeStr + "'.");
137          }
138       }
139       else
140       {
141          pageSize = defaultValue.getPageSize();
142       }
143
144       // eager-load-group
145
Element JavaDoc eagerLoadGroupElement = MetaData.getOptionalChild(element, "eager-load-group");
146       if(eagerLoadGroupElement != null)
147       {
148          eagerLoadGroup = MetaData.getElementContent(eagerLoadGroupElement);
149       }
150       else
151       {
152          eagerLoadGroup = defaultValue.getEagerLoadGroup();
153       }
154
155       // left-join
156
Iterator JavaDoc iter = MetaData.getChildrenByTagName(element, "left-join");
157       leftJoinList = JDBCLeftJoinMetaData.readLeftJoinList(iter);
158    }
159
160    /**
161     * Is read ahead strategy is none.
162     */

163    public boolean isNone()
164    {
165       return (strategy == NONE);
166    }
167
168    /**
169     * Is the read ahead stratey on-load
170     */

171    public boolean isOnLoad()
172    {
173       return (strategy == ON_LOAD);
174    }
175
176    /**
177     * Is the read ahead stratey on-find
178     */

179    public boolean isOnFind()
180    {
181       return (strategy == ON_FIND);
182    }
183
184    /**
185     * Gets the read ahead page size.
186     */

187    public int getPageSize()
188    {
189       return pageSize;
190    }
191
192    /**
193     * Gets the eager load group.
194     */

195    public String JavaDoc getEagerLoadGroup()
196    {
197       return eagerLoadGroup;
198    }
199
200    public Iterator JavaDoc getLeftJoins()
201    {
202       return leftJoinList.iterator();
203    }
204
205    /**
206     * Returns a string describing this JDBCReadAheadMetaData.
207     * @return a string representation of the object
208     */

209    public String JavaDoc toString()
210    {
211       return "[JDBCReadAheadMetaData :" +
212          " strategy=" + STRATEGIES.get(strategy) +
213          ", pageSize=" + pageSize +
214          ", eagerLoadGroup=" + eagerLoadGroup +
215          ", left-join" + leftJoinList + "]";
216    }
217 }
218
Popular Tags