KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > sample > SampleDatabase


1 package com.sslexplorer.sample;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import com.sslexplorer.core.CoreServlet;
11 import com.sslexplorer.core.Database;
12 import com.sslexplorer.policyframework.Resource;
13
14 /**
15  * <p>
16  * In Memory storage for the Sample Resorces.
17  *
18  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
19  *
20  */

21 public class SampleDatabase implements Database {
22
23     private Map JavaDoc data;
24     private int counter;
25
26     /**
27      * Constructor
28      */

29     public SampleDatabase() {
30         this.data = new HashMap JavaDoc();
31         this.counter = 0;
32     }
33
34     /**
35      * @return A List of all SampleDatabase.
36      */

37     public List JavaDoc getSamples() {
38         return new ArrayList JavaDoc(this.data.values());
39     }
40
41     /**
42      * @param id The id of the Sample
43      * @return The sample with the specified id.
44      */

45     public Sample getSample(int id) {
46         if (this.data.containsKey(String.valueOf(id))) {
47             return (Sample) this.data.get(String.valueOf(id));
48         } else {
49             return null;
50         }
51     }
52
53     /**
54      * @param name The name of the sample
55      * @param description The description of the sample
56      * @param parentResourcePermission The parent resource permission of the
57      * sample.
58      * @return The newly created sample.
59      * @throws Exception If the sample already exists with the latest id.
60      */

61     public Sample addSample(String JavaDoc name, String JavaDoc description, int parentResourcePermission) throws Exception JavaDoc {
62         Calendar JavaDoc now = Calendar.getInstance();
63         Sample sample = new DefaultSample(counter, name, description, parentResourcePermission, now, now);
64         if (this.data.containsKey(String.valueOf(counter))) {
65             throw new Exception JavaDoc("Sample id already exists.");
66         } else {
67             this.data.put(String.valueOf(sample.getResourceId()), sample);
68             counter++;
69             return sample;
70         }
71     }
72
73     /**
74      * @param name The name of the sample to retrieve.
75      * @return The resource.
76      */

77     public Resource getSample(String JavaDoc name) {
78         for (Iterator JavaDoc iter = this.data.values().iterator(); iter.hasNext();) {
79             Sample element = (Sample) iter.next();
80             if (element.getResourceName().equals(name)) {
81                 return element;
82             }
83             return null;
84         }
85
86         return null;
87     }
88
89     /**
90      * @param resourceId The resource id to be removed.
91      * @return the sample that was removed
92      * @throws Exception If the resource doesnot exist.
93      */

94     public Sample removeSample(int resourceId) throws Exception JavaDoc {
95         Sample s = getSample(resourceId);
96         if (s != null) {
97             this.data.remove(String.valueOf(resourceId));
98         } else {
99             throw new Exception JavaDoc("No Sample with id " + resourceId);
100         }
101         return s;
102     }
103
104     /**
105      * @param sample The sample to update.
106      * @throws Exception If there is not a resource with the same name.
107      */

108     public void updateSample(Sample sample) throws Exception JavaDoc {
109         Calendar JavaDoc now = Calendar.getInstance();
110         sample.setDateAmended(now);
111         if (this.data.containsKey(String.valueOf(sample.getResourceId()))) {
112             this.data.put(String.valueOf(sample.getResourceId()), sample);
113         } else {
114             throw new Exception JavaDoc("The resource id does not exist.");
115         }
116     }
117     
118     /* (non-Javadoc)
119      * @see com.sslexplorer.core.Database#cleanup()
120      */

121     public void cleanup() throws Exception JavaDoc {
122         
123     }
124
125     /* (non-Javadoc)
126      * @see com.sslexplorer.core.Database#open(com.sslexplorer.core.CoreServlet)
127      */

128     public void open(CoreServlet controllingServlet) throws Exception JavaDoc {
129         
130     }
131
132     /* (non-Javadoc)
133      * @see com.sslexplorer.core.Database#close()
134      */

135     public void close() throws Exception JavaDoc {
136     }
137 }
138
Popular Tags