001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.startup;
019:
020: import org.apache.tomcat.util.digester.Digester;
021: import org.apache.tomcat.util.digester.RuleSetBase;
022: import java.lang.reflect.Constructor;
023:
024: import org.apache.juli.logging.Log;
025: import org.apache.juli.logging.LogFactory;
026:
027: import java.lang.reflect.InvocationTargetException;
028:
029: public class ClusterRuleSetFactory {
030:
031: public static Log log = LogFactory
032: .getLog(ClusterRuleSetFactory.class);
033:
034: public static RuleSetBase getClusterRuleSet(String prefix) {
035:
036: //OLD CLUSTER 1
037: //first try the same classloader as this class server/lib
038: try {
039: return loadRuleSet(prefix,
040: "org.apache.catalina.cluster.ClusterRuleSet",
041: ClusterRuleSetFactory.class.getClassLoader());
042: } catch (Exception x) {
043: //display warning
044: if (log.isDebugEnabled())
045: log
046: .debug("Unable to load ClusterRuleSet (org.apache.catalina.cluster.ClusterRuleSet), falling back on context classloader");
047: }
048: //try to load it from the context class loader
049: try {
050: return loadRuleSet(prefix,
051: "org.apache.catalina.cluster.ClusterRuleSet",
052: Thread.currentThread().getContextClassLoader());
053: } catch (Exception x) {
054: //display warning
055: if (log.isDebugEnabled())
056: log
057: .debug("Unable to load ClusterRuleSet (org.apache.catalina.cluster.ClusterRuleSet), will try to load the HA cluster");
058: }
059:
060: //NEW CLUSTER 2
061: //first try the same classloader as this class server/lib
062: try {
063: return loadRuleSet(prefix,
064: "org.apache.catalina.ha.ClusterRuleSet",
065: ClusterRuleSetFactory.class.getClassLoader());
066: } catch (Exception x) {
067: //display warning
068: if (log.isDebugEnabled())
069: log
070: .debug("Unable to load HA ClusterRuleSet (org.apache.catalina.ha.ClusterRuleSet), falling back on context classloader");
071: }
072: //try to load it from the context class loader
073: try {
074: return loadRuleSet(prefix,
075: "org.apache.catalina.ha.ClusterRuleSet", Thread
076: .currentThread().getContextClassLoader());
077: } catch (Exception x) {
078: //display warning
079: if (log.isDebugEnabled())
080: log
081: .debug("Unable to load HA ClusterRuleSet (org.apache.catalina.ha.ClusterRuleSet), falling back on DefaultClusterRuleSet");
082: }
083:
084: log
085: .info("Unable to find a cluster rule set in the classpath. Will load the default rule set.");
086: return new DefaultClusterRuleSet(prefix);
087: }
088:
089: protected static RuleSetBase loadRuleSet(String prefix,
090: String className, ClassLoader cl)
091: throws ClassNotFoundException, InstantiationException,
092: NoSuchMethodException, IllegalAccessException,
093: InvocationTargetException {
094: Class clazz = Class.forName(className, true, cl);
095: Constructor cons = clazz
096: .getConstructor(new Class[] { String.class });
097: return (RuleSetBase) cons.newInstance(prefix);
098: }
099:
100: /**
101: * <p><strong>RuleSet</strong> for processing the contents of a
102: * Cluster definition element. </p>
103: *
104: * @author Filip Hanik
105: * @author Peter Rossbach
106: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
107: */
108:
109: public static class DefaultClusterRuleSet extends RuleSetBase {
110:
111: // ----------------------------------------------------- Instance Variables
112:
113: /**
114: * The matching pattern prefix to use for recognizing our elements.
115: */
116: protected String prefix = null;
117:
118: // ------------------------------------------------------------ Constructor
119:
120: /**
121: * Construct an instance of this <code>RuleSet</code> with the default
122: * matching pattern prefix.
123: */
124: public DefaultClusterRuleSet() {
125:
126: this ("");
127:
128: }
129:
130: /**
131: * Construct an instance of this <code>RuleSet</code> with the specified
132: * matching pattern prefix.
133: *
134: * @param prefix Prefix for matching pattern rules (including the
135: * trailing slash character)
136: */
137: public DefaultClusterRuleSet(String prefix) {
138: super ();
139: this .namespaceURI = null;
140: this .prefix = prefix;
141: }
142:
143: // --------------------------------------------------------- Public Methods
144:
145: /**
146: * <p>Add the set of Rule instances defined in this RuleSet to the
147: * specified <code>Digester</code> instance, associating them with
148: * our namespace URI (if any). This method should only be called
149: * by a Digester instance.</p>
150: *
151: * @param digester Digester instance to which the new Rule instances
152: * should be added.
153: */
154: public void addRuleInstances(Digester digester) {
155: //Cluster configuration start
156: digester.addObjectCreate(prefix + "Membership", null, // MUST be specified in the element
157: "className");
158: digester.addSetProperties(prefix + "Membership");
159: digester.addSetNext(prefix + "Membership",
160: "setMembershipService",
161: "org.apache.catalina.cluster.MembershipService");
162:
163: digester.addObjectCreate(prefix + "Sender", null, // MUST be specified in the element
164: "className");
165: digester.addSetProperties(prefix + "Sender");
166: digester.addSetNext(prefix + "Sender", "setClusterSender",
167: "org.apache.catalina.cluster.ClusterSender");
168:
169: digester.addObjectCreate(prefix + "Receiver", null, // MUST be specified in the element
170: "className");
171: digester.addSetProperties(prefix + "Receiver");
172: digester.addSetNext(prefix + "Receiver",
173: "setClusterReceiver",
174: "org.apache.catalina.cluster.ClusterReceiver");
175:
176: digester.addObjectCreate(prefix + "Valve", null, // MUST be specified in the element
177: "className");
178: digester.addSetProperties(prefix + "Valve");
179: digester.addSetNext(prefix + "Valve", "addValve",
180: "org.apache.catalina.Valve");
181:
182: digester.addObjectCreate(prefix + "Deployer", null, // MUST be specified in the element
183: "className");
184: digester.addSetProperties(prefix + "Deployer");
185: digester.addSetNext(prefix + "Deployer",
186: "setClusterDeployer",
187: "org.apache.catalina.cluster.ClusterDeployer");
188:
189: digester.addObjectCreate(prefix + "Listener", null, // MUST be specified in the element
190: "className");
191: digester.addSetProperties(prefix + "Listener");
192: digester.addSetNext(prefix + "Listener",
193: "addLifecycleListener",
194: "org.apache.catalina.LifecycleListener");
195:
196: digester.addObjectCreate(prefix + "ClusterListener", null, // MUST be specified in the element
197: "className");
198: digester.addSetProperties(prefix + "ClusterListener");
199: digester.addSetNext(prefix + "ClusterListener",
200: "addClusterListener",
201: "org.apache.catalina.cluster.MessageListener");
202: //Cluster configuration end
203: }
204:
205: }
206: }
|