1 23 24 29 30 package com.sun.enterprise.admin.configbeans; 31 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import com.sun.enterprise.util.i18n.StringManager; 35 import com.sun.enterprise.util.i18n.StringManagerBase; 36 import com.sun.enterprise.admin.util.IAdminConstants; 37 import com.sun.enterprise.admin.target.Target; 38 import com.sun.enterprise.admin.target.TargetType; 39 import com.sun.enterprise.admin.target.TargetBuilder; 40 import com.sun.enterprise.config.ConfigException; 41 import com.sun.enterprise.config.ConfigContext; 42 import com.sun.enterprise.config.serverbeans.Domain; 43 import com.sun.enterprise.config.serverbeans.Resources; 44 import com.sun.enterprise.config.serverbeans.Server; 45 import com.sun.enterprise.config.serverbeans.Cluster; 46 import com.sun.enterprise.config.serverbeans.ResourceRef; 47 import com.sun.enterprise.config.serverbeans.ServerTags; 48 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper; 49 import com.sun.enterprise.config.serverbeans.ResourceHelper; 50 import com.sun.enterprise.config.serverbeans.ServerHelper; 51 import com.sun.enterprise.config.serverbeans.ClusterHelper; 52 53 57 public class ResourceReferenceHelper extends BaseConfigBean 58 { 59 private static final TargetType[] VALID_CREATE_DELETE_TYPES = new TargetType[] { 60 TargetType.CLUSTER, TargetType.UNCLUSTERED_SERVER, TargetType.DAS}; 61 62 private static final StringManager _strMgr = 63 StringManager.getManager(ResourceReferenceHelper.class); 64 65 public ResourceReferenceHelper(ConfigContext configContext) 66 { 67 super(configContext); 68 } 69 70 private String validateResourceAndGetType(String referenceName, 71 boolean allowSystemRefs) throws ConfigException 72 { 73 final String type = getResourceType(referenceName); 75 if (!allowSystemRefs) { 78 if (ResourceHelper.isSystemResource(getConfigContext(), referenceName)) { 79 throw new ConfigException(_strMgr.getString("systemResourceRefNotAllowed", 80 referenceName)); 81 } 82 } 83 return type; 84 } 85 86 92 private void addResourceReferenceToCluster(Cluster cluster, boolean enabled, 93 String referenceName) throws ConfigException 94 { 95 ResourceRef ref = cluster.getResourceRefByRef(referenceName); 96 if (ref != null) { 97 throw new ConfigException(_strMgr.getString("clusterResourceRefAlreadyExists", 99 referenceName, cluster.getName())); 100 } 101 ref = new ResourceRef(); 102 ref.setEnabled(enabled); 103 ref.setRef(referenceName); 104 cluster.addResourceRef(ref, OVERWRITE); 105 } 106 107 113 private void addResourceReferenceToClusteredServers(Cluster cluster, Server[] servers, boolean enabled, 114 String referenceName) throws ConfigException 115 { 116 for (int i = 0; i < servers.length; i++) { 117 final ResourceRef ref = servers[i].getResourceRefByRef(referenceName); 118 if (ref != null) { 119 throw new ConfigException(_strMgr.getString("clusterResourceRefInconsistency", 122 referenceName, cluster.getName(), servers[i].getName())); 123 } 124 addResourceReferenceToServer(servers[i], enabled, referenceName); 125 } 126 } 127 128 134 private void addResourceReferenceToServer(Server server, boolean enabled, 135 String referenceName) throws ConfigException 136 { 137 ResourceRef ref = server.getResourceRefByRef(referenceName); 138 if (ref != null) { 139 throw new ConfigException(_strMgr.getString("serverResourceRefAlreadyExists", 141 referenceName, server.getName())); 142 } 143 ref = new ResourceRef(); 144 ref.setEnabled(enabled); 145 ref.setRef(referenceName); 146 server.addResourceRef(ref, OVERWRITE); 147 } 148 149 153 public String getResourceType (String resourceName) throws ConfigException 154 { 155 final ConfigContext configContext = getConfigContext(); 156 final Domain domain = ConfigAPIHelper.getDomainConfigBean(configContext); 157 final Resources resources = domain.getResources(); 158 if (resources.getAdminObjectResourceByJndiName(resourceName) != null) { 159 return ServerTags.ADMIN_OBJECT_RESOURCE; 160 } else if (resources.getConnectorConnectionPoolByName(resourceName) != null) { 161 return ServerTags.CONNECTOR_CONNECTION_POOL; 162 } else if (resources.getConnectorResourceByJndiName(resourceName) != null) { 163 return ServerTags.CONNECTOR_RESOURCE; 164 } else if (resources.getCustomResourceByJndiName(resourceName) != null) { 165 return ServerTags.CUSTOM_RESOURCE; 166 } else if (resources.getExternalJndiResourceByJndiName(resourceName) != null) { 167 return ServerTags.EXTERNAL_JNDI_RESOURCE; 168 } else if (resources.getJdbcConnectionPoolByName(resourceName) != null) { 169 return ServerTags.JDBC_CONNECTION_POOL; 170 } else if (resources.getJdbcResourceByJndiName(resourceName) != null) { 171 return ServerTags.JDBC_RESOURCE_JNDI_NAME; 172 } else if (resources.getMailResourceByJndiName(resourceName) != null) { 173 return ServerTags.MAIL_RESOURCE; 174 } else if (resources.getPersistenceManagerFactoryResourceByJndiName(resourceName) != null) { 175 return ServerTags.PERSISTENCE_MANAGER_FACTORY_RESOURCE; 176 } else if (resources.getResourceAdapterConfigByResourceAdapterName(resourceName) != null) { 177 return ServerTags.RESOURCE_ADAPTER_CONFIG; 178 } else { 179 throw new ConfigException(_strMgr.getString("resourceDoesNotExist", 180 resourceName)); 181 } 182 } 183 184 191 public void createResourceReference(String targetName, boolean enabled, 192 String referenceName, boolean allowSystemRefs) throws ConfigException 193 { 194 createResourceReference(VALID_CREATE_DELETE_TYPES, targetName, enabled, 195 referenceName, allowSystemRefs); 196 } 197 198 public void createResourceReference(String targetName, boolean enabled, 199 String referenceName) throws ConfigException 200 { 201 createResourceReference(targetName, enabled, referenceName, false); 202 } 203 204 public void createResourceReference(TargetType[] validTypes, 205 String targetName, boolean enabled, 206 String referenceName) throws ConfigException 207 { 208 createResourceReference(validTypes, targetName, enabled, referenceName, false); 209 } 210 211 public void createResourceReference(TargetType[] validTypes, 212 String targetName, boolean enabled, 213 String referenceName, 214 boolean allowSystemRefs) throws ConfigException 215 { 216 final String type = validateResourceAndGetType(referenceName, 220 allowSystemRefs); 221 final ConfigContext configContext = getConfigContext(); 222 final Target target = TargetBuilder.INSTANCE.createTarget( 223 validTypes, targetName, configContext); 224 if (target.getType() == TargetType.CLUSTER) { 225 final Cluster cluster = ClusterHelper.getClusterByName(configContext, target.getName()); 226 addResourceReferenceToCluster(cluster, enabled, referenceName); 227 final Server[] servers = ServerHelper.getServersInCluster(configContext, target.getName()); 228 addResourceReferenceToClusteredServers(cluster, servers, enabled, referenceName); 229 } else if (target.getType() == TargetType.SERVER || 230 target.getType() == TargetType.DAS) { 231 final Server server = ServerHelper.getServerByName(configContext, target.getName()); 232 addResourceReferenceToServer(server, enabled, referenceName); 233 } else { 234 throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget", 235 target.getName())); 236 } 237 } 238 239 245 private void deleteResourceReferenceFromCluster(Cluster cluster, 246 String referenceName) throws ConfigException 247 { 248 final ResourceRef ref = cluster.getResourceRefByRef(referenceName); 249 if (ref == null) { 250 throw new ConfigException(_strMgr.getString("clusterResourceRefDoesNotExist", 252 cluster.getName(), referenceName)); 253 } 254 cluster.removeResourceRef(ref, OVERWRITE); 255 } 256 257 263 private void deleteResourceReferenceFromClusteredServers(Cluster cluster, Server[] servers, 264 String referenceName) throws ConfigException 265 { 266 for (int i = 0; i < servers.length; i++) { 267 final ResourceRef ref = servers[i].getResourceRefByRef(referenceName); 268 if (ref == null) { 269 throw new ConfigException(_strMgr.getString("clusterResourceRefInconsistency", 272 referenceName, cluster.getName(), servers[i].getName())); 273 } 274 deleteResourceReferenceFromServer(servers[i], referenceName); 275 } 276 } 277 278 284 private void deleteResourceReferenceFromServer(Server server, 285 String referenceName) throws ConfigException 286 { 287 final ResourceRef ref = server.getResourceRefByRef(referenceName); 288 if (ref == null) { 289 throw new ConfigException(_strMgr.getString("serverResourceRefDoesNotExist", 291 server.getName(), referenceName)); 292 } 293 server.removeResourceRef(ref, OVERWRITE); 294 } 295 296 303 public void deleteResourceReference(String targetName, String referenceName, 304 boolean allowSystemRefs) throws ConfigException 305 { 306 deleteResourceReference(VALID_CREATE_DELETE_TYPES, targetName, 307 referenceName, allowSystemRefs); 308 } 309 310 public void deleteResourceReference(String targetName, String referenceName) 311 throws ConfigException 312 { 313 deleteResourceReference(targetName, referenceName, false); 314 } 315 316 public void deleteResourceReference(TargetType[] validTypes, 317 String targetName, String referenceName) 318 throws ConfigException 319 { 320 deleteResourceReference(validTypes, targetName, referenceName, false); 321 } 322 323 public void deleteResourceReference(TargetType[] validTypes, 324 String targetName, String referenceName, boolean allowSystemRefs) 325 throws ConfigException 326 { 327 final String type = validateResourceAndGetType(referenceName, 331 allowSystemRefs); 332 final ConfigContext configContext = getConfigContext(); 333 final Target target = TargetBuilder.INSTANCE.createTarget( 334 validTypes, targetName, configContext); 335 336 if (target.getType() == TargetType.SERVER || 337 target.getType() == TargetType.DAS) { 338 final Server server = ServerHelper.getServerByName(configContext, target.getName()); 339 deleteResourceReferenceFromServer(server, referenceName); 340 } else if (target.getType().equals(TargetType.CLUSTER)) { 341 final Cluster cluster = ClusterHelper.getClusterByName(configContext, target.getName()); 342 deleteResourceReferenceFromCluster(cluster, referenceName); 343 final Server[] servers = ServerHelper.getServersInCluster(configContext, target.getName()); 344 deleteResourceReferenceFromClusteredServers(cluster, servers, referenceName); 345 } else { 346 throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget", 347 target.getName())); 348 } 349 350 } 351 } 352 | Popular Tags |