001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.cms.impl.cache;
023:
024: import org.jboss.cache.CacheException;
025: import org.jboss.cache.TreeCacheMBean;
026: import org.jboss.logging.Logger;
027: import org.jboss.portal.cms.impl.CMSCacheException;
028: import org.jboss.portal.cms.impl.CMSTreeCacheService;
029: import org.jboss.portal.cms.model.File;
030: import org.jboss.portal.jems.as.JNDI;
031: import org.jboss.portal.jems.as.system.AbstractJBossService;
032:
033: import java.util.Locale;
034:
035: /**
036: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
037: * @version $Revision: 6728 $
038: */
039: public class CMSTreeCacheServiceImpl extends AbstractJBossService
040: implements CMSTreeCacheService {
041: private TreeCacheMBean cache;
042:
043: private Logger log = Logger
044: .getLogger(CMSTreeCacheServiceImpl.class);
045:
046: private String jndiName;
047:
048: protected JNDI.Binding jndiBinding;
049:
050: public CMSTreeCacheServiceImpl() {
051: }
052:
053: public String getJNDIName() {
054: return jndiName;
055: }
056:
057: public void setJNDIName(String jndiName) {
058: this .jndiName = jndiName;
059: }
060:
061: public File get(String path, Locale locale) {
062: if (path == null) {
063: throw new IllegalArgumentException("No null path accepted");
064: }
065: if (locale == null) {
066: throw new IllegalArgumentException(
067: "No null locale accepted");
068: }
069: try {
070: File file = (File) cache.get(path, locale);
071: if (file != null && log.isDebugEnabled()) {
072: log.debug("File " + path
073: + " taken from cache for locale "
074: + locale.getCountry() + "/"
075: + locale.getLanguage());
076: }
077: return (File) cache.get(path, locale);
078: } catch (CacheException e) {
079: e.printStackTrace();
080: return null;
081: }
082: }
083:
084: public void put(String path, Locale locale, File file) {
085: if (path == null) {
086: throw new IllegalArgumentException("No null path accepted");
087: }
088: if (locale == null) {
089: throw new IllegalArgumentException(
090: "No null locale accepted");
091: }
092: if (file == null) {
093: throw new IllegalArgumentException("No null file accepted");
094: }
095: try {
096: cache.put(path, locale, file);
097: } catch (CacheException e) {
098: e.printStackTrace();
099: }
100: }
101:
102: public void remove(String path, Locale locale)
103: throws CMSCacheException {
104: if (path == null) {
105: throw new IllegalArgumentException("No null path accepted");
106: }
107: if (locale == null) {
108: throw new IllegalArgumentException(
109: "No null locale accepted");
110: }
111: try {
112: cache.remove(path, locale);
113: } catch (CacheException e) {
114: throw new CMSCacheException(e);
115: }
116: }
117:
118: public void remove(String path) throws CMSCacheException {
119: if (path == null) {
120: throw new IllegalArgumentException("No null path accepted");
121: }
122: try {
123: cache.remove(path);
124: } catch (CacheException e) {
125: throw new CMSCacheException(e);
126: }
127: }
128:
129: public void startService() {
130: try {
131: log.info("Starting CMS cache service");
132:
133: //
134: if (jndiName != null) {
135: jndiBinding = new JNDI.Binding(jndiName, this );
136: jndiBinding.bind();
137: }
138:
139: log.info("CMS cache service started");
140: } catch (Exception e) {
141: log.info("CMS cache service failed to start");
142: e.printStackTrace();
143: }
144: }
145:
146: public void stopService() {
147: log.info("Stopping CMS cache service");
148:
149: if (jndiBinding != null) {
150: jndiBinding.unbind();
151: jndiBinding = null;
152: }
153:
154: log.info("CMS cache service stopped");
155: }
156:
157: public TreeCacheMBean getCache() {
158: return cache;
159: }
160:
161: public void setCache(TreeCacheMBean cache) {
162: this.cache = cache;
163: }
164: }
|