PageRenderTime 9ms CodeModel.GetById 0ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/jpa/core/src/main/java/org/jboss/as/jpa/persistenceprovider/PersistenceProviderLoader.java

#
Java | 72 lines | 24 code | 9 blank | 39 comment | 3 complexity | 77a165efb45e8077d8a27870ce04650a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
 1/*
 2 * JBoss, Home of Professional Open Source.
 3 * Copyright 2011, Red Hat, Inc., and individual contributors
 4 * as indicated by the @author tags. See the copyright.txt file in the
 5 * distribution for a full listing of individual contributors.
 6 *
 7 * This is free software; you can redistribute it and/or modify it
 8 * under the terms of the GNU Lesser General Public License as
 9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22
23package org.jboss.as.jpa.persistenceprovider;
24
25import java.util.ServiceLoader;
26
27import javax.persistence.spi.PersistenceProvider;
28
29import org.jboss.as.jpa.config.Configuration;
30import org.jboss.modules.Module;
31import org.jboss.modules.ModuleIdentifier;
32import org.jboss.modules.ModuleLoadException;
33import org.jboss.modules.ModuleLoader;
34
35/**
36 * For loading persistence provider modules
37 *
38 * @author Scott Marlow
39 */
40public class PersistenceProviderLoader {
41
42    /**
43     * pre-loads the default persistence provider
44     *
45     * @throws ModuleLoadException
46     */
47    public static void loadDefaultProvider() throws ModuleLoadException {
48        loadProviderModuleByName(Configuration.PROVIDER_MODULE_DEFAULT);
49    }
50
51    /**
52     * Loads the specified JPA persistence provider module name
53     *
54     * @param moduleName
55     * @throws ModuleLoadException
56     */
57    public static void loadProviderModuleByName(String moduleName) throws ModuleLoadException {
58        final ModuleLoader moduleLoader = Module.getBootModuleLoader();
59        Module module = moduleLoader.loadModule(ModuleIdentifier.fromString(moduleName));
60        final ServiceLoader<PersistenceProvider> serviceLoader =
61            module.loadService(PersistenceProvider.class);
62        if (serviceLoader != null) {
63            for (PersistenceProvider provider1 : serviceLoader) {
64                // persistence provider jar may contain multiple provider service implementations
65                // save each provider
66                PersistenceProviderResolverImpl.getInstance().addPersistenceProvider(provider1);
67            }
68        }
69    }
70
71}
72