1 19 20 package org.netbeans.modules.j2ee.deployment.impl.projects; 21 22 import org.netbeans.modules.j2ee.deployment.execution.*; 23 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 24 import org.netbeans.modules.j2ee.deployment.impl.*; 25 import org.netbeans.modules.j2ee.deployment.devmodules.spi.*; 26 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 27 import javax.enterprise.deploy.spi.TargetModuleID ; 28 import javax.enterprise.deploy.shared.ModuleType ; 29 import org.openide.ErrorManager; 30 import org.openide.filesystems.FileUtil; 31 import java.util.*; 32 import java.io.*; 33 import org.netbeans.modules.j2ee.deployment.config.*; 34 import org.openide.util.NbBundle; 35 36 40 public final class DeploymentTargetImpl implements DeploymentTarget { 41 42 J2eeModuleProvider moduleProvider; 43 String clientName; 44 ServerString server; 45 TargetModule[] targetModules; 46 47 public DeploymentTargetImpl(J2eeModuleProvider moduleProvider, String clientName) { 48 this.moduleProvider = moduleProvider; 49 this.clientName = clientName; 50 } 51 52 public J2eeModule getModule() { 53 return moduleProvider.getJ2eeModule (); 54 } 55 56 public ModuleChangeReporter getModuleChangeReporter() { 57 return moduleProvider.getModuleChangeReporter (); 58 } 59 60 67 public String getClientUrl(String partUrl) { 68 J2eeModule clientModule = null; 70 String url = null; 71 72 if (moduleProvider instanceof J2eeAppProvider) { 73 J2eeAppProvider ear = (J2eeAppProvider) moduleProvider; 74 J2eeModuleProvider clientProvider = getChildModuleProvider(ear, clientName); 75 if (clientProvider != null) 76 clientModule = clientProvider.getJ2eeModule(); 77 else { 78 J2eeModuleContainer jmc = (J2eeModuleContainer) ear.getJ2eeModule(); 80 J2eeModule[] modules = jmc.getModules(null); 81 for (int i=0; i<modules.length; i++) { 82 if (J2eeModule.WAR.equals(modules[i].getModuleType())) { 83 clientModule = modules[i]; 84 break; 85 } 86 } 87 } 88 } else { 89 clientModule = moduleProvider.getJ2eeModule(); 90 } 91 92 url = findWebUrl(clientModule); 93 if (url != null) { 94 if (partUrl.startsWith("/")) 95 return (url + partUrl); 96 else 97 return (url + "/" + partUrl); } else { 99 return null; 100 } 101 } 102 103 private J2eeModuleProvider getChildModuleProvider(J2eeModuleProvider jmp, String uri) { 104 if (uri == null) 105 return null; 106 J2eeModuleProvider child = null; 107 if (jmp instanceof J2eeAppProvider) { 108 J2eeAppProvider jap = (J2eeAppProvider) jmp; 109 child = jap.getChildModuleProvider(uri); 110 if (child == null) { 111 String root = "/" ; if (uri.startsWith(root)) { 113 uri = uri.substring(1); 114 } else { 115 uri = root + uri; 116 } 117 child = jap.getChildModuleProvider(uri); 118 } 119 } 120 return child; 121 } 122 123 private TargetModule getTargetModule() { 124 TargetModule[] mods = getTargetModules(); 125 if (mods == null || mods.length == 0) 126 return null; 127 128 if (mods[0].delegate() != null) 129 return mods[0]; 130 131 ServerString defaultTarget = ServerRegistry.getInstance().getDefaultInstance(); 133 TargetModule execMod = null; 134 if ( defaultTarget != null ) { 135 for (int i=0; i<mods.length; i++) { 136 if (mods[i].getInstanceUrl().equals(defaultTarget.getUrl()) && 137 mods[i].getTargetName().equals(defaultTarget.getTargets(true)[0])) { 138 execMod = mods[i]; 139 break; 140 } 141 } 142 } 143 144 if (execMod == null) execMod = mods[0]; 145 execMod.initDelegate((ModuleType )getModule().getModuleType()); 146 return execMod; 147 } 148 149 154 private String findWebUrl(J2eeModule client) { 155 TargetModule module = getTargetModule(); 156 if (module == null) { 157 return null; 158 } 159 if (getModule() == client) { return module.getWebURL(); 161 } 162 163 ServerInstance instance = ServerRegistry.getInstance().getServerInstance(module.getInstanceUrl()); 164 IncrementalDeployment mur = instance.getIncrementalDeployment (); 165 String clientModuleUri = client == null ? "" : client.getUrl(); 166 if (clientModuleUri.startsWith("/")) { clientModuleUri = clientModuleUri.substring(1); 168 } 169 TargetModuleID [] children = module.getChildTargetModuleID(); 170 String urlString = null; 171 TargetModuleID tmid = null; 172 for (int i=0; children != null && i<children.length; i++) { 173 if (urlString == null || urlString.trim().equals("")) { 175 urlString = children[i].getWebURL(); 176 } 177 178 String uri = children[i].getModuleID(); if (mur != null) { 180 uri = mur.getModuleUrl(children[i]); 181 } else { 182 int j = uri.indexOf('#'); 183 if (j > -1) { 184 uri = uri.substring(j+1); 185 } 186 } 187 188 if (mur != null && clientModuleUri.equalsIgnoreCase(uri)) { 189 tmid = children[i]; 190 break; 191 } 192 } 193 if (tmid != null) { 195 urlString = tmid.getWebURL(); 196 } 197 198 return urlString; 199 } 200 201 private ConfigSupportImpl getConfigSupportImpl () { 202 return (ConfigSupportImpl) moduleProvider.getConfigSupport (); 203 } 204 205 public File getConfigurationFile() { 206 return getConfigSupportImpl ().getConfigurationFile (); 207 } 208 209 public ServerString getServer() { 210 if (server == null) { 211 String instanceID = moduleProvider.getServerInstanceID (); 212 ServerInstance inst = ServerRegistry.getInstance ().getServerInstance (instanceID); 213 if (inst == null) { 214 throw new RuntimeException (NbBundle.getMessage(DeploymentTargetImpl.class, "MSG_TargetServerNotFound",instanceID)); 215 } 216 server = new ServerString(inst); 217 } 218 return server; 219 } 220 221 public TargetModule[] getTargetModules() { 222 if (targetModules == null || targetModules.length == 0) { 223 String fname = getTargetModuleFileName(); 224 if (fname == null) { 225 return null; 226 } 227 targetModules = TargetModule.load(getServer(), fname); 228 } 229 return targetModules; 230 } 231 232 public void setTargetModules(TargetModule[] targetModules) { 233 this.targetModules = targetModules; 234 for (int i=0; i< targetModules.length; i++) { 235 targetModules[i].save(getTargetModuleFileName()); 236 } 237 } 238 239 public DeploymentConfigurationProvider getDeploymentConfigurationProvider() { 240 return getConfigSupportImpl (); 241 } 242 243 public J2eeModuleProvider.ConfigSupport getConfigSupport () { 244 return moduleProvider.getConfigSupport(); 245 } 246 247 private String getTargetModuleFileName() { 248 String fileName = getDeploymentName(); 249 if (fileName != null) 250 return fileName; 251 252 File f = null; 253 try { 254 if (getModule().getContentDirectory() != null) { 255 f = FileUtil.toFile(getModule().getContentDirectory()); 256 } 257 } catch (IOException ioe) { 258 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 259 } 260 if (f == null) { 261 fileName = getConfigSupportImpl().getDeploymentName(); 262 } else { 263 String pathName = f.getAbsolutePath(); 264 fileName = TargetModule.shortNameFromPath(pathName); 265 } 266 return fileName; 267 } 268 269 public String getDeploymentName() { 270 return moduleProvider.getDeploymentName(); 271 } 272 } | Popular Tags |