KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > projects > DeploymentTargetImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
28 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
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 /**
37  *
38  * @author George FinKlang
39  */

40 public final class DeploymentTargetImpl implements DeploymentTarget {
41     
42     J2eeModuleProvider moduleProvider;
43     String JavaDoc clientName;
44     ServerString server;
45     TargetModule[] targetModules;
46     
47     public DeploymentTargetImpl(J2eeModuleProvider moduleProvider, String JavaDoc 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     /**
61      * This will return url to invoke webbrowser for web client.
62      * If there is no webclient, null will be returned.
63      *
64      * Caution: this call does not attempt to detect whehter the client specified
65      * by clientName is app client.
66      */

67     public String JavaDoc getClientUrl(String JavaDoc partUrl) {
68         // determine client module
69
J2eeModule clientModule = null;
70         String JavaDoc 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                 //findWebUrl(null) will take care to find a first weburl it sees, but just to be sure...
79
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); //NOI18N
98
} else {
99             return null;
100         }
101     }
102     
103     private J2eeModuleProvider getChildModuleProvider(J2eeModuleProvider jmp, String JavaDoc 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 JavaDoc root = "/" ; // NOI18N
112
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         // determine target server instance
132
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 JavaDoc)getModule().getModuleType());
146         return execMod;
147     }
148     
149     /**
150      * Find the web URL for the given client module.
151      * If null is passed, or when plugin failed to resolve the child module url,
152      * this will attempt to return the first web url it sees.
153      */

154     private String JavaDoc findWebUrl(J2eeModule client) {
155         TargetModule module = getTargetModule();
156         if (module == null) {
157             return null;
158         }
159         if (getModule() == client) { // stand-alone web
160
return module.getWebURL();
161         }
162         
163         ServerInstance instance = ServerRegistry.getInstance().getServerInstance(module.getInstanceUrl());
164         IncrementalDeployment mur = instance.getIncrementalDeployment ();
165         String JavaDoc clientModuleUri = client == null ? "" : client.getUrl();
166         if (clientModuleUri.startsWith("/")) { //NOI18N
167
clientModuleUri = clientModuleUri.substring(1);
168         }
169         TargetModuleID JavaDoc[] children = module.getChildTargetModuleID();
170         String JavaDoc urlString = null;
171         TargetModuleID JavaDoc tmid = null;
172         for (int i=0; children != null && i<children.length; i++) {
173             // remember when see one, just for a rainy day
174
if (urlString == null || urlString.trim().equals("")) {
175                 urlString = children[i].getWebURL();
176             }
177             
178             String JavaDoc uri = children[i].getModuleID(); //NOI18N
179
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         // prefer the matched
194
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 JavaDoc instanceID = moduleProvider.getServerInstanceID ();
212             ServerInstance inst = ServerRegistry.getInstance ().getServerInstance (instanceID);
213             if (inst == null) {
214                 throw new RuntimeException JavaDoc(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 JavaDoc 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 JavaDoc getTargetModuleFileName() {
248         String JavaDoc 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 JavaDoc pathName = f.getAbsolutePath();
264             fileName = TargetModule.shortNameFromPath(pathName);
265         }
266         return fileName;
267     }
268     
269     public String JavaDoc getDeploymentName() {
270         return moduleProvider.getDeploymentName();
271     }
272 }
Popular Tags