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.server;
019:
020: import java.io.IOException;
021: import java.util.Iterator;
022:
023: import javax.management.MBeanServer;
024: import javax.management.ObjectName;
025:
026: import org.apache.coyote.Adapter;
027: import org.apache.coyote.ProtocolHandler;
028: import org.apache.coyote.Request;
029: import org.apache.coyote.Response;
030: import org.apache.coyote.RequestInfo;
031: import org.apache.coyote.Constants;
032: import org.apache.jk.core.JkHandler;
033: import org.apache.jk.core.Msg;
034: import org.apache.jk.core.MsgContext;
035: import org.apache.tomcat.util.modeler.Registry;
036:
037: /** Plugs Jk into Coyote. Must be named "type=JkHandler,name=container"
038: *
039: * jmx:notification-handler name="org.apache.jk.SEND_PACKET
040: * jmx:notification-handler name="org.apache.coyote.ACTION_COMMIT
041: */
042: public class JkCoyoteHandler extends JkHandler implements
043: ProtocolHandler {
044: protected static org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory
045: .getLog(JkCoyoteHandler.class);
046: // Set debug on this logger to see the container request time
047:
048: // ----------------------------------------------------------- DoPrivileged
049: private boolean paused = false;
050: int epNote;
051: Adapter adapter;
052: protected JkMain jkMain = null;
053:
054: /** Set a property. Name is a "component.property". JMX should
055: * be used instead.
056: */
057: public void setProperty(String name, String value) {
058: if (log.isTraceEnabled())
059: log.trace("setProperty " + name + " " + value);
060: getJkMain().setProperty(name, value);
061: properties.put(name, value);
062: }
063:
064: public String getProperty(String name) {
065: return properties.getProperty(name);
066: }
067:
068: public Iterator getAttributeNames() {
069: return properties.keySet().iterator();
070: }
071:
072: /** Pass config info
073: */
074: public void setAttribute(String name, Object value) {
075: if (log.isDebugEnabled())
076: log.debug("setAttribute " + name + " " + value);
077: if (value instanceof String)
078: this .setProperty(name, (String) value);
079: }
080:
081: /**
082: * Retrieve config info.
083: * Primarily for use with the admin webapp.
084: */
085: public Object getAttribute(String name) {
086: return getJkMain().getProperty(name);
087: }
088:
089: /** The adapter, used to call the connector
090: */
091: public void setAdapter(Adapter adapter) {
092: this .adapter = adapter;
093: }
094:
095: public Adapter getAdapter() {
096: return adapter;
097: }
098:
099: public JkMain getJkMain() {
100: if (jkMain == null) {
101: jkMain = new JkMain();
102: jkMain.setWorkerEnv(wEnv);
103:
104: }
105: return jkMain;
106: }
107:
108: boolean started = false;
109:
110: /** Start the protocol
111: */
112: public void init() {
113: if (started)
114: return;
115:
116: started = true;
117:
118: if (wEnv == null) {
119: // we are probably not registered - not very good.
120: wEnv = getJkMain().getWorkerEnv();
121: wEnv.addHandler("container", this );
122: }
123:
124: try {
125: // jkMain.setJkHome() XXX;
126:
127: getJkMain().init();
128:
129: } catch (Exception ex) {
130: log.error("Error during init", ex);
131: }
132: }
133:
134: public void start() {
135: try {
136: if (oname != null && getJkMain().getDomain() == null) {
137: try {
138: ObjectName jkmainOname = new ObjectName(oname
139: .getDomain()
140: + ":type=JkMain");
141: Registry.getRegistry(null, null).registerComponent(
142: getJkMain(), jkmainOname, "JkMain");
143: } catch (Exception e) {
144: log.error("Error registering jkmain " + e);
145: }
146: }
147: getJkMain().start();
148: } catch (Exception ex) {
149: log.error("Error during startup", ex);
150: }
151: }
152:
153: public void pause() throws Exception {
154: if (!paused) {
155: paused = true;
156: getJkMain().pause();
157: }
158: }
159:
160: public void resume() throws Exception {
161: if (paused) {
162: paused = false;
163: getJkMain().resume();
164: }
165: }
166:
167: public void destroy() {
168: if (!started)
169: return;
170:
171: started = false;
172: getJkMain().stop();
173: }
174:
175: // -------------------- Jk handler implementation --------------------
176: // Jk Handler mehod
177: public int invoke(Msg msg, MsgContext ep) throws IOException {
178: if (ep.isLogTimeEnabled())
179: ep.setLong(MsgContext.TIMER_PRE_REQUEST, System
180: .currentTimeMillis());
181:
182: Request req = ep.getRequest();
183: Response res = req.getResponse();
184:
185: if (log.isDebugEnabled())
186: log.debug("Invoke " + req + " " + res + " "
187: + req.requestURI().toString());
188:
189: res.setNote(epNote, ep);
190: ep.setStatus(MsgContext.JK_STATUS_HEAD);
191: RequestInfo rp = req.getRequestProcessor();
192: rp.setStage(Constants.STAGE_SERVICE);
193: try {
194: adapter.service(req, res);
195: } catch (Exception ex) {
196: log.info("Error servicing request " + req, ex);
197: }
198: if (ep.getStatus() != MsgContext.JK_STATUS_CLOSED) {
199: res.finish();
200: }
201:
202: req.recycle();
203: req.updateCounters();
204: res.recycle();
205: ep.recycle();
206: if (ep.getStatus() == MsgContext.JK_STATUS_ERROR) {
207: return ERROR;
208: }
209: ep.setStatus(MsgContext.JK_STATUS_NEW);
210: rp.setStage(Constants.STAGE_KEEPALIVE);
211: return OK;
212: }
213:
214: public ObjectName preRegister(MBeanServer server, ObjectName oname)
215: throws Exception {
216: // override - we must be registered as "container"
217: this .name = "container";
218: return super.preRegister(server, oname);
219: }
220: }
|