001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.cache;
034:
035: import com.flexive.shared.cache.impl.FxJBossEmbeddedCacheProvider;
036: import com.flexive.shared.cache.impl.FxJBossExternalCacheProvider;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * Factory class to create FxBackingCache providers
042: *
043: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
044: */
045: public class FxBackingCacheProviderFactory {
046:
047: private static transient Log LOG = LogFactory
048: .getLog(FxBackingCacheProviderFactory.class);
049:
050: /**
051: * Name of the environment property to set to force a provider
052: */
053: public final static String KEY = FxBackingCacheProvider.class
054: .getCanonicalName();
055:
056: /**
057: * Factory method to create a new FxBackingCacheProvider
058: * <p/>
059: * Strategy used in this order in case of failure:
060: * <ol>
061: * <li>if System property is set try to obtain a new instance of the given class</li>
062: * <li>try to get a JNDI TreeCache instance with key "FxJBossTreeCache"</li>
063: * <li>create a local TreeCache MBean</li>
064: * </ol>
065: *
066: * @return FxBackingCacheProvider
067: */
068: public static FxBackingCacheProvider createNew() {
069: String provider = System.getProperty(KEY);
070: FxBackingCacheProvider instance = null;
071: try {
072: if (provider != null) {
073: //try provided class
074: try {
075: instance = (FxBackingCacheProvider) Class.forName(
076: provider).newInstance();
077: return instance;
078: } catch (InstantiationException e) {
079: LOG.error("Failed to instantiate " + provider
080: + ": " + e.getMessage(), e);
081: } catch (IllegalAccessException e) {
082: LOG.error(e.getMessage(), e);
083: } catch (ClassNotFoundException e) {
084: LOG.error("Could not find class " + provider + ": "
085: + e.getMessage(), e);
086: }
087: }
088: try {
089: instance = new FxJBossExternalCacheProvider();
090: instance.init();
091: return instance;
092: } catch (FxCacheException e) {
093: LOG
094: .info("Failed to instantiate FxJBossExternalCacheProvider: "
095: + e.getMessage());
096: }
097: instance = new FxJBossEmbeddedCacheProvider();
098: } finally {
099: if (instance != null)
100: LOG.info("Using FxBackingCacheProvider instance "
101: + instance.getClass().getCanonicalName());
102: else
103: LOG
104: .fatal("Failed to create a FxBackingCacheProvider instance!");
105: }
106: return instance;
107: }
108:
109: }
|