/**
   * Get the {@link ProgrammerConfig} with the given ID.
   *
   * <p>If the config has been requested before, a reference to the config in the internal cache is
   * returned. All modifications to the returned config will affect the config in the cache.
   *
   * <p>While these changes are only persisted when saveConfig() is called, it is usually better to
   * use the {@link #getConfigEditable(ProgrammerConfig)} call to get a safely modifiable config.
   *
   * @see #getConfigEditable(ProgrammerConfig)
   * @param id <code>String</code> with an ID value.
   * @return The requested <code>ProgrammerConfig</code> or <code>null</code> if no config with the
   *     given ID exists.
   */
  public ProgrammerConfig getConfig(String id) {

    // Test for empty / null id
    if (id == null) return null;
    if (id.length() == 0) return null;

    // Test if the config is already in the cache
    if (fConfigsCache.containsKey(id)) {
      return fConfigsCache.get(id);
    }

    // The config was not in the cache

    // The node must exist, otherwise return null
    try {

      if (!fPreferences.nodeExists(id)) {
        return null;
      }
    } catch (BackingStoreException bse) {
      // TODO What shall we do if we can't access the Preferences?
      // For now log an error and return null.
      logException(bse);
      return null;
    }

    // Load the Config from the Preferences
    Preferences cfgprefs = fPreferences.node(id);
    ProgrammerConfig config = new ProgrammerConfig(id, cfgprefs);

    fConfigsCache.put(id, config);

    return config;
  }
  /**
   * Create a new ProgrammerConfig.
   *
   * <p>The returned ProgrammerConfig is filled with some default values. It is not created in the
   * preference store.
   *
   * <p>Call {@link #saveConfig(ProgrammerConfig)} with the returned config to persist any
   * modifications and to add the newly created config to the list of all existing configs.
   *
   * @return A new <code>ProgrammerConfig</code>
   */
  public ProgrammerConfig createNewConfig() {
    // The id has the form "programmerconfig.#" where # is a running
    // number.

    // Find the first free id.
    // Check both the list of existing config nodes in the preferences and
    // the list of pending config ids (ids that have been assigned, but not
    // yet saved).
    String newid = null;
    int i = 1;

    try {
      do {
        newid = CONFIG_PREFIX + i++;
      } while (fPreferences.nodeExists(newid) || fPendingIds.contains(newid));
    } catch (BackingStoreException bse) {
      // TODO What shall we do if we can't access the Preferences?
      // For now log an error and return null.
      logException(bse);
      return null;
    }
    ProgrammerConfig newconfig = new ProgrammerConfig(newid);

    fPendingIds.add(newid);

    return newconfig;
  }
예제 #3
0
 /**
  * Returns the preference node associated with this request. This method controls exactly what
  * preference nodes are exposed via this service. If there is no matching preference node for the
  * request, this method handles the appropriate response and returns <code>null</code>.
  *
  * @param req
  * @param resp
  * @param create If <code>true</code>, the node will be created if it does not already exist. If
  *     <code>false</code>, this method sets the response status to 404 and returns null.
  */
 private IEclipsePreferences getNode(
     HttpServletRequest req, HttpServletResponse resp, boolean create) throws ServletException {
   if (prefRoot == null) {
     handleException(resp, "Unable to obtain preference service", null);
     return null;
   }
   String pathString = req.getPathInfo();
   if (pathString == null) pathString = ""; // $NON-NLS-1$
   IPath path = new Path(pathString);
   int segmentCount = path.segmentCount();
   String scope = path.segment(0);
   // note that the preference service API scope names don't match those used in our persistence
   // layer.
   IPath nodePath = null;
   if ("user".equalsIgnoreCase(scope)) { // $NON-NLS-1$
     String username = req.getRemoteUser();
     if (username == null) {
       resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
       return null;
     }
     nodePath = new Path("Users").append(username); // $NON-NLS-1$
   } else if ("workspace".equalsIgnoreCase(scope) && segmentCount > 1) { // $NON-NLS-1$
     nodePath = new Path("Workspaces"); // $NON-NLS-1$
   } else if ("project".equalsIgnoreCase(scope) && segmentCount > 1) { // $NON-NLS-1$
     nodePath = new Path("Projects"); // $NON-NLS-1$
   } else {
     // invalid prefix
     handleNotFound(req, resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
     return null;
   }
   // we allow arbitrary subtrees beneath our three supported roots
   if (nodePath != null) {
     String childPath = nodePath.append(path.removeFirstSegments(1)).toString();
     try {
       if (create || prefRoot.nodeExists(childPath))
         return (IEclipsePreferences) prefRoot.node(childPath);
     } catch (BackingStoreException e) {
       String msg = NLS.bind("Error retrieving preferences for path {0}", pathString);
       handleException(
           resp,
           new Status(IStatus.ERROR, Activator.PI_SERVER_SERVLETS, msg),
           HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
       return null;
     }
   }
   handleNotFound(req, resp, HttpServletResponse.SC_NOT_FOUND);
   return null;
 }
  /**
   * Deletes the given configuration from the preference storage area.
   *
   * <p>Note: This Object is still valid and further calls to {@link #saveConfig(ProgrammerConfig)}
   * will add this configuration back to the preference storage.
   *
   * @throws BackingStoreException
   */
  public void deleteConfig(ProgrammerConfig config) throws BackingStoreException {

    String id = config.getId();

    // If the config is in the cache, remove it from the cache
    if (fConfigsCache.containsKey(id)) {
      fConfigsCache.remove(id);
    }

    // Remove the Preference node for the config and flush the preferences
    // If the node does not exist do nothing - no need to create the node
    // just to remove it again
    if (fPreferences.nodeExists(id)) {
      Preferences cfgnode = fPreferences.node(id);
      cfgnode.removeNode();
      fPreferences.flush();
    }
  }
  /**
   * Test if the given ID is valid, i.e. a <code>ProgrammerConfig</code> with the given ID exists in
   * the Preferences.
   *
   * @param id <code>String</code> with the ID value to test
   * @return <code>true</code> if a config with the given ID exists in the Preferences.
   */
  public boolean isValidId(String id) {
    // Test the cache first (quicker)
    if (fConfigsCache.containsKey(id)) {
      return true;
    }

    // Not in the cache, try the preferences
    try {
      if (fPreferences.nodeExists(id)) {
        return true;
      }
    } catch (BackingStoreException bse) {
      // TODO What shall we do if we can't access the Preferences?
      // For now log an error and return false.
      logException(bse);
    }

    // Id not found anywhere
    return false;
  }
 /**
  * Loads the system property defaults from the preferences.
  *
  * @return the system property defaults.
  */
 public static Collection<SystemPropertyPreferenceEntry> loadFromPreferences() {
   Collection<SystemPropertyPreferenceEntry> result =
       new ArrayList<SystemPropertyPreferenceEntry>();
   IEclipsePreferences platformPrefs = getPlatformPreferences();
   Preferences systemPropertyPrefs;
   try {
     if (platformPrefs.nodeExists(PREFERENCE_NODE)) {
       systemPropertyPrefs = platformPrefs.node(PREFERENCE_NODE);
     } else {
       systemPropertyPrefs = getDefaultPlatformPreferences().node(PREFERENCE_NODE);
     }
     String[] keys = systemPropertyPrefs.keys();
     for (String key : keys) {
       String value = systemPropertyPrefs.get(key, "");
       SystemPropertyPreferenceEntry entry = new SystemPropertyPreferenceEntry(key, value);
       result.add(entry);
     }
   } catch (BackingStoreException e) {
     log.log(Level.SEVERE, "Error reading preferences", e);
   }
   return result;
 }
예제 #7
0
 private static Collection<SonarServer> loadServers() {
   IEclipsePreferences rootNode = new InstanceScope().getNode(NODE);
   List<SonarServer> servers = Lists.newArrayList();
   try {
     rootNode.sync();
     if (rootNode.nodeExists(PREF_SERVERS)) {
       Preferences serversNode = rootNode.node(PREF_SERVERS);
       for (String encodedUrl : serversNode.childrenNames()) {
         Preferences serverNode = serversNode.node(encodedUrl);
         String url = EncodingUtils.decodeSlashes(encodedUrl);
         boolean auth = serverNode.getBoolean("auth", false);
         servers.add(new SonarServer(url, auth));
       }
     } else {
       // Defaults
       return Arrays.asList(
           new SonarServer("http://localhost:9000"),
           new SonarServer("http://nemo.sonarsource.org"));
     }
   } catch (BackingStoreException e) {
     LoggerFactory.getLogger(SecurityManager.class).error(e.getMessage(), e);
   }
   return servers;
 }
  public void initializeDefaultPreferences() {
    IScopeContext context = new DefaultScope();
    IEclipsePreferences node =
        context.getNode(WorkbenchPlugin.getDefault().getBundle().getSymbolicName());

    node.putBoolean(IPreferenceConstants.SHOULD_PROMPT_FOR_ENABLEMENT, true);

    node.putBoolean(IPreferenceConstants.EDITORLIST_PULLDOWN_ACTIVE, false);
    node.putBoolean(IPreferenceConstants.EDITORLIST_DISPLAY_FULL_NAME, false);
    node.putBoolean(IPreferenceConstants.STICKY_CYCLE, false);
    node.putBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, false);
    node.putBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS, true);
    node.putInt(IPreferenceConstants.REUSE_EDITORS, 8);
    node.putBoolean(IPreferenceConstants.OPEN_ON_SINGLE_CLICK, false);
    node.putBoolean(IPreferenceConstants.SELECT_ON_HOVER, false);
    node.putBoolean(IPreferenceConstants.OPEN_AFTER_DELAY, false);
    node.putInt(IPreferenceConstants.RECENT_FILES, 4);

    node.putInt(IWorkbenchPreferenceConstants.VIEW_TAB_POSITION, SWT.TOP);
    node.putInt(IWorkbenchPreferenceConstants.EDITOR_TAB_POSITION, SWT.TOP);

    node.putBoolean(IWorkbenchPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS, true);
    node.putBoolean(IWorkbenchPreferenceConstants.DISABLE_OPEN_EDITOR_IN_PLACE, false);

    node.putBoolean(IPreferenceConstants.USE_IPERSISTABLE_EDITORS, true);

    node.putInt(IPreferenceConstants.EDITOR_TAB_WIDTH, 3); // high
    node.putInt(IPreferenceConstants.OPEN_VIEW_MODE, IPreferenceConstants.OVM_EMBED);
    node.putInt(IPreferenceConstants.OPEN_PERSP_MODE, IPreferenceConstants.OPM_ACTIVE_PAGE);
    node.put(IPreferenceConstants.ENABLED_DECORATORS, ""); // $NON-NLS-1$
    node.putInt(
        IPreferenceConstants.EDITORLIST_SELECTION_SCOPE,
        IPreferenceConstants.EDITORLIST_SET_PAGE_SCOPE); // Current
    // Window
    node.putInt(
        IPreferenceConstants.EDITORLIST_SORT_CRITERIA,
        IPreferenceConstants.EDITORLIST_NAME_SORT); // Name Sort
    node.putBoolean(IPreferenceConstants.COLOR_ICONS, true);
    node.putInt(IPreferenceConstants.KEYS_PREFERENCE_SELECTED_TAB, 0);
    node.putBoolean(IPreferenceConstants.MULTI_KEY_ASSIST, true);
    node.putInt(IPreferenceConstants.MULTI_KEY_ASSIST_TIME, 1000);

    // Temporary option to enable wizard for project capability
    node.putBoolean("ENABLE_CONFIGURABLE_PROJECT_WIZARD", false); // $NON-NLS-1$
    // Temporary option to enable single click
    node.putInt("SINGLE_CLICK_METHOD", OpenStrategy.DOUBLE_CLICK); // $NON-NLS-1$
    // Temporary option to enable cool bars
    node.putBoolean("ENABLE_COOL_BARS", true); // $NON-NLS-1$
    // Temporary option to enable new menu organization
    node.putBoolean("ENABLE_NEW_MENUS", true); // $NON-NLS-1$
    // Temporary option to turn off the dialog font
    node.putBoolean("DISABLE_DIALOG_FONT", false); // $NON-NLS-1$

    // Heap status preferences
    node.putBoolean(IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR, false);
    node.putInt(IHeapStatusConstants.PREF_UPDATE_INTERVAL, 500);
    node.putBoolean(IHeapStatusConstants.PREF_SHOW_MAX, false);
    node.putBoolean(IPreferenceConstants.OVERRIDE_PRESENTATION, false);

    IEclipsePreferences rootNode =
        (IEclipsePreferences)
            Platform.getPreferencesService().getRootNode().node(InstanceScope.SCOPE);

    final String workbenchName = WorkbenchPlugin.getDefault().getBundle().getSymbolicName();
    try {
      if (rootNode.nodeExists(workbenchName)) {
        ((IEclipsePreferences) rootNode.node(workbenchName))
            .addPreferenceChangeListener(PlatformUIPreferenceListener.getSingleton());
      }
    } catch (BackingStoreException e) {
      IStatus status =
          new Status(
              IStatus.ERROR,
              WorkbenchPlugin.getDefault().getBundle().getSymbolicName(),
              IStatus.ERROR,
              e.getLocalizedMessage(),
              e);
      WorkbenchPlugin.getDefault().getLog().log(status);
    }
  }