001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.rewrite;
031:
032: import com.caucho.config.program.ContainerProgram;
033: import com.caucho.config.program.ConfigProgram;
034: import com.caucho.config.*;
035: import com.caucho.server.dispatch.DispatchServer;
036: import com.caucho.server.cluster.Server;
037: import com.caucho.server.webapp.WebApp;
038: import com.caucho.util.L10N;
039:
040: import javax.annotation.PostConstruct;
041: import javax.annotation.PreDestroy;
042: import javax.servlet.FilterChain;
043: import javax.servlet.ServletException;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046: import java.util.regex.Pattern;
047:
048: /**
049: * Configuration for a rewrite-dispatch
050: */
051: public class RewriteDispatch {
052: private static final L10N L = new L10N(RewriteDispatch.class);
053: private static final Logger log = Logger
054: .getLogger(RewriteDispatch.class.getName());
055:
056: private final WebApp _webApp;
057: private final Server _server;
058:
059: private MatchRule _matchRule;
060:
061: private ContainerProgram _program = new ContainerProgram();
062:
063: private final boolean _isFiner;
064: private final boolean _isFinest;
065:
066: public RewriteDispatch(Server server) {
067: this (server, null);
068: }
069:
070: public RewriteDispatch(WebApp webApp) {
071: this (null, webApp);
072: }
073:
074: private RewriteDispatch(Server server, WebApp webApp) {
075: _server = server;
076: _webApp = webApp;
077:
078: _isFiner = log.isLoggable(Level.FINER);
079: _isFinest = log.isLoggable(Level.FINEST);
080: }
081:
082: public WebApp getWebApp() {
083: return _webApp;
084: }
085:
086: /**
087: * Adds to the builder program.
088: */
089: public void addBuilderProgram(ConfigProgram program) {
090: _program.addProgram(program);
091: }
092:
093: @PostConstruct
094: public void init() {
095: _matchRule = new MatchRule(this );
096: _matchRule.setRegexp(Pattern.compile(".*"));
097:
098: _program.configure(_matchRule);
099:
100: _matchRule.init();
101: }
102:
103: public FilterChain map(String uri, String queryString,
104: FilterChain next) throws ServletException {
105: if (_isFinest)
106: log.finest("rewrite-dispatch check uri '" + uri + "'");
107:
108: if (_matchRule == null || _matchRule.isModified()) {
109: if (_matchRule != null)
110: _matchRule.destroy();
111:
112: _matchRule = new MatchRule(this );
113: _matchRule.setRegexp(Pattern.compile(".*"));
114:
115: _program.configure(_matchRule);
116:
117: _matchRule.init();
118: }
119:
120: return _matchRule.map(uri, queryString, next);
121: }
122:
123: public void clearCache() {
124: if (_webApp != null)
125: _webApp.clearCache();
126: else if (_server != null)
127: _server.clearCache();
128: }
129:
130: /*
131: @PreDestroy
132: public void destroy()
133: {
134: _matchRule.destroy();
135: }
136: */
137: }
|