Пример #1
0
 /**
  * @param object The object to get an XML representation of
  * @param shared true if the XML should be shared, false if the XML should be per-node
  * @return An XML element representing object
  */
 public static Element elementFromObject(Object object, boolean shared) {
   String aName = adapterName(object);
   log.debug("store using {}", aName);
   XmlAdapter adapter = null;
   try {
     adapter = (XmlAdapter) Class.forName(adapterName(object)).newInstance();
   } catch (java.lang.ClassNotFoundException
       | java.lang.IllegalAccessException
       | java.lang.InstantiationException ex) {
     log.error("Cannot load configuration adapter for {}", object.getClass().getName(), ex);
   }
   if (adapter != null) {
     return adapter.store(object, shared);
   } else {
     log.error("Cannot store configuration for {}", object.getClass().getName());
     return null;
   }
 }
Пример #2
0
  @Override
  public boolean loadDeferred(URL url) {
    boolean result = true;
    // Now process the load-later list
    log.debug("Start processing deferred load list (size): " + loadDeferredList.size());
    if (!loadDeferredList.isEmpty()) {
      for (Element item : loadDeferredList) {
        String adapterName = item.getAttribute("class").getValue();
        log.debug("deferred load via " + adapterName);
        XmlAdapter adapter = null;
        try {
          adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
          boolean loadStatus = adapter.load(item, null);
          log.debug("deferred load status for " + adapterName + " is " + loadStatus);

          // if any adaptor load fails, then the entire load has failed
          if (!loadStatus) {
            result = false;
          }
        } catch (Exception e) {
          creationErrorEncountered(
              adapter,
              "deferred load(" + url.getFile() + ")",
              "Unexpected error (Exception)",
              null,
              null,
              e);
          result = false; // keep going, but return false to signal problem
        } catch (Throwable et) {
          creationErrorEncountered(
              adapter,
              "in deferred load(" + url.getFile() + ")",
              "Unexpected error (Throwable)",
              null,
              null,
              et);
          result = false; // keep going, but return false to signal problem
        }
      }
    }
    log.debug("Done processing deferred load list with result: " + result);
    return result;
  }
Пример #3
0
  /**
   * Load a file.
   *
   * <p>Handles problems locally to the extent that it can, by routing them to the
   * creationErrorEncountered method.
   *
   * @param url URL of file to load
   * @param registerDeferred true to register objects to defer
   * @return true if no problems during the load
   * @throws JmriConfigureXmlException
   * @see jmri.configurexml.XmlAdapter#loadDeferred()
   * @since 3.3.2
   */
  @Override
  public boolean load(URL url, boolean registerDeferred) throws JmriConfigureXmlException {
    boolean result = true;
    Element root = null;
    /* We will put all the elements into a load list, along with the load order
    As XML files prior to 2.13.1 had no order to the store, beans would be stored/loaded
    before beans that they were dependant upon had been stored/loaded
    */
    Map<Element, Integer> loadlist =
        Collections.synchronizedMap(new LinkedHashMap<Element, Integer>());

    try {
      root = super.rootFromURL(url);
      // get the objects to load
      List<Element> items = root.getChildren();
      for (int i = 0; i < items.size(); i++) {
        // Put things into an ordered list
        Element item = items.get(i);
        if (item.getAttribute("class") == null) {
          // this is an element that we're not meant to read
          if (log.isDebugEnabled()) {
            log.debug("skipping " + item);
          }
          continue;
        }
        String adapterName = item.getAttribute("class").getValue();
        if (log.isDebugEnabled()) {
          log.debug("attempt to get adapter " + adapterName + " for " + item);
        }
        XmlAdapter adapter = null;

        adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
        int order = adapter.loadOrder();
        if (log.isDebugEnabled()) {
          log.debug("add " + item + " to load list with order id of " + order);
        }
        loadlist.put(item, order);
      }

      ArrayList<Map.Entry<Element, Integer>> l =
          new ArrayList<Map.Entry<Element, Integer>>(loadlist.entrySet());
      Collections.sort(
          l,
          new Comparator<Map.Entry<Element, Integer>>() {

            public int compare(Map.Entry<Element, Integer> o1, Map.Entry<Element, Integer> o2) {
              return o1.getValue().compareTo(o2.getValue());
            }
          });
      for (int i = 0; i < l.size(); i++) {
        Element item = l.get(i).getKey();
        String adapterName = item.getAttribute("class").getValue();
        if (log.isDebugEnabled()) {
          log.debug("load " + item + " via " + adapterName);
        }
        XmlAdapter adapter = null;
        try {
          adapter = (XmlAdapter) Class.forName(adapterName).newInstance();

          // get version info
          // loadVersion(root, adapter);
          // and do it
          if (adapter.loadDeferred() && registerDeferred) {
            // register in the list for deferred load
            loadDeferredList.add(item);
            if (log.isDebugEnabled()) {
              log.debug("deferred load registered for " + item + " " + adapterName);
            }
          } else {
            boolean loadStatus = adapter.load(item, null);
            if (log.isDebugEnabled()) {
              log.debug("load status for " + item + " " + adapterName + " is " + loadStatus);
            }

            // if any adaptor load fails, then the entire load has failed
            if (!loadStatus) {
              result = false;
            }
          }
        } catch (Exception e) {
          creationErrorEncountered(
              adapter,
              "load(" + url.getFile() + ")",
              "Unexpected error (Exception)",
              null,
              null,
              e);

          result = false; // keep going, but return false to signal problem
        } catch (Throwable et) {
          creationErrorEncountered(
              adapter,
              "in load(" + url.getFile() + ")",
              "Unexpected error (Throwable)",
              null,
              null,
              et);

          result = false; // keep going, but return false to signal problem
        }
      }

    } catch (java.io.FileNotFoundException e1) {
      // this returns false to indicate un-success, but not enough
      // of an error to require a message
      creationErrorEncountered(
          null, "opening file " + url.getFile(), "File not found", null, null, e1);
      result = false;
    } catch (org.jdom2.JDOMException e) {
      creationErrorEncountered(null, "parsing file " + url.getFile(), "Parse error", null, null, e);
      result = false;
    } catch (java.lang.Exception e) {
      creationErrorEncountered(
          null, "loading from file " + url.getFile(), "Unknown error (Exception)", null, null, e);
      result = false;
    } finally {
      // no matter what, close error reporting
      handler.done();
    }

    /*try {
    root = super.rootFromFile(fi);
    // get the objects to load
    List<Element> items = root.getChildren();
    for (int i = 0; i<items.size(); i++) {
    // get the class, hence the adapter object to do loading
    Element item = items.get(i);
    if (item.getAttribute("class") == null) {
    // this is an element that we're not meant to read
    continue;
    }
    String adapterName = item.getAttribute("class").getValue();
    log.debug("load via "+adapterName);
    XmlAdapter adapter = null;

    try {
    adapter = (XmlAdapter)Class.forName(adapterName).newInstance();

    // get version info
    // loadVersion(root, adapter);

    // and do it
    if (adapter.loadDeferred() && registerDeferred) {
    // register in the list for deferred load
    loadDeferredList.add(item);
    log.debug("deferred load registered for " + adapterName);
    } else {
    boolean loadStatus = adapter.load(item);
    log.debug("load status for "+adapterName+" is "+loadStatus);

    // if any adaptor load fails, then the entire load has failed
    if (!loadStatus)
    result = false;
    }
    } catch (Exception e) {
    creationErrorEncountered (adapter, "load("+fi.getName()+")",Level.ERROR,
    "Unexpected error (Exception)",null,null,e);

    result = false;  // keep going, but return false to signal problem
    } catch (Throwable et) {
    creationErrorEncountered (adapter, "in load("+fi.getName()+")", Level.ERROR,
    "Unexpected error (Throwable)",null,null,et);

    result = false;  // keep going, but return false to signal problem
    }
    }


    } catch (java.io.FileNotFoundException e1) {
    // this returns false to indicate un-success, but not enough
    // of an error to require a message
    creationErrorEncountered (null, "opening file "+fi.getName(), Level.ERROR,
    "File not found", null,null,e1);
    result = false;
    } catch (org.jdom2.JDOMException e) {
    creationErrorEncountered (null, "parsing file "+fi.getName(), Level.ERROR,
    "Parse error", null,null,e);
    result = false;
    } catch (java.lang.Exception e) {
    creationErrorEncountered (null, "loading from file "+fi.getName(), Level.ERROR,
    "Unknown error (Exception)", null,null,e);
    result = false;
    } finally {
    // no matter what, close error reporting
    handler.done();
    }*/
    // loading complete, as far as it got, make history entry
    FileHistory r = InstanceManager.getDefault(FileHistory.class);
    if (r != null) {
      FileHistory included = null;
      if (root != null) {
        Element filehistory = root.getChild("filehistory");
        if (filehistory != null) {
          included = jmri.jmrit.revhistory.configurexml.FileHistoryXml.loadFileHistory(filehistory);
        }
      }
      r.addOperation((result ? "Load OK" : "Load with errors"), url.getFile(), included);
    } else {
      log.info("Not recording file history");
    }

    return result;
  }