mbien commented on code in PR #5530:
URL: https://github.com/apache/netbeans/pull/5530#discussion_r1111323102
##########
ide/httpserver/src/org/netbeans/modules/httpserver/HttpServerModule.java:
##########
@@ -171,113 +173,104 @@ static void stopHTTPServer() {
}
}
-
- private static ContextManager getContextManager(EmbededTomcat tc) {
- try {
- java.lang.reflect.Field fm =
EmbededTomcat.class.getDeclaredField("contextM"); // NOI18N
- fm.setAccessible(true);
- return (ContextManager)fm.get(tc);
- }
- catch (NoSuchFieldException e) {
- return null;
- }
- catch (IllegalAccessException e) {
- return null;
- }
- }
-
- /** Removes WebXmlReader interceptor to avoid attempt
- * to load JspServlet that processes jsp file and produces confusing
message
- */
- private static void removeWebXmlReader (EmbededTomcat tc) {
- try {
- java.lang.reflect.Field fm =
EmbededTomcat.class.getDeclaredField("contextInt"); // NOI18N
- fm.setAccessible(true);
- Vector contextInt = (Vector)fm.get(tc);
- Iterator it = contextInt.iterator ();
- while (it.hasNext ()) {
- Object o = it.next ();
- if (o instanceof WebXmlReader) {
- contextInt.remove (o);
- break;
- }
+ private static void buildServer() throws Exception {
+ tomcat = new Tomcat();
+ tomcat.setPort(httpserverSettings().getPort());
+ tomcat.getServer().setUtilityThreads(1);
+ tomcat.getConnector().setXpoweredBy(false);
+ TaskQueue tq = new TaskQueue(10);
+ ThreadPoolExecutor tf = new ThreadPoolExecutor(0, 3, 60,
TimeUnit.SECONDS, tq);
+ tomcat.getConnector().getProtocolHandler().setExecutor(tf);
+
+ File wd = Places.getCacheSubdirectory("httpwork");
+ Context ctx = tomcat.addContext("", wd.getAbsolutePath());
+
+ ctx.setResources(new StandardRoot() {
+ @Override
+ protected void registerURLStreamHandlerFactory() {
+ // Tomcat tries to override the URLStreamHandlerFactory, this
+ // collides with NetBeans usage and alternative registration
+ // methods are only available on JDK 9+
+ // So disable this here, we don't really need it.
}
- }
- catch (NoSuchFieldException e) {
- return;
- }
- catch (IllegalAccessException e) {
- return;
- }
- }
-
+ });
- private static ContextManager buildServer() throws Exception {
- HttpServerSettings op = httpserverSettings ();
+ ctx.setSessionTimeout(30);
- NbLogger logger = new NbLogger();
- logger.setName("tc_log"); // NOI18N
+ initContext(ctx);
- final EmbededTomcat tc=new EmbededTomcat();
-
- File wd = FileUtil.toFile (FileUtil.getConfigRoot());
- wd = new File(wd, "httpwork"); // NOI18N
- tc.setWorkDir(wd.getAbsolutePath());
-
- // install interceptors which need to be initialized BEFORE the
default server interceptors
- NbLoaderInterceptor nbL =new NbLoaderInterceptor();
- tc.addContextInterceptor( nbL );
-
- // hack - force initialization of default interceptors, so our
interceptor is after them
- tc.addApplicationAdapter(null);
+ reloader = new ContextReloader (tomcat, ctx);
+ }
- // install interceptors which need to be initialized AFTER the default
server interceptors
- NbServletsInterceptor nbI =new NbServletsInterceptor();
- tc.addContextInterceptor( nbI );
+ private static void initContext(Context ctx) {
- removeWebXmlReader (tc);
-
- ServletContext sctx;
- sctx=tc.addContext("", wd.toURI().toURL()); // NOI18N
- tc.initContext( sctx );
-
//ctxt.getServletLoader().setParentLoader(TopManager.getDefault().systemClassLoader());
-
- tc.addEndpoint( op.getPort(), null, null);
+
ctx.setParentClassLoader(Lookup.getDefault().lookup(ClassLoader.class));
- final ContextManager cm = getContextManager(tc);
-
- reloader = new ContextReloader (tc, cm, sctx);
-
- // reduce number of threads
- Enumeration e = cm.getConnectors ();
- while (e.hasMoreElements ()) {
- Object o = e.nextElement ();
- if (o instanceof PoolTcpConnector) {
- org.apache.tomcat.core.ServerConnector conn =
(PoolTcpConnector)o;
- conn.setAttribute (PoolTcpConnector.MIN_SPARE_THREADS, "0");
// NOI18N
- conn.setAttribute (PoolTcpConnector.MAX_SPARE_THREADS, "1");
// NOI18N
- conn.setAttribute (PoolTcpConnector.MAX_THREADS, "3");
// NOI18N
- }
+ for (String mapping : ctx.findServletMappings()) {
+ ctx.removeServletMapping(mapping);
}
-
- return cm;
-
- }
- private static class NbLogger extends TomcatLogger {
- public NbLogger() {
- super();
+ for (Container container : asList(ctx.findChildren())) {
+ ctx.removeChild(container);
}
- protected void realLog(String message) {
+ for(FilterMap fm: ctx.findFilterMaps()) {
+ ctx.removeFilterMap(fm);
}
- protected void realLog(String message, Throwable t) {
+ for(FilterDef fd: ctx.findFilterDefs()) {
+ ctx.removeFilterDef(fd);
}
-
- public void flush() {
+
+ Wrapper sw = ctx.createWrapper();
+ sw.setServletClass("org.netbeans.modules.httpserver.WrapperServlet");
+ sw.setName("WrapperServlet");
+ ctx.addChild(sw);
+
+ ctx.addServletMappingDecoded(httpserverSettings().getWrapperBaseURL()
+ "*", "WrapperServlet");
+
+ // Originally the Apache Tomcat InvokerServlet took care of invoking
+ // servlet. This servlet was remove from Tomcat. As a replacement
Review Comment:
-> 'was removed'
##########
enterprise/web.monitor/serversrc/org/netbeans/modules/web/monitor/server/NotifyUtil.java:
##########
@@ -117,28 +119,32 @@ void setIDE(String server) throws MalformedURLException {
}
void sendRecord(MonitorData monData, String queryStr) {
-
- if(debug) log("NotifyUtil::notifyServer"); //NOI18N
-
- if(ideServer != null) {
- String urlStr = ideServer.concat(queryStr);
- if(debug) log("NotifyUtil: url is " + urlStr); //NOI18N
- sendRecord(urlStr, monData);
- }
-
- if(otherIDEs.isEmpty()) return;
-
- Enumeration ides = otherIDEs.elements();
- while(ides.hasMoreElements()) {
-
- String base = (String)ides.nextElement();
-
- if(debug)
- log("NotifyUtil: url is " + base.concat(queryStr)); //NOI18N
- if(!sendRecord(base.concat(queryStr), monData))
- otherIDEs.remove(base);
- }
- return;
+ try {
+ if(debug) log("NotifyUtil::notifyServer"); //NOI18N
+
+ if(ideServer != null) {
+ String urlStr =
ideServer.concat(URLEncoder.encode(queryStr, "UTF-8"));
Review Comment:
nitpick: the UTF String literals need all the `//NOI18N`, right?
or: `StandardCharsets.UTF_8.name()`
##########
ide/httpserver/src/org/netbeans/modules/httpserver/AccessFilter.java:
##########
@@ -21,60 +21,44 @@
import java.io.IOException;
import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.rmi.ServerException;
import java.util.Set;
import javax.servlet.*;
import javax.servlet.http.*;
import org.openide.util.Exceptions;
-
import org.openide.util.NbBundle;
-/** Base servlet for servlets which access NetBeans Open APIs
-*
-* @author Petr Jiricka
-* @version 0.11 May 5, 1999
-*/
-public abstract class NbBaseServlet extends HttpServlet {
+/**
+ * Base servlet for servlets which access NetBeans Open APIs
+ *
+ * @author Petr Jiricka
+ * @version 0.11 May 5, 1999
+ */
+public class AccessFilter implements Filter {
Review Comment:
is now a filter and no longer a servlet -> doc needs tweaks
##########
ide/httpserver/src/org/netbeans/modules/httpserver/HttpServerModule.java:
##########
@@ -171,113 +173,104 @@ static void stopHTTPServer() {
}
}
-
- private static ContextManager getContextManager(EmbededTomcat tc) {
- try {
- java.lang.reflect.Field fm =
EmbededTomcat.class.getDeclaredField("contextM"); // NOI18N
- fm.setAccessible(true);
- return (ContextManager)fm.get(tc);
- }
- catch (NoSuchFieldException e) {
- return null;
- }
- catch (IllegalAccessException e) {
- return null;
- }
- }
-
- /** Removes WebXmlReader interceptor to avoid attempt
- * to load JspServlet that processes jsp file and produces confusing
message
- */
- private static void removeWebXmlReader (EmbededTomcat tc) {
- try {
- java.lang.reflect.Field fm =
EmbededTomcat.class.getDeclaredField("contextInt"); // NOI18N
- fm.setAccessible(true);
- Vector contextInt = (Vector)fm.get(tc);
- Iterator it = contextInt.iterator ();
- while (it.hasNext ()) {
- Object o = it.next ();
- if (o instanceof WebXmlReader) {
- contextInt.remove (o);
- break;
- }
+ private static void buildServer() throws Exception {
+ tomcat = new Tomcat();
+ tomcat.setPort(httpserverSettings().getPort());
+ tomcat.getServer().setUtilityThreads(1);
+ tomcat.getConnector().setXpoweredBy(false);
+ TaskQueue tq = new TaskQueue(10);
+ ThreadPoolExecutor tf = new ThreadPoolExecutor(0, 3, 60,
TimeUnit.SECONDS, tq);
+ tomcat.getConnector().getProtocolHandler().setExecutor(tf);
+
+ File wd = Places.getCacheSubdirectory("httpwork");
+ Context ctx = tomcat.addContext("", wd.getAbsolutePath());
+
+ ctx.setResources(new StandardRoot() {
+ @Override
+ protected void registerURLStreamHandlerFactory() {
+ // Tomcat tries to override the URLStreamHandlerFactory, this
+ // collides with NetBeans usage and alternative registration
+ // methods are only available on JDK 9+
+ // So disable this here, we don't really need it.
}
- }
- catch (NoSuchFieldException e) {
- return;
- }
- catch (IllegalAccessException e) {
- return;
- }
- }
-
+ });
- private static ContextManager buildServer() throws Exception {
- HttpServerSettings op = httpserverSettings ();
+ ctx.setSessionTimeout(30);
- NbLogger logger = new NbLogger();
- logger.setName("tc_log"); // NOI18N
+ initContext(ctx);
- final EmbededTomcat tc=new EmbededTomcat();
-
- File wd = FileUtil.toFile (FileUtil.getConfigRoot());
- wd = new File(wd, "httpwork"); // NOI18N
- tc.setWorkDir(wd.getAbsolutePath());
-
- // install interceptors which need to be initialized BEFORE the
default server interceptors
- NbLoaderInterceptor nbL =new NbLoaderInterceptor();
- tc.addContextInterceptor( nbL );
-
- // hack - force initialization of default interceptors, so our
interceptor is after them
- tc.addApplicationAdapter(null);
+ reloader = new ContextReloader (tomcat, ctx);
+ }
- // install interceptors which need to be initialized AFTER the default
server interceptors
- NbServletsInterceptor nbI =new NbServletsInterceptor();
- tc.addContextInterceptor( nbI );
+ private static void initContext(Context ctx) {
- removeWebXmlReader (tc);
-
- ServletContext sctx;
- sctx=tc.addContext("", wd.toURI().toURL()); // NOI18N
- tc.initContext( sctx );
-
//ctxt.getServletLoader().setParentLoader(TopManager.getDefault().systemClassLoader());
-
- tc.addEndpoint( op.getPort(), null, null);
+
ctx.setParentClassLoader(Lookup.getDefault().lookup(ClassLoader.class));
- final ContextManager cm = getContextManager(tc);
-
- reloader = new ContextReloader (tc, cm, sctx);
-
- // reduce number of threads
- Enumeration e = cm.getConnectors ();
- while (e.hasMoreElements ()) {
- Object o = e.nextElement ();
- if (o instanceof PoolTcpConnector) {
- org.apache.tomcat.core.ServerConnector conn =
(PoolTcpConnector)o;
- conn.setAttribute (PoolTcpConnector.MIN_SPARE_THREADS, "0");
// NOI18N
- conn.setAttribute (PoolTcpConnector.MAX_SPARE_THREADS, "1");
// NOI18N
- conn.setAttribute (PoolTcpConnector.MAX_THREADS, "3");
// NOI18N
- }
+ for (String mapping : ctx.findServletMappings()) {
+ ctx.removeServletMapping(mapping);
}
-
- return cm;
-
- }
- private static class NbLogger extends TomcatLogger {
- public NbLogger() {
- super();
+ for (Container container : asList(ctx.findChildren())) {
+ ctx.removeChild(container);
}
- protected void realLog(String message) {
+ for(FilterMap fm: ctx.findFilterMaps()) {
+ ctx.removeFilterMap(fm);
}
- protected void realLog(String message, Throwable t) {
+ for(FilterDef fd: ctx.findFilterDefs()) {
+ ctx.removeFilterDef(fd);
}
-
- public void flush() {
+
+ Wrapper sw = ctx.createWrapper();
+ sw.setServletClass("org.netbeans.modules.httpserver.WrapperServlet");
+ sw.setName("WrapperServlet");
+ ctx.addChild(sw);
+
+ ctx.addServletMappingDecoded(httpserverSettings().getWrapperBaseURL()
+ "*", "WrapperServlet");
+
+ // Originally the Apache Tomcat InvokerServlet took care of invoking
+ // servlet. This servlet was remove from Tomcat. As a replacement
+ // this servlet is used. The idea is, that this servlet serves as a
+ // fallback servlet. When it is invoked, it looksup the right servlet,
+ // registers it and redispatches the request.
+ sw = ctx.createWrapper();
+ sw.setServlet(new HttpServlet() {
+ @Override
+ protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
+ String className = classFromPath((HttpServletRequest) request);
+ LOG.log(Level.FINE, "Servlet class name: {0}", className);
+ if (className != null) {
+ try {
+ Class<?> clazz = Class.forName(className, true,
ctx.getParentClassLoader());
+ Tomcat.addServlet(ctx, className, (Servlet)
clazz.getConstructor().newInstance());
+ ctx.addServletMappingDecoded("/servlet/" + className +
"/*", className);
+
request.getRequestDispatcher(request.getRequestURI()).forward(request,
response);
+ } catch (ClassNotFoundException | NoSuchMethodException |
SecurityException | InstantiationException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException ex) {
Review Comment:
some of those can be combined to the `ReflectiveOperationException` to
shorten the line
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists