KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > async > JournalFacade


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.kaha.impl.async;
19
20 import java.io.IOException JavaDoc;
21
22 import org.apache.activeio.journal.InvalidRecordLocationException;
23 import org.apache.activeio.journal.Journal;
24 import org.apache.activeio.journal.JournalEventListener;
25 import org.apache.activeio.journal.RecordLocation;
26 import org.apache.activeio.packet.ByteArrayPacket;
27 import org.apache.activeio.packet.Packet;
28 import org.apache.activemq.util.ByteSequence;
29
30 /**
31  * Provides a Journal Facade to the DataManager.
32  *
33  * @version $Revision: 1.1.1.1 $
34  */

35 public final class JournalFacade implements Journal {
36
37     
38     public static class RecordLocationFacade implements RecordLocation {
39         private final Location location;
40
41         public RecordLocationFacade(Location location) {
42             this.location = location;
43         }
44
45         public Location getLocation() {
46             return location;
47         }
48
49         public int compareTo(Object JavaDoc o) {
50             RecordLocationFacade rlf = (RecordLocationFacade)o;
51             int rc = location.compareTo(rlf.location);
52             return rc;
53         }
54     }
55
56     static private RecordLocation convertToRecordLocation(Location location) {
57         if(location==null)
58             return null;
59         return new RecordLocationFacade(location);
60     }
61     
62     static private Location convertFromRecordLocation(RecordLocation location) {
63         
64         if(location==null)
65             return null;
66         
67         return ((RecordLocationFacade)location).getLocation();
68     }
69
70     AsyncDataManager dataManager;
71     
72     public JournalFacade(AsyncDataManager dataManager) {
73         this.dataManager = dataManager;
74     }
75
76     public void close() throws IOException JavaDoc {
77         dataManager.close();
78     }
79
80     public RecordLocation getMark() throws IllegalStateException JavaDoc {
81         return convertToRecordLocation(dataManager.getMark());
82     }
83
84     public RecordLocation getNextRecordLocation(RecordLocation location) throws InvalidRecordLocationException, IOException JavaDoc, IllegalStateException JavaDoc {
85         return convertToRecordLocation(dataManager.getNextLocation(convertFromRecordLocation(location)));
86     }
87
88     public Packet read(RecordLocation location) throws InvalidRecordLocationException, IOException JavaDoc, IllegalStateException JavaDoc {
89         ByteSequence rc = dataManager.read(convertFromRecordLocation(location));
90         if( rc == null )
91             return null;
92         return new ByteArrayPacket(rc.getData(), rc.getOffset(), rc.getLength());
93     }
94
95     public void setJournalEventListener(JournalEventListener listener) throws IllegalStateException JavaDoc {
96     }
97
98     public void setMark(RecordLocation location, boolean sync) throws InvalidRecordLocationException, IOException JavaDoc, IllegalStateException JavaDoc {
99         dataManager.setMark(convertFromRecordLocation(location), sync);
100     }
101
102     public RecordLocation write(Packet packet, boolean sync) throws IOException JavaDoc, IllegalStateException JavaDoc {
103         org.apache.activeio.packet.ByteSequence data = packet.asByteSequence();
104         ByteSequence sequence = new ByteSequence(data.getData(), data.getOffset(), data.getLength());
105         return convertToRecordLocation(dataManager.write(sequence, sync));
106     }
107     
108 }
109
Popular Tags