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