public JcrProducer(JcrEndpoint jcrEndpoint) throws RepositoryException {
super(jcrEndpoint);
}
public void process(Exchange exchange) throws Exception {
TypeConverter converter = exchange.getContext().getTypeConverter();
Session session = openSession();
Message message = exchange.getIn();
String operation = determineOperation(message);
try {
if (JcrConstants.JCR_INSERT.equals(operation)) {
Node base = findOrCreateNode(session.getRootNode(), getJcrEndpoint().getBase());
Node node = findOrCreateNode(base, getNodeName(message));
Map<String, Object> headers = filterComponentHeaders(message.getHeaders());
for (String key : headers.keySet()) {
Object header = message.getHeader(key);
if (header != null && Object[].class.isAssignableFrom(header.getClass())) {
Value[] value = converter.convertTo(Value[].class, exchange, header);
node.setProperty(key, value);
} else {
Value value = converter.convertTo(Value.class, exchange, header);
node.setProperty(key, value);
}
}
node.addMixin("mix:referenceable");
exchange.getOut().setBody(node.getIdentifier());
session.save();
} else if (JcrConstants.JCR_GET_BY_ID.equals(operation)) {
Node node = session.getNodeByIdentifier(exchange.getIn()
.getMandatoryBody(String.class));
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
Class<?> aClass = classForJCRType(property);
Object value;
if (property.isMultiple()) {
value = converter.convertTo(aClass, exchange, property.getValues());
} else {
value = converter.convertTo(aClass, exchange, property.getValue());
}
message.setHeader(property.getName(), value);
}
} else {
throw new RuntimeException("Unsupported operation: " + operation);