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.servlet.PathMapping;
028: import org.jboss.portal.server.servlet.PathMappingResult;
029: import org.jboss.portal.server.servlet.PathParser;
030:
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: /**
035: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
036: * @version $Revision: 8786 $
037: */
038: public class DelegatingCommandFactoryService extends
039: AbstractCommandFactory implements DelegatingCommandFactory {
040:
041: private PathParser parser;
042: private CommandFactory nextFactory;
043: private Map factories;
044: private PathMapping mapping;
045:
046: public DelegatingCommandFactoryService() {
047: factories = new HashMap();
048: parser = new PathParser();
049: mapping = new PathMapping() {
050: public Object getRoot() {
051: return this ;
052: }
053:
054: public Object getChild(Object parent, String name) {
055: if (parent == this ) {
056: CommandFactory factory = (CommandFactory) factories
057: .get(name);
058: if (factory != null) {
059: return factory;
060: }
061: return factories.get("");
062: }
063: return null;
064: }
065: };
066: }
067:
068: public CommandFactory getNextFactory() {
069: return nextFactory;
070: }
071:
072: public void setNextFactory(CommandFactory nextFactory) {
073: this .nextFactory = nextFactory;
074: }
075:
076: public void register(String path, CommandFactory factory) {
077: if (path == null) {
078: throw new IllegalArgumentException("no path");
079: }
080: if (factory == null) {
081: throw new IllegalArgumentException("no mapper");
082: }
083: synchronized (this ) {
084: path = path.substring(1);
085: if (factories.containsKey(path)) {
086: throw new IllegalArgumentException(
087: "path already registered");
088: }
089: Map copy = new HashMap(factories);
090: copy.put(path, factory);
091: factories = copy;
092: }
093: }
094:
095: public void unregister(String path) {
096: if (path == null) {
097: throw new IllegalArgumentException("no path");
098: }
099: synchronized (this ) {
100: path = path.substring(1);
101: if (!factories.containsKey(path)) {
102: throw new IllegalArgumentException(
103: "path not registered");
104: }
105: Map copy = new HashMap(factories);
106: copy.remove(path);
107: factories = copy;
108: }
109: }
110:
111: public ControllerCommand doMapping(
112: ControllerContext controllerContext,
113: ServerInvocation invocation, String host,
114: String contextPath, String requestPath) {
115: //
116: if (requestPath.length() == 0) {
117: return null;
118: }
119:
120: //
121: PathMappingResult result = parser.map(mapping, requestPath);
122: Object target = result.getTarget();
123: ControllerCommand cmd = null;
124: if (target instanceof CommandFactory) {
125: CommandFactory delegate = (CommandFactory) target;
126: String remainingPath = result.getRemainingPath();
127: cmd = delegate.doMapping(controllerContext, invocation,
128: host, contextPath + result.getMatchedPath(),
129: remainingPath);
130: }
131:
132: //
133: if (cmd == null && nextFactory != null) {
134: cmd = nextFactory.doMapping(controllerContext, invocation,
135: host, contextPath, requestPath);
136: }
137:
138: return cmd;
139: }
140: }
|