/** Performs the necessary initialization. */
  protected void init() {
    if (debug) {
      DebugTrace.print(this, "init"); // $NON-NLS-1$
    }

    LibraryPlugin.getDefault()
        .getPreferenceStore()
        .addPropertyChangeListener(preferenceStoreChangeListener);

    // Create the adapter factory.
    List factories = new ArrayList();
    factories.add(new ResourceItemProviderAdapterFactory());
    factories.add(new ReflectiveItemProviderAdapterFactory());
    ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(factories);

    // Create the command stack.
    BasicCommandStack commandStack = new BasicCommandStack();

    // Create the resource set.
    ILibraryResourceSet resourceSet = createResourceSet();
    resourceSet.addRefreshListener(refreshListener);
    RefreshJob.getInstance().setResourceSet(resourceSet);

    // Initialize the library save options.
    saveOptions = resourceSet.getDefaultSaveOptions();
    saveOptions.put(
        MultiFileXMISaveImpl.DISCARD_UNRESOLVED_REFERENCES,
        LibraryPreferences.getDiscardUnresolvedReferences());

    // Create the editing domain.
    editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, resourceSet);

    // Register the editing domain.
    registerEditingDomain(editingDomain);
  }
  /**
   * Checks whether the managed method library has any unresolved proxy.
   *
   * @return <code>true</code> if the managed method library has an unresolved proxy.
   */
  public boolean hasUnresolvedProxy() {
    if (debug) {
      DebugTrace.print(this, "hasUnresolvedProxy"); // $NON-NLS-1$
    }

    ILibraryResourceSet resourceSet = ((ILibraryResourceSet) editingDomain.getResourceSet());
    return resourceSet.hasUnresolvedProxy();
  }
  /**
   * Closes the managed method library.
   *
   * @return a method library
   * @throw <code>LibraryServiceException</code> if an error occurs while performing the operation
   */
  public void closeMethodLibrary() throws LibraryServiceException {
    if (debug) {
      String msg =
          "library="
              + library
              + ", memory on entry=" //$NON-NLS-1$ //$NON-NLS-2$
              + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
      DebugTrace.print(this, "closeMethodLibrary", msg); // $NON-NLS-1$
    }

    // String libPath = LibraryService.getInstance()
    // .getCurrentMethodLibraryPath();
    File libFile = new File(library.eResource().getURI().toFileString());
    String libPath = libFile.getParentFile().getAbsolutePath();

    // remove the configuration managers associated with this library
    LibraryService.getInstance().removeConfigurationManagers(library);

    removeResourceChangedListeners();

    // Clear the temp layout resources.
    LayoutResources.clear();

    Suppression.clearCachedSuppressions();

    TngUtil.umaItemProviderAdapterFactory.dispose();

    ILibraryResourceSet resourceSet = (ILibraryResourceSet) editingDomain.getResourceSet();
    resourceSet.unload();

    try {
      // Close the method library project file.
      MethodLibraryProject.closeProject(libPath, null);
    } catch (Exception e) {
      throw new LibraryServiceException(e);
    }

    RefreshJob.getInstance().reset();

    // Activates the garbage collector.
    Runtime.getRuntime().gc();

    if (debug) {
      String msg =
          "library="
              + library
              + ", memory on exit=" //$NON-NLS-1$ //$NON-NLS-2$
              + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
      DebugTrace.print(this, "closeMethodLibrary", msg); // $NON-NLS-1$
    }
  }
 /**
  * Gets a method element from the managed method library.
  *
  * @param guid the method element's GUID.
  * @return a method element of <code>null</code>
  */
 public MethodElement getMethodElement(String guid) {
   // this printed out too much useless information, comment out
   //		if (debug) {
   //			DebugTrace.print(this, "getMethodElement", "guid=" + guid); //$NON-NLS-1$ //$NON-NLS-2$
   //		}
   if (guid == null) {
     return null;
   }
   try {
     ILibraryResourceSet resourceSet = (ILibraryResourceSet) library.eResource().getResourceSet();
     if (resourceSet != null) {
       return (MethodElement) resourceSet.getEObject(guid);
     }
   } catch (Throwable th) {
     // Log error here
     th.printStackTrace();
   }
   return null;
 }
  /**
   * Saves the managed method library.
   *
   * @throw <code>LibraryServiceException</code> if an error occurs while performing the operation
   */
  public void saveMethodLibrary() throws LibraryServiceException {
    if (debug) {
      DebugTrace.print(this, "saveMethodLibrary"); // $NON-NLS-1$
    }

    try {
      if (library != null) {
        skipEventProcessing = true;

        ILibraryResourceSet resourceSet = ((ILibraryResourceSet) editingDomain.getResourceSet());
        resourceSet.save(saveOptions);

        ((BasicCommandStack) editingDomain.getCommandStack()).saveIsDone();

        skipEventProcessing = false;

        firePropertyChange(library, PROP_DIRTY);
      }
    } catch (Exception e) {
      throw new LibraryServiceException(e);
    } finally {
      skipEventProcessing = false;
    }
  }
  private Collection doReloadResources(Collection resources) {
    if (debug) {
      DebugTrace.print(this, "reloadResources"); // $NON-NLS-1$
    }
    if (library == null) {
      return Collections.EMPTY_LIST;
    }

    // check if resources to reload contains any elements cached in
    // LibraryService
    // to update them
    //
    LibraryService libSvc = (LibraryService) LibraryService.getInstance();
    Resource currentLibResource = null;
    ILibraryManager currentLibMgr = null;
    Resource currentConfigResource = null;
    MethodConfiguration currentConfig = null;
    List configResources = new ArrayList();
    List configs = new ArrayList();
    for (Iterator iter = resources.iterator(); iter.hasNext(); ) {
      Resource resource = (Resource) iter.next();
      MethodElement e = PersistenceUtil.getMethodElement(resource);
      if (e == libSvc.getCurrentMethodLibrary()) {
        currentLibMgr = libSvc.getCurrentLibraryManager();
        currentLibResource = resource;
      } else if (e == libSvc.getCurrentMethodConfiguration()) {
        currentConfigResource = resource;
        currentConfig = libSvc.getCurrentMethodConfiguration();
      } else if (e instanceof MethodConfiguration) {
        configResources.add(resource);
        configs.add(e);
      }
    }

    ILibraryResourceSet resourceSet = (ILibraryResourceSet) library.eResource().getResourceSet();
    Collection reloadedResources = resourceSet.reloadResources(resources);
    if (!reloadedResources.isEmpty()) {
      if (currentLibResource != null || currentConfigResource != null) {
        // update cached elements in LibraryService and this library
        // manager
        //
        for (Iterator iter = reloadedResources.iterator(); iter.hasNext(); ) {
          Resource resource = (Resource) iter.next();
          if (resource == currentLibResource) {
            MethodElement e = PersistenceUtil.getMethodElement(resource);
            if (e instanceof MethodLibrary) {
              MethodLibrary newLib = (MethodLibrary) e;
              libSvc.setCurrentMethodLibrary(newLib);
              if (currentLibMgr instanceof AbstractLibraryManager) {
                libSvc.removeLibraryManager(currentLibMgr);
                ((AbstractLibraryManager) currentLibMgr).updateMethodLibrary(newLib);
                libSvc.setLibraryManager(currentLibMgr);
              }
            }
          }
          if (resource == currentConfigResource) {
            MethodElement e = PersistenceUtil.getMethodElement(resource);
            if (e instanceof MethodConfiguration) {
              // remove config manager of old current config
              //
              libSvc.removeConfigurationManager(currentConfig);
              MethodConfiguration config = (MethodConfiguration) e;
              libSvc.setCurrentMethodConfiguration(config);
            }
          } else if (!configResources.isEmpty()) {
            int id = configResources.indexOf(resource);
            if (id != -1) {
              // remove config manager of old config
              //
              libSvc.removeConfigurationManager((MethodConfiguration) configs.get(id));
            }
          }
        }
      }

      // TODO: Review implementation.
      Suppression.cleanUp();
    }
    return reloadedResources;
  }