/**********************************************************************
Copyright (c) 2007 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
2008 Andy Jefferson - check on datastore when getting query support
...
***********************************************************************/
package org.jpox.store.query;
import java.lang.reflect.InvocationTargetException;
import org.jpox.OMFContext;
import org.jpox.ObjectManager;
import org.jpox.exceptions.JPOXException;
import org.jpox.management.ManagementServer;
import org.jpox.management.runtime.QueryRuntime;
import org.jpox.util.ClassUtils;
/**
* Manages the runtime, metadata and lifecycle of queries.
*/
public class QueryManager
{
/** Query Runtime. Used when providing management of services. */
QueryRuntime queryRuntime = null;
public QueryManager(OMFContext omfContext)
{
if (omfContext.getManagement() != null)
{
// register MBean in MbeanServer
ManagementServer mgntServer = omfContext.getManagement().getManagementServer();
queryRuntime = new QueryRuntime();
String mbeanName = omfContext.getDomainName() + ":InstanceName=" + omfContext.getInstanceName() +
",Type=" + ClassUtils.getClassNameForClass(queryRuntime.getClass())+
",Name=QueryRuntime";
mgntServer.registerMBean(this.queryRuntime, mbeanName);
}
}
public QueryRuntime getQueryRuntime()
{
return queryRuntime;
}
/**
* Method to generate a new query using the passed query as basis.
* @param language The query language
* @param om The Object Manager
* @param query The query filter (String) or a previous Query
* @return The Query
*/
public Query newQuery(String language, ObjectManager om, Object query)
{
if (language == null)
{
return null;
}
// Find the query support for this language and this datastore
try
{
if (query == null)
{
Class[] argsClass = new Class[] {org.jpox.ObjectManager.class};
Object[] args = new Object[] {om};
Query q = (Query) om.getOMFContext().getPluginManager().createExecutableExtension(
"org.jpox.store_query_query", new String[] {"name", "datastore"},
new String[] {language, om.getStoreManager().getStoreManagerKey()},
"class-name", argsClass, args);
if (q == null)
{
// No query support for this language
throw new JPOXException("JPOX doesnt currently support queries of language " + language + " for this datastore");
}
return q;
}
else
{
Query q = null;
if (query instanceof String)
{
// Try XXXQuery(ObjectManager, String);
Class[] argsClass = new Class[]{org.jpox.ObjectManager.class, String.class};
Object[] args = new Object[]{om, query};
q = (Query) om.getOMFContext().getPluginManager().createExecutableExtension(
"org.jpox.store_query_query", new String[] {"name", "datastore"},
new String[] {language, om.getStoreManager().getStoreManagerKey()},
"class-name", argsClass, args);
if (q == null)
{
// No query support for this language
throw new JPOXException("JPOX doesnt currently support queries of language " + language + " for this datastore");
}
}
else if (query instanceof Query)
{
// Try XXXQuery(ObjectManager, query.class);
Class[] argsClass = new Class[]{org.jpox.ObjectManager.class, query.getClass()};
Object[] args = new Object[]{om, query};
q = (Query) om.getOMFContext().getPluginManager().createExecutableExtension(
"org.jpox.store_query_query", new String[] {"name", "datastore"},
new String[] {language, om.getStoreManager().getStoreManagerKey()},
"class-name", argsClass, args);
if (q == null)
{
// No query support for this language
throw new JPOXException("JPOX doesnt currently support queries of language " + language + " for this datastore");
}
}
else
{
// Try XXXQuery(ObjectManager, Object);
Class[] argsClass = new Class[]{org.jpox.ObjectManager.class, Object.class};
Object[] args = new Object[]{om, query};
q = (Query) om.getOMFContext().getPluginManager().createExecutableExtension(
"org.jpox.store_query_query", new String[] {"name", "datastore"},
new String[] {language, om.getStoreManager().getStoreManagerKey()},
"class-name", argsClass, args);
if (q == null)
{
// No query support for this language
throw new JPOXException("JPOX doesnt currently support queries of language " + language + " for this datastore");
}
}
return q;
}
}
catch (InvocationTargetException e)
{
Throwable t = e.getTargetException();
if (t instanceof RuntimeException)
{
throw (RuntimeException) t;
}
else if (t instanceof Error)
{
throw (Error) t;
}
else
{
throw new JPOXException(t.getMessage(), t).setFatal();
}
}
catch (Exception e)
{
throw new JPOXException(e.getMessage(), e).setFatal();
}
}
}