View Javadoc

1   /**
2    *
3    * Copyright 2003-2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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  package org.apache.geronimo.tomcat;
18  
19  import java.util.Map;
20  
21  import org.apache.catalina.Valve;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.geronimo.gbean.GBeanInfo;
25  import org.apache.geronimo.gbean.GBeanInfoBuilder;
26  import org.apache.geronimo.gbean.GBeanLifecycle;
27  
28  /**
29   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
30   */
31  public class ValveGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
32  
33      private static final Log log = LogFactory.getLog(ValveGBean.class);
34  
35      public static final String J2EE_TYPE = "TomcatValve";
36          
37      private final Valve valve;
38      private final ValveGBean nextValve;
39      private final String className;
40   
41      
42      public ValveGBean(){      
43          valve = null;
44          nextValve = null;
45          className = null;
46      }
47      
48      public ValveGBean(String className, Map initParams, ValveGBean nextValve) throws Exception{
49  
50          //Validate
51          if (className == null){
52              throw new IllegalArgumentException("className cannot be null.");
53          }
54          
55          if (nextValve != null){
56              if (!(nextValve.getInternalObject() instanceof Valve)){
57                  throw new IllegalArgumentException("The class given as the NextValve attribute does not wrap an object of org.apache.catalina.Valve type.");                
58              }
59              this.nextValve = nextValve;
60          } else {
61              this.nextValve = null;
62          }
63          
64          this.className = className;
65          
66          //Create the Valve object
67          valve = (Valve)Class.forName(className).newInstance();
68  
69          //Set the parameters
70          setParameters(valve, initParams);
71          
72      }
73      
74      public void doStart() throws Exception {
75          log.debug(className + " started.");
76      }
77  
78      public void doStop() throws Exception {
79          log.debug(className + " stopped.");
80      }
81  
82      public void doFail() {
83          log.warn(className + " failed.");
84      }
85  
86      public Object getInternalObject() {
87          return valve;
88      }
89  
90      public ValveGBean getNextValve() {
91          return nextValve;
92      }
93      
94      public static final GBeanInfo GBEAN_INFO;
95  
96      static {
97          GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ValveGBean.class, J2EE_TYPE);
98          infoFactory.addAttribute("className", String.class, true);
99          infoFactory.addAttribute("initParams", Map.class, true);
100         infoFactory.addReference("NextValve", ValveGBean.class, J2EE_TYPE);
101         infoFactory.addOperation("getInternalObject");
102         infoFactory.addOperation("getNextValve");
103         infoFactory.setConstructor(new String[] { "className", "initParams", "NextValve" });
104         GBEAN_INFO = infoFactory.getBeanInfo();
105     }
106 
107     public static GBeanInfo getGBeanInfo() {
108         return GBEAN_INFO;
109     }
110 
111 }