public static String toString(Object target, Class stopClass) { Map map = new LinkedHashMap(); addFields(target, target.getClass(), stopClass, map); StringBuffer buffer = new StringBuffer(simpleName(target.getClass())); buffer.append(" {"); Set entrySet = map.entrySet(); boolean first = true; for (Iterator iter = entrySet.iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); if (first) { first = false; } else { buffer.append(", "); } buffer.append(entry.getKey()); buffer.append(" = "); buffer.append(entry.getValue()); } buffer.append("}"); return buffer.toString(); }
private static boolean setProperty(Object target, String name, Object value) { try { Class clazz = target.getClass(); Method setter = findSetterMethod(clazz, name); if (setter == null) { return false; } // If the type is null or it matches the needed type, just use the // value directly if (value == null || value.getClass() == setter.getParameterTypes()[0]) { setter.invoke(target, new Object[] {value }); } else { // We need to convert it setter.invoke(target, new Object[] {convert(value, setter.getParameterTypes()[0]) }); } return true; } catch (Throwable ignore) { return false; } }
private static Method findSetterMethod(Class clazz, String name) { // Build the method name. name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Method[] methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; Class params[] = method.getParameterTypes(); if (method.getName().equals(name) && params.length == 1 && isSettableType(params[0])) { return method; } } return null; }
protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception { FtpSenderEndpoint ftpEndpoint = new FtpSenderEndpoint(this, ep); URI uri = new URI(ep.getEndpointName()); Map map = URISupport.parseQuery(uri.getQuery()); IntrospectionSupport.setProperties(ftpEndpoint, map); ftpEndpoint.setUri(uri); ftpEndpoint.validate(); return ftpEndpoint; }
public static void setProperties(Object target, Map props) { if (target == null) { throw new IllegalArgumentException("target was null."); } if (props == null) { throw new IllegalArgumentException("props was null."); } for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Entry) iter.next(); if (setProperty(target, (String) entry.getKey(), entry.getValue())) { iter.remove(); } } }
private static void addFields(Object target, Class startClass, Class stopClass, Map map) { if (startClass != stopClass) { addFields(target, startClass.getSuperclass(), stopClass, map); } Field[] fields = startClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || Modifier.isPrivate(field.getModifiers())) { continue; } try { field.setAccessible(true); Object o = field.get(target); if (o != null && o.getClass().isArray()) { try { o = Arrays.asList((Object[]) o); } catch (Throwable e) { // Ignore } } map.put(field.getName(), o); } catch (Throwable e) { e.printStackTrace(); } } }
IntrospectionSupport.setProperties(fileEp, map);
public static boolean setProperties(Object target, Map props, String optionPrefix) { boolean rc = false; if (target == null) { throw new IllegalArgumentException("target was null."); } if (props == null) { throw new IllegalArgumentException("props was null."); } for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); if (name.startsWith(optionPrefix)) { Object value = props.get(name); name = name.substring(optionPrefix.length()); if (setProperty(target, name, value)) { iter.remove(); rc = true; } } } return rc; }
@SuppressWarnings("unchecked") public CamelProviderEndpoint createEndpoint(String uriString, JbiComponent jbiComponent) throws URISyntaxException { URI uri = new URI(uriString); Map map = URISupport.parseQuery(uri.getQuery()); String camelUri = uri.getSchemeSpecificPart(); Endpoint camelEndpoint = jbiComponent.getCamelContext().getEndpoint(camelUri); AsyncProcessor processor = jbiComponent.createCamelProcessor(camelEndpoint); CamelProviderEndpoint endpoint = new CamelProviderEndpoint(getServiceUnit(), camelEndpoint, jbiComponent.createBinding(camelEndpoint), processor); IntrospectionSupport.setProperties(endpoint, map); return endpoint; }
protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception { // We receive an exchange for an EPR that has not been used yet. // Register a provider endpoint and restart processing. BeanEndpoint endpoint = new BeanEndpoint(this, ep); // TODO //endpoint.setRole(MessageExchange.Role.PROVIDER); // lets use a URL to parse the path URI uri = new URI(ep.getEndpointName()); String beanName = null; // lets try the host first for hierarchial URIs if (uri.getHost() != null) { // it must start bean://host/path beanName = uri.getHost(); } else { // it must be an absolute URI of the form bean:name beanName = uri.getSchemeSpecificPart(); } if (beanName != null) { endpoint.setBeanName(beanName); } else { throw new IllegalArgumentException("No bean name defined for URI: " + uri + ". Please use a URI of bean:name or bean://name?property=value"); } Map map = URISupport.parseQuery(uri.getQuery()); if (endpoint.getBean() == null) { endpoint.setBean(endpoint.createBean()); } IntrospectionSupport.setProperties(endpoint.getBean(), map); return endpoint; }
protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception { // We receive an exchange for an EPR that has not been used yet. // Register a provider endpoint and restart processing. JmsEndpoint jmsEp = new JmsEndpoint(true); jmsEp.setServiceUnit(new DefaultServiceUnit(component)); jmsEp.setService(ep.getServiceName()); jmsEp.setEndpoint(ep.getEndpointName()); jmsEp.setRole(MessageExchange.Role.PROVIDER); URI uri = new URI(ep.getEndpointName()); Map map = URISupport.parseQuery(uri.getQuery()); if (IntrospectionSupport.setProperties(jmsEp, map, "jms.")) { uri = URISupport.createRemainingURI(uri, map); } if (uri.getPath() != null) { String path = uri.getSchemeSpecificPart(); while (path.startsWith("/")) { path = path.substring(1); } if (path.startsWith(AbstractJmsProcessor.STYLE_QUEUE + "/")) { jmsEp.setDestinationStyle(AbstractJmsProcessor.STYLE_QUEUE); jmsEp.setJmsProviderDestinationName(path.substring(AbstractJmsProcessor.STYLE_QUEUE.length() + 1)); } else if (path.startsWith(AbstractJmsProcessor.STYLE_TOPIC + "/")) { jmsEp.setDestinationStyle(AbstractJmsProcessor.STYLE_TOPIC); jmsEp.setJmsProviderDestinationName(path.substring(AbstractJmsProcessor.STYLE_TOPIC.length() + 1)); } } return jmsEp; }
protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception { // We receive an exchange for an EPR that has not been used yet. // Register a provider endpoint and restart processing. HttpEndpoint httpEp = new HttpEndpoint(true); httpEp.setServiceUnit(new DefaultServiceUnit(component)); httpEp.setService(ep.getServiceName()); httpEp.setEndpoint(ep.getEndpointName()); httpEp.setRole(MessageExchange.Role.PROVIDER); URI uri = new URI(ep.getEndpointName()); Map map = URISupport.parseQuery(uri.getQuery()); if (IntrospectionSupport.setProperties(httpEp, map, "http.")) { uri = URISupport.createRemainingURI(uri, map); } if (httpEp.getLocationURI() == null) { httpEp.setLocationURI(uri.toString()); } return httpEp; }