KickJava   Java API By Example, From Geeks To Geeks.

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


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.activemq.kaha.Marshaller;
23 import org.apache.activemq.kaha.StoreLocation;
24 import org.apache.activemq.kaha.impl.data.RedoListener;
25 import org.apache.activemq.util.ByteSequence;
26 import org.apache.activemq.util.DataByteArrayInputStream;
27 import org.apache.activemq.util.DataByteArrayOutputStream;
28
29 /**
30  * Provides a Kaha DataManager Facade to the DataManager.
31  *
32  * @version $Revision: 1.1.1.1 $
33  */

34 public final class DataManagerFacade implements org.apache.activemq.kaha.impl.DataManager {
35
36     private static class StoreLocationFacade implements StoreLocation {
37         private final Location location;
38
39         public StoreLocationFacade(Location location) {
40             this.location = location;
41         }
42
43         public int getFile() {
44             return location.getDataFileId();
45         }
46
47         public long getOffset() {
48             return location.getOffset();
49         }
50
51         public int getSize() {
52             return location.getSize();
53         }
54
55         public Location getLocation() {
56             return location;
57         }
58     }
59
60     static private StoreLocation convertToStoreLocation(Location location) {
61         if(location==null)
62             return null;
63         return new StoreLocationFacade(location);
64     }
65     
66     static private Location convertFromStoreLocation(StoreLocation location) {
67         
68         if(location==null)
69             return null;
70         
71         if( location.getClass()== StoreLocationFacade.class )
72             return ((StoreLocationFacade)location).getLocation();
73         
74         Location l = new Location();
75         l.setOffset((int) location.getOffset());
76         l.setSize(location.getSize());
77         l.setDataFileId(location.getFile());
78         return l;
79     }
80
81     static final private ByteSequence FORCE_COMMAND = new ByteSequence(new byte[]{'F', 'O', 'R', 'C', 'E'});
82     
83     AsyncDataManager dataManager;
84     private final String JavaDoc name;
85     private Marshaller redoMarshaller;
86     
87     
88     public DataManagerFacade(AsyncDataManager dataManager, String JavaDoc name) {
89         this.dataManager=dataManager;
90         this.name = name;
91     }
92     
93     public Object JavaDoc readItem(Marshaller marshaller, StoreLocation location) throws IOException JavaDoc {
94         ByteSequence sequence = dataManager.read(convertFromStoreLocation(location));
95         DataByteArrayInputStream dataIn = new DataByteArrayInputStream(sequence);
96         return marshaller.readPayload(dataIn);
97     }
98
99
100     public StoreLocation storeDataItem(Marshaller marshaller, Object JavaDoc payload) throws IOException JavaDoc {
101         final DataByteArrayOutputStream buffer = new DataByteArrayOutputStream();
102         marshaller.writePayload(payload,buffer);
103         ByteSequence data = buffer.toByteSequence();
104         return convertToStoreLocation(dataManager.write(data, (byte)1, false));
105     }
106
107
108     public void force() throws IOException JavaDoc {
109         dataManager.write(FORCE_COMMAND, (byte)2, true);
110     }
111
112     public void updateItem(StoreLocation location, Marshaller marshaller, Object JavaDoc payload) throws IOException JavaDoc {
113         final DataByteArrayOutputStream buffer = new DataByteArrayOutputStream();
114         marshaller.writePayload(payload,buffer);
115         ByteSequence data = buffer.toByteSequence();
116         dataManager.update(convertFromStoreLocation(location), data, false);
117     }
118     
119     public void close() throws IOException JavaDoc {
120         dataManager.close();
121     }
122
123     public void consolidateDataFiles() throws IOException JavaDoc {
124         dataManager.consolidateDataFiles();
125     }
126
127     public boolean delete() throws IOException JavaDoc {
128         return dataManager.delete();
129     }
130     
131     public void addInterestInFile(int file) throws IOException JavaDoc {
132         dataManager.addInterestInFile(file);
133     }
134     public void removeInterestInFile(int file) throws IOException JavaDoc {
135         dataManager.removeInterestInFile(file);
136     }
137
138     public void recoverRedoItems(RedoListener listener) throws IOException JavaDoc {
139         throw new RuntimeException JavaDoc("Not Implemented..");
140     }
141     public StoreLocation storeRedoItem(Object JavaDoc payload) throws IOException JavaDoc {
142         throw new RuntimeException JavaDoc("Not Implemented..");
143     }
144
145     public Marshaller getRedoMarshaller() {
146         return redoMarshaller;
147     }
148     public void setRedoMarshaller(Marshaller redoMarshaller) {
149         this.redoMarshaller = redoMarshaller;
150     }
151
152     public String JavaDoc getName() {
153         return name;
154     }
155
156     
157 }
158
Popular Tags