Search in sources :

Example 6 with ConversionException

use of com.thoughtworks.xstream.converters.ConversionException in project jmeter by apache.

the class JMeter method startGui.

/**
     * Starts up JMeter in GUI mode
     */
private void startGui(String testFile) {
    //NOSONAR
    System.out.println("================================================================================");
    //NOSONAR
    System.out.println("Don't use GUI mode for load testing, only for Test creation and Test debugging !");
    //NOSONAR
    System.out.println("For load testing, use NON GUI Mode:");
    //NOSONAR
    System.out.println("   jmeter -n -t [jmx file] -l [results file] -e -o [Path to output folder]");
    //NOSONAR
    System.out.println("& adapt Java Heap to your test requirements:");
    //NOSONAR
    System.out.println("   Modify HEAP=\"-Xms512m -Xmx512m\" in the JMeter batch file");
    //NOSONAR
    System.out.println("================================================================================");
    SplashScreen splash = new SplashScreen();
    splash.showScreen();
    String jMeterLaf = LookAndFeelCommand.getJMeterLaf();
    try {
        UIManager.setLookAndFeel(jMeterLaf);
    } catch (Exception ex) {
        log.warn("Could not set LAF to: {}", jMeterLaf, ex);
    }
    splash.setProgress(10);
    JMeterUtils.applyHiDPIOnFonts();
    PluginManager.install(this, true);
    JMeterTreeModel treeModel = new JMeterTreeModel();
    splash.setProgress(30);
    JMeterTreeListener treeLis = new JMeterTreeListener(treeModel);
    final ActionRouter instance = ActionRouter.getInstance();
    instance.populateCommandMap();
    splash.setProgress(60);
    treeLis.setActionHandler(instance);
    GuiPackage.initInstance(treeLis, treeModel);
    splash.setProgress(80);
    MainFrame main = new MainFrame(treeModel, treeLis);
    splash.setProgress(100);
    ComponentUtil.centerComponentInWindow(main, 80);
    main.setVisible(true);
    instance.actionPerformed(new ActionEvent(main, 1, ActionNames.ADD_ALL));
    if (testFile != null) {
        try {
            File f = new File(testFile);
            log.info("Loading file: {}", f);
            FileServer.getFileServer().setBaseForScript(f);
            HashTree tree = SaveService.loadTree(f);
            GuiPackage.getInstance().setTestPlanFile(f.getAbsolutePath());
            Load.insertLoadedTree(1, tree);
        } catch (ConversionException e) {
            log.error("Failure loading test file", e);
            JMeterUtils.reportErrorToUser(SaveService.CEtoString(e));
        } catch (Exception e) {
            log.error("Failure loading test file", e);
            JMeterUtils.reportErrorToUser(e.toString());
        }
    } else {
        JTree jTree = GuiPackage.getInstance().getMainFrame().getTree();
        TreePath path = jTree.getPathForRow(0);
        jTree.setSelectionPath(path);
        FocusRequester.requestFocus(jTree);
    }
    splash.setProgress(100);
    splash.close();
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) HashTree(org.apache.jorphan.collections.HashTree) ActionEvent(java.awt.event.ActionEvent) ActionRouter(org.apache.jmeter.gui.action.ActionRouter) ConfigurationException(org.apache.jmeter.report.config.ConfigurationException) JMeterException(org.apache.jorphan.util.JMeterException) FileNotFoundException(java.io.FileNotFoundException) ConversionException(com.thoughtworks.xstream.converters.ConversionException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) GenerationException(org.apache.jmeter.report.dashboard.GenerationException) MainFrame(org.apache.jmeter.gui.MainFrame) JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) JMeterTreeListener(org.apache.jmeter.gui.tree.JMeterTreeListener) File(java.io.File)

Example 7 with ConversionException

use of com.thoughtworks.xstream.converters.ConversionException in project ddf by codice.

the class XStreamAttributeCopier method copyXml.

/**
     * Copies the entire XML element {@code reader} is currently at into {@code writer} and returns
     * a new reader ready to read the copied element. After the call, {@code reader} will be at the
     * end of the element that was copied.
     * <p>
     * If {@code attributeMap} is provided, the attributes will be added to the copy.
     *
     * @param reader       the reader currently at the XML element you want to copy
     * @param writer       the writer that the element will be copied into
     * @param attributeMap the map of attribute names to values that will be added as attributes of
     *                     the copy, may be null
     * @return a new reader ready to read the copied element
     * @throws ConversionException if a parser to use for the new reader can't be created
     */
public static HierarchicalStreamReader copyXml(HierarchicalStreamReader reader, StringWriter writer, Map<String, String> attributeMap) {
    copyElementWithAttributes(reader, new CompactWriter(writer, new NoNameCoder()), attributeMap);
    XmlPullParser parser;
    try {
        parser = XmlPullParserFactory.newInstance().newPullParser();
    } catch (XmlPullParserException e) {
        throw new ConversionException("Unable to create XmlPullParser, cannot parse XML.", e);
    }
    try {
        // encoding will be used which will not always work
        return new XppReader(new InputStreamReader(IOUtils.toInputStream(writer.toString(), StandardCharsets.UTF_8.name())), parser);
    } catch (IOException e) {
        LOGGER.debug("Unable create reader with UTF-8 encoding, Exception {}", e);
        return new XppReader(new InputStreamReader(IOUtils.toInputStream(writer.toString())), parser);
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) InputStreamReader(java.io.InputStreamReader) XppReader(com.thoughtworks.xstream.io.xml.XppReader) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder)

Example 8 with ConversionException

use of com.thoughtworks.xstream.converters.ConversionException in project ddf by codice.

the class TransactionRequestConverter method getElementFromXml.

private <T> T getElementFromXml(String xml, Class<T> clazz) {
    JAXBElement<T> root;
    try {
        JAXBContext jaxbContext = getJaxBContext();
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xml));
        root = jaxbContext.createUnmarshaller().unmarshal(xmlStreamReader, clazz);
    } catch (JAXBException | XMLStreamException e) {
        throw new ConversionException(e);
    }
    return root.getValue();
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 9 with ConversionException

use of com.thoughtworks.xstream.converters.ConversionException in project hudson-2.x by hudson.

the class RobustReflectionConverter method doUnmarshal.

public Object doUnmarshal(final Object result, final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final SeenFields seenFields = new SeenFields();
    Iterator it = reader.getAttributeNames();
    // Remember outermost Saveable encountered, for reporting below
    if (result instanceof Saveable && context.get("Saveable") == null)
        context.put("Saveable", result);
    // Process attributes before recursing into child elements.
    while (it.hasNext()) {
        String attrAlias = (String) it.next();
        String attrName = mapper.attributeForAlias(attrAlias);
        Class classDefiningField = determineWhichClassDefinesField(reader);
        boolean fieldExistsInClass = fieldDefinedInClass(result, attrName);
        if (fieldExistsInClass) {
            Field field = reflectionProvider.getField(result.getClass(), attrName);
            SingleValueConverter converter = mapper.getConverterFromAttribute(field.getDeclaringClass(), attrName, field.getType());
            Class type = field.getType();
            if (converter == null) {
                converter = mapper.getConverterFromItemType(type);
            }
            if (converter != null) {
                Object value = converter.fromString(reader.getAttribute(attrAlias));
                if (type.isPrimitive()) {
                    type = Primitives.box(type);
                }
                if (value != null && !type.isAssignableFrom(value.getClass())) {
                    throw new ConversionException("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
                }
                reflectionProvider.writeField(result, attrName, value, classDefiningField);
                seenFields.add(classDefiningField, attrName);
            }
        }
    }
    Map implicitCollectionsForCurrentObject = null;
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        try {
            String fieldName = mapper.realMember(result.getClass(), reader.getNodeName());
            boolean implicitCollectionHasSameName = mapper.getImplicitCollectionDefForFieldName(result.getClass(), reader.getNodeName()) != null;
            Class classDefiningField = determineWhichClassDefinesField(reader);
            boolean fieldExistsInClass = !implicitCollectionHasSameName && fieldDefinedInClass(result, fieldName);
            Class type = determineType(reader, fieldExistsInClass, result, fieldName, classDefiningField);
            final Object value;
            if (fieldExistsInClass) {
                Field field = reflectionProvider.getField(result.getClass(), fieldName);
                value = unmarshalField(context, result, type, field);
                // TODO the reflection provider should have returned the proper field in first place ....
                Class definedType = reflectionProvider.getFieldType(result, fieldName, classDefiningField);
                if (!definedType.isPrimitive()) {
                    type = definedType;
                }
            } else {
                value = context.convertAnother(result, type);
            }
            if (value != null && !type.isAssignableFrom(value.getClass())) {
                LOGGER.warning("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
            // behave as if we didn't see this element
            } else {
                if (fieldExistsInClass) {
                    reflectionProvider.writeField(result, fieldName, value, classDefiningField);
                    seenFields.add(classDefiningField, fieldName);
                } else {
                    implicitCollectionsForCurrentObject = writeValueToImplicitCollection(context, value, implicitCollectionsForCurrentObject, result, fieldName);
                }
            }
        } catch (NonExistentFieldException e) {
            LOGGER.log(WARNING, "Skipping a non-existent field " + e.getFieldName());
            addErrorInContext(context, e);
        } catch (CannotResolveClassException e) {
            LOGGER.log(WARNING, "Skipping a non-existent type " + e.getMessage());
            addErrorInContext(context, e);
        } catch (LinkageError e) {
            LOGGER.log(WARNING, "Failed to resolve a type " + e.getMessage());
            addErrorInContext(context, e);
        }
        reader.moveUp();
    }
    // Report any class/field errors in Saveable objects
    if (context.get("ReadError") != null && context.get("Saveable") == result) {
        OldDataMonitor.report((Saveable) result, (ArrayList<Throwable>) context.get("ReadError"));
        context.put("ReadError", null);
    }
    return result;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Saveable(hudson.model.Saveable) NonExistentFieldException(com.thoughtworks.xstream.converters.reflection.NonExistentFieldException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) SingleValueConverter(com.thoughtworks.xstream.converters.SingleValueConverter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with ConversionException

use of com.thoughtworks.xstream.converters.ConversionException in project hudson-2.x by hudson.

the class RobustReflectionConverterTest method testIfWeNeedWorkaround.

public void testIfWeNeedWorkaround() {
    try {
        read(new XStream());
        fail();
    } catch (ConversionException e) {
        // expected
        assertTrue(e.getMessage().contains("z"));
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) XStream(com.thoughtworks.xstream.XStream)

Aggregations

ConversionException (com.thoughtworks.xstream.converters.ConversionException)21 IOException (java.io.IOException)5 InputStreamReader (java.io.InputStreamReader)4 Method (java.lang.reflect.Method)4 XppReader (com.thoughtworks.xstream.io.xml.XppReader)3 Metacard (ddf.catalog.data.Metacard)3 CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)2 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 ActionEvent (java.awt.event.ActionEvent)2 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)2 HashTree (org.apache.jorphan.collections.HashTree)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 XStream (com.thoughtworks.xstream.XStream)1 SingleValueConverter (com.thoughtworks.xstream.converters.SingleValueConverter)1 NonExistentFieldException (com.thoughtworks.xstream.converters.reflection.NonExistentFieldException)1 StreamException (com.thoughtworks.xstream.io.StreamException)1 HierarchicalStreamCopier (com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier)1