-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from apache/maintenance/FELIX-6746-websockets…
…ervlet-init-NPE FELIX-6746 Lazy initialization for servlets extending JettyWebSocketServlet
- Loading branch information
Showing
16 changed files
with
449 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../java/org/apache/felix/http/base/internal/handler/HttpServiceWebSocketServletHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.felix.http.base.internal.handler; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
|
||
import org.apache.felix.http.base.internal.context.ExtServletContext; | ||
import org.apache.felix.http.base.internal.runtime.ServletInfo; | ||
|
||
/** | ||
* Servlet handler for servlets extending JettyWebSocketServlet registered through the http service. | ||
*/ | ||
public final class HttpServiceWebSocketServletHandler extends HttpServiceServletHandler | ||
{ | ||
private final WebSocketHandler webSocketHandler; | ||
|
||
public HttpServiceWebSocketServletHandler(final ExtServletContext context, | ||
final ServletInfo servletInfo, | ||
final javax.servlet.Servlet servlet) | ||
{ | ||
super(context, servletInfo, servlet); | ||
this.webSocketHandler = new WebSocketHandler(this); | ||
} | ||
|
||
@Override | ||
public int init() { | ||
if (webSocketHandler.shouldInit()) { | ||
return super.init(); | ||
} | ||
// do nothing, delay init until first service call | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void handle(ServletRequest req, ServletResponse res) throws ServletException, IOException { | ||
this.webSocketHandler.lazyInit(); | ||
super.handle(req, res); | ||
} | ||
|
||
@Override | ||
public boolean destroy() { | ||
if (webSocketHandler.shouldDestroy()) { | ||
return super.destroy(); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
http/base/src/main/java/org/apache/felix/http/base/internal/handler/WebSocketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.felix.http.base.internal.handler; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.apache.felix.http.base.internal.logger.SystemLogger; | ||
|
||
/** | ||
* Class that handles initialization for servlets extending JettyWebSocketServlet. | ||
*/ | ||
public final class WebSocketHandler { | ||
// The Jetty class used for Jetty WebSocket servlets | ||
private static final String JETTY_WEB_SOCKET_SERVLET_CLASS = "JettyWebSocketServlet"; | ||
|
||
private final AtomicBoolean lazyFirstInitCall = new AtomicBoolean(true); | ||
private final CountDownLatch initBarrier = new CountDownLatch(1); | ||
private final ServletHandler servletHandler; | ||
|
||
public WebSocketHandler(ServletHandler servletHandler) { | ||
this.servletHandler = servletHandler; | ||
} | ||
|
||
/* | ||
* Lazy initialization of the servlet. | ||
* Will only be called once for each servlet instance and is thread-safe. | ||
*/ | ||
public void lazyInit() { | ||
if (lazyFirstInitCall.compareAndSet(true, false)) { | ||
try { | ||
this.servletHandler.init(); | ||
} catch (final Exception e) { | ||
SystemLogger.LOGGER.error(SystemLogger.formatMessage( | ||
this.servletHandler.getServletInfo().getServiceReference(), | ||
"Error calling init() lazy on servlet ".concat( | ||
this.servletHandler.getServletInfo().getClassName(this.servletHandler.getServlet()))), e); | ||
} finally { | ||
initBarrier.countDown(); | ||
} | ||
} else { | ||
// already initialized, await the first initialization | ||
try { | ||
initBarrier.await(); | ||
} catch (final InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if the servlet should be initialized, false otherwise. | ||
* @return true if the servlet should be initialized, false otherwise | ||
*/ | ||
public boolean shouldInit() { | ||
return !lazyFirstInitCall.get() && initBarrier.getCount() > 0; | ||
} | ||
|
||
/** | ||
* Returns true if the servlet was initialized earlier, false otherwise. | ||
* @return true if the servlet should be destroyed, false otherwise | ||
*/ | ||
public boolean shouldDestroy() { | ||
if (!lazyFirstInitCall.get()){ | ||
try { | ||
initBarrier.await(); | ||
} catch (final InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Check if the servlet is a JettyWebSocketServlet. | ||
* JettyWebSocket classes are handled differently due to FELIX-6746. | ||
* @param servlet the servlet to check | ||
* @return true if the servlet is a JettyWebSocketServlet, false otherwise | ||
*/ | ||
public static boolean isJettyWebSocketServlet(Object servlet) { | ||
final Class<?> superClass = servlet.getClass().getSuperclass(); | ||
SystemLogger.LOGGER.debug("Checking if the servlet is a JettyWebSocketServlet: '" + superClass.getSimpleName() + "'"); | ||
|
||
// Now check if the servlet class extends 'JettyWebSocketServlet' | ||
boolean isJettyWebSocketServlet = superClass.getSimpleName().endsWith(JETTY_WEB_SOCKET_SERVLET_CLASS); | ||
if (!isJettyWebSocketServlet) { | ||
// Recurse through the wrapped servlets, in case of double-wrapping | ||
if (servlet instanceof org.apache.felix.http.jakartawrappers.ServletWrapper) { | ||
final javax.servlet.Servlet wrappedServlet = ((org.apache.felix.http.jakartawrappers.ServletWrapper) servlet).getServlet(); | ||
return isJettyWebSocketServlet(wrappedServlet); | ||
} else if (servlet instanceof org.apache.felix.http.javaxwrappers.ServletWrapper) { | ||
final jakarta.servlet.Servlet wrappedServlet = ((org.apache.felix.http.javaxwrappers.ServletWrapper) servlet).getServlet(); | ||
return isJettyWebSocketServlet(wrappedServlet); | ||
} | ||
} | ||
return isJettyWebSocketServlet; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...n/java/org/apache/felix/http/base/internal/handler/WhiteboardWebSocketServletHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.felix.http.base.internal.handler; | ||
|
||
import java.io.FilePermission; | ||
import java.io.IOException; | ||
|
||
import jakarta.servlet.Servlet; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
|
||
import org.apache.felix.http.base.internal.context.ExtServletContext; | ||
import org.apache.felix.http.base.internal.logger.SystemLogger; | ||
import org.apache.felix.http.base.internal.runtime.ServletInfo; | ||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.service.servlet.runtime.dto.DTOConstants; | ||
|
||
/** | ||
* Servlet handler for servlets extending JettyWebSocketServlet registered through the http whiteboard. | ||
*/ | ||
public final class WhiteboardWebSocketServletHandler extends WhiteboardServletHandler | ||
{ | ||
private final WebSocketHandler webSocketHandler; | ||
|
||
public WhiteboardWebSocketServletHandler(final long contextServiceId, | ||
final ExtServletContext context, | ||
final ServletInfo servletInfo, | ||
final BundleContext contextBundleContext, | ||
final Bundle registeringBundle, | ||
final Bundle httpWhiteboardBundle, | ||
final Object servlet) | ||
{ | ||
super(contextServiceId, context, servletInfo, contextBundleContext, registeringBundle, httpWhiteboardBundle); | ||
this.webSocketHandler = new WebSocketHandler(this); | ||
this.setServlet((Servlet) servlet); | ||
} | ||
|
||
@Override | ||
public int init() { | ||
if (webSocketHandler.shouldInit()) { | ||
return super.init(); | ||
} | ||
// do nothing, delay init until first service call | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void handle(ServletRequest req, ServletResponse res) throws ServletException, IOException { | ||
this.webSocketHandler.lazyInit(); | ||
super.handle(req, res); | ||
} | ||
|
||
@Override | ||
public boolean destroy() { | ||
if (webSocketHandler.shouldDestroy()) { | ||
return super.destroy(); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.