KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > query > lib > SpeedoQueryManager


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S. Chassande-Barrioz
25  *
26  */

27
28 package org.objectweb.speedo.query.lib;
29
30 import org.objectweb.fractal.api.control.BindingController;
31 import org.objectweb.jorm.api.PMapper;
32 import org.objectweb.medor.lib.Log;
33 import org.objectweb.medor.eval.prefetch.api.PrefetchBufferFactory;
34 import org.objectweb.medor.eval.prefetch.lib.PrefetchBufferFactoryImpl;
35 import org.objectweb.perseus.cache.api.CacheEntryFactory;
36 import org.objectweb.perseus.cache.api.CacheEventListener;
37 import org.objectweb.perseus.cache.api.CacheException;
38 import org.objectweb.perseus.cache.api.CacheManager;
39 import org.objectweb.perseus.cache.api.FixableCacheEntry;
40 import org.objectweb.perseus.cache.api.CacheEvent;
41 import org.objectweb.speedo.mapper.api.JormFactory;
42 import org.objectweb.speedo.query.api.CompiledQuery;
43 import org.objectweb.speedo.query.api.QueryDefinition;
44 import org.objectweb.speedo.query.api.QueryManager;
45 import org.objectweb.speedo.query.api.QueryManagerAttribute;
46 import org.objectweb.util.monolog.api.BasicLevel;
47 import org.objectweb.util.monolog.api.Logger;
48 import org.objectweb.util.monolog.api.LoggerFactory;
49
50 /**
51  * SpeedoQueryManager manages the association between SpeedoQuery and the
52  * the compiled query: SpeedoCompiledQuery.
53  *
54  * @author S.Chassande-Barrioz
55  */

56 public class SpeedoQueryManager
57         implements QueryManager, CacheEntryFactory, CacheEventListener, BindingController, QueryManagerAttribute {
58
59     public final static String JavaDoc MAPPER_BINDING = "mapper";
60     public final static String JavaDoc JORM_FACTORY_BINDING = "jorm-factory";
61     public final static String JavaDoc COMPILED_QUERY_CACHE_BINDING = "compiled-query-cache";
62
63     private PMapper mapper = null;
64
65     private JormFactory jormFactory = null;
66
67     private CacheManager compiledQueriesCache = null;
68     private PrefetchBufferFactory prefetchBufferFactory = null;
69
70     private Logger logger = null;
71     private Logger cqlogger = null;
72     private Logger cqpvlogger = null;
73     private Logger cqpflogger = null;
74     private boolean prefetchingOnQuery = true;
75     private boolean prefetchingOnExtent = true;
76
77     /**
78      * creates a new SpeedoQueryManager object.
79      */

80     public SpeedoQueryManager() {
81         prefetchBufferFactory = new PrefetchBufferFactoryImpl();
82     }
83
84     public FixableCacheEntry create(Object JavaDoc id, Object JavaDoc obj) {
85         return (SpeedoCompiledQuery) obj;
86     }
87
88     public boolean getPrefetchActivatedOnQuery() {
89         return prefetchingOnQuery;
90     }
91
92     public void setPrefetchActivatedOnQuery(boolean prefetch) {
93         prefetchingOnQuery = prefetch;
94     }
95
96     public boolean getPrefetchActivatedOnExtent() {
97         return prefetchingOnExtent;
98     }
99
100     public void setPrefetchActivatedOnExtent(boolean prefetch) {
101         prefetchingOnExtent = prefetch;
102     }
103     
104     // IMPLEMENTATION OF UserBindingController INTERFACE //
105
//---------------------------------------------------//
106

107     public String JavaDoc[] listFc() {
108         return new String JavaDoc[] {
109             MAPPER_BINDING,
110             JORM_FACTORY_BINDING,
111             COMPILED_QUERY_CACHE_BINDING
112         };
113     }
114
115     public Object JavaDoc lookupFc(String JavaDoc s) {
116         if (MAPPER_BINDING.equals(s))
117             return mapper;
118         else if (JORM_FACTORY_BINDING.equals(s))
119             return jormFactory;
120         else if (COMPILED_QUERY_CACHE_BINDING.equals(s))
121             return compiledQueriesCache;
122         else
123             return null;
124     }
125
126     public void bindFc(String JavaDoc s, Object JavaDoc o) {
127         if ("monolog-factory".equals(s)) {
128             Log.loggerFactory = (LoggerFactory) o;
129             String JavaDoc bn = logger.getName();
130             cqlogger = Log.loggerFactory.getLogger(bn + ".compiled-query");
131             cqpvlogger = Log.loggerFactory.getLogger(cqlogger.getName() + ".parser.variable");
132             cqpflogger = Log.loggerFactory.getLogger(cqlogger.getName() + ".parser.filter");
133             logger = Log.loggerFactory.getLogger(cqlogger.getName() + ".allocator");
134         } else if ("logger".equals(s)) {
135             logger = (Logger) o;
136         } else if (MAPPER_BINDING.equals(s))
137             mapper = (PMapper) o;
138         else if (COMPILED_QUERY_CACHE_BINDING.equals(s))
139             compiledQueriesCache = (CacheManager) o;
140         else if (JORM_FACTORY_BINDING.equals(s))
141             jormFactory = (JormFactory) o;
142     }
143
144     public void unbindFc(String JavaDoc s) {
145         if (MAPPER_BINDING.equals(s))
146             mapper = null;
147         else if (JORM_FACTORY_BINDING.equals(s))
148             jormFactory = null;
149         else if (COMPILED_QUERY_CACHE_BINDING.equals(s))
150             compiledQueriesCache = null;
151     }
152
153
154     // IMPLEMENTATION OF CacheEventListener INTERFACE //
155
//------------------------------------------------//
156

157     /**
158      * An entry has been added in the cache.
159      * @param event describes the added entry
160      */

161     public void entryBound(CacheEvent event) {
162     }
163
164     /**
165      * An entry has been evicted from the cache.
166      * @param event describes the evicted entry
167      */

168     public void entryUnbound(CacheEvent event) {
169     }
170
171     // IMPLEMENTATION OF QueryManager INTERFACE //
172
//------------------------------------------//
173

174     /**
175      * returns a CompiledQuery implementation, creates it if it does not exists,
176      * or just returns an existing one.
177      * @param qd a QueryDefinition
178      * @return a (new/existing) CompiledQuery instance.
179      */

180     public synchronized CompiledQuery getQueryCompiler(QueryDefinition qd) {
181         SpeedoCompiledQuery scq = (SpeedoCompiledQuery) compiledQueriesCache.lookup(qd);
182         if (scq == null) {
183             if (logger.isLoggable(BasicLevel.DEBUG)) {
184                 logger.log(BasicLevel.DEBUG, "Allocation of a new compiled query: "
185                         + qd.qdToString(false));
186             }
187             scq = new SpeedoCompiledQuery();
188             scq.init(cqlogger, cqpvlogger, cqpflogger, mapper,
189                 prefetchBufferFactory, jormFactory);
190             qd.withPrefetch(prefetchingOnExtent && qd.withPrefetch());
191             scq.defineQuery(qd);
192             try {
193                 compiledQueriesCache.bind(scq.getCeIdentifier(), scq);
194             } catch (CacheException e) {
195                 logger.log(BasicLevel.WARN,
196                         "Impossible to bind a new compiled query from the cache: ", e);
197                 //nothing to do, the compiled query is not cached
198
}
199         } else {
200             if (logger.isLoggable(BasicLevel.DEBUG)) {
201                 logger.log(BasicLevel.DEBUG, "Reuse a compiled query: "
202                         + qd.qdToString(false));
203             }
204         }
205         return scq;
206     }
207 }
208
Popular Tags