001: /**********************************************************************
002: Copyright (c) 2006 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:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.plugin;
019:
020: import org.jpox.ObjectManagerFactoryImpl;
021: import org.jpox.ClassLoaderResolver;
022: import org.jpox.PersistenceConfiguration;
023: import org.jpox.util.JPOXLogger;
024: import org.jpox.util.JavaUtils;
025: import org.jpox.util.Localiser;
026:
027: /**
028: * Factory for PluginRegistry.
029: * Creates an instance of PluginRegistry based on the available
030: * PluginRegistry implementation in the JPOX Core classpath
031: */
032: public class PluginRegistryFactory {
033: protected static final Localiser LOCALISER = Localiser
034: .getInstance("org.jpox.Localisation");
035:
036: /**
037: * Instantiates a PluginRegistry. If the JPOX Core is deployed as an
038: * eclipse plugin, uses the Eclipse OSGI Registry to find other JPOX plug-ins
039: * @param conf the PersistenceConfiguration
040: * @param clr the ClassLoaderResolver
041: * @return instance of the PluginRegistry
042: */
043: public static PluginRegistry newPluginRegistry(
044: PersistenceConfiguration conf, ClassLoaderResolver clr) {
045: PluginRegistry registry = null;
046: if (conf.getPluginRegistryClassName() != null) {
047: registry = newInstance(conf.getPluginRegistryClassName(),
048: conf.getPluginRegistryClassName(), clr);
049: }
050: if (registry != null) {
051: if (JPOXLogger.PLUGIN.isDebugEnabled()) {
052: JPOXLogger.PLUGIN.debug("Using PluginRegistry "
053: + registry.getClass().getName());
054: }
055: return registry;
056: }
057: registry = newInstance(
058: "org.eclipse.core.runtime.RegistryFactory",
059: "org.jpox.plugin.EclipsePluginRegistry", clr);
060: if (registry != null) {
061: if (JPOXLogger.PLUGIN.isDebugEnabled()) {
062: JPOXLogger.PLUGIN.debug("Using PluginRegistry "
063: + registry.getClass().getName());
064: }
065: return registry;
066: }
067:
068: if (JPOXLogger.PLUGIN.isDebugEnabled()) {
069: JPOXLogger.PLUGIN.debug("Using PluginRegistry "
070: + NonManagedPluginRegistry.class.getName());
071: }
072: return new NonManagedPluginRegistry(clr, conf
073: .getPluginRegistryBundleCheck());
074: }
075:
076: /**
077: * Instantiates a PluginRegistry. Only proceed if the testClass is found in
078: * the classpath
079: * @param testClass A test class
080: * @param pluginRegistryClass The class that implements
081: * {@link PluginRegistry}
082: * @return instance of the PluginRegistry
083: */
084: private static PluginRegistry newInstance(String testClass,
085: String pluginRegistryClass, ClassLoaderResolver clr) {
086: try {
087: if (clr.classForName(testClass,
088: ObjectManagerFactoryImpl.class.getClassLoader()) == null) {
089: // Just treat all exceptions the same since this registry doesnt work either way
090: if (JPOXLogger.PLUGIN.isDebugEnabled()) {
091: JPOXLogger.PLUGIN.debug(LOCALISER.msg("024005",
092: pluginRegistryClass));
093: }
094: }
095: return (PluginRegistry) clr.classForName(
096: pluginRegistryClass,
097: ObjectManagerFactoryImpl.class.getClassLoader())
098: .getConstructor(
099: new Class[] { ClassLoaderResolver.class })
100: .newInstance(new Object[] { clr });
101: } catch (Exception e) {
102: // Just treat all exceptions the same since this registry doesnt work either way
103: if (JPOXLogger.PLUGIN.isDebugEnabled()) {
104: if (JavaUtils.isJRE1_4OrAbove()) {
105: // Exception.getCause only in JDK1.4+
106: JPOXLogger.PLUGIN.debug(LOCALISER.msg("024006",
107: pluginRegistryClass,
108: e.getCause() != null ? e.getCause()
109: .getMessage() : e.getMessage()));
110: } else {
111: JPOXLogger.PLUGIN.debug(LOCALISER.msg("024006",
112: pluginRegistryClass, e.getMessage()));
113: }
114: }
115: }
116: return null;
117: }
118: }
|