001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jk.core;
018:
019: import java.io.IOException;
020: import java.util.Properties;
021:
022: import javax.management.MBeanRegistration;
023: import javax.management.MBeanServer;
024: import javax.management.Notification;
025: import javax.management.NotificationListener;
026: import javax.management.ObjectName;
027:
028: import org.apache.commons.modeler.Registry;
029:
030: /**
031: *
032: * @author Costin Manolache
033: */
034: public class JkHandler implements MBeanRegistration,
035: NotificationListener {
036: public static final int OK = 0;
037: public static final int LAST = 1;
038: public static final int ERROR = 2;
039:
040: protected Properties properties = new Properties();
041: protected WorkerEnv wEnv;
042: protected JkHandler next;
043: protected String nextName = null;
044: protected String name;
045: protected int id;
046:
047: // XXX Will be replaced with notes and (configurable) ids
048: // Each represents a 'chain' - similar with ActionCode in Coyote ( the concepts
049: // will be merged ).
050: public static final int HANDLE_RECEIVE_PACKET = 10;
051: public static final int HANDLE_SEND_PACKET = 11;
052: public static final int HANDLE_FLUSH = 12;
053: public static final int HANDLE_THREAD_END = 13;
054:
055: public void setWorkerEnv(WorkerEnv we) {
056: this .wEnv = we;
057: }
058:
059: /** Set the name of the handler. Will allways be called by
060: * worker env after creating the worker.
061: */
062: public void setName(String s) {
063: name = s;
064: }
065:
066: public String getName() {
067: return name;
068: }
069:
070: /** Set the id of the worker. We use an id for faster dispatch.
071: * Since we expect a decent number of handler in system, the
072: * id is unique - that means we may have to allocate bigger
073: * dispatch tables. ( easy to fix if needed )
074: */
075: public void setId(int id) {
076: this .id = id;
077: }
078:
079: public int getId() {
080: return id;
081: }
082:
083: /** Catalina-style "recursive" invocation.
084: * A chain is used for Apache/3.3 style iterative invocation.
085: */
086: public void setNext(JkHandler h) {
087: next = h;
088: }
089:
090: public void setNext(String s) {
091: nextName = s;
092: }
093:
094: public String getNext() {
095: if (nextName == null) {
096: if (next != null)
097: nextName = next.getName();
098: }
099: return nextName;
100: }
101:
102: /** Should register the request types it can handle,
103: * same style as apache2.
104: */
105: public void init() throws IOException {
106: }
107:
108: /** Clean up and stop the handler
109: */
110: public void destroy() throws IOException {
111: }
112:
113: public MsgContext createMsgContext() {
114: return new MsgContext();
115: }
116:
117: public int invoke(Msg msg, MsgContext mc) throws IOException {
118: return OK;
119: }
120:
121: public void setProperty(String name, String value) {
122: properties.put(name, value);
123: }
124:
125: public String getProperty(String name) {
126: return properties.getProperty(name);
127: }
128:
129: /** Experimental, will be replaced. This allows handlers to be
130: * notified when other handlers are added.
131: */
132: public void addHandlerCallback(JkHandler w) {
133:
134: }
135:
136: public void handleNotification(Notification notification,
137: Object handback) {
138: // BaseNotification bNot=(BaseNotification)notification;
139: // int code=bNot.getCode();
140: //
141: // MsgContext ctx=(MsgContext)bNot.getSource();
142:
143: }
144:
145: protected String domain;
146: protected ObjectName oname;
147: protected MBeanServer mserver;
148:
149: public ObjectName getObjectName() {
150: return oname;
151: }
152:
153: public String getDomain() {
154: return domain;
155: }
156:
157: public ObjectName preRegister(MBeanServer server, ObjectName oname)
158: throws Exception {
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 wEnvName = new ObjectName(domain
168: + ":type=JkWorkerEnv");
169: if (wEnv == null) {
170: wEnv = new WorkerEnv();
171: }
172: if (!mserver.isRegistered(wEnvName)) {
173: Registry.getRegistry().registerComponent(wEnv, wEnvName,
174: null);
175: }
176: mserver.invoke(wEnvName, "addHandler", new Object[] { name,
177: this }, new String[] { "java.lang.String",
178: "org.apache.jk.core.JkHandler" });
179: return oname;
180: }
181:
182: public void postRegister(Boolean registrationDone) {
183: }
184:
185: public void preDeregister() throws Exception {
186: }
187:
188: public void postDeregister() {
189: }
190:
191: public void pause() throws Exception {
192: }
193:
194: public void resume() throws Exception {
195: }
196:
197: }
|