01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.kernel;
20:
21: import java.security.AccessController;
22: import java.security.PrivilegedActionException;
23: import java.util.HashMap;
24: import java.util.Map;
25:
26: import org.apache.openjpa.kernel.exps.ExpressionParser;
27: import org.apache.openjpa.lib.util.J2DoPrivHelper;
28: import org.apache.openjpa.lib.util.Services;
29: import org.apache.openjpa.util.InternalException;
30:
31: /**
32: * Constants and utilities for query languages.
33: */
34: public class QueryLanguages {
35:
36: public static final String LANG_SQL = "openjpa.SQL";
37: public static final String LANG_METHODQL = "openjpa.MethodQL";
38:
39: private static Map _expressionParsers = new HashMap();
40: static {
41: // Load and cache all the query languages available in the system.
42: Class[] classes = Services
43: .getImplementorClasses(
44: ExpressionParser.class,
45: (ClassLoader) AccessController
46: .doPrivileged(J2DoPrivHelper
47: .getClassLoaderAction(ExpressionParser.class)));
48: for (int i = 0; i < classes.length; i++) {
49: ExpressionParser ep;
50: try {
51: ep = (ExpressionParser) AccessController
52: .doPrivileged(J2DoPrivHelper
53: .newInstanceAction(classes[i]));
54: } catch (PrivilegedActionException pae) {
55: throw new InternalException(pae.getException());
56: } catch (InstantiationException e) {
57: throw new InternalException(e);
58: } catch (IllegalAccessException e) {
59: throw new InternalException(e);
60: }
61: _expressionParsers.put(ep.getLanguage(), ep);
62: }
63: }
64:
65: /**
66: * Return the {@link ExpressionParser} for <code>language</code>, or
67: * <code>null</code> if no expression parser exists in the system for
68: * the specified language.
69: */
70: public static ExpressionParser parserForLanguage(String language) {
71: return (ExpressionParser) _expressionParsers.get(language);
72: }
73: }
|