private MApplication loadApplicationModel(
      IApplicationContext appContext, IEclipseContext eclipseContext) {
    MApplication theApp = null;

    Location instanceLocation = WorkbenchSWTActivator.getDefault().getInstanceLocation();

    URI applicationModelURI = determineApplicationModelURI(appContext);
    eclipseContext.set(E4Workbench.INITIAL_WORKBENCH_MODEL_URI, applicationModelURI);

    // Save and restore
    Boolean saveAndRestore =
        getArgValue(IWorkbench.PERSIST_STATE, appContext, false)
            .map(value -> Boolean.parseBoolean(value))
            .orElse(Boolean.TRUE);

    eclipseContext.set(IWorkbench.PERSIST_STATE, saveAndRestore);

    // when -data @none or -data @noDefault options
    if (instanceLocation != null && instanceLocation.getURL() != null) {
      eclipseContext.set(E4Workbench.INSTANCE_LOCATION, instanceLocation);
    } else {
      eclipseContext.set(IWorkbench.PERSIST_STATE, false);
    }

    // Persisted state
    Boolean clearPersistedState =
        getArgValue(IWorkbench.CLEAR_PERSISTED_STATE, appContext, true)
            .map(value -> Boolean.parseBoolean(value))
            .orElse(Boolean.FALSE);
    eclipseContext.set(IWorkbench.CLEAR_PERSISTED_STATE, clearPersistedState);

    String resourceHandler =
        getArgValue(IWorkbench.MODEL_RESOURCE_HANDLER, appContext, false)
            .orElse("bundleclass://org.eclipse.e4.ui.workbench/" + ResourceHandler.class.getName());

    IContributionFactory factory = eclipseContext.get(IContributionFactory.class);

    handler = (IModelResourceHandler) factory.create(resourceHandler, eclipseContext);
    eclipseContext.set(IModelResourceHandler.class, handler);

    Resource resource = handler.loadMostRecentModel();
    theApp = (MApplication) resource.getContents().get(0);

    return theApp;
  }
  public void saveModel() {
    // Save the model into the targetURI
    if (lcManager != null && workbench != null) {
      ContextInjectionFactory.invoke(lcManager, PreSave.class, workbench.getContext(), null);
    }

    try {
      if (!(handler instanceof ResourceHandler)
          || ((ResourceHandler) handler).hasTopLevelWindows()) {
        handler.save();
      } else {
        Logger logger = new WorkbenchLogger(PLUGIN_ID);
        logger.error(
            new Exception(), // log a stack trace for debugging
            "Attempted to save a workbench model that had no top-level windows! " //$NON-NLS-1$
                + "Skipped saving the model to avoid corruption."); //$NON-NLS-1$
      }
    } catch (IOException e) {
      Logger logger = new WorkbenchLogger(PLUGIN_ID);
      logger.error(e, "Error saving the workbench model"); // $NON-NLS-1$
    }
  }