/** * Loads the plugin descriptor from the specified file. The file path specified should * be a class path relative path. For example, if the descriptor file is located at * org.rhq.enterprise.server.configuration.my-descriptor.xml, then you should specify * /org/rhq/enterprise/server/configuration/my-descriptor.xml. * * @param pluginDescriptorURL The class path relative path of the descriptor file * @return The {@link PluginDescriptor} */ public static PluginDescriptor loadPluginDescriptor(URL pluginDescriptorURL) { try { JAXBContext jaxbContext = JAXBContext.newInstance(DescriptorPackages.PC_PLUGIN); URL pluginSchemaURL = PluginDescriptorUtil.class.getClassLoader().getResource("rhq-plugin.xsd"); Schema pluginSchema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( pluginSchemaURL); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); ValidationEventCollector vec = new ValidationEventCollector(); unmarshaller.setEventHandler(vec); unmarshaller.setSchema(pluginSchema); return (PluginDescriptor) unmarshaller.unmarshal(pluginDescriptorURL.openStream()); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Transforms the given string into a plugin descriptor object. * * @param string The plugin descriptor specified as a string * @return The {@link PluginDescriptor} */ public static PluginDescriptor toPluginDescriptor(String string) { try { JAXBContext jaxbContext = JAXBContext.newInstance(DescriptorPackages.PC_PLUGIN); URL pluginSchemaURL = PluginMetadataParser.class.getClassLoader().getResource("rhq-plugin.xsd"); Schema pluginSchema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(pluginSchemaURL); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); ValidationEventCollector vec = new ValidationEventCollector(); unmarshaller.setEventHandler(vec); unmarshaller.setSchema(pluginSchema); StringReader reader = new StringReader(string); return (PluginDescriptor) unmarshaller.unmarshal(reader); } catch (Exception e) { throw new RuntimeException(e); } }
public static byte[] marshalToBytes(PcGtsType page) throws JAXBException { ValidationEventCollector vec = new ValidationEventCollector(); Marshaller marshaller = createMarshaller(vec); ObjectFactory objectFactory = new ObjectFactory(); JAXBElement<PcGtsType> je = objectFactory.createPcGts(page); byte[] data; ByteArrayOutputStream out = new ByteArrayOutputStream(); try { try { marshaller.marshal(je, out); data = out.toByteArray(); } finally { out.close(); } } catch (Exception e) { throw new MarshalException(e); } String msg=buildMsg(vec, page); if (!msg.startsWith(NO_EVENTS_MSG)) logger.info(msg); return data; }
private static <T> ValidationEvent[] marshalToStream(T object, OutputStream out, boolean doFormatting, MarshallerType type, Class<?>... nestedClasses) throws JAXBException { ValidationEventCollector vec = new ValidationEventCollector(); final Marshaller marshaller; // switch(type) { // case JSON: // marshaller = createJsonMarshaller(object, doFormatting, nestedClasses); // break; // default: // marshaller = createXmlMarshaller(object, doFormatting, nestedClasses); // break; // } marshaller = createXmlMarshaller(object, doFormatting, nestedClasses); marshaller.setEventHandler(vec); marshaller.marshal(object, out); checkEvents(vec); return vec.getEvents(); }
private Unmarshaller getUnmarshaller() throws JAXBException { JAXBContext jctx = JAXBContext.newInstance(WorkflowDefinition.class); Unmarshaller unmarshaller = jctx.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL schemaUrl = WorkflowDefinition.class.getResource("workflow.xsd"); Schema schema = null; try { schema = sf.newSchema(new StreamSource(schemaUrl.toExternalForm())); } catch (SAXException ex) { throw new JAXBException("Missing schema workflow.xsd!", ex); } unmarshaller.setSchema(schema); ValidationEventCollector errors = new ValidationEventCollector() { @Override public boolean handleEvent(ValidationEvent event) { super.handleEvent(event); return true; } }; unmarshaller.setEventHandler(errors); return unmarshaller; }
/** * If there are any events, convert the events into an error message string and throw a new InterfaceValidationException. * * @param jaxbException that prompted the check for validation events * @param handler ValidationEventController with any validation events * @param messageType String describing what type of message caused the error (Request, Response, or exception message) */ protected static void handleValidationEvents(JAXBException jaxbException, ValidationEventCollector handler, String messageType) { if (handler.hasEvents()) { ValidationEvent[] events = handler.getEvents(); StringBuffer errorMessage = new StringBuffer(); for (int i = 0; i < events.length; i++) { if (i > 0) { errorMessage.append(" "); } errorMessage.append(events[i].getMessage()); } if (jaxbException == null) { throw new InterfaceValidationException(InterfaceValidationException.VALIDATION_ERROR, InterfaceException.PRE_ENCAPSULATION, messageType, errorMessage); } else { throw new InterfaceValidationException(jaxbException, InterfaceValidationException.VALIDATION_ERROR, InterfaceException.PRE_ENCAPSULATION, messageType, errorMessage); } } }
private static Marshaller createMarshaller(ValidationEventCollector vec) throws JAXBException { JAXBContext jc = createPageJAXBContext(); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocStr); m.setEventHandler(vec); m.setListener(new TrpPageMarshalListener()); return m; }
public static File marshalToFile(PcGtsType page, File fileOut) throws JAXBException, IOException { ValidationEventCollector vec = new ValidationEventCollector(); Marshaller marshaller = createMarshaller(vec); ObjectFactory objectFactory = new ObjectFactory(); JAXBElement<PcGtsType> je = objectFactory.createPcGts(page); File backup=null; if (fileOut.exists()) { logger.debug("file exists: "+fileOut.getAbsolutePath()+ " - backing up!"); backup = CoreUtils.backupFile(fileOut); } try { marshaller.marshal(je, fileOut); } catch (Exception e) { if (backup!=null) { logger.debug("restoring backup: "+backup.getAbsolutePath()); FileUtils.copyFile(backup, fileOut); } if (e instanceof JAXBException) throw e; else throw new JAXBException(e.getMessage(), e); } finally { if (backup!=null) backup.delete(); } String msg=buildMsg(vec, page); if (!msg.startsWith(NO_EVENTS_MSG)) logger.info(msg); return fileOut; }
private static String buildMsg(ValidationEventCollector vec, PcGtsType page) { String imgFn = ""; if (page.getPage()!=null) { imgFn = page.getPage().getImageFilename(); } String msg; if (vec.hasEvents()) { msg="Events occured while marshalling xml file: " + vec.getEvents().length; } else { msg=NO_EVENTS_MSG; } if (!imgFn.isEmpty()) msg += " (img: "+imgFn+")"; return msg; }
private static void checkEvents(ValidationEventCollector vec) { if (vec.hasEvents()) { logger.info("Events occured while marshalling xml file: " + vec.getEvents().length); ValidationEvent[] events = vec.getEvents(); for(ValidationEvent e : events){ logger.info(e.getMessage()); } } else { logger.debug("No events occured during marshalling xml file!"); } }
private void read() throws JAXBException { long currentTime = file.lastModified(); if (currentTime == lastModified) { return ; } Unmarshaller unmarshaller = getUnmarshaller(); ValidationEventCollector errors = (ValidationEventCollector) unmarshaller.getEventHandler(); WorkflowDefinition fetchedWf = null; try { WorkflowDefinition wf = (WorkflowDefinition) unmarshaller.unmarshal(file); if (!errors.hasEvents()) { readCaches(wf); fetchedWf = wf; } } catch (UnmarshalException ex) { if (!errors.hasEvents()) { throw ex; } } finally { setProfiles(fetchedWf, currentTime); } if (errors.hasEvents()) { StringBuilder err = new StringBuilder(); for (ValidationEvent event : errors.getEvents()) { err.append(event).append('\n'); } throw new JAXBException(err.toString()); } }
/** * Parses and loads a particular plugin descriptor by stream, typically * obtained by calling {@link Class#getResourceAsStream(String)}. */ protected PluginDescriptor loadDescriptor(InputStream is) throws Exception { ValidationEventCollector vec = new ValidationEventCollector(); PluginDescriptor pd = AgentPluginDescriptorUtil.parsePluginDescriptor(is, vec); if (vec.hasEvents()) log.warn("Validation failed " + asList(vec.getEvents())); return pd; }
private void validate(ValidationEventCollector handler) throws ValidationException { if(handler.hasEvents()) { ValidationEvent[] events = handler.getEvents(); StringBuilder sb = new StringBuilder(); for(ValidationEvent event : events) { sb.append(event.getMessage()); } throw new ValidationException(sb.toString()); } }
private static Marshaller createMarshaller() throws JAXBException { return createMarshaller(new ValidationEventCollector()); }
/** * If there are any events, convert the events into an error message string and throw a new InterfaceValidationException. * * @param handler ValidationEventController with any validation events * @param messageType String describing what type of message caused the error (Request, Response, or exception message) */ protected static void handleValidationEvents(ValidationEventCollector handler, String messageType) { handleValidationEvents(null, handler, messageType); }