]> git.argeo.org Git - lgpl/argeo-commons.git/blob - provisioning/SimpleProvisioningService.java
Prepare next development cycle
[lgpl/argeo-commons.git] / provisioning / SimpleProvisioningService.java
1 package org.argeo.osgi.provisioning;
2
3 import java.io.IOException;
4 import java.util.Collections;
5 import java.util.Dictionary;
6 import java.util.Enumeration;
7 import java.util.Iterator;
8 import java.util.Map;
9 import java.util.TreeMap;
10 import java.util.zip.ZipInputStream;
11
12 import org.osgi.service.provisioning.ProvisioningService;
13
14 public class SimpleProvisioningService implements ProvisioningService {
15 private Map<String, Object> map = Collections.synchronizedSortedMap(new TreeMap<>());
16
17 public SimpleProvisioningService() {
18 // update count
19 map.put(PROVISIONING_UPDATE_COUNT, 0);
20 }
21
22 @Override
23 public Dictionary<String, Object> getInformation() {
24 return new Information();
25 }
26
27 @SuppressWarnings("rawtypes")
28 @Override
29 public synchronized void setInformation(Dictionary info) {
30 map.clear();
31 addInformation(info);
32 }
33
34 @SuppressWarnings({ "rawtypes", "unchecked" })
35 @Override
36 public synchronized void addInformation(Dictionary info) {
37 Enumeration<String> e = info.keys();
38 while (e.hasMoreElements()) {
39 String key = e.nextElement();
40 map.put(key, info.get(key));
41 }
42 incrementProvisioningUpdateCount();
43 }
44
45 protected synchronized void incrementProvisioningUpdateCount() {
46 Integer current = (Integer) map.get(PROVISIONING_UPDATE_COUNT);
47 Integer newValue = current + 1;
48 map.put(PROVISIONING_UPDATE_COUNT, newValue);
49 }
50
51 @Override
52 public synchronized void addInformation(ZipInputStream zis) throws IOException {
53 throw new UnsupportedOperationException();
54 }
55
56
57
58 class Information extends Dictionary<String, Object> {
59
60 @Override
61 public int size() {
62 return map.size();
63 }
64
65 @Override
66 public boolean isEmpty() {
67 return map.isEmpty();
68 }
69
70 @Override
71 public Enumeration<String> keys() {
72 Iterator<String> it = map.keySet().iterator();
73 return new Enumeration<String>() {
74
75 @Override
76 public boolean hasMoreElements() {
77 return it.hasNext();
78 }
79
80 @Override
81 public String nextElement() {
82 return it.next();
83 }
84
85 };
86 }
87
88 @Override
89 public Enumeration<Object> elements() {
90 Iterator<Object> it = map.values().iterator();
91 return new Enumeration<Object>() {
92
93 @Override
94 public boolean hasMoreElements() {
95 return it.hasNext();
96 }
97
98 @Override
99 public Object nextElement() {
100 return it.next();
101 }
102
103 };
104 }
105
106 @Override
107 public Object get(Object key) {
108 return map.get(key);
109 }
110
111 @Override
112 public Object put(String key, Object value) {
113 throw new UnsupportedOperationException();
114 }
115
116 @Override
117 public Object remove(Object key) {
118 throw new UnsupportedOperationException();
119 }
120
121 }
122 }