@Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { doBeforeParse(element); super.doParse(element, parserContext, builder); // now lets parse the routes with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e); } Object value = parseUsingJaxb(element, parserContext, binder); if (value instanceof CamelRouteContextFactoryBean) { CamelRouteContextFactoryBean factoryBean = (CamelRouteContextFactoryBean) value; builder.addPropertyValue("routes", factoryBean.getRoutes()); } // lets inject the namespaces into any namespace aware POJOs injectNamespaces(element, binder); }
@Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { doBeforeParse(element); super.doParse(element, parserContext, builder); // now lets parse the routes with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e); } Object value = parseUsingJaxb(element, parserContext, binder); if (value instanceof CamelEndpointFactoryBean) { CamelEndpointFactoryBean factoryBean = (CamelEndpointFactoryBean) value; builder.addPropertyValue("properties", factoryBean.getProperties()); } }
@Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { doBeforeParse(element); super.doParse(element, parserContext, builder); // now lets parse the routes with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e); } Object value = parseUsingJaxb(element, parserContext, binder); if (value instanceof CamelRestContextFactoryBean) { CamelRestContextFactoryBean factoryBean = (CamelRestContextFactoryBean) value; builder.addPropertyValue("rests", factoryBean.getRests()); } // lets inject the namespaces into any namespace aware POJOs injectNamespaces(element, binder); }
/** * Used for auto registering endpoints from the <tt>from</tt> or <tt>to</tt> DSL if they have an id attribute set */ protected void registerEndpointsWithIdsDefinedInFromOrToTypes(Element element, ParserContext parserContext, String contextId, Binder<Node> binder) { NodeList list = element.getChildNodes(); int size = list.getLength(); for (int i = 0; i < size; i++) { Node child = list.item(i); if (child instanceof Element) { Element childElement = (Element) child; Object object = binder.getJAXBNode(child); // we only want from/to types to be registered as endpoints if (object instanceof FromDefinition || object instanceof SendDefinition) { registerEndpoint(childElement, parserContext, contextId); } // recursive registerEndpointsWithIdsDefinedInFromOrToTypes(childElement, parserContext, contextId, binder); } } }
/** * Creates camel model object like CamelContextFactoryBean, RouteDefinition or RoutesDefinition * from XML file. * * @param xmlPath path to the file * @return created camel model object * @throws Exception failed to unmarshall camel model object from XML file */ public static Object createCamelModelObjectFromXML(String xmlPath) throws Exception { InputStream input = Classes.getResourceAsStream(xmlPath); if (input == null) { throw CommonCamelMessages.MESSAGES.specifiedCamelContextFileIsNotFound(xmlPath); } InputSource source = new InputSource(input); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(source); Element element = document.getDocumentElement(); Element camelModelElement = findCamelModelElement(element, xmlPath); if (camelModelElement == null) { throw CommonCamelMessages.MESSAGES.noCamelContextElementFound(xmlPath); } Binder<Node> binder = JAXB_CONTEXT.createBinder(); Object obj = binder.unmarshal(camelModelElement); injectNamespaces(camelModelElement, binder); return obj; }
Identifier extractFromSearch(SearchType search, Binder<Node> binder) throws InvalidIdentifierException, UnmarshalException { NullCheck.check(search, "search null"); Object o = null; if (search.getAccessRequest() != null) { o = search.getAccessRequest(); } else if (search.getDevice() != null) { o = search.getDevice(); } else if (search.getIdentity() != null) { o = search.getIdentity(); } else if (search.getIpAddress() != null) { o = search.getIpAddress(); } else if (search.getMacAddress() != null) { o = search.getMacAddress(); } else { throw new UnmarshalException("identifier not set in search"); } return transformJaxbObjectToIdentifier(o, binder); }
/** * Helper for the last 3 methods * * @param olist * @param max * @return * @throws UnmarshalException * @throws InvalidIdentifierException */ private Identifier[] extractIdentifierArray(List<Object> olist, int max, Binder<Node> binder) throws UnmarshalException, InvalidIdentifierException { Identifier ret[]; if (olist == null) { throw new UnmarshalException("no identifiers in publish"); } if (olist.size() == 0 || olist.size() > max) { throw new UnmarshalException("bad number of identifiers in update"); } ret = new Identifier[max]; for (int i = 0; i < olist.size(); i++) { Object o = olist.get(i); if (o == null) { throw new UnmarshalException("invalid identifier"); } ret[i] = transformJaxbObjectToIdentifier(o, binder); } return ret; }
@Override public <T> Binder<T> createBinder(Class<T> domType) { if(domType==Node.class) return (Binder<T>)createBinder(); else return super.createBinder(domType); }
private Metadata parseRestContextNode(Element element, ParserContext context) { LOG.trace("Parsing RestContext {}", element); // now parse the rests with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e); } Object value = parseUsingJaxb(element, context, binder); if (!(value instanceof CamelRestContextFactoryBean)) { throw new ComponentDefinitionException("Expected an instance of " + CamelRestContextFactoryBean.class); } CamelRestContextFactoryBean rcfb = (CamelRestContextFactoryBean) value; String id = rcfb.getId(); MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class); factory.setId(".camelBlueprint.passThrough." + id); factory.setObject(new PassThroughCallable<Object>(rcfb)); MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class); factory2.setId(".camelBlueprint.factory." + id); factory2.setFactoryComponent(factory); factory2.setFactoryMethod("call"); MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class); ctx.setId(id); ctx.setRuntimeClass(List.class); ctx.setFactoryComponent(factory2); ctx.setFactoryMethod("getRests"); // must be lazy as we want CamelContext to be activated first ctx.setActivation(ACTIVATION_LAZY); // lets inject the namespaces into any namespace aware POJOs injectNamespaces(element, binder); LOG.trace("Parsing RestContext done, returning {}", element, ctx); return ctx; }
private Metadata parseEndpointNode(Element element, ParserContext context) { LOG.trace("Parsing Endpoint {}", element); // now parse the rests with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e); } Object value = parseUsingJaxb(element, context, binder); if (!(value instanceof CamelEndpointFactoryBean)) { throw new ComponentDefinitionException("Expected an instance of " + CamelEndpointFactoryBean.class); } CamelEndpointFactoryBean rcfb = (CamelEndpointFactoryBean) value; String id = rcfb.getId(); MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class); factory.setId(".camelBlueprint.passThrough." + id); factory.setObject(new PassThroughCallable<Object>(rcfb)); MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class); factory2.setId(".camelBlueprint.factory." + id); factory2.setFactoryComponent(factory); factory2.setFactoryMethod("call"); factory2.setInitMethod("afterPropertiesSet"); factory2.setDestroyMethod("destroy"); factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer")); MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class); ctx.setId(id); ctx.setRuntimeClass(Endpoint.class); ctx.setFactoryComponent(factory2); ctx.setFactoryMethod("getObject"); // must be lazy as we want CamelContext to be activated first ctx.setActivation(ACTIVATION_LAZY); LOG.trace("Parsing endpoint done, returning {}", element, ctx); return ctx; }
private Metadata parseKeyStoreParametersNode(Element element, ParserContext context) { LOG.trace("Parsing KeyStoreParameters {}", element); // now parse the key store parameters with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e); } Object value = parseUsingJaxb(element, context, binder); if (!(value instanceof KeyStoreParametersFactoryBean)) { throw new ComponentDefinitionException("Expected an instance of " + KeyStoreParametersFactoryBean.class); } KeyStoreParametersFactoryBean kspfb = (KeyStoreParametersFactoryBean) value; String id = kspfb.getId(); MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class); factory.setId(".camelBlueprint.passThrough." + id); factory.setObject(new PassThroughCallable<Object>(kspfb)); MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class); factory2.setId(".camelBlueprint.factory." + id); factory2.setFactoryComponent(factory); factory2.setFactoryMethod("call"); factory2.setInitMethod("afterPropertiesSet"); factory2.setDestroyMethod("destroy"); factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer")); MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class); ctx.setId(id); ctx.setRuntimeClass(KeyStoreParameters.class); ctx.setFactoryComponent(factory2); ctx.setFactoryMethod("getObject"); // must be lazy as we want CamelContext to be activated first ctx.setActivation(ACTIVATION_LAZY); LOG.trace("Parsing KeyStoreParameters done, returning {}", ctx); return ctx; }
private Metadata parseSecureRandomParametersNode(Element element, ParserContext context) { LOG.trace("Parsing SecureRandomParameters {}", element); // now parse the key store parameters with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e); } Object value = parseUsingJaxb(element, context, binder); if (!(value instanceof SecureRandomParametersFactoryBean)) { throw new ComponentDefinitionException("Expected an instance of " + SecureRandomParametersFactoryBean.class); } SecureRandomParametersFactoryBean srfb = (SecureRandomParametersFactoryBean) value; String id = srfb.getId(); MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class); factory.setId(".camelBlueprint.passThrough." + id); factory.setObject(new PassThroughCallable<Object>(srfb)); MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class); factory2.setId(".camelBlueprint.factory." + id); factory2.setFactoryComponent(factory); factory2.setFactoryMethod("call"); factory2.setInitMethod("afterPropertiesSet"); factory2.setDestroyMethod("destroy"); factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer")); MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class); ctx.setId(id); ctx.setRuntimeClass(SecureRandomParameters.class); ctx.setFactoryComponent(factory2); ctx.setFactoryMethod("getObject"); // must be lazy as we want CamelContext to be activated first ctx.setActivation(ACTIVATION_LAZY); LOG.trace("Parsing SecureRandomParameters done, returning {}", ctx); return ctx; }
private Metadata parseSSLContextParametersNode(Element element, ParserContext context) { LOG.trace("Parsing SSLContextParameters {}", element); // now parse the key store parameters with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e); } Object value = parseUsingJaxb(element, context, binder); if (!(value instanceof SSLContextParametersFactoryBean)) { throw new ComponentDefinitionException("Expected an instance of " + SSLContextParametersFactoryBean.class); } SSLContextParametersFactoryBean scpfb = (SSLContextParametersFactoryBean) value; String id = scpfb.getId(); MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class); factory.setId(".camelBlueprint.passThrough." + id); factory.setObject(new PassThroughCallable<Object>(scpfb)); MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class); factory2.setId(".camelBlueprint.factory." + id); factory2.setFactoryComponent(factory); factory2.setFactoryMethod("call"); factory2.setInitMethod("afterPropertiesSet"); factory2.setDestroyMethod("destroy"); factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer")); MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class); ctx.setId(id); ctx.setRuntimeClass(SSLContextParameters.class); ctx.setFactoryComponent(factory2); ctx.setFactoryMethod("getObject"); // must be lazy as we want CamelContext to be activated first ctx.setActivation(ACTIVATION_LAZY); LOG.trace("Parsing SSLContextParameters done, returning {}", ctx); return ctx; }
protected Object parseUsingJaxb(Element element, ParserContext parserContext, Binder<Node> binder) { try { return binder.unmarshal(element); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to parse JAXB element: " + e, e); } }
protected Object parseUsingJaxb(Element element, ParserContext parserContext, Binder<Node> binder) { try { return binder.unmarshal(element); } catch (JAXBException e) { throw new BeanDefinitionStoreException("Failed to parse JAXB element", e); } }
@Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { doBeforeParse(element); super.doParse(element, builder); // Note: prefer to use doParse from parent and postProcess; however, parseUsingJaxb requires // parserContext for no apparent reason. Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e); } Object value = parseUsingJaxb(element, parserContext, binder); if (value instanceof SSLContextParametersFactoryBean) { SSLContextParametersFactoryBean bean = (SSLContextParametersFactoryBean)value; builder.addPropertyValue("cipherSuites", bean.getCipherSuites()); builder.addPropertyValue("cipherSuitesFilter", bean.getCipherSuitesFilter()); builder.addPropertyValue("secureSocketProtocols", bean.getSecureSocketProtocols()); builder.addPropertyValue("secureSocketProtocolsFilter", bean.getSecureSocketProtocolsFilter()); builder.addPropertyValue("keyManagers", bean.getKeyManagers()); builder.addPropertyValue("trustManagers", bean.getTrustManagers()); builder.addPropertyValue("secureRandom", bean.getSecureRandom()); builder.addPropertyValue("clientParameters", bean.getClientParameters()); builder.addPropertyValue("serverParameters", bean.getServerParameters()); } else { throw new BeanDefinitionStoreException("Parsed type is not of the expected type. Expected " + SSLContextParametersFactoryBean.class.getName() + " but found " + value.getClass().getName()); } }
public static WindowState fromXMLString(String s){ try { JAXBContext context = JAXBContext.newInstance(WindowState.class); Binder<Node> binder = context.createBinder(); Document doc = XMLUtils.parse(s); return (WindowState)binder.unmarshal(doc); } catch (Throwable e) { return null; } }
/** * Deep copy the script by serialising to xml and then deserialising. * UUID is preserved. * * @param script * @return */ public Script deepCopy(Script script) { Binder<Node> binder = context.createBinder(); Document xml = toXML(script,binder); Script ret = fromXML(xml,binder); ret.setUuid(script.getUuid()); return ret; }
public Script fromFile(File file) { Document doc = XMLUtils.load(file); // generate uuid from filename Binder<Node> binder =context.createBinder(); Script script = fromXML(doc,binder); script.setUuid(getScriptUUID(file)); if (script.getScriptEditorUIType() == null) { script.setScriptEditorUIType(ScriptEditorType.WIZARD_GENERATED_EDITOR); } return script; }
public String toXMLString(Script script) { Binder<Node> binder =context.createBinder(); Document document = toXML(script,binder); if (document != null) { return XMLUtils.toString(document, XMLUtils.getPrettyPrintFormat()); } return null; }
/** * @param doc * @param config * @throws JAXBException * @throws IOException */ private void marshallComponentConfig(Document doc,Binder<Node> binder, ComponentConfig config) throws JAXBException, IOException { Node instructionNode = binder.getXMLNode(config); Serializable componentConf = config.getComponentConfig(); if (componentConf != null) { Node confNode = doc.createElement(COMPONENT_CONFIG_NODE); instructionNode.appendChild(confNode); switch (getComponentConfigIOType(componentConf.getClass())) { case JAXB: // marshall using JAXB... JAXBContext compContext = byClass.get(componentConf.getClass()); Marshaller m = compContext.createMarshaller(); m.marshal(componentConf, confNode); break; case SERIALISE: byte[] bytes = Serialization.convertToBytes((Serializable) componentConf); String encoded = DatatypeConverter.printBase64Binary(bytes); confNode.setTextContent(encoded); break; case STRING: confNode.setTextContent((String) componentConf); break; } } }
@SuppressWarnings("unchecked") @Override public List<Object> fromXML(Document document) { ArrayList<Object> ret = new ArrayList<>(); // try unmarshalling at all different supported script items if(document.getFirstChild()!=null){ String typeName = document.getFirstChild().getNodeName(); for (Map.Entry<ScriptElementType, Binder<Node>> entry : binderByType.entrySet()) { try { ScriptElementType elemType = entry.getKey(); // check names match before attempting unmarshalling (will cut down on the number of exceptions) XmlRootElement rootElement = elemType.getScriptElementClass().getAnnotation(XmlRootElement.class); if(rootElement!=null && Strings.equalsStd(rootElement.name(), typeName)){ Object item = entry.getValue().unmarshal(document); if (item != null) { processUnmarshalled(elemType, item, ret); break; } } } catch (Throwable e) { // non-fatal error as may not be correct object type in clipboard... } } } return ret; }
/** * Helper method which converts from a given Object o into * a datamodel Identifier. * * @param o * @return * @throws InvalidIdentifierException */ Identifier transformJaxbObjectToIdentifier(Object o, Binder<Node> binder) throws InvalidIdentifierException { Identifier ret = null; if (o instanceof AccessRequestType) { ret = transformAR((AccessRequestType)o); } else if (o instanceof DeviceType) { ret = transformDevice((DeviceType)o); } else if (o instanceof IPAddressType) { ret = transformIP((IPAddressType)o); } else if (o instanceof IdentityType) { ret = transformIdentity((IdentityType)o); } else if (o instanceof MACAddressType) { ret = transformMacAddress((MACAddressType)o); } else { throw new InvalidIdentifierException("unknown identifier"); } Node n = binder.getXMLNode(o); if (n == null) { throw new SystemErrorException("Could not get XML Node"); } if (!(n instanceof Element)) { throw new SystemErrorException("Identifier is not Element"); } Document identDoc = DomHelpers.deepCopy((Element)n); if (identDoc == null) { throw new SystemErrorException("deep copy gave null"); } ret.setXmlDocument(identDoc); return ret; }
@Override public Binder<Node> createBinder() { return new BinderImpl<Node>(this,new DOMScanner()); }
private Metadata parseRouteContextNode(Element element, ParserContext context) { LOG.trace("Parsing RouteContext {}", element); // now parse the routes with JAXB Binder<Node> binder; try { binder = getJaxbContext().createBinder(); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e); } Object value = parseUsingJaxb(element, context, binder); if (!(value instanceof CamelRouteContextFactoryBean)) { throw new ComponentDefinitionException("Expected an instance of " + CamelRouteContextFactoryBean.class); } CamelRouteContextFactoryBean rcfb = (CamelRouteContextFactoryBean) value; String id = rcfb.getId(); MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class); factory.setId(".camelBlueprint.passThrough." + id); factory.setObject(new PassThroughCallable<Object>(rcfb)); MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class); factory2.setId(".camelBlueprint.factory." + id); factory2.setFactoryComponent(factory); factory2.setFactoryMethod("call"); MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class); ctx.setId(id); ctx.setRuntimeClass(List.class); ctx.setFactoryComponent(factory2); ctx.setFactoryMethod("getRoutes"); // must be lazy as we want CamelContext to be activated first ctx.setActivation(ACTIVATION_LAZY); // lets inject the namespaces into any namespace aware POJOs injectNamespaces(element, binder); LOG.trace("Parsing RouteContext done, returning {}", element, ctx); return ctx; }
@Override protected Binder<Node> initialValue() { return null; }