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