88import info .magnolia .module .model .ModuleDefinition ;
99import info .magnolia .module .model .ServletDefinition ;
1010
11+ import java .util .Optional ;
12+
1113import javax .inject .Inject ;
14+ import javax .jcr .Node ;
15+ import javax .jcr .PathNotFoundException ;
1216import javax .jcr .RepositoryException ;
1317import javax .jcr .Session ;
1418
19+ import com .machinezoo .noexception .Exceptions ;
1520import com .merkle .oss .magnolia .setup .task .type .InstallAndUpdateTask ;
1621
1722/**
@@ -28,7 +33,7 @@ public ReregisterServletsTask(final NodeNameHelper nodeNameHelper) {
2833 }
2934
3035 @ Override
31- public void execute (InstallContext installContext ) throws TaskExecutionException {
36+ public void execute (final InstallContext installContext ) throws TaskExecutionException {
3237 final ModuleDefinition moduleDefinition = installContext .getCurrentModuleDefinition ();
3338 for (ServletDefinition servletDefinition : moduleDefinition .getServlets ()) {
3439 addTask (new ReregisterServletTask (servletDefinition , nodeNameHelper ));
@@ -43,17 +48,20 @@ public ReregisterServletTask(ServletDefinition servletDefinition, NodeNameHelper
4348
4449 @ Override
4550 public void execute (final InstallContext installContext ) throws TaskExecutionException {
46- if (!isRegistered (installContext )) {
51+ try {
52+ getServletNode (installContext ).ifPresent (servletNode -> Exceptions .wrap ().run (servletNode ::remove ));
4753 super .execute (installContext );
54+ } catch (Exception e ) {
55+ throw new TaskExecutionException ("Failed to reregister servlet " +getServletDefinition ().getName (), e );
4856 }
4957 }
5058
51- private boolean isRegistered (final InstallContext installContext ) throws TaskExecutionException {
59+ private Optional < Node > getServletNode (final InstallContext installContext ) throws RepositoryException {
5260 try {
5361 final Session session = installContext .getConfigJCRSession ();
54- return session .getRootNode ().hasNode (DEFAULT_SERVLET_FILTER_PATH + "/" + getServletDefinition ().getName ());
55- } catch (RepositoryException e ) {
56- throw new TaskExecutionException ( "Failed to reregister servlet " + getServletDefinition (). getName (), e );
62+ return Optional . of ( session .getRootNode ().getNode (DEFAULT_SERVLET_FILTER_PATH + "/" + getServletDefinition ().getName () ));
63+ } catch (PathNotFoundException e ) {
64+ return Optional . empty ( );
5765 }
5866 }
5967 }
0 commit comments