| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.jpa; |
| 18 |
|
|
| 19 |
|
import java.util.Map; |
| 20 |
|
|
| 21 |
|
import javax.persistence.EntityManager; |
| 22 |
|
import javax.persistence.EntityManagerFactory; |
| 23 |
|
import javax.persistence.Persistence; |
| 24 |
|
|
| 25 |
|
import org.apache.camel.Consumer; |
| 26 |
|
import org.apache.camel.Exchange; |
| 27 |
|
import org.apache.camel.Expression; |
| 28 |
|
import org.apache.camel.NoTypeConversionAvailableException; |
| 29 |
|
import org.apache.camel.Processor; |
| 30 |
|
import org.apache.camel.Producer; |
| 31 |
|
import org.apache.camel.builder.ExpressionBuilder; |
| 32 |
|
import org.apache.camel.impl.DefaultExchange; |
| 33 |
|
import org.apache.camel.impl.ScheduledPollEndpoint; |
| 34 |
|
import org.apache.camel.util.IntrospectionSupport; |
| 35 |
|
|
| 36 |
|
import org.springframework.orm.jpa.JpaTemplate; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
public class JpaEndpoint extends ScheduledPollEndpoint<Exchange> { |
| 42 |
|
private EntityManagerFactory entityManagerFactory; |
| 43 |
3 |
private String persistenceUnit = "camel"; |
| 44 |
|
private JpaTemplate template; |
| 45 |
|
private Expression<Exchange> producerExpression; |
| 46 |
3 |
private int maximumResults = -1; |
| 47 |
|
private Class<?> entityType; |
| 48 |
|
private Map entityManagerProperties; |
| 49 |
3 |
private boolean consumeDelete = true; |
| 50 |
3 |
private boolean consumeLockEntity = true; |
| 51 |
|
|
| 52 |
|
public JpaEndpoint(String uri, JpaComponent component) { |
| 53 |
3 |
super(uri, component); |
| 54 |
3 |
entityManagerFactory = component.getEntityManagerFactory(); |
| 55 |
3 |
} |
| 56 |
|
|
| 57 |
|
public Exchange createExchange() { |
| 58 |
6 |
return new DefaultExchange(getContext()); |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
public Producer<Exchange> createProducer() throws Exception { |
| 62 |
3 |
return new JpaProducer(this, getProducerExpression()); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
public Consumer<Exchange> createConsumer(Processor processor) throws Exception { |
| 66 |
3 |
JpaConsumer consumer = new JpaConsumer(this, processor); |
| 67 |
3 |
configureConsumer(consumer); |
| 68 |
3 |
return consumer; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
@Override |
| 72 |
|
public void configureProperties(Map options) { |
| 73 |
3 |
super.configureProperties(options); |
| 74 |
3 |
Map emProperties = IntrospectionSupport.extractProperties(options, "emf."); |
| 75 |
3 |
if (emProperties != null) { |
| 76 |
3 |
setEntityManagerProperties(emProperties); |
| 77 |
|
} |
| 78 |
3 |
} |
| 79 |
|
|
| 80 |
|
public boolean isSingleton() { |
| 81 |
3 |
return false; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
public JpaTemplate getTemplate() { |
| 87 |
12 |
if (template == null) { |
| 88 |
3 |
template = createTemplate(); |
| 89 |
|
} |
| 90 |
12 |
return template; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
public void setTemplate(JpaTemplate template) { |
| 94 |
0 |
this.template = template; |
| 95 |
0 |
} |
| 96 |
|
|
| 97 |
|
public Expression<Exchange> getProducerExpression() { |
| 98 |
3 |
if (producerExpression == null) { |
| 99 |
3 |
producerExpression = createProducerExpression(); |
| 100 |
|
} |
| 101 |
3 |
return producerExpression; |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
public void setProducerExpression(Expression<Exchange> producerExpression) { |
| 105 |
0 |
this.producerExpression = producerExpression; |
| 106 |
0 |
} |
| 107 |
|
|
| 108 |
|
public int getMaximumResults() { |
| 109 |
5 |
return maximumResults; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
public void setMaximumResults(int maximumResults) { |
| 113 |
0 |
this.maximumResults = maximumResults; |
| 114 |
0 |
} |
| 115 |
|
|
| 116 |
|
public Class<?> getEntityType() { |
| 117 |
8 |
return entityType; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
public void setEntityType(Class<?> entityType) { |
| 121 |
3 |
this.entityType = entityType; |
| 122 |
3 |
} |
| 123 |
|
|
| 124 |
|
public EntityManagerFactory getEntityManagerFactory() { |
| 125 |
12 |
if (entityManagerFactory == null) { |
| 126 |
3 |
entityManagerFactory = createEntityManagerFactory(); |
| 127 |
|
} |
| 128 |
12 |
return entityManagerFactory; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { |
| 132 |
0 |
this.entityManagerFactory = entityManagerFactory; |
| 133 |
0 |
} |
| 134 |
|
|
| 135 |
|
public Map getEntityManagerProperties() { |
| 136 |
3 |
if (entityManagerProperties == null) { |
| 137 |
0 |
entityManagerProperties = System.getProperties(); |
| 138 |
|
} |
| 139 |
3 |
return entityManagerProperties; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
public void setEntityManagerProperties(Map entityManagerProperties) { |
| 143 |
3 |
this.entityManagerProperties = entityManagerProperties; |
| 144 |
3 |
} |
| 145 |
|
|
| 146 |
|
public String getPersistenceUnit() { |
| 147 |
0 |
return persistenceUnit; |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
public void setPersistenceUnit(String persistenceUnit) { |
| 151 |
1 |
this.persistenceUnit = persistenceUnit; |
| 152 |
1 |
} |
| 153 |
|
|
| 154 |
|
public boolean isConsumeDelete() { |
| 155 |
5 |
return consumeDelete; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
public void setConsumeDelete(boolean consumeDelete) { |
| 159 |
0 |
this.consumeDelete = consumeDelete; |
| 160 |
0 |
} |
| 161 |
|
|
| 162 |
|
public boolean isConsumeLockEntity() { |
| 163 |
3 |
return consumeLockEntity; |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
public void setConsumeLockEntity(boolean consumeLockEntity) { |
| 167 |
0 |
this.consumeLockEntity = consumeLockEntity; |
| 168 |
0 |
} |
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
protected JpaTemplate createTemplate() { |
| 173 |
3 |
return new JpaTemplate(getEntityManagerFactory()); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
protected EntityManagerFactory createEntityManagerFactory() { |
| 177 |
3 |
return Persistence.createEntityManagerFactory(persistenceUnit, getEntityManagerProperties()); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
protected EntityManager createEntityManager() { |
| 181 |
0 |
return getEntityManagerFactory().createEntityManager(); |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
protected TransactionStrategy createTransactionStrategy() { |
| 185 |
9 |
EntityManagerFactory emf = getEntityManagerFactory(); |
| 186 |
9 |
return JpaTemplateTransactionStrategy.newInstance(emf, getTemplate()); |
| 187 |
|
|
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
protected Expression<Exchange> createProducerExpression() { |
| 191 |
3 |
final Class<?> type = getEntityType(); |
| 192 |
3 |
if (type == null) { |
| 193 |
0 |
return ExpressionBuilder.bodyExpression(); |
| 194 |
|
} else { |
| 195 |
3 |
return new Expression<Exchange>() { |
| 196 |
3 |
public Object evaluate(Exchange exchange) { |
| 197 |
3 |
Object answer = exchange.getIn().getBody(type); |
| 198 |
3 |
if (answer == null) { |
| 199 |
0 |
Object defaultValue = exchange.getIn().getBody(); |
| 200 |
0 |
if (defaultValue != null) { |
| 201 |
0 |
throw new NoTypeConversionAvailableException(defaultValue, type); |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
0 |
answer = exchange.getContext().getInjector().newInstance(type); |
| 207 |
|
} |
| 208 |
3 |
return answer; |
| 209 |
|
} |
| 210 |
|
}; |
| 211 |
|
} |
| 212 |
|
} |
| 213 |
|
} |