KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployapi > SunDeploymentFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployapi;
25
26 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier;
27 import com.sun.enterprise.deployment.util.DOLUtils;
28 import com.sun.enterprise.util.LocalStringManagerImpl;
29 import java.util.Hashtable JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
32 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
33 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
34
35 /**
36  *Concrete implementation of the JSR 88 DeploymentFactory interface.
37  * @author dochez
38  * @author tjquinn
39  */

40 public class SunDeploymentFactory implements DeploymentFactory JavaDoc {
41     
42     private static LocalStringManagerImpl xlocalStrings =
43     new LocalStringManagerImpl(SunDeploymentFactory.class);
44     
45     private static String JavaDoc DMGR_NOT_CONNECTED = xlocalStrings.getLocalString(
46     "enterprise.deployapi.spi.DMgrnotconnected", // NOI18N
47
"Deployment Manager is not connected to J2EE Resources"); // NOI18N
48

49     // URI String that this factory can handle. May need to be rebuilt.
50

51     //The following URISTRING is what we supported in PE Beta. Keeping it for
52
//backward compatibility
53
private final static String JavaDoc PE_BETA_URISTRING = "deployer:Sun:S1AS::"; // NOI18N
54

55     //The following URISTRINNG is what we use for PE FCS and in the future
56
private final static String JavaDoc DEFAULT_URISTRING = "deployer:Sun:AppServer::"; // NOI18N
57
private final static String JavaDoc HTTPS = "https";
58     private final static String JavaDoc URI_SEPARATOR = ":";// NOI18N
59
private final static String JavaDoc LOCAL_HOST = "localhost";// NOI18N
60
private final static int HOST_PORT = 4848; // default DAS port
61

62     private final static String JavaDoc[] supportedURIs = { PE_BETA_URISTRING,
63         DEFAULT_URISTRING };
64     
65     // All the registered mangers are shared by all instances of the Factory
66
private static Hashtable JavaDoc connectedDeploymentManagers;
67     private static Hashtable JavaDoc disconnectedDeploymentManagers;
68
69     private final static String JavaDoc HTTPS_PROTOCOL = "s1ashttps";
70     private final static String JavaDoc HTTP_PROTOCOL = "s1ashttp";
71     
72     /** Creates a new instance of SunDeploymentFactory */
73     public SunDeploymentFactory() {
74     }
75     
76     
77     /** Return a <tt>connected</tt> DeploymentManager instance.
78      *
79      * @param uri The URI that specifies the connection parameters
80      * @param username An optional username (may be <tt>null</tt> if
81      * no authentication is required for this platform).
82      * @param password An optional password (may be <tt>null</yy> if
83      * no authentication is required for this platform).
84      * @return A ready DeploymentManager instance.
85      * @throws DeploymentManagerCreationException occurs when a
86      * DeploymentManager could not be returned (server down,
87      * unable to authenticate, etc).
88      */

89     public DeploymentManager JavaDoc getDeploymentManager(String JavaDoc uri, String JavaDoc username, String JavaDoc password) throws DeploymentManagerCreationException JavaDoc {
90         
91         if (handlesURI(uri)) {
92             ServerConnectionIdentifier hostInfo = null;
93             try {
94                 hostInfo = parseURIForHostInfo(uri);
95             } catch(Exception JavaDoc ex) {
96                 DeploymentManagerCreationException JavaDoc e = new DeploymentManagerCreationException JavaDoc(
97                 xlocalStrings.getLocalString(
98                 "enterprise.deployapi.spi.wronghostidentifier",
99                 "Wrong host identifier in uri {0} ", new Object JavaDoc[] { uri }));
100                 e.initCause(ex);
101                 throw e;
102             }
103             try {
104                 hostInfo.setUserName(username);
105                 hostInfo.setPassword(password);
106                 DeploymentManager JavaDoc answer = null;
107
108                 answer = new SunDeploymentManager(hostInfo);
109                 return answer;
110             } catch(Throwable JavaDoc t) {
111                 DeploymentManagerCreationException JavaDoc e = new DeploymentManagerCreationException JavaDoc(xlocalStrings.getLocalString(
112                 "enterprise.deployapi.spi.exceptionwhileconnecting", //NOI18N
113
"Exception while connecting to {0} : {1}", new Object JavaDoc[] { uri, t.getMessage() })); //NOI18N
114
e.initCause(t);
115                 throw e;
116             }
117         } else {
118             return null;
119         }
120     }
121     
122     /** Return a <tt>disconnected</tt> DeploymentManager instance.
123      *
124      * @param uri the uri of the DeploymentManager to return.
125      * @return A DeploymentManager <tt>disconnected</tt> instance.
126      * @throws DeploymentManagerCreationException occurs if the
127      * DeploymentManager could not be created.
128      */

129     public DeploymentManager JavaDoc getDisconnectedDeploymentManager(String JavaDoc uri) throws DeploymentManagerCreationException JavaDoc {
130         if (handlesURI(uri)) {
131             return new SunDeploymentManager();
132         } else {
133             return null;
134         }
135     }
136     
137     /** Provide a string with the name of this vendor's DeploymentManager.
138      * @return the name of the vendor's DeploymentManager.
139      */

140     public String JavaDoc getDisplayName() {
141         return xlocalStrings.getLocalString(
142                 "enterprise.deployapi.spi.DisplayName",
143                 "Sun Java System Application Server");
144     }
145     
146     /** Provide a string identifying version of this vendor's
147      * DeploymentManager.
148      * @return the name of the vendor's DeploymentManager.
149      */

150     public String JavaDoc getProductVersion() {
151         return xlocalStrings.getLocalString(
152                 "enterprise.deployapi.spi.ProductVersion", "9.0");
153     }
154     
155     /** Tests whether this factory can create a DeploymentManager
156      * object based on the specificed URI. This does not indicate
157      * whether such an attempt will be successful, only whether the
158      * factory can handle the uri.
159      * @param uri The uri to check
160      * @return <tt>true</tt> if the factory can handle the uri.
161      */

162     public boolean handlesURI(String JavaDoc uri) {
163         if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
164             DOLUtils.getDefaultLogger().fine("handlesURI: URI ["+uri+"]");// NOI18N
165
}
166         
167         if (uri != null) {
168             try {
169                 return (parseURIForHostInfo(uri)!=null);
170             } catch (Exception JavaDoc ex) {
171                 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure", // NOI18N
172
new Object JavaDoc[] {uri});
173             }
174         }
175         return false;
176     }
177     
178     
179     /**
180      * @return the host name/port from the URI passed see JSR88 paragraph 9.2.3
181      */

182     
183     public ServerConnectionIdentifier parseURIForHostInfo(String JavaDoc uri) throws Exception JavaDoc {
184         
185         String JavaDoc targetURI = null;
186         for (int i=0;i<supportedURIs.length;i++) {
187             if (uri.indexOf(supportedURIs[i])==0) {
188                 targetURI = supportedURIs[i];
189             }
190         }
191         
192         //if the URI does not contain DEFAULT_URISTRINNG or PE_BETA_URISTRING,
193
//then the URI is not valid.
194
if (targetURI == null) {
195             throw new Exception JavaDoc(xlocalStrings.getLocalString(
196             "enterprise.deployapi.spi.invaliduri", // NOI18N
197
"Invalid URI")); // NOI18N
198
}
199         
200         ServerConnectionIdentifier sci = new ServerConnectionIdentifier();
201         
202         if(uri.length() == targetURI.length()) {
203             sci.setHostName(LOCAL_HOST);
204             sci.setHostPort(HOST_PORT);
205         } else {
206             String JavaDoc reminder = uri.substring(targetURI.length());
207             String JavaDoc[] splitted = reminder.split(URI_SEPARATOR);
208             if (splitted.length<2) {
209                 throw new Exception JavaDoc(xlocalStrings.getLocalString(
210                     "enterprise.deployapi.spi.invaliduri", // NOI18N
211
"Invalid URI")); // NOI18N
212
}
213             if ("".equals(splitted[0])) {
214                 sci.setHostName(LOCAL_HOST);
215             } else {
216                 sci.setHostName(splitted[0]);
217             }
218             if ("".equals(splitted[1])) {
219                 sci.setHostPort(HOST_PORT);
220             } else {
221                 sci.setHostPort(Integer.parseInt(splitted[1]));
222             }
223             
224             if (splitted.length>2) {
225                 if (HTTPS.equals(splitted[2])) {
226                     sci.setSecure(true);
227                 }
228             }
229         }
230         return sci;
231     }
232 }
233
Popular Tags