001: /**********************************************************************
002: Copyright (c) 2007 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: 2008 Andy Jefferson - check on datastore when getting query support
017: ...
018: ***********************************************************************/package org.jpox.store.query;
019:
020: import java.lang.reflect.InvocationTargetException;
021:
022: import org.jpox.OMFContext;
023: import org.jpox.ObjectManager;
024: import org.jpox.exceptions.JPOXException;
025: import org.jpox.management.ManagementServer;
026: import org.jpox.management.runtime.QueryRuntime;
027: import org.jpox.util.ClassUtils;
028:
029: /**
030: * Manages the runtime, metadata and lifecycle of queries.
031: */
032: public class QueryManager {
033: /** Query Runtime. Used when providing management of services. */
034: QueryRuntime queryRuntime = null;
035:
036: public QueryManager(OMFContext omfContext) {
037: if (omfContext.getManagement() != null) {
038: // register MBean in MbeanServer
039: ManagementServer mgntServer = omfContext.getManagement()
040: .getManagementServer();
041: queryRuntime = new QueryRuntime();
042: String mbeanName = omfContext.getDomainName()
043: + ":InstanceName="
044: + omfContext.getInstanceName()
045: + ",Type="
046: + ClassUtils.getClassNameForClass(queryRuntime
047: .getClass()) + ",Name=QueryRuntime";
048: mgntServer.registerMBean(this .queryRuntime, mbeanName);
049: }
050: }
051:
052: public QueryRuntime getQueryRuntime() {
053: return queryRuntime;
054: }
055:
056: /**
057: * Method to generate a new query using the passed query as basis.
058: * @param language The query language
059: * @param om The Object Manager
060: * @param query The query filter (String) or a previous Query
061: * @return The Query
062: */
063: public Query newQuery(String language, ObjectManager om,
064: Object query) {
065: if (language == null) {
066: return null;
067: }
068:
069: // Find the query support for this language and this datastore
070: try {
071: if (query == null) {
072: Class[] argsClass = new Class[] { org.jpox.ObjectManager.class };
073: Object[] args = new Object[] { om };
074: Query q = (Query) om
075: .getOMFContext()
076: .getPluginManager()
077: .createExecutableExtension(
078: "org.jpox.store_query_query",
079: new String[] { "name", "datastore" },
080: new String[] {
081: language,
082: om.getStoreManager()
083: .getStoreManagerKey() },
084: "class-name", argsClass, args);
085: if (q == null) {
086: // No query support for this language
087: throw new JPOXException(
088: "JPOX doesnt currently support queries of language "
089: + language + " for this datastore");
090: }
091: return q;
092: } else {
093: Query q = null;
094: if (query instanceof String) {
095: // Try XXXQuery(ObjectManager, String);
096: Class[] argsClass = new Class[] {
097: org.jpox.ObjectManager.class, String.class };
098: Object[] args = new Object[] { om, query };
099: q = (Query) om
100: .getOMFContext()
101: .getPluginManager()
102: .createExecutableExtension(
103: "org.jpox.store_query_query",
104: new String[] { "name", "datastore" },
105: new String[] {
106: language,
107: om
108: .getStoreManager()
109: .getStoreManagerKey() },
110: "class-name", argsClass, args);
111: if (q == null) {
112: // No query support for this language
113: throw new JPOXException(
114: "JPOX doesnt currently support queries of language "
115: + language
116: + " for this datastore");
117: }
118: } else if (query instanceof Query) {
119: // Try XXXQuery(ObjectManager, query.class);
120: Class[] argsClass = new Class[] {
121: org.jpox.ObjectManager.class,
122: query.getClass() };
123: Object[] args = new Object[] { om, query };
124: q = (Query) om
125: .getOMFContext()
126: .getPluginManager()
127: .createExecutableExtension(
128: "org.jpox.store_query_query",
129: new String[] { "name", "datastore" },
130: new String[] {
131: language,
132: om
133: .getStoreManager()
134: .getStoreManagerKey() },
135: "class-name", argsClass, args);
136: if (q == null) {
137: // No query support for this language
138: throw new JPOXException(
139: "JPOX doesnt currently support queries of language "
140: + language
141: + " for this datastore");
142: }
143: } else {
144: // Try XXXQuery(ObjectManager, Object);
145: Class[] argsClass = new Class[] {
146: org.jpox.ObjectManager.class, Object.class };
147: Object[] args = new Object[] { om, query };
148: q = (Query) om
149: .getOMFContext()
150: .getPluginManager()
151: .createExecutableExtension(
152: "org.jpox.store_query_query",
153: new String[] { "name", "datastore" },
154: new String[] {
155: language,
156: om
157: .getStoreManager()
158: .getStoreManagerKey() },
159: "class-name", argsClass, args);
160: if (q == null) {
161: // No query support for this language
162: throw new JPOXException(
163: "JPOX doesnt currently support queries of language "
164: + language
165: + " for this datastore");
166: }
167: }
168: return q;
169: }
170: } catch (InvocationTargetException e) {
171: Throwable t = e.getTargetException();
172: if (t instanceof RuntimeException) {
173: throw (RuntimeException) t;
174: } else if (t instanceof Error) {
175: throw (Error) t;
176: } else {
177: throw new JPOXException(t.getMessage(), t).setFatal();
178: }
179: } catch (Exception e) {
180: throw new JPOXException(e.getMessage(), e).setFatal();
181: }
182: }
183: }
|