KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > storage > impl > AxisFileStorage


1 package org.apache.axis2.storage.impl;
2
3 import java.io.File JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.HashMap JavaDoc;
8
9 /*
10  * Copyright 2004,2005 The Apache Software Foundation.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  *
25  */

26 public class AxisFileStorage extends AbstractStorage{
27
28     private File JavaDoc file;
29     private FileOutputStream JavaDoc fos;
30     private HashMap JavaDoc map;
31
32     public AxisFileStorage() {
33         map = new HashMap JavaDoc();
34     }
35
36     public AxisFileStorage(File JavaDoc file) {
37         this();
38         this.setFile(file);
39     }
40
41     public File JavaDoc getFile() {
42         return file;
43     }
44
45     public void setFile(File JavaDoc file) {
46         try {
47             this.file = file;
48             this.fos = new FileOutputStream JavaDoc(file);
49         } catch (FileNotFoundException JavaDoc e) {
50             throw new UnsupportedOperationException JavaDoc("No such file!");
51         }
52     }
53
54     public Object JavaDoc put(Object JavaDoc value) {
55
56         try {
57             String JavaDoc key = getUniqueKey();
58             map.put(key,value);
59
60             updateFileState();
61
62             return key;
63         } catch (IOException JavaDoc e) {
64             throw new UnsupportedOperationException JavaDoc(e.getMessage());
65         } catch (Exception JavaDoc e) {
66             throw new UnsupportedOperationException JavaDoc(e.getMessage());
67         }
68
69     }
70
71     public Object JavaDoc get(Object JavaDoc key) {
72         return map.get(key);
73     }
74
75     public Object JavaDoc remove(Object JavaDoc key) {
76         try {
77             Object JavaDoc objToRemove = map.remove(key);
78
79             updateFileState();
80
81             return objToRemove;
82         } catch (IOException JavaDoc e) {
83             throw new UnsupportedOperationException JavaDoc(" file writing failed!");
84         }
85     }
86
87     private void updateFileState() throws IOException JavaDoc {
88 // ObjectOutput out = new ObjectOutputStream(fos);
89
// out.writeObject(map);
90
// out.close();
91
}
92
93     public boolean clean() {
94        try {
95             map.clear();
96             updateFileState();
97             return true;
98         } catch (Exception JavaDoc e) {
99             return false;
100         }
101     }
102
103
104 }
105
Popular Tags