001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005 *
006 * The contents of this file are subject to the terms of either the GNU
007 * General Public License Version 2 only ("GPL") or the Common
008 * Development and Distribution License("CDDL") (collectively, the
009 * "License"). You may not use this file except in compliance with the
010 * License. You can obtain a copy of the License at
011 * http://www.netbeans.org/cddl-gplv2.html
012 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013 * specific language governing permissions and limitations under the
014 * License. When distributing the software, include this License Header
015 * Notice in each file and include the License file at
016 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017 * particular file as subject to the "Classpath" exception as provided
018 * by Sun in the GPL Version 2 section of the License file that
019 * accompanied this code. If applicable, add the following below the
020 * License Header, with the fields enclosed by brackets [] replaced by
021 * your own identifying information:
022 * "Portions Copyrighted [year] [name of copyright owner]"
023 *
024 * Contributor(s):
025 *
026 * The Original Software is NetBeans. The Initial Developer of the Original
027 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028 * Microsystems, Inc. All Rights Reserved.
029 *
030 * If you wish your version of this file to be governed by only the CDDL
031 * or only the GPL Version 2, indicate your decision by adding
032 * "[Contributor] elects to include this software in this distribution
033 * under the [CDDL or GPL Version 2] license." If you do not indicate a
034 * single choice of license, a recipient has the option to distribute
035 * your version of this file under either the CDDL, the GPL Version 2 or
036 * to extend the choice of license to its licensees as provided above.
037 * However, if you add GPL Version 2 code and therefore, elected the GPL
038 * Version 2 license, then the option applies only if the new code is
039 * made subject to such option by the copyright holder.
040 */
041
042 package org.netbeans.modules.j2ee.deployment.impl.projects;
043
044 import org.netbeans.modules.j2ee.deployment.execution.*;
045 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
046 import org.netbeans.modules.j2ee.deployment.impl.*;
047 import org.netbeans.modules.j2ee.deployment.devmodules.spi.*;
048 import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
049 import javax.enterprise.deploy.spi.TargetModuleID;
050 import javax.enterprise.deploy.shared.ModuleType;
051 import org.openide.filesystems.FileUtil;
052 import java.util.*;
053 import java.io.*;
054 import java.util.logging.Level;
055 import java.util.logging.Logger;
056 import org.netbeans.modules.j2ee.deployment.config.*;
057 import org.netbeans.modules.j2ee.deployment.plugins.spi.IncrementalDeployment;
058 import org.openide.util.NbBundle;
059
060 /**
061 *
062 * @author George FinKlang
063 */
064 public final class DeploymentTargetImpl implements DeploymentTarget {
065
066 J2eeModuleProvider moduleProvider;
067 String clientName;
068 ServerString server;
069 TargetModule[] targetModules;
070
071 public DeploymentTargetImpl(J2eeModuleProvider moduleProvider,
072 String clientName) {
073 this .moduleProvider = moduleProvider;
074 this .clientName = clientName;
075 }
076
077 public J2eeModule getModule() {
078 return moduleProvider.getJ2eeModule();
079 }
080
081 public ModuleChangeReporter getModuleChangeReporter() {
082 return moduleProvider.getModuleChangeReporter();
083 }
084
085 /**
086 * This will return url to invoke webbrowser for web client.
087 * If there is no webclient, null will be returned.
088 *
089 * Caution: this call does not attempt to detect whehter the client specified
090 * by clientName is app client.
091 */
092 public String getClientUrl(String partUrl) {
093 // determine client module
094 J2eeModule clientModule = null;
095 String url = null;
096
097 if (moduleProvider instanceof J2eeApplicationProvider) {
098 J2eeApplicationProvider ear = (J2eeApplicationProvider) moduleProvider;
099 J2eeModuleProvider clientProvider = getChildModuleProvider(
100 ear, clientName);
101 if (clientProvider != null)
102 clientModule = clientProvider.getJ2eeModule();
103 else {
104 //findWebUrl(null) will take care to find a first weburl it sees, but just to be sure...
105 J2eeApplication jmc = (J2eeApplication) ear
106 .getJ2eeModule();
107 J2eeModule[] modules = jmc.getModules();
108 for (int i = 0; i < modules.length; i++) {
109 if (J2eeModule.WAR.equals(modules[i]
110 .getModuleType())) {
111 clientModule = modules[i];
112 break;
113 }
114 }
115 }
116 } else {
117 clientModule = moduleProvider.getJ2eeModule();
118 }
119
120 url = findWebUrl(clientModule);
121 if (url != null) {
122 if (partUrl.startsWith("/"))
123 return (url + partUrl);
124 else
125 return (url + "/" + partUrl); //NOI18N
126 } else {
127 return null;
128 }
129 }
130
131 private J2eeModuleProvider getChildModuleProvider(
132 J2eeModuleProvider jmp, String uri) {
133 if (uri == null)
134 return null;
135 J2eeModuleProvider child = null;
136 if (jmp instanceof J2eeApplicationProvider) {
137 J2eeApplicationProvider jap = (J2eeApplicationProvider) jmp;
138 child = jap.getChildModuleProvider(uri);
139 if (child == null) {
140 String root = "/"; // NOI18N
141 if (uri.startsWith(root)) {
142 uri = uri.substring(1);
143 } else {
144 uri = root + uri;
145 }
146 child = jap.getChildModuleProvider(uri);
147 }
148 }
149 return child;
150 }
151
152 private TargetModule getTargetModule() {
153 TargetModule[] mods = getTargetModules();
154 if (mods == null || mods.length == 0)
155 return null;
156
157 if (mods[0].delegate() != null)
158 return mods[0];
159
160 // determine target server instance
161 ServerString defaultTarget = ServerRegistry.getInstance()
162 .getDefaultInstance();
163 TargetModule execMod = null;
164 if (defaultTarget != null) {
165 for (int i = 0; i < mods.length; i++) {
166 if (mods[i].getInstanceUrl().equals(
167 defaultTarget.getUrl())
168 && mods[i].getTargetName().equals(
169 defaultTarget.getTargets(true)[0])) {
170 execMod = mods[i];
171 break;
172 }
173 }
174 }
175
176 if (execMod == null)
177 execMod = mods[0];
178 execMod.initDelegate((ModuleType) getModule().getModuleType());
179 return execMod;
180 }
181
182 /**
183 * Find the web URL for the given client module.
184 * If null is passed, or when plugin failed to resolve the child module url,
185 * this will attempt to return the first web url it sees.
186 */
187 private String findWebUrl(J2eeModule client) {
188 TargetModule module = getTargetModule();
189 if (module == null) {
190 return null;
191 }
192 if (getModule() == client) { // stand-alone web
193 return module.getWebURL();
194 }
195
196 ServerInstance instance = ServerRegistry.getInstance()
197 .getServerInstance(module.getInstanceUrl());
198 IncrementalDeployment mur = instance.getIncrementalDeployment();
199 String clientModuleUri = client == null ? "" : client.getUrl();
200 if (clientModuleUri.startsWith("/")) { //NOI18N
201 clientModuleUri = clientModuleUri.substring(1);
202 }
203 TargetModuleID[] children = module.getChildTargetModuleID();
204 String urlString = null;
205 TargetModuleID tmid = null;
206 for (int i = 0; children != null && i < children.length; i++) {
207 // remember when see one, just for a rainy day
208 if (urlString == null || urlString.trim().equals("")) {
209 urlString = children[i].getWebURL();
210 }
211
212 String uri = children[i].getModuleID(); //NOI18N
213 if (mur != null) {
214 uri = mur.getModuleUrl(children[i]);
215 } else {
216 int j = uri.indexOf('#');
217 if (j > -1) {
218 uri = uri.substring(j + 1);
219 }
220 }
221
222 if (mur != null && clientModuleUri.equalsIgnoreCase(uri)) {
223 tmid = children[i];
224 break;
225 }
226 }
227 // prefer the matched
228 if (tmid != null) {
229 urlString = tmid.getWebURL();
230 }
231
232 return urlString;
233 }
234
235 private ConfigSupportImpl getConfigSupportImpl() {
236 return (ConfigSupportImpl) moduleProvider.getConfigSupport();
237 }
238
239 public File getConfigurationFile() {
240 return getConfigSupportImpl().getConfigurationFile();
241 }
242
243 public ServerString getServer() {
244 if (server == null) {
245 String instanceID = moduleProvider.getServerInstanceID();
246 ServerInstance inst = ServerRegistry.getInstance()
247 .getServerInstance(instanceID);
248 if (inst == null) {
249 throw new RuntimeException(NbBundle.getMessage(
250 DeploymentTargetImpl.class,
251 "MSG_TargetServerNotFound", instanceID));
252 }
253 server = new ServerString(inst);
254 }
255 return server;
256 }
257
258 public TargetModule[] getTargetModules() {
259 if (targetModules == null || targetModules.length == 0) {
260 String fname = getTargetModuleFileName();
261 if (fname == null) {
262 return null;
263 }
264 targetModules = TargetModule.load(getServer(), fname);
265 }
266 return targetModules.clone();
267 }
268
269 public void setTargetModules(TargetModule[] targetModules) {
270 this .targetModules = targetModules.clone();
271 for (int i = 0; i < targetModules.length; i++) {
272 targetModules[i].save(getTargetModuleFileName());
273 }
274 }
275
276 public ModuleConfigurationProvider getModuleConfigurationProvider() {
277 return getConfigSupportImpl();
278 }
279
280 public J2eeModuleProvider.ConfigSupport getConfigSupport() {
281 return moduleProvider.getConfigSupport();
282 }
283
284 private String getTargetModuleFileName() {
285 String fileName = getDeploymentName();
286 if (fileName != null)
287 return fileName;
288
289 File f = null;
290 try {
291 if (getModule().getContentDirectory() != null) {
292 f = FileUtil.toFile(getModule().getContentDirectory());
293 }
294 } catch (IOException ioe) {
295 Logger.getLogger("global").log(Level.INFO, null, ioe);
296 }
297 if (f == null) {
298 fileName = getConfigSupportImpl().getDeploymentName();
299 } else {
300 String pathName = f.getAbsolutePath();
301 fileName = TargetModule.shortNameFromPath(pathName);
302 }
303 return fileName;
304 }
305
306 public String getDeploymentName() {
307 return moduleProvider.getDeploymentName();
308 }
309 }
|