public static synchronized void instanceDelete(String uri) {
    BaseUtil.out("SuiteManager.instanceDelete uri=" + uri);
    if (uri == null) {
      BaseUtil.out("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! URI == NULL ");
      BaseUtil.out("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! URI == NULL ");
      return;
    }
    SuiteNodesNotifier suiteNotifier =
        SuiteManager.getServerSuiteProject(uri).getLookup().lookup(SuiteNodesNotifier.class);

    Properties props = ServerUtil.removeInstanceProperties(uri);

    //
    // Delete InstancePreferencies
    //
    String instancePath = SuiteUtil.extractInstancePath(uri);
    if (instancePath != null) {
      DirectoryPreferences registry = new DirectoryPreferences(Paths.get(instancePath));
      registry.remove();
      Project suite = getServerSuiteProject(uri);
      if (suite != null) {
        suite.getLookup().lookup(DistributeModulesManager.class).remove(instancePath);
      }

      //        DistributeModulesManager m = getInstance(uri);
    }

    suiteNotifier.instancesChanged(); // Can invoke this method too
  }
  private void onDeploy() {
    if (isCompleteImmediately()) {
      return;
    }

    List<Pair<BaseTargetModuleID, BaseTargetModuleID>> modules =
        getManager().getInitialDeployedModulesOld();
    Project wp =
        BaseUtil.getOwnerProject(
            FileUtil.toFileObject(new File(getTargetModuleID().getProjectDir())));

    int i = 0;
    for (Pair<BaseTargetModuleID, BaseTargetModuleID> pair : modules) {
      if (pair.first().getContextPath().equals(getTargetModuleID().getContextPath())) {
        modules.set(i, Pair.of(getTargetModuleID(), (BaseTargetModuleID) null));
        return;
      }
      i++;
    }
    i = 0;
    for (Pair<BaseTargetModuleID, BaseTargetModuleID> pair : modules) {
      if (pair.first().getProjectDir().equals(getTargetModuleID().getProjectDir())) {
        modules.set(i, Pair.of(getTargetModuleID(), (BaseTargetModuleID) null));
        return;
      }
      i++;
    }

    modules.add(Pair.of(getTargetModuleID(), (BaseTargetModuleID) null));
    //      }
  }
  public static synchronized List<String> getServerInstanceIds(FileObject suiteDir) {

    if (suiteDir == null) {
      return null;
    }
    Path suitePath = Paths.get(suiteDir.getPath());
    Deployment d = Deployment.getDefault();

    if (d == null || d.getServerInstanceIDs() == null) {
      return null;
    }
    List<String> result = new ArrayList<>();
    for (String uri : d.getServerInstanceIDs()) {
      InstanceProperties ip = InstanceProperties.getInstanceProperties(uri);

      String foundSuiteLocation = SuiteUtil.getSuiteProjectLocation(ip);
      if (foundSuiteLocation == null || !new File(foundSuiteLocation).exists()) {
        // May be not a native plugin server
        continue;
      }
      Project foundSuite =
          BaseUtil.getOwnerProject(FileUtil.toFileObject(new File(foundSuiteLocation)));

      if (foundSuite == null) {
        continue;
      }
      Path p = Paths.get(foundSuiteLocation);

      if (suitePath.equals(p)) {
        result.add(uri);
      }
    }
    return result;
  }
  public void register(Project webApp) {
    int result = SUCCESS;

    Path target = createRegistry();

    if (target == null) {
      return;
    }
    FileObject propsFo =
        FileUtil.toFileObject(target.toFile())
            .getFileObject(SuiteConstants.SERVER_INSTANCE_WEB_APPS_PROPS);
    Properties props = new Properties();
    if (propsFo != null) {
      props = BaseUtil.loadProperties(propsFo);
      try {
        propsFo.delete();
      } catch (IOException ex) {
        LOG.log(Level.INFO, ex.getMessage());
      }
    }

    WebModule wm = WebModule.getWebModule(webApp.getProjectDirectory());

    String cp = wm.getContextPath();

    if (cp != null) {
      props.setProperty(cp, webApp.getProjectDirectory().getPath());
    } else {
      result = CONTEXTPATH_NOT_FOUND;
    }
    if (result == SUCCESS) {
      BaseUtil.storeProperties(
          props,
          FileUtil.toFileObject(target.toFile()),
          SuiteConstants.SERVER_INSTANCE_WEB_APPS_PROPS);
      String uri = SuiteManager.getManager(serverInstance).getUri();
      SuiteNotifier sn =
          SuiteManager.getServerSuiteProject(uri).getLookup().lookup(SuiteNotifier.class);
      sn.childrenChanged(this, webApp);
    }
    return;
  }
  protected static void saveInstanceProperties(Properties props) {
    BaseUtil.out("--- 1 SuiteManager : saveInstanceProperties()");
    String uri = props.getProperty(BaseConstants.URL_PROP);
    String projDir = props.getProperty(BaseConstants.SERVER_LOCATION_PROP);
    if (uri == null) {
      BaseUtil.out("--- 2 SuiteManager : saveInstanceProperties() uri == null");

      return;
    }
    BaseUtil.out("--- 3 SuiteManager : saveInstanceProperties() uri = " + uri);

    InstancePreferences prefs = getNbInstanceProperties(projDir, uri);
    props.forEach(
        (k, v) -> {
          BaseUtil.out(
              "--- 4 SuiteManager : saveInstanceProperties() key = " + k + "; value = " + v);

          prefs.setProperty((String) k, (String) v);
        });
  }
 public BaseDeployProgressObject destroy(
     BaseTargetModuleID module, FileObject projDir, boolean completeImmediately) {
   command = "destroy";
   this.setTargetModuleID(module);
   setCompleteImmediately(completeImmediately);
   setMode(getManager().getCurrentDeploymentMode());
   BaseUtil.out("command = 'destroy'");
   fireRunning(CommandType.UNDEPLOY, getManager().getDefaultTarget().getName());
   RP.post(this, 0, Thread.NORM_PRIORITY);
   return this;
 }
 protected Properties getWebAppsProperties() {
   Properties props = new Properties();
   FileObject target = getRegistry();
   if (target == null) {
     return props;
   }
   FileObject propsFo = target.getFileObject(SuiteConstants.SERVER_INSTANCE_WEB_APPS_PROPS);
   if (propsFo == null) {
     return props;
   }
   props = BaseUtil.loadProperties(propsFo);
   return props;
 }
    public ContextAction(final Lookup webapplookup) {

      webProject = webapplookup.lookup(Project.class);
      J2eeModuleProvider p = BaseUtil.getJ2eeModuleProvider(webProject);
      String id = "";
      if (p != null) {
        id = p.getServerInstanceID();
      }
      if (p != null
          && id.startsWith("jettystandalone:deploy:server")
          && p.getInstanceProperties() != null) {
        File file = new File(BaseUtil.getServerLocation(p.getInstanceProperties()));
        FileObject fo = FileUtil.toFileObject(file);
        serverProject = BaseUtil.getOwnerProject(fo);
      } else {
        serverProject = null;
      }

      boolean enabled = serverProject == null ? false : true;

      if (enabled) {

        File f =
            Paths.get(serverProject.getProjectDirectory().getPath(), Utils.jettyBase(serverProject))
                .toFile();
        String jsfListener = JettyConfig.getInstance(serverProject).getJsfListener();
        // BaseUtil.out("@@@@@@@@@@@@@@@@@ AddListenerAction listener=" + jsfListener);
        String jsfModule = JettyConfig.getInstance(serverProject).getJSFModuleName();

        enabled = jsfModule != null && !DDHelper.hasJsfListener(serverProject, webProject);
        setEnabled(enabled);

        String s = isEnabled() ? "(" + jsfModule + ") " : "";
        putValue(NAME, "Add Listener " + s + " to web.xml ");
      }

      putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
    }
  public static InstancePreferences getNbInstanceProperties(String instanceDir, String uri) {
    BaseUtil.out("**** 1 SuiteRegistry.getNbInstancePreferences instanceDir=" + instanceDir);
    BaseUtil.out("**** 2 SuiteRegistry.getNbInstancePreferences uri=" + uri);
    Project suite = SuiteManager.getServerSuiteProject(uri);
    FileObject fo = FileUtil.toFileObject(new File(instanceDir));

    if (fo == null) {
      return null;
    }
    BaseUtil.out("**** 3 SuiteRegistry.getNbInstancePreferences fo.getPath()=" + fo.getPath());

    // FileObject suite = SuiteManager.getServerSuiteProject(serverInstance)
    String uid = SuiteUtil.getSuiteUID(suite.getProjectDirectory());

    BaseUtil.out("**** 4 SuiteRegistry.getNbInstancePreferences UID" + uid);

    if (uid == null) {
      return null;
    }
    BaseUtil.out("**** 5 NbSuitePreferences.getNbInstancePreferences UID" + uid);

    return NbSuitePreferences.newInstance(instanceDir, uid).getProperties();
  }
  public String getServerInstanceProperty(String name) {
    Properties props = new Properties();
    Path target = createRegistry();
    if (target == null) {
      return null;
    }
    FileObject propsFo =
        FileUtil.toFileObject(target.toFile()).getFileObject("server-instance.properties");

    if (propsFo != null) {
      props = BaseUtil.loadProperties(propsFo);
    }
    return props.getProperty(name);
  }
  protected static void updateInstanceRegistry(Properties props) {
    Path dirpath = Paths.get("d:\\Netbeans_810_Plugins\\TestApps\\AMEmbServer04");

    NbDirectoryPreferences ppp =
        new NbDirectoryPreferences(dirpath, "uid-025c0287-3243-4a16-837e-94be30ecfc19");
    int l = ppp.childrenNames().length;
    BaseUtil.out(
        "--- 0000001 SuiteManager : updateInstanceRegistry I test NbDirectoryPreferences lengt="
            + l);

    BaseUtil.out("--- 1 SuiteManager : updateInstanceRegistry(Properties)");

    String serverLocation = props.getProperty(BaseConstants.SERVER_LOCATION_PROP);
    BaseUtil.out(
        "--- 2 SuiteManager : updateInstanceRegistry(Properties) serverLocation = "
            + serverLocation);

    InstancePreferences prefs = getInstanceProperties(serverLocation);

    props.forEach(
        (k, v) -> {
          BaseUtil.out(
              "--- 2 SuiteManager : updateInstanceRegistry(Properties) key = "
                  + k
                  + "; value = "
                  + v);

          prefs.setProperty((String) k, (String) v);
        });

    String shutdownPort = props.getProperty(BaseConstants.SHUTDOWN_PORT_PROP);
    if (shutdownPort == null) { // Cannot be
      shutdownPort = String.valueOf(Integer.MAX_VALUE);
      prefs.setProperty(BaseConstants.SHUTDOWN_PORT_PROP, shutdownPort);
    }
  }
  public void setServerInstanceProperty(String name, String value) {
    Properties props = new Properties();
    Path target = createRegistry();
    if (target == null) {
      return;
    }
    FileObject propsFo =
        FileUtil.toFileObject(target.toFile())
            .getFileObject(SuiteConstants.SERVER_INSTANCE_PROPERTIES_FILE);

    if (propsFo != null) {
      props = BaseUtil.loadProperties(propsFo);
      try {
        propsFo.delete();
      } catch (IOException ex) {
        LOG.log(Level.INFO, ex.getMessage());
      }
    }
    props.setProperty(name, value);
    BaseUtil.storeProperties(
        props,
        FileUtil.toFileObject(target.toFile()),
        SuiteConstants.SERVER_INSTANCE_PROPERTIES_FILE);
  }
  public static Project getServerSuiteProject(String uri) {

    InstanceProperties ip = InstanceProperties.getInstanceProperties(uri);
    String suiteLocation;
    if (ip != null) {
      suiteLocation = SuiteUtil.getSuiteProjectLocation(ip);
    } else {
      // extract from url
      String s = SuiteConstants.SUITE_URL_ID; // ":server:suite:project:";
      int i = uri.indexOf(s);
      suiteLocation = uri.substring(i + s.length());
      Path path = Paths.get(suiteLocation);
      suiteLocation = path.getParent().toString().replace("\\", "/");
    }
    if (suiteLocation == null || !new File(suiteLocation).exists()) {
      // May be not a native plugin server
      return null;
    }
    return BaseUtil.getOwnerProject(FileUtil.toFileObject(new File(suiteLocation)));
  }
  @Override
  public void run() {
    String command = this.command;
    // BaseUtil.out("BaseDeployProgressObject run command=" + command  );

    if (!isCompleteImmediately()) {
      //
      // actual execution
      //
      // BaseUtil.out("BaseDeployProgressObject run 1 command" + command  );

      executeServerCommand();
    }
    CommandType commandType = CommandType.DISTRIBUTE;
    switch (command) {
      case "deploy":
        onDeploy();
        commandType = CommandType.DISTRIBUTE;
        break;
      case "undeploy":
        onUndeploy();
        commandType = CommandType.UNDEPLOY;
        break;
      case "redeploy":
        onRedeploy();
        commandType = CommandType.REDEPLOY;
        break;
      case "start":
        commandType = CommandType.START;
        break;
      case "stop":
        commandType = CommandType.STOP;
        break;
    }
    try {
      fireCompleted(commandType, getManager().getDefaultTarget().getName());
    } catch (Throwable ex) {
      BaseUtil.out("BaseDeploProgressObject run() EXCEPTION " + ex.getMessage());
      LOG.log(Level.INFO, ex.getMessage());
    }
  }
  public static BaseDeploymentManager getManager(Project serverInstance) {

    return BaseUtil.managerOf(serverInstance);
  }
  public int unregister(Project webApp) {
    int result = SUCCESS;
    Path serverDir = Paths.get(serverInstance.getProjectDirectory().getPath());
    String root = serverDir.getRoot().toString().replaceAll(":", "_");
    if (root.startsWith("/")) {
      root = root.substring(1);
    }
    Path targetPath = serverDir.getRoot().relativize(serverDir);
    String tmp = System.getProperty("java.io.tmpdir");

    Path target = Paths.get(tmp, SuiteConstants.TMP_DIST_WEB_APPS, root, targetPath.toString());

    File file = target.toFile();
    if (!file.exists()) {
      try {
        FileUtil.createFolder(file);
      } catch (IOException ex) {
        result = CREATE_FOLDER_ERROR;
        LOG.log(Level.INFO, ex.getMessage());
      }
    }

    FileObject propsFo =
        FileUtil.toFileObject(target.toFile())
            .getFileObject(SuiteConstants.SERVER_INSTANCE_WEB_APPS_PROPS);
    Properties props = new Properties();
    if (propsFo != null) {
      props = BaseUtil.loadProperties(propsFo);
      try {
        propsFo.delete();
      } catch (IOException ex) {
        result = CREATE_FOLDER_ERROR;
        LOG.log(Level.INFO, ex.getMessage());
      }
    }

    WebModule wm = WebModule.getWebModule(webApp.getProjectDirectory());

    String cp = wm.getContextPath();

    if (cp != null) {
      props.remove(cp);
      FileObject targetFo = FileUtil.toFileObject(target.toFile());
    } else {
      result = CONTEXTPATH_NOT_FOUND;
    }
    if (result == SUCCESS) {
      BaseUtil.storeProperties(
          props,
          FileUtil.toFileObject(target.toFile()),
          SuiteConstants.SERVER_INSTANCE_WEB_APPS_PROPS);
      String uri = SuiteManager.getManager(serverInstance).getUri();
      Project suite = SuiteManager.getServerSuiteProject(uri);
      if (suite == null) {
        result = NOT_A_SUITE;
      } else {
        SuiteNotifier sn = suite.getLookup().lookup(SuiteNotifier.class);
        sn.childrenChanged(this, webApp);
      }
    }
    return result;
  }