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.wsrp.services;
023:
024: import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
025: import org.jboss.portal.jems.as.system.AbstractJBossService;
026:
027: import java.util.Map;
028:
029: /**
030: * A service factory that statically cache implementations. It is mainly used in the test environment to void the very
031: * expensive creation of SOAP service proxies.
032: *
033: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
034: * @version $Revision: 8784 $
035: */
036: public class CachingServiceFactory extends AbstractJBossService
037: implements ServiceFactory {
038:
039: /** . */
040: private static final Map cache = new ConcurrentReaderHashMap();
041:
042: /** . */
043: private ServiceFactory delegate;
044:
045: public Object getService(Class clazz) throws Exception {
046: if (delegate == null) {
047: throw new IllegalStateException(
048: "No delegate service factory");
049: }
050: if (clazz == null) {
051: throw new IllegalArgumentException();
052: }
053:
054: //
055: Object service = cache.get(clazz.getName());
056: if (service == null) {
057: service = delegate.getService(clazz);
058: if (service != null) {
059: cache.put(clazz.getName(), service);
060: }
061: }
062: return service;
063: }
064:
065: public ServiceFactory getDelegate() {
066: return delegate;
067: }
068:
069: public void setDelegate(ServiceFactory delegate) {
070: this .delegate = delegate;
071: }
072:
073: public boolean isAvailable() {
074: return delegate != null && delegate.isAvailable();
075: }
076:
077: public boolean isFailed() {
078: return delegate == null || delegate.isFailed();
079: }
080:
081: public String getServiceDescriptionURL() {
082: if (delegate != null) {
083: return delegate.getServiceDescriptionURL();
084: }
085: return null;
086: }
087:
088: public String getMarkupURL() {
089: if (delegate != null) {
090: return delegate.getMarkupURL();
091: }
092: return null;
093: }
094:
095: public String getRegistrationURL() {
096: if (delegate != null) {
097: return delegate.getRegistrationURL();
098: }
099: return null;
100: }
101:
102: public String getPortletManagementURL() {
103: if (delegate != null) {
104: return delegate.getPortletManagementURL();
105: }
106: return null;
107: }
108:
109: public void setServiceDescriptionURL(String serviceDescriptionURL) {
110: if (delegate == null) {
111: throw new IllegalStateException(
112: "No delegate service factory");
113: }
114: delegate.setServiceDescriptionURL(serviceDescriptionURL);
115: }
116:
117: public void setMarkupURL(String markupURL) {
118: if (delegate == null) {
119: throw new IllegalStateException(
120: "No delegate service factory");
121: }
122: delegate.setMarkupURL(markupURL);
123: }
124:
125: public void setRegistrationURL(String registrationURL) {
126: if (delegate == null) {
127: throw new IllegalStateException(
128: "No delegate service factory");
129: }
130: delegate.setRegistrationURL(registrationURL);
131: }
132:
133: public void setPortletManagementURL(String portletManagementURL) {
134: if (delegate == null) {
135: throw new IllegalStateException(
136: "No delegate service factory");
137: }
138: delegate.setPortletManagementURL(portletManagementURL);
139: }
140: }
|