KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > ConfigurationModel


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

17 package org.apache.geronimo.kernel.config;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedHashSet JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.TreeMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import org.apache.geronimo.kernel.repository.Artifact;
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class ConfigurationModel {
33     private final Map JavaDoc configurations = new TreeMap JavaDoc();
34
35     public void addConfiguation(Artifact configurationId, Set JavaDoc loadParentIds, Set JavaDoc startParentIds) throws NoSuchConfigException {
36         Set JavaDoc startParents = getStatuses(startParentIds);
37
38         // load parents are a superset of start parents
39
Set JavaDoc loadParents = new LinkedHashSet JavaDoc(startParents);
40         loadParents.addAll(getStatuses(loadParentIds));
41
42         ConfigurationStatus configurationStatus = new ConfigurationStatus(configurationId, loadParents, startParents);
43         configurations.put(configurationId, configurationStatus);
44     }
45
46     private Set JavaDoc getStatuses(Set JavaDoc configurationIds) throws NoSuchConfigException {
47         LinkedHashSet JavaDoc statuses = new LinkedHashSet JavaDoc(configurationIds.size());
48         for (Iterator JavaDoc iterator = configurationIds.iterator(); iterator.hasNext();) {
49             Artifact configurationId = (Artifact) iterator.next();
50             ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
51             if (configurationStatus == null) {
52                 throw new NoSuchConfigException(configurationId);
53             }
54
55             statuses.add(configurationStatus);
56         }
57         return statuses;
58     }
59
60     public void removeConfiguration(Artifact configurationId) throws NoSuchConfigException {
61         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
62         if (configurationStatus == null) {
63             throw new NoSuchConfigException(configurationId);
64         }
65         configurationStatus.destroy();
66         configurations.remove(configurationId);
67     }
68
69     public boolean containsConfiguration(Artifact configurationId) {
70         return configurations.containsKey(configurationId);
71     }
72
73     public void upgradeConfiguration(Artifact existingId, Artifact newId, Set JavaDoc newLoadParentIds, Set JavaDoc newStartParentIds) throws NoSuchConfigException {
74         Set JavaDoc newStartParents = getStatuses(newStartParentIds);
75
76         // load parents are a superset of start parents
77
Set JavaDoc newLoadParents = new LinkedHashSet JavaDoc(newStartParents);
78         newLoadParents.addAll(getStatuses(newLoadParentIds));
79
80         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.remove(existingId);
81         if (configurationStatus == null) {
82             throw new NoSuchConfigException(existingId);
83         }
84         configurations.put(newId, configurationStatus);
85         configurationStatus.upgrade(newId, newLoadParents, newStartParents);
86     }
87
88     public boolean isLoaded(Artifact configurationId) {
89         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
90         if (configurationStatus != null) {
91             return configurationStatus.isLoaded();
92         }
93         return false;
94     }
95
96     public Artifact[] getLoaded(Artifact query) {
97         List JavaDoc results = new ArrayList JavaDoc();
98         for (Iterator JavaDoc it = configurations.entrySet().iterator(); it.hasNext();) {
99             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
100             Artifact test = (Artifact) entry.getKey();
101             ConfigurationStatus status = (ConfigurationStatus) entry.getValue();
102             if(query.matches(test) && status.isLoaded()) {
103                 results.add(test);
104             }
105         }
106         return (Artifact[]) results.toArray(new Artifact[results.size()]);
107     }
108
109     public boolean isStarted(Artifact configurationId) {
110         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
111         if (configurationStatus != null) {
112             return configurationStatus.isStarted();
113         }
114         return false;
115     }
116
117     public Artifact[] getStarted(Artifact query) {
118         List JavaDoc results = new ArrayList JavaDoc();
119         for (Iterator JavaDoc it = configurations.entrySet().iterator(); it.hasNext();) {
120             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
121             Artifact test = (Artifact) entry.getKey();
122             ConfigurationStatus status = (ConfigurationStatus) entry.getValue();
123             if(query.matches(test) && status.isStarted()) {
124                 results.add(test);
125             }
126         }
127         return (Artifact[]) results.toArray(new Artifact[results.size()]);
128     }
129
130     public LinkedHashSet JavaDoc load(Artifact configurationId) throws NoSuchConfigException {
131         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
132         if (configurationStatus == null) {
133             throw new NoSuchConfigException(configurationId);
134         }
135         return configurationStatus.load();
136     }
137
138     public LinkedHashSet JavaDoc start(Artifact configurationId) throws NoSuchConfigException {
139         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
140         if (configurationStatus == null) {
141             throw new NoSuchConfigException(configurationId);
142         }
143         return configurationStatus.start();
144     }
145
146     public LinkedHashSet JavaDoc stop(Artifact configurationId) throws NoSuchConfigException {
147         return stop(configurationId, true);
148     }
149
150     public LinkedHashSet JavaDoc stop(Artifact configurationId, boolean gc) throws NoSuchConfigException {
151         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
152         if (configurationStatus == null) {
153             throw new NoSuchConfigException(configurationId);
154         }
155         return configurationStatus.stop(gc);
156     }
157
158     public LinkedHashSet JavaDoc restart(Artifact configurationId) throws NoSuchConfigException {
159         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
160         if (configurationStatus == null) {
161             throw new NoSuchConfigException(configurationId);
162         }
163         return configurationStatus.restart();
164     }
165
166     public LinkedHashSet JavaDoc unload(Artifact configurationId) throws NoSuchConfigException {
167         return unload(configurationId, true);
168     }
169
170     public LinkedHashSet JavaDoc unload(Artifact configurationId, boolean gc) throws NoSuchConfigException {
171         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId);
172         if (configurationStatus == null) {
173             throw new NoSuchConfigException(configurationId);
174         }
175         return configurationStatus.unload(gc);
176     }
177
178     public LinkedHashSet JavaDoc reload(Artifact existingConfigurationId) throws NoSuchConfigException {
179         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(existingConfigurationId);
180         if (configurationStatus == null) {
181             return new LinkedHashSet JavaDoc();
182         }
183         return configurationStatus.reload();
184     }
185
186     public Set JavaDoc getLoaded() {
187         Set JavaDoc result = new LinkedHashSet JavaDoc();
188         for (Iterator JavaDoc iterator = configurations.values().iterator(); iterator.hasNext();) {
189             ConfigurationStatus status = (ConfigurationStatus) iterator.next();
190             if (status.isLoaded()) {
191                 result.add(status.getConfigurationId());
192             }
193         }
194         return result;
195     }
196
197     public Set JavaDoc getStarted() {
198         Set JavaDoc result = new LinkedHashSet JavaDoc();
199         for (Iterator JavaDoc iterator = configurations.values().iterator(); iterator.hasNext();) {
200             ConfigurationStatus status = (ConfigurationStatus) iterator.next();
201             if (status.isStarted()) {
202                 result.add(status.getConfigurationId());
203             }
204         }
205         return result;
206     }
207
208     public Set JavaDoc getUserLoaded() {
209         Set JavaDoc result = new LinkedHashSet JavaDoc();
210         for (Iterator JavaDoc iterator = configurations.values().iterator(); iterator.hasNext();) {
211             ConfigurationStatus status = (ConfigurationStatus) iterator.next();
212             if (status.isUserLoaded()) {
213                 result.add(status.getConfigurationId());
214             }
215         }
216         return result;
217     }
218
219     public Set JavaDoc getUserStarted() {
220         Set JavaDoc result = new LinkedHashSet JavaDoc();
221         for (Iterator JavaDoc iterator = configurations.values().iterator(); iterator.hasNext();) {
222             ConfigurationStatus status = (ConfigurationStatus) iterator.next();
223             if (status.isUserStarted()) {
224                 result.add(status.getConfigurationId());
225             }
226         }
227         return result;
228     }
229 }
230
Popular Tags