KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > core > JkHandler


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

16
17 package org.apache.jk.core;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.management.MBeanRegistration JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.Notification JavaDoc;
25 import javax.management.NotificationListener JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import org.apache.commons.modeler.Registry;
29
30 /**
31  *
32  * @author Costin Manolache
33  */

34 public class JkHandler implements MBeanRegistration JavaDoc, NotificationListener JavaDoc {
35     public static final int OK=0;
36     public static final int LAST=1;
37     public static final int ERROR=2;
38
39     protected Properties JavaDoc properties=new Properties JavaDoc();
40     protected WorkerEnv wEnv;
41     protected JkHandler next;
42     protected String JavaDoc nextName=null;
43     protected String JavaDoc name;
44     protected int id;
45
46     // XXX Will be replaced with notes and (configurable) ids
47
// Each represents a 'chain' - similar with ActionCode in Coyote ( the concepts
48
// will be merged ).
49
public static final int HANDLE_RECEIVE_PACKET = 10;
50     public static final int HANDLE_SEND_PACKET = 11;
51     public static final int HANDLE_FLUSH = 12;
52     public static final int HANDLE_THREAD_END = 13;
53     
54     public void setWorkerEnv( WorkerEnv we ) {
55         this.wEnv=we;
56     }
57
58     /** Set the name of the handler. Will allways be called by
59      * worker env after creating the worker.
60      */

61     public void setName(String JavaDoc s ) {
62         name=s;
63     }
64
65     public String JavaDoc getName() {
66         return name;
67     }
68
69     /** Set the id of the worker. We use an id for faster dispatch.
70      * Since we expect a decent number of handler in system, the
71      * id is unique - that means we may have to allocate bigger
72      * dispatch tables. ( easy to fix if needed )
73      */

74     public void setId( int id ) {
75         this.id=id;
76     }
77
78     public int getId() {
79         return id;
80     }
81     
82     /** Catalina-style "recursive" invocation.
83      * A chain is used for Apache/3.3 style iterative invocation.
84      */

85     public void setNext( JkHandler h ) {
86         next=h;
87     }
88
89     public void setNext( String JavaDoc s ) {
90         nextName=s;
91     }
92
93     public String JavaDoc getNext() {
94         if( nextName==null ) {
95             if( next!=null)
96                 nextName=next.getName();
97         }
98         return nextName;
99     }
100
101     /** Should register the request types it can handle,
102      * same style as apache2.
103      */

104     public void init() throws IOException JavaDoc {
105     }
106
107     /** Clean up and stop the handler
108      */

109     public void destroy() throws IOException JavaDoc {
110     }
111
112     public MsgContext createMsgContext() {
113         return new MsgContext();
114     }
115     
116     public int invoke(Msg msg, MsgContext mc ) throws IOException JavaDoc {
117         return OK;
118     }
119     
120     public void setProperty( String JavaDoc name, String JavaDoc value ) {
121         properties.put( name, value );
122     }
123
124     public String JavaDoc getProperty( String JavaDoc name ) {
125         return properties.getProperty(name) ;
126     }
127
128     /** Experimental, will be replaced. This allows handlers to be
129      * notified when other handlers are added.
130      */

131     public void addHandlerCallback( JkHandler w ) {
132
133     }
134
135     public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
136     {
137 // BaseNotification bNot=(BaseNotification)notification;
138
// int code=bNot.getCode();
139
//
140
// MsgContext ctx=(MsgContext)bNot.getSource();
141

142
143     }
144
145     protected String JavaDoc domain;
146     protected ObjectName JavaDoc oname;
147     protected MBeanServer JavaDoc mserver;
148
149     public ObjectName JavaDoc getObjectName() {
150         return oname;
151     }
152
153     public String JavaDoc getDomain() {
154         return domain;
155     }
156
157     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server,
158                                   ObjectName JavaDoc oname) throws Exception JavaDoc {
159         this.oname=oname;
160         mserver=server;
161         domain=oname.getDomain();
162         if( name==null ) {
163             name=oname.getKeyProperty("name");
164         }
165         
166         // we need to create a workerEnv or set one.
167
ObjectName JavaDoc wEnvName=new ObjectName JavaDoc(domain + ":type=JkWorkerEnv");
168         if ( wEnv == null ) {
169             wEnv=new WorkerEnv();
170         }
171         if( ! mserver.isRegistered(wEnvName )) {
172             Registry.getRegistry(null, null).registerComponent(wEnv, wEnvName, null);
173         }
174         mserver.invoke( wEnvName, "addHandler",
175                 new Object JavaDoc[] {name, this},
176                 new String JavaDoc[] {"java.lang.String",
177                               "org.apache.jk.core.JkHandler"});
178         return oname;
179     }
180     
181     public void postRegister(Boolean JavaDoc registrationDone) {
182     }
183
184     public void preDeregister() throws Exception JavaDoc {
185     }
186
187     public void postDeregister() {
188     }
189
190     public void pause() throws Exception JavaDoc {
191     }
192
193     public void resume() throws Exception JavaDoc {
194     }
195
196 }
197
Popular Tags