KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > ClusterRuleSetFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.startup;
20
21
22 import org.apache.tomcat.util.digester.Digester;
23 import org.apache.tomcat.util.digester.RuleSetBase;
24 import java.lang.reflect.Constructor JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 public class ClusterRuleSetFactory {
31     
32     public static Log log = LogFactory.getLog(ClusterRuleSetFactory.class);
33     
34     public static RuleSetBase getClusterRuleSet(String JavaDoc prefix) {
35         
36         //OLD CLUSTER 1
37
//first try the same classloader as this class server/lib
38
try {
39             return loadRuleSet(prefix,"org.apache.catalina.cluster.ClusterRuleSet",ClusterRuleSetFactory.class.getClassLoader());
40         } catch ( Exception JavaDoc x ) {
41             //display warning
42
if ( log.isDebugEnabled() ) log.debug("Unable to load ClusterRuleSet (org.apache.catalina.cluster.ClusterRuleSet), falling back on context classloader");
43         }
44         //try to load it from the context class loader
45
try {
46             return loadRuleSet(prefix,"org.apache.catalina.cluster.ClusterRuleSet",Thread.currentThread().getContextClassLoader());
47         } catch ( Exception JavaDoc x ) {
48             //display warning
49
if ( log.isDebugEnabled() ) log.debug("Unable to load ClusterRuleSet (org.apache.catalina.cluster.ClusterRuleSet), will try to load the HA cluster");
50         }
51         
52         //NEW CLUSTER 2
53
//first try the same classloader as this class server/lib
54
try {
55             return loadRuleSet(prefix,"org.apache.catalina.ha.ClusterRuleSet",ClusterRuleSetFactory.class.getClassLoader());
56         } catch ( Exception JavaDoc x ) {
57             //display warning
58
if ( log.isDebugEnabled() ) log.debug("Unable to load HA ClusterRuleSet (org.apache.catalina.ha.ClusterRuleSet), falling back on context classloader");
59         }
60         //try to load it from the context class loader
61
try {
62             return loadRuleSet(prefix,"org.apache.catalina.ha.ClusterRuleSet",Thread.currentThread().getContextClassLoader());
63         } catch ( Exception JavaDoc x ) {
64             //display warning
65
if ( log.isDebugEnabled() ) log.debug("Unable to load HA ClusterRuleSet (org.apache.catalina.ha.ClusterRuleSet), falling back on DefaultClusterRuleSet");
66         }
67
68         log.info("Unable to find a cluster rule set in the classpath. Will load the default rule set.");
69         return new DefaultClusterRuleSet(prefix);
70     }
71     
72     
73     protected static RuleSetBase loadRuleSet(String JavaDoc prefix, String JavaDoc className, ClassLoader JavaDoc cl)
74         throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc,
75                NoSuchMethodException JavaDoc,IllegalAccessException JavaDoc,
76                InvocationTargetException JavaDoc {
77         Class JavaDoc clazz = Class.forName(className,true,cl);
78         Constructor JavaDoc cons = clazz.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
79         return (RuleSetBase)cons.newInstance(prefix);
80     }
81     
82     /**
83      * <p><strong>RuleSet</strong> for processing the contents of a
84      * Cluster definition element. </p>
85      *
86      * @author Filip Hanik
87      * @author Peter Rossbach
88      * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
89      */

90
91     public static class DefaultClusterRuleSet extends RuleSetBase {
92
93
94         // ----------------------------------------------------- Instance Variables
95

96
97         /**
98          * The matching pattern prefix to use for recognizing our elements.
99          */

100         protected String JavaDoc prefix = null;
101
102
103         // ------------------------------------------------------------ Constructor
104

105
106         /**
107          * Construct an instance of this <code>RuleSet</code> with the default
108          * matching pattern prefix.
109          */

110         public DefaultClusterRuleSet() {
111
112             this("");
113
114         }
115
116
117         /**
118          * Construct an instance of this <code>RuleSet</code> with the specified
119          * matching pattern prefix.
120          *
121          * @param prefix Prefix for matching pattern rules (including the
122          * trailing slash character)
123          */

124         public DefaultClusterRuleSet(String JavaDoc prefix) {
125             super();
126             this.namespaceURI = null;
127             this.prefix = prefix;
128         }
129
130
131         // --------------------------------------------------------- Public Methods
132

133
134         /**
135          * <p>Add the set of Rule instances defined in this RuleSet to the
136          * specified <code>Digester</code> instance, associating them with
137          * our namespace URI (if any). This method should only be called
138          * by a Digester instance.</p>
139          *
140          * @param digester Digester instance to which the new Rule instances
141          * should be added.
142          */

143         public void addRuleInstances(Digester digester) {
144             //Cluster configuration start
145
digester.addObjectCreate(prefix + "Membership",
146                                      null, // MUST be specified in the element
147
"className");
148             digester.addSetProperties(prefix + "Membership");
149             digester.addSetNext(prefix + "Membership",
150                                 "setMembershipService",
151                                 "org.apache.catalina.cluster.MembershipService");
152
153             digester.addObjectCreate(prefix + "Sender",
154                                      null, // MUST be specified in the element
155
"className");
156             digester.addSetProperties(prefix + "Sender");
157             digester.addSetNext(prefix + "Sender",
158                                 "setClusterSender",
159                                 "org.apache.catalina.cluster.ClusterSender");
160
161             digester.addObjectCreate(prefix + "Receiver",
162                                      null, // MUST be specified in the element
163
"className");
164             digester.addSetProperties(prefix + "Receiver");
165             digester.addSetNext(prefix + "Receiver",
166                                 "setClusterReceiver",
167                                 "org.apache.catalina.cluster.ClusterReceiver");
168
169             digester.addObjectCreate(prefix + "Valve",
170                                      null, // MUST be specified in the element
171
"className");
172             digester.addSetProperties(prefix + "Valve");
173             digester.addSetNext(prefix + "Valve",
174                                 "addValve",
175                                 "org.apache.catalina.Valve");
176
177             digester.addObjectCreate(prefix + "Deployer",
178                                      null, // MUST be specified in the element
179
"className");
180             digester.addSetProperties(prefix + "Deployer");
181             digester.addSetNext(prefix + "Deployer",
182                                 "setClusterDeployer",
183                                 "org.apache.catalina.cluster.ClusterDeployer");
184
185             digester.addObjectCreate(prefix + "Listener",
186                     null, // MUST be specified in the element
187
"className");
188             digester.addSetProperties(prefix + "Listener");
189             digester.addSetNext(prefix + "Listener",
190                                 "addLifecycleListener",
191                                 "org.apache.catalina.LifecycleListener");
192
193             digester.addObjectCreate(prefix + "ClusterListener",
194                     null, // MUST be specified in the element
195
"className");
196             digester.addSetProperties(prefix + "ClusterListener");
197             digester.addSetNext(prefix + "ClusterListener",
198                                 "addClusterListener",
199                                 "org.apache.catalina.cluster.MessageListener");
200             //Cluster configuration end
201
}
202
203
204     }
205 }
206
Popular Tags