001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.metadata;
023:
024: import java.util.Arrays;
025: import java.util.List;
026: import java.util.Collections;
027: import java.util.Iterator;
028:
029: import org.jboss.deployment.DeploymentException;
030: import org.jboss.metadata.MetaData;
031:
032: import org.w3c.dom.Element;
033:
034: /**
035: * Imutable class which holds all the information about read-ahead settings.
036: * It loads its data from standardjbosscmp-jdbc.xml and jbosscmp-jdbc.xml
037: *
038: * @author <a href="mailto:on@ibis.odessa.ua">Oleg Nitz</a>
039: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
040: * @version $Revision: 57209 $
041: */
042: public class JDBCReadAheadMetaData {
043: public static final JDBCReadAheadMetaData DEFAULT = new JDBCReadAheadMetaData();
044:
045: /** Don't read ahead. */
046: private static final byte NONE = 0;
047:
048: /** Read ahead when some entity is being loaded (lazily, good for all queries). */
049: private static final byte ON_LOAD = 1;
050:
051: /** Read ahead during "find" (not lazily, the best for queries with small result set). */
052: private static final byte ON_FIND = 2;
053:
054: private static final List STRATEGIES = Arrays.asList(new String[] {
055: "none", "on-load", "on-find" });
056:
057: /** The strategy of reading ahead, one of {@link #NONE}, {@link #ON_LOAD}, {@link #ON_FIND}. */
058: private final byte strategy;
059:
060: /** The page size of the read ahead buffer */
061: private final int pageSize;
062:
063: /** The name of the load group to eager load. */
064: private final String eagerLoadGroup;
065:
066: /** a list of left-join */
067: private final List leftJoinList;
068:
069: /**
070: * Constructs default read ahead meta data: no read ahead.
071: */
072: private JDBCReadAheadMetaData() {
073: strategy = ON_LOAD;
074: pageSize = 255;
075: eagerLoadGroup = "*";
076: leftJoinList = Collections.EMPTY_LIST;
077: }
078:
079: /**
080: * Constructs read ahead meta data with specified strategy, pageSize and
081: * eagerLoadGroup.
082: * NOTE: used only in tests.
083: */
084: public JDBCReadAheadMetaData(String strategy, int pageSize,
085: String eagerLoadGroup) {
086: this (strategy, pageSize, eagerLoadGroup, Collections.EMPTY_LIST);
087: }
088:
089: public JDBCReadAheadMetaData(String strategy, int pageSize,
090: String eagerLoadGroup, List leftJoins) {
091: this .strategy = (byte) STRATEGIES.indexOf(strategy);
092: if (this .strategy < 0) {
093: throw new IllegalArgumentException(
094: "Unknown read ahead strategy '" + strategy + "'.");
095: }
096: this .pageSize = pageSize;
097: this .eagerLoadGroup = eagerLoadGroup;
098: leftJoinList = leftJoins;
099: }
100:
101: /**
102: * Constructs read ahead meta data with the data contained in the read-ahead
103: * xml element from a jbosscmp-jdbc xml file. Optional values of the xml
104: * element that are not present are instead loaded from the defalutValues
105: * parameter.
106: *
107: * @param element the xml Element which contains the read-ahead metadata
108: * @throws DeploymentException if the xml element is invalid
109: */
110: public JDBCReadAheadMetaData(Element element,
111: JDBCReadAheadMetaData defaultValue)
112: throws DeploymentException {
113: // Strategy
114: String strategyStr = MetaData.getUniqueChildContent(element,
115: "strategy");
116: strategy = (byte) STRATEGIES.indexOf(strategyStr);
117: if (strategy < 0) {
118: throw new DeploymentException(
119: "Unknown read ahead strategy '" + strategyStr
120: + "'.");
121: }
122:
123: // page-size
124: String pageSizeStr = MetaData.getOptionalChildContent(element,
125: "page-size");
126: if (pageSizeStr != null) {
127: try {
128: pageSize = Integer.parseInt(pageSizeStr);
129: } catch (NumberFormatException ex) {
130: throw new DeploymentException(
131: "Invalid number format in read-ahead page-size '"
132: + pageSizeStr + "': " + ex);
133: }
134: if (pageSize < 0) {
135: throw new DeploymentException(
136: "Negative value for read ahead page-size '"
137: + pageSizeStr + "'.");
138: }
139: } else {
140: pageSize = defaultValue.getPageSize();
141: }
142:
143: // eager-load-group
144: Element eagerLoadGroupElement = MetaData.getOptionalChild(
145: element, "eager-load-group");
146: if (eagerLoadGroupElement != null) {
147: eagerLoadGroup = MetaData
148: .getElementContent(eagerLoadGroupElement);
149: } else {
150: eagerLoadGroup = defaultValue.getEagerLoadGroup();
151: }
152:
153: // left-join
154: Iterator iter = MetaData.getChildrenByTagName(element,
155: "left-join");
156: leftJoinList = JDBCLeftJoinMetaData.readLeftJoinList(iter);
157: }
158:
159: /**
160: * Is read ahead strategy is none.
161: */
162: public boolean isNone() {
163: return (strategy == NONE);
164: }
165:
166: /**
167: * Is the read ahead stratey on-load
168: */
169: public boolean isOnLoad() {
170: return (strategy == ON_LOAD);
171: }
172:
173: /**
174: * Is the read ahead stratey on-find
175: */
176: public boolean isOnFind() {
177: return (strategy == ON_FIND);
178: }
179:
180: /**
181: * Gets the read ahead page size.
182: */
183: public int getPageSize() {
184: return pageSize;
185: }
186:
187: /**
188: * Gets the eager load group.
189: */
190: public String getEagerLoadGroup() {
191: return eagerLoadGroup;
192: }
193:
194: public Iterator getLeftJoins() {
195: return leftJoinList.iterator();
196: }
197:
198: /**
199: * Returns a string describing this JDBCReadAheadMetaData.
200: * @return a string representation of the object
201: */
202: public String toString() {
203: return "[JDBCReadAheadMetaData :" + " strategy="
204: + STRATEGIES.get(strategy) + ", pageSize=" + pageSize
205: + ", eagerLoadGroup=" + eagerLoadGroup + ", left-join"
206: + leftJoinList + "]";
207: }
208: }
|