001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jpa;
019    
020    import org.apache.camel.Consumer;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Expression;
023    import org.apache.camel.NoTypeConversionAvailableException;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Producer;
026    import org.apache.camel.builder.ExpressionBuilder;
027    import org.apache.camel.impl.DefaultExchange;
028    import org.apache.camel.impl.ScheduledPollEndpoint;
029    import org.apache.camel.util.IntrospectionSupport;
030    import org.springframework.orm.jpa.JpaTemplate;
031    
032    import javax.persistence.EntityManager;
033    import javax.persistence.EntityManagerFactory;
034    import javax.persistence.Persistence;
035    import java.util.Map;
036    
037    /**
038     * @version $Revision: 541335 $
039     */
040    public class JpaEndpoint extends ScheduledPollEndpoint<Exchange> {
041        private EntityManagerFactory entityManagerFactory;
042        private String persistenceUnit = "camel";
043        private JpaTemplate template;
044        private Expression<Exchange> producerExpression;
045        private int maximumResults = -1;
046        private Class<?> entityType;
047        private Map entityManagerProperties;
048    
049        public JpaEndpoint(String uri, JpaComponent component) {
050            super(uri, component);
051            entityManagerFactory = component.getEntityManagerFactory();
052        }
053    
054        public Exchange createExchange() {
055            return new DefaultExchange(getContext());
056        }
057    
058        public Producer<Exchange> createProducer() throws Exception {
059            return new JpaProducer(this, getProducerExpression());
060        }
061    
062        public Consumer<Exchange> createConsumer(Processor processor) throws Exception {
063            JpaConsumer consumer = new JpaConsumer(this, processor);
064            configureConsumer(consumer);
065            return consumer;
066        }
067    
068        @Override
069        public void configureProperties(Map options) {
070            super.configureProperties(options);
071            Map emProperties = IntrospectionSupport.extractProperties(options, "emf.");
072            if (emProperties != null) {
073                setEntityManagerProperties(emProperties);
074            }
075        }
076    
077        public boolean isSingleton() {
078                    return false;
079            }
080    
081        // Properties
082        //-------------------------------------------------------------------------
083        public JpaTemplate getTemplate() {
084            if (template == null) {
085                template = createTemplate();
086            }
087            return template;
088        }
089    
090        public void setTemplate(JpaTemplate template) {
091            this.template = template;
092        }
093    
094        public Expression<Exchange> getProducerExpression() {
095            if (producerExpression == null) {
096                producerExpression = createProducerExpression();
097            }
098            return producerExpression;
099        }
100    
101        public void setProducerExpression(Expression<Exchange> producerExpression) {
102            this.producerExpression = producerExpression;
103        }
104    
105        public int getMaximumResults() {
106            return maximumResults;
107        }
108    
109        public void setMaximumResults(int maximumResults) {
110            this.maximumResults = maximumResults;
111        }
112    
113        public Class<?> getEntityType() {
114            return entityType;
115        }
116    
117        public void setEntityType(Class<?> entityType) {
118            this.entityType = entityType;
119        }
120    
121        public EntityManagerFactory getEntityManagerFactory() {
122            if (entityManagerFactory == null) {
123                entityManagerFactory = createEntityManagerFactory();
124            }
125            return entityManagerFactory;
126        }
127    
128        public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
129            this.entityManagerFactory = entityManagerFactory;
130        }
131    
132        public Map getEntityManagerProperties() {
133            if (entityManagerProperties == null) {
134                entityManagerProperties = System.getProperties();
135            }
136            return entityManagerProperties;
137        }
138    
139        public void setEntityManagerProperties(Map entityManagerProperties) {
140            this.entityManagerProperties = entityManagerProperties;
141        }
142    
143        public String getPersistenceUnit() {
144            return persistenceUnit;
145        }
146    
147        public void setPersistenceUnit(String persistenceUnit) {
148            this.persistenceUnit = persistenceUnit;
149        }
150    
151        // Implementation methods
152        //-------------------------------------------------------------------------
153        protected JpaTemplate createTemplate() {
154            return new JpaTemplate(getEntityManagerFactory());
155        }
156    
157        protected EntityManagerFactory createEntityManagerFactory() {
158            return Persistence.createEntityManagerFactory(persistenceUnit, getEntityManagerProperties());
159        }
160    
161        protected EntityManager createEntityManager() {
162            return getEntityManagerFactory().createEntityManager();
163        }
164    
165        protected TransactionStrategy createTransactionStrategy() {
166            EntityManagerFactory emf = getEntityManagerFactory();
167            return JpaTemplateTransactionStrategy.newInstance(emf, getTemplate());
168            //return new DefaultTransactionStrategy(emf);
169        }
170    
171        protected Expression<Exchange> createProducerExpression() {
172            final Class<?> type = getEntityType();
173            if (type == null) {
174                return ExpressionBuilder.bodyExpression();
175            }
176            else {
177                return new Expression<Exchange>() {
178                    public Object evaluate(Exchange exchange) {
179                        Object answer = exchange.getIn().getBody(type);
180                        if (answer == null) {
181                            Object defaultValue = exchange.getIn().getBody();
182                            if (defaultValue != null) {
183                                throw new NoTypeConversionAvailableException(defaultValue, type);
184                            }
185    
186                            // if we don't have a body then
187                            // lets instantiate and inject a new instance
188                            answer = exchange.getContext().getInjector().newInstance(type);
189                        }
190                        return answer;
191                    }
192                };
193            }
194        }
195    }