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.core.controller.command.mapper;
023:
024: import org.jboss.portal.core.controller.ControllerCommand;
025: import org.jboss.portal.core.controller.ControllerContext;
026: import org.jboss.portal.server.ServerInvocation;
027: import org.jboss.portal.server.ServerURL;
028:
029: import java.util.ArrayList;
030:
031: /**
032: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
033: * @version $Revision: 8786 $
034: */
035: public class DelegatingURLFactoryService extends AbstractURLFactory
036: implements DelegatingURLFactory {
037:
038: /** The next factory. */
039: private URLFactory nextFactory;
040:
041: /** The different delegates. */
042: private ArrayList delegates = new ArrayList();
043:
044: public URLFactory getNextFactory() {
045: return nextFactory;
046: }
047:
048: public void setNextFactory(URLFactory nextFactory) {
049: this .nextFactory = nextFactory;
050: }
051:
052: public ServerURL doMapping(ControllerContext controllerContext,
053: ServerInvocation invocation, ControllerCommand cmd) {
054: if (cmd == null) {
055: throw new IllegalArgumentException(
056: "No null command accepted");
057: }
058:
059: //
060: if (delegates != null) {
061: for (int i = 0; i < delegates.size(); i++) {
062: URLFactory delegate = (URLFactory) delegates.get(i);
063: ServerURL url = delegate.doMapping(controllerContext,
064: invocation, cmd);
065: if (url != null) {
066: return url;
067: }
068: }
069: }
070:
071: //
072: if (nextFactory != null) {
073: return nextFactory.doMapping(controllerContext, invocation,
074: cmd);
075: }
076:
077: //
078: return null;
079: }
080:
081: public void register(URLFactoryDelegate factory) {
082: if (factory == null) {
083: throw new IllegalArgumentException(
084: "Command class name must not be null");
085: }
086: synchronized (this ) {
087: if (delegates.contains(factory)) {
088: log.warn("Dual registration of URL factory " + factory);
089: } else {
090: ArrayList copy = new ArrayList(delegates);
091: copy.add(factory);
092: delegates = copy;
093: }
094: }
095: }
096:
097: public void unregister(URLFactoryDelegate factory) {
098: if (factory == null) {
099: throw new IllegalArgumentException(
100: "Command class name must not be null");
101: }
102: synchronized (this ) {
103: if (delegates.contains(factory)) {
104: log.warn("Unregistration of URL factory " + factory
105: + " failed because it is not registered");
106: } else {
107: ArrayList copy = new ArrayList(delegates);
108: copy.remove(factory);
109: delegates = copy;
110: }
111: }
112: }
113: }
|