001 /** 002 * 003 * Copyright 2005 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.kernel.config; 018 019 import java.util.Iterator; 020 import java.util.LinkedHashSet; 021 import java.util.Map; 022 import java.util.Set; 023 import java.util.TreeMap; 024 import java.util.List; 025 import java.util.ArrayList; 026 027 import org.apache.geronimo.kernel.repository.Artifact; 028 029 /** 030 * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $ 031 */ 032 public class ConfigurationModel { 033 private final Map configurations = new TreeMap(); 034 035 public void addConfiguation(Artifact configurationId, Set loadParentIds, Set startParentIds) throws NoSuchConfigException { 036 Set startParents = getStatuses(startParentIds); 037 038 // load parents are a superset of start parents 039 Set loadParents = new LinkedHashSet(startParents); 040 loadParents.addAll(getStatuses(loadParentIds)); 041 042 ConfigurationStatus configurationStatus = new ConfigurationStatus(configurationId, loadParents, startParents); 043 configurations.put(configurationId, configurationStatus); 044 } 045 046 private Set getStatuses(Set configurationIds) throws NoSuchConfigException { 047 LinkedHashSet statuses = new LinkedHashSet(configurationIds.size()); 048 for (Iterator iterator = configurationIds.iterator(); iterator.hasNext();) { 049 Artifact configurationId = (Artifact) iterator.next(); 050 ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId); 051 if (configurationStatus == null) { 052 throw new NoSuchConfigException(configurationId); 053 } 054 055 statuses.add(configurationStatus); 056 } 057 return statuses; 058 } 059 060 public void removeConfiguration(Artifact configurationId) throws NoSuchConfigException { 061 ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId); 062 if (configurationStatus == null) { 063 throw new NoSuchConfigException(configurationId); 064 } 065 configurationStatus.destroy(); 066 configurations.remove(configurationId); 067 } 068 069 public boolean containsConfiguration(Artifact configurationId) { 070 return configurations.containsKey(configurationId); 071 } 072 073 public void upgradeConfiguration(Artifact existingId, Artifact newId, Set newLoadParentIds, Set newStartParentIds) throws NoSuchConfigException { 074 Set newStartParents = getStatuses(newStartParentIds); 075 076 // load parents are a superset of start parents 077 Set newLoadParents = new LinkedHashSet(newStartParents); 078 newLoadParents.addAll(getStatuses(newLoadParentIds)); 079 080 ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.remove(existingId); 081 if (configurationStatus == null) { 082 throw new NoSuchConfigException(existingId); 083 } 084 configurations.put(newId, configurationStatus); 085 configurationStatus.upgrade(newId, newLoadParents, newStartParents); 086 } 087 088 public boolean isLoaded(Artifact configurationId) { 089 ConfigurationStatus configurationStatus = (ConfigurationStatus) configurations.get(configurationId); 090 if (configurationStatus != null) { 091 return configurationStatus.isLoaded(); 092 } 093 return false; 094 } 095 096 public Artifact[] getLoaded(Artifact query) { 097 List results = new ArrayList(); 098 for (Iterator it = configurations.entrySet().iterator(); it.hasNext();) { 099 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 }