KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > store > jpa > JPAReferenceStoreAdapter


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.store.jpa;
19
20 import java.io.IOException JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.persistence.EntityManager;
26 import javax.persistence.Query;
27
28 import org.apache.activemq.command.ActiveMQDestination;
29 import org.apache.activemq.command.ActiveMQQueue;
30 import org.apache.activemq.command.ActiveMQTopic;
31 import org.apache.activemq.store.MessageStore;
32 import org.apache.activemq.store.ReferenceStore;
33 import org.apache.activemq.store.ReferenceStoreAdapter;
34 import org.apache.activemq.store.TopicMessageStore;
35 import org.apache.activemq.store.TopicReferenceStore;
36 import org.apache.activemq.util.IOExceptionSupport;
37
38 /**
39  * An implementation of {@link ReferenceStoreAdapter} that uses JPA to
40  * store it's message references.
41  *
42  * @org.apache.xbean.XBean element="jpaReferenceStoreAdapter"
43  *
44  * @version $Revision: 1.17 $
45  */

46 public class JPAReferenceStoreAdapter extends JPAPersistenceAdapter implements ReferenceStoreAdapter {
47
48     @Override JavaDoc
49     public MessageStore createQueueMessageStore(ActiveMQQueue destination) throws IOException JavaDoc {
50         throw new RuntimeException JavaDoc("Use createQueueReferenceStore instead.");
51     }
52     
53     @Override JavaDoc
54     public TopicMessageStore createTopicMessageStore(ActiveMQTopic destination) throws IOException JavaDoc {
55         throw new RuntimeException JavaDoc("Use createTopicReferenceStore instead.");
56     }
57     
58     public ReferenceStore createQueueReferenceStore(ActiveMQQueue destination) throws IOException JavaDoc {
59         JPAReferenceStore rc = new JPAReferenceStore(this, destination);
60         return rc;
61     }
62
63     public TopicReferenceStore createTopicReferenceStore(ActiveMQTopic destination) throws IOException JavaDoc {
64         JPATopicReferenceStore rc = new JPATopicReferenceStore(this, destination);
65         return rc;
66     }
67     
68
69     public void deleteAllMessages() throws IOException JavaDoc {
70         EntityManager manager = beginEntityManager(null);
71         try {
72             Query query = manager.createQuery("delete from StoredMessageReference m");
73             query.executeUpdate();
74             query = manager.createQuery("delete from StoredSubscription ss");
75             query.executeUpdate();
76         } catch (Throwable JavaDoc e) {
77             rollbackEntityManager(null,manager);
78             throw IOExceptionSupport.create(e);
79         }
80         commitEntityManager(null,manager);
81     }
82
83     public Set JavaDoc<ActiveMQDestination> getDestinations() {
84         HashSet JavaDoc<ActiveMQDestination> rc = new HashSet JavaDoc<ActiveMQDestination>();
85         
86         EntityManager manager = beginEntityManager(null);
87         try {
88             Query query = manager.createQuery("select distinct m.destination from StoredMessageReference m");
89             for (String JavaDoc dest : (List JavaDoc<String JavaDoc>)query.getResultList()) {
90                 rc.add(ActiveMQDestination.createDestination(dest,ActiveMQDestination.QUEUE_TYPE));
91             }
92         } catch (RuntimeException JavaDoc e) {
93             rollbackEntityManager(null,manager);
94             throw e;
95         }
96         commitEntityManager(null,manager);
97         return rc;
98     }
99
100     public long getLastMessageBrokerSequenceId() throws IOException JavaDoc {
101         long rc=0;
102         EntityManager manager = beginEntityManager(null);
103         try {
104             Query query = manager.createQuery("select max(m.id) from StoredMessageReference m");
105             Long JavaDoc t = (Long JavaDoc) query.getSingleResult();
106             if( t != null ) {
107                 rc = t;
108             }
109         } catch (Throwable JavaDoc e) {
110             rollbackEntityManager(null,manager);
111             throw IOExceptionSupport.create(e);
112         }
113         commitEntityManager(null,manager);
114         return rc;
115     }
116
117     public Set JavaDoc<Integer JavaDoc> getReferenceFileIdsInUse() throws IOException JavaDoc {
118         HashSet JavaDoc<Integer JavaDoc> rc=null;
119         EntityManager manager = beginEntityManager(null);
120         try {
121             Query query = manager.createQuery("select distinct m.fileId from StoredMessageReference m");
122             rc=new HashSet JavaDoc<Integer JavaDoc>((List JavaDoc<Integer JavaDoc>)query.getResultList());
123         } catch (Throwable JavaDoc e) {
124             rollbackEntityManager(null,manager);
125             throw IOExceptionSupport.create(e);
126         }
127         commitEntityManager(null,manager);
128         return rc;
129     }
130
131     /**
132      * @return
133      * @see org.apache.activemq.store.ReferenceStoreAdapter#isStoreValid()
134      */

135     public boolean isStoreValid(){
136         return true;
137     }
138
139 }
140
Popular Tags