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.impl;
034:
035: import com.flexive.shared.cache.FxBackingCache;
036: import com.flexive.shared.cache.FxCacheException;
037: import com.flexive.shared.mbeans.MBeanHelper;
038: import org.apache.commons.lang.StringUtils;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.jboss.cache.CacheException;
042: import org.jboss.cache.Fqn;
043: import org.jboss.cache.jmx.CacheJmxWrapperMBean;
044:
045: import javax.management.MBeanServerInvocationHandler;
046: import javax.management.ObjectName;
047:
048: /**
049: * FxBackingCache Provider for a JBossCache instance registered via an external -service.xml deployment
050: *
051: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
052: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
053: *
054: */
055: public class FxJBossExternalCacheProvider extends
056: AbstractBackingCacheProvider<FxJBossTreeCacheMBeanWrapper> {
057: private static transient Log LOG = LogFactory
058: .getLog(FxJBossExternalCacheProvider.class);
059:
060: /**
061: * {@inheritDoc}
062: */
063: public String getDescription() {
064: return getClass().getName();
065: }
066:
067: /**
068: * {@inheritDoc}
069: */
070: public void init() throws FxCacheException {
071: if (cache != null)
072: return;
073: try {
074: // first check if the cache MBean exists
075: MBeanHelper
076: .locateServer()
077: .getMBeanInfo(
078: new ObjectName(
079: "jboss.cache:service=JNDITreeCache"));
080: // create wrapper MBean (cast necessary for java 1.5)
081: final CacheJmxWrapperMBean wrapper = (CacheJmxWrapperMBean) MBeanServerInvocationHandler
082: .newProxyInstance(
083: MBeanHelper.locateServer(),
084: new ObjectName(
085: "jboss.cache:service=JNDITreeCache"),
086: CacheJmxWrapperMBean.class, false);
087: cache = new FxJBossTreeCacheMBeanWrapper(wrapper);
088: evictChildren(""); // clean up possible leftovers from previous deployment
089: LOG.trace(Fqn.class);
090: } catch (Exception e) {
091: throw new FxCacheException(e);
092: }
093: }
094:
095: /**
096: * {@inheritDoc}
097: */
098: public void shutdown() throws FxCacheException {
099: //do nothing since we just "use" a cache and dont provide it
100: }
101:
102: private void evictChildren(String fqn) throws CacheException {
103: final CacheJmxWrapperMBean<Object, Object> treeCache = cache
104: .getCacheWrapper();
105: if (StringUtils.isNotBlank(fqn)) {
106: // evict local cache entry
107: if (LOG.isInfoEnabled()) {
108: LOG.info("Evicting " + fqn);
109: }
110: treeCache.getCache().evict(Fqn.fromString(fqn), true);
111: }
112: // also evict children
113: // final Set childrenNames = treeCache.getChildrenNames(fqn);
114: // if (childrenNames != null) {
115: // for (Object childFqn: childrenNames) {
116: // evictChildren(fqn + "/" + childFqn);
117: // }
118: // }
119: }
120:
121: /**
122: * {@inheritDoc}
123: */
124: public FxBackingCache getInstance() throws FxCacheException {
125: return cache;
126: }
127: }
|