1 23 24 29 30 package com.sun.enterprise.deployment.phasing; 31 32 import com.sun.enterprise.util.i18n.StringManager; 33 import com.sun.enterprise.util.i18n.StringManagerBase; 34 import com.sun.enterprise.admin.util.IAdminConstants; 35 import com.sun.enterprise.admin.target.Target; 36 import com.sun.enterprise.admin.target.TargetType; 37 import com.sun.enterprise.admin.target.TargetBuilder; 38 39 import com.sun.enterprise.config.ConfigException; 40 import com.sun.enterprise.config.ConfigContext; 41 import com.sun.enterprise.config.serverbeans.Domain; 42 import com.sun.enterprise.config.serverbeans.Applications; 43 import com.sun.enterprise.config.serverbeans.Server; 44 import com.sun.enterprise.config.serverbeans.Cluster; 45 import com.sun.enterprise.config.serverbeans.ApplicationRef; 46 import com.sun.enterprise.config.serverbeans.ServerTags; 47 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper; 48 import com.sun.enterprise.config.serverbeans.ServerHelper; 49 import com.sun.enterprise.config.serverbeans.ClusterHelper; 50 51 import com.sun.enterprise.admin.config.BaseConfigMBean; 52 53 57 public class ApplicationReferenceHelper implements IAdminConstants 58 { 59 private static final StringManager _strMgr = 60 StringManager.getManager(ApplicationReferenceHelper.class); 61 62 private ConfigContext _configContext = null; 63 64 65 public ApplicationReferenceHelper(ConfigContext configContext) 66 { 67 _configContext = configContext; 68 } 69 70 private ConfigContext getConfigContext() 71 { 72 return _configContext; 73 } 74 75 81 private void addApplicationReferenceToCluster(Cluster cluster, boolean enabled, 82 String virtualServers, String referenceName) throws ConfigException 83 { 84 ApplicationRef ref = cluster.getApplicationRefByRef(referenceName); 85 if (ref != null) { 86 throw new ConfigException(_strMgr.getString("clusterApplicationRefAlreadyExists", 88 referenceName, cluster.getName())); 89 } 90 ref = new ApplicationRef(); 91 ref.setEnabled(enabled); 92 ref.setRef(referenceName); 93 ref.setVirtualServers(virtualServers); 94 cluster.addApplicationRef(ref, BaseConfigMBean.OVERWRITE); 95 } 96 97 103 private void addApplicationReferenceToClusteredServers(Cluster cluster, Server[] servers, boolean enabled, 104 String virtualServers, String referenceName) throws ConfigException 105 { 106 for (int i = 0; i < servers.length; i++) { 107 final ApplicationRef ref = servers[i].getApplicationRefByRef(referenceName); 108 if (ref != null) { 109 throw new ConfigException(_strMgr.getString("clusterApplicationRefInconsistency", 112 referenceName, cluster.getName(), servers[i].getName())); 113 } 114 addApplicationReferenceToServer(servers[i], enabled, virtualServers, 115 referenceName); 116 } 117 } 118 119 125 private void addApplicationReferenceToServer(Server server, boolean enabled, 126 String virtualServers, String referenceName) throws ConfigException 127 { 128 ApplicationRef ref = server.getApplicationRefByRef(referenceName); 129 if (ref != null) { 130 throw new ConfigException(_strMgr.getString("serverApplicationRefAlreadyExists", 132 referenceName, server.getName())); 133 } 134 ref = new ApplicationRef(); 135 ref.setEnabled(enabled); 136 ref.setRef(referenceName); 137 ref.setVirtualServers(virtualServers); 138 server.addApplicationRef(ref, BaseConfigMBean.OVERWRITE); 139 } 140 141 147 protected String getApplicationType (String appName) throws ConfigException 148 { 149 final ConfigContext configContext = getConfigContext(); 150 final Domain domain = ConfigAPIHelper.getDomainConfigBean(configContext); 151 final Applications applications = domain.getApplications(); 152 if (applications.getAppclientModuleByName(appName) != null) { 153 return ServerTags.APPCLIENT_MODULE; 154 } else if (applications.getConnectorModuleByName(appName) != null) { 155 return ServerTags.CONNECTOR_MODULE; 156 } else if (applications.getEjbModuleByName(appName) != null) { 157 return ServerTags.EJB_MODULE; 158 } else if (applications.getJ2eeApplicationByName(appName) != null) { 159 return ServerTags.J2EE_APPLICATION; 160 } else if (applications.getLifecycleModuleByName(appName) != null) { 161 return ServerTags.LIFECYCLE_MODULE; 162 } else if (applications.getWebModuleByName(appName) != null) { 163 return ServerTags.WEB_MODULE; 164 } else { 165 throw new ConfigException(_strMgr.getString("applicationDoesNotExist", 166 appName)); 167 } 168 } 169 170 177 public void createApplicationReference(TargetType[] validTypes, 178 String targetName, boolean enabled, 179 String virtualServers, String referenceName) throws ConfigException 180 { 181 final ConfigContext configContext = getConfigContext(); 182 final String type = getApplicationType(referenceName); 184 final Target target = TargetBuilder.INSTANCE.createTarget( 185 validTypes, targetName, configContext); 186 if (target.getType() == TargetType.CLUSTER) { 187 final Cluster cluster = ClusterHelper.getClusterByName(configContext, target.getName()); 188 addApplicationReferenceToCluster(cluster, enabled, virtualServers, referenceName); 189 final Server[] servers = ServerHelper.getServersInCluster(configContext, target.getName()); 190 addApplicationReferenceToClusteredServers(cluster, servers, enabled, 191 virtualServers, referenceName); 192 } else if (target.getType() == TargetType.SERVER || 193 target.getType() == TargetType.DAS) { 194 final Server server = ServerHelper.getServerByName(configContext, target.getName()); 195 addApplicationReferenceToServer(server, enabled, virtualServers, referenceName); 196 } else { 197 throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget", 198 target.getName())); 199 } 200 } 201 202 208 private void deleteApplicationReferenceFromCluster(Cluster cluster, 209 String referenceName) throws ConfigException 210 { 211 final ApplicationRef ref = cluster.getApplicationRefByRef(referenceName); 212 if (ref == null) { 213 throw new ConfigException(_strMgr.getString("clusterApplicationRefDoesNotExist", 215 cluster.getName(), referenceName)); 216 } 217 cluster.removeApplicationRef(ref, BaseConfigMBean.OVERWRITE); 218 } 219 220 226 private void deleteApplicationReferenceFromClusteredServers(Cluster cluster, Server[] servers, 227 String referenceName) throws ConfigException 228 { 229 for (int i = 0; i < servers.length; i++) { 230 final ApplicationRef ref = servers[i].getApplicationRefByRef(referenceName); 231 if (ref == null) { 232 throw new ConfigException(_strMgr.getString("clusterApplicationRefInconsistency", 235 referenceName, cluster.getName(), servers[i].getName())); 236 } 237 deleteApplicationReferenceFromServer(servers[i], referenceName); 238 } 239 } 240 241 247 private void deleteApplicationReferenceFromServer(Server server, 248 String referenceName) throws ConfigException 249 { 250 final ApplicationRef ref = server.getApplicationRefByRef(referenceName); 251 if (ref == null) { 252 throw new ConfigException(_strMgr.getString("serverApplicationRefDoesNotExist", 254 server.getName(), referenceName)); 255 } 256 server.removeApplicationRef(ref, BaseConfigMBean.OVERWRITE); 257 } 258 259 266 public void deleteApplicationReference(TargetType[] validTypes, 267 String targetName, String referenceName) 268 throws ConfigException 269 { 270 final ConfigContext configContext = getConfigContext(); 271 final String type = getApplicationType(referenceName); 273 final Target target = TargetBuilder.INSTANCE.createTarget( 274 validTypes, targetName, configContext); 275 if (target.getType() == TargetType.SERVER || 276 target.getType() == TargetType.DAS) { 277 final Server server = ServerHelper.getServerByName(configContext, targetName); 278 deleteApplicationReferenceFromServer(server, referenceName); 279 } else if (target.getType() == TargetType.CLUSTER) { 280 final Cluster cluster = ClusterHelper.getClusterByName(configContext, targetName); 281 deleteApplicationReferenceFromCluster(cluster, referenceName); 282 final Server[] servers = ServerHelper.getServersInCluster(configContext, targetName); 283 deleteApplicationReferenceFromClusteredServers(cluster, servers, referenceName); 284 } else { 285 throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget", 286 targetName)); 287 } 288 } 289 } 290 | Popular Tags |