KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > dm > SunDeploymentFactory


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.sun.ide.dm;
21
22 import java.io.File JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26
27 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
28 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
29 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
30 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
31 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
32 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
33
34 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager;
35 import org.netbeans.modules.j2ee.sun.api.SunURIManager;
36
37 /** This deploymenmt factory can creates an alternate deployment manager for
38  * S1AS.
39  * @author vkraemer,ludo
40  */

41 public class SunDeploymentFactory implements Constants, DeploymentFactory JavaDoc, InstanceListener {
42     
43     /* default server as defined in the nbdep.xml file by out plugin
44      * if we get this URL, we need to calculate an existing default domain from
45      * the registry, preferably a AS 9.0 one, if we have the choice between AS 8.x and 9.0
46      *
47      **/

48     private static final String JavaDoc DEFAULTSERVERDEF="[ ]deployer:Sun:AppServer::localhost:4849";// NOI18N
49

50     //
51
// this whole class should probably be a subclass of the
52
// com.sun.enterprise.deployapi.SunDeploymentFactory...
53
private DeploymentFactory JavaDoc innerDF = null;
54     
55     
56     /** resource bundle
57      */

58     protected static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
59             "org.netbeans.modules.j2ee.sun.ide.dm.Bundle"); // NOI18N
60

61     private boolean instanceListenerAdded;
62     
63     public SunDeploymentFactory() {
64         instanceListenerAdded = false;
65     }
66     
67     static final private Map JavaDoc<String JavaDoc,DeploymentManager JavaDoc> dms = new HashMap JavaDoc<String JavaDoc,DeploymentManager JavaDoc>();
68     
69     /** This method returns a connected deployment manager.
70      *
71      * @param uri
72      * @param userName
73      * @param password
74      * @throws DeploymentManagerCreationException
75      * @return a deployment manager for a particular server instance
76      */

77     public DeploymentManager JavaDoc getDeploymentManager(String JavaDoc uri, String JavaDoc userName,String JavaDoc password) throws DeploymentManagerCreationException JavaDoc {
78         registerInstanceListener();
79         
80         try {
81             if (DEFAULTSERVERDEF.equals(uri)){
82                 String JavaDoc defaultURI = null;//don;t know yet which one
83
String JavaDoc ServerUrls[] = InstanceProperties.getInstanceList();
84                 for (String JavaDoc elem : ServerUrls) {
85                     if(handlesURI(elem)){
86                         if (defaultURI == null){
87                             defaultURI = elem; //get the first one available. Could be 8 or 9.0 server
88
}
89                         File JavaDoc serverLocation = getServerLocationFromURI(elem);
90                         if (ServerLocationManager.isGlassFish(serverLocation)){
91                             defaultURI = elem;// make sure we pick one AS 9 is one is available
92
break;
93                         }
94                     }
95                 }
96                 if (defaultURI != null){
97                     uri = defaultURI;
98                 }
99             }
100             innerDF = ServerLocationManager.getDeploymentFactory(getServerLocationFromURI(uri));
101             if (innerDF==null){
102                 throw new DeploymentManagerCreationException JavaDoc(getRealURI(uri)+getServerLocationFromURI( uri)+bundle.getString("MSG_WrongInstallDir"));
103             }
104             synchronized (dms) {
105                 DeploymentManager JavaDoc retVal = dms.get(uri);
106                 if (null == retVal) {
107                     retVal = new SunDeploymentManager(innerDF, getRealURI(uri), userName, password,getServerLocationFromURI( uri));
108                     dms.put(uri,retVal);
109                 }
110                 return retVal;
111             }
112         } catch (Exception JavaDoc e) {
113             throw new DeploymentManagerCreationException JavaDoc(getRealURI(uri)+getServerLocationFromURI( uri)+bundle.getString("MSG_WrongInstallDir"));
114         }
115     }
116     
117     /** This method returns a disconnected deployment manager.
118      *
119      * Should a disconnected deployment manager be able to become connected?
120      *
121      * @param uri
122      * @throws DeploymentManagerCreationException
123      * @return a deployment manager for doing configuration.
124      */

125     public DeploymentManager JavaDoc getDisconnectedDeploymentManager(String JavaDoc uri) throws DeploymentManagerCreationException JavaDoc {
126         try {
127             
128             return getDeploymentManager(uri,null,null);
129         } catch (Exception JavaDoc e) {
130             throw new DeploymentManagerCreationException JavaDoc(getRealURI(uri)+getServerLocationFromURI( uri)+bundle.getString("MSG_WrongInstallDir"));
131         }
132     }
133     
134     /** Determines whether this URI is handled by the Deployment factory
135      * Iniitally we need to test the prefix. If the factory will support
136      * multiple managers, we need to extend this test to catch those cases.
137      *
138      *The tests should also be extended to deteremine if the URI is "complete"
139      * for this factory. It has to have a machine name (that can be resolved
140      * to an IP address) and a port. Whether the server is "up" may be an open
141      * question.
142      *
143      * @param uri
144      * @return boolean value
145      */

146     public boolean handlesURI(String JavaDoc uri) {
147         if (uri==null){
148             return false;
149         }
150         if(uri.startsWith("[")){//NOI18N
151
if (uri.indexOf(SunURIManager.SUNSERVERSURI)!=-1){
152                 return true;
153             }
154         }
155         
156         
157         return false;
158     }
159     
160     public String JavaDoc getDisplayName() {
161         return bundle.getString("FACTORY_DISPLAYNAME");//NOI18N
162
}
163     
164     public String JavaDoc getProductVersion() {
165         if (null != innerDF){
166             return innerDF.getProductVersion();
167         }
168         return "1.0";//NOI18N
169
}
170     
171     private static File JavaDoc getServerLocationFromURI(String JavaDoc uri) throws DeploymentManagerCreationException JavaDoc{
172         
173         if(uri.startsWith("[")){//NOI18N
174
String JavaDoc loc = uri.substring(1,uri.indexOf("]"));
175             return new File JavaDoc(loc);
176         }
177         throw new DeploymentManagerCreationException JavaDoc(uri+bundle.getString("MSG_WrongInstallDir"));
178     }
179     
180     private static String JavaDoc getRealURI(String JavaDoc uri) throws DeploymentManagerCreationException JavaDoc{
181         if(uri.startsWith("[")){//NOI18N
182
return uri.substring(uri.indexOf("]")+1,uri.length());
183         }
184         return uri;// the old one.
185
}
186     
187     private void registerInstanceListener() {
188         synchronized(dms) {
189             if(!instanceListenerAdded) {
190                 Deployment.getDefault().addInstanceListener(this);
191                 instanceListenerAdded = true;
192             }
193         }
194     }
195     
196     // Listen for server instance removed event so we can clear it's DM from the cache, if necessary
197
public void instanceRemoved(String JavaDoc serverInstanceID) {
198         synchronized (dms) {
199             // serverInstanceID is really the URI of this installed server :)
200
dms.remove(serverInstanceID);
201         }
202     }
203     
204     public void instanceAdded(String JavaDoc serverInstanceID) {
205         // n/a
206
}
207     
208     public void changeDefaultInstance(String JavaDoc oldServerInstanceID, String JavaDoc newServerInstanceID) {
209         // n/a
210
}
211     
212 }
213
Popular Tags