KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > store > ProcessStore


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

20 package org.apache.slide.projector.store;
21
22 import java.io.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.slide.projector.Projector;
27 import org.apache.slide.projector.Store;
28 import org.apache.slide.projector.value.MapValue;
29
30 public class ProcessStore extends AbstractStore {
31     private final static String JavaDoc STORAGE_PREFIX = "process/";
32
33     private Store store;
34     private String JavaDoc processId, storageKey;
35     private Map JavaDoc map;
36     private MapValue mapResource;
37     
38     public ProcessStore(Store store) {
39         this.store = store;
40     }
41     
42     public String JavaDoc getProcessId() {
43         return processId;
44     }
45
46     public void setProcessId(String JavaDoc processId) {
47         this.processId = processId;
48         this.storageKey = Projector.getWorkDir()+STORAGE_PREFIX+processId;
49     }
50
51     public void put(String JavaDoc key, Object JavaDoc value) throws IOException JavaDoc {
52         getMap().put(key, value);
53         store.put(storageKey, mapResource);
54     }
55
56     public Object JavaDoc get(String JavaDoc key) throws IOException JavaDoc {
57         return getMap().get(key);
58     }
59
60     public void dispose(String JavaDoc key) throws IOException JavaDoc {
61         getMap().remove(key);
62         store.put(storageKey, mapResource);
63     }
64     
65     private Map JavaDoc getMap() throws IOException JavaDoc {
66         if ( mapResource != null ) return mapResource.getMap();
67         mapResource = (MapValue)store.get(storageKey);
68         if ( mapResource == null ) {
69             map = new HashMap JavaDoc();
70             mapResource = new MapValue(map);
71             return map;
72         } else {
73             return mapResource.getMap();
74         }
75     }
76 }
Popular Tags