1 /*
2 * #%L
3 * prolobjectlink-jpi
4 * %%
5 * Copyright (C) 2019 Prolobjectlink Project
6 * %%
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 * #L%
25 */
26 package io.github.prolobjectlink.prolog;
27
28 import java.lang.reflect.Constructor;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import javax.script.ScriptEngine;
33 import javax.script.ScriptEngineManager;
34
35 /**
36 * Bootstrap platform class. Contains {@link #getProvider(Class)} method that
37 * return an instance of Prolog Provider from the given class. Alternatively can
38 * be used {@link #getProvider()} for create and return a {@link PrologProvider}
39 * using Java Platform discovery pattern.
40 *
41 * @author Jose Zalacain
42 * @since 1.0
43 */
44 public final class Prolog {
45
46 private Prolog() {
47 }
48
49 /**
50 * Create and return an instance of Prolog Provider using Java Platform
51 * discovery pattern.
52 *
53 * @return an instance of Prolog Provider from the given class.
54 * @since 1.1
55 */
56 public static PrologProvider getProvider() {
57 ScriptEngineManager manager = new ScriptEngineManager();
58 ScriptEngine engine = manager.getEngineByName("prolog");
59 return ((PrologScriptEngine) engine).getProvider();
60 }
61
62 /**
63 * Create and return an instance of Prolog Provider using the Prolog Provider
64 * class name.
65 *
66 * @return an instance of Prolog Provider from the given class.
67 * @since 1.1
68 */
69 public static PrologProvider getProvider(String className) {
70 PrologProvider provider = null;
71 try {
72 return getProvider(Class.forName(className));
73 } catch (ClassNotFoundException e) {
74 Logger.getLogger(Prolog.class.getName()).log(Level.FINEST, null, e);
75 }
76 return provider;
77 }
78
79 /**
80 * Create and return an instance of Prolog Provider from the given class.
81 *
82 * @param providerClass Prolog Provider class used to create a new instance.
83 * @return an instance of Prolog Provider from the given class.
84 * @since 1.0
85 */
86 public static PrologProvider getProvider(Class<?> providerClass) {
87 PrologProvider provider = null;
88 try {
89 Constructor<?> constructor = providerClass.getDeclaredConstructor();
90 constructor.setAccessible(true);
91 provider = (PrologProvider) providerClass.newInstance();
92 constructor.setAccessible(false);
93 } catch (InstantiationException e) {
94 Logger.getLogger(Prolog.class.getName()).log(Level.FINEST, null, e);
95 } catch (IllegalAccessException e) {
96 Logger.getLogger(Prolog.class.getName()).log(Level.FINEST, null, e);
97 } catch (NoSuchMethodException e) {
98 Logger.getLogger(Prolog.class.getName()).log(Level.FINEST, null, e);
99 } catch (SecurityException e) {
100 Logger.getLogger(Prolog.class.getName()).log(Level.FINEST, null, e);
101 }
102 return provider;
103 }
104
105 }