View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  import java.util.LinkedHashSet;
21  import java.util.Map;
22  import java.util.Set;
23  import java.util.TreeMap;
24  import java.util.List;
25  import java.util.ArrayList;
26  
27  import org.apache.geronimo.kernel.repository.Artifact;
28  
29  /**
30   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
31   */
32  public class ConfigurationModel {
33      private final Map configurations = new TreeMap();
34  
35      public void addConfiguation(Artifact configurationId, Set loadParentIds, Set startParentIds) throws NoSuchConfigException {
36          Set startParents = getStatuses(startParentIds);
37  
38          // load parents are a superset of start parents
39          Set loadParents = new LinkedHashSet(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 getStatuses(Set configurationIds) throws NoSuchConfigException {
47          LinkedHashSet statuses = new LinkedHashSet(configurationIds.size());
48          for (Iterator 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 newLoadParentIds, Set newStartParentIds) throws NoSuchConfigException {
74          Set newStartParents = getStatuses(newStartParentIds);
75  
76          // load parents are a superset of start parents
77          Set newLoadParents = new LinkedHashSet(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 results = new ArrayList();
98          for (Iterator it = configurations.entrySet().iterator(); it.hasNext();) {
99              Map.Entry entry = (Map.Entry) 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 results = new ArrayList();
119         for (Iterator it = configurations.entrySet().iterator(); it.hasNext();) {
120             Map.Entry entry = (Map.Entry) 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 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 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 stop(Artifact configurationId) throws NoSuchConfigException {
147         return stop(configurationId, true);
148     }
149 
150     public LinkedHashSet 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 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 unload(Artifact configurationId) throws NoSuchConfigException {
167         return unload(configurationId, true);
168     }
169 
170     public LinkedHashSet 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 reload(Artifact existingConfigurationId) throws NoSuchConfigException {
179         ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(existingConfigurationId);
180         if (configurationStatus == null) {
181             return new LinkedHashSet();
182         }
183         return configurationStatus.reload();
184     }
185 
186     public Set getLoaded() {
187         Set result = new LinkedHashSet();
188         for (Iterator 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 getStarted() {
198         Set result = new LinkedHashSet();
199         for (Iterator 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 getUserLoaded() {
209         Set result = new LinkedHashSet();
210         for (Iterator 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 getUserStarted() {
220         Set result = new LinkedHashSet();
221         for (Iterator 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 }