@@ -774,6 +774,20 @@ private enum Verb {
774774 private static class Resource {
775775 Map <String , Resource > resources = new TreeMap <>();
776776 Map <Verb , List <Method >> handlerLists = new TreeMap <>();
777+
778+ static final Comparator <Method > handlerComparator ;
779+ static {
780+ var nameComparator = Comparator .comparing (Method ::getName );
781+ var parameterCountComparator = Comparator .comparing (Method ::getParameterCount );
782+
783+ handlerComparator = nameComparator .thenComparing (parameterCountComparator .reversed ());
784+ }
785+
786+ void sort () {
787+ for (var handlerList : handlerLists .values ()) {
788+ handlerList .sort (handlerComparator );
789+ }
790+ }
777791 }
778792
779793 private Resource root ;
@@ -814,15 +828,7 @@ private static class Resource {
814828 private static final ThreadLocal <HttpServletRequest > request = new ThreadLocal <>();
815829 private static final ThreadLocal <HttpServletResponse > response = new ThreadLocal <>();
816830
817- private static final Comparator <Method > handlerComparator ;
818- static {
819- var nameComparator = Comparator .comparing (Method ::getName );
820- var parameterCountComparator = Comparator .comparing (Method ::getParameterCount );
821-
822- handlerComparator = nameComparator .thenComparing (parameterCountComparator .reversed ());
823- }
824-
825- private static final Map <Class <? extends WebService >, WebService > instances = new HashMap <>();
831+ static final Map <Class <? extends WebService >, WebService > instances = synchronizedMapOf (new HashMap <>());
826832
827833 /**
828834 * Returns a service instance.
@@ -838,20 +844,10 @@ private static class Resource {
838844 * exists.
839845 */
840846 @ SuppressWarnings ("unchecked" )
841- public static synchronized <T extends WebService > T getInstance (Class <T > type ) {
847+ public static <T extends WebService > T getInstance (Class <T > type ) {
842848 return (T )instances .get (type );
843849 }
844850
845- /**
846- * Returns a map of all active service instances.
847- *
848- * @return
849- * A map of all active service instances, keyed by type.
850- */
851- public static synchronized Map <Class <? extends WebService >, WebService > getInstances () {
852- return immutableMapOf (instances );
853- }
854-
855851 @ Override
856852 public void init () throws ServletException {
857853 var type = getClass ();
@@ -915,25 +911,11 @@ public void init() throws ServletException {
915911 resource .handlerLists .computeIfAbsent (verb , key -> new LinkedList <>()).add (handler );
916912 }
917913
918- sort (root );
919-
920914 serviceDescriptor = new ServiceDescriptor (path , type );
921915
922916 describeResource (path , root );
923917
924- synchronized (WebService .class ) {
925- instances .put (type , this );
926- }
927- }
928-
929- private static void sort (Resource root ) {
930- for (var handlerList : root .handlerLists .values ()) {
931- handlerList .sort (handlerComparator );
932- }
933-
934- for (var resource : root .resources .values ()) {
935- sort (resource );
936- }
918+ instances .put (type , this );
937919 }
938920
939921 @ Override
@@ -1472,6 +1454,8 @@ public ServiceDescriptor getServiceDescriptor() {
14721454 }
14731455
14741456 private void describeResource (String path , Resource resource ) {
1457+ resource .sort ();
1458+
14751459 if (!resource .handlerLists .isEmpty ()) {
14761460 var endpoint = new EndpointDescriptor (path );
14771461
0 commit comments