KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > enterprise > deploy > shared > factories > DeploymentFactoryManager


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 /*
25  * DeploymentFactoryManager.java
26  *
27  * Created on January 28, 2002, 6:24 PM
28  */

29
30 package javax.enterprise.deploy.shared.factories;
31
32
33 import java.util.Vector JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
36 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
37 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
38
39 /**
40  * The DeploymentFactoryManager class is a central registry for
41  * J2EE DeploymentFactory objects. The DeploymentFactoryManager
42  * retains references to DeploymentFactory objects loaded by
43  * a tool. A DeploymentFactory object provides a reference to
44  * a DeploymentManager.
45  *
46  * The DeploymentFactoryManager has been implemented as a singleton.
47  * A tool gets a reference to the DeploymentFactoryManager via the
48  * getInstance method.
49  *
50  * The DeploymentFactoryManager can return two types of
51  * DeploymentManagers, a connected DeploymentManager and a
52  * disconnected DeploymentManager. The connected DeploymentManager
53  * provides access to any product resources that may be required
54  * for configurations and deployment. The method to retrieve a
55  * connected DeploymentManager is getDeploymentManager. This method
56  * provides parameters for user name and password that the product
57  * may require for user authentication. A disconnected DeploymentManager
58  * does not provide access to a running J2EE product. The method
59  * to retrieve a disconnected DeploymentManager is
60  * getDisconnectedDeploymentManager. A disconnected DeploymentManager
61  * does not need user authentication information.
62  */

63 public final class DeploymentFactoryManager {
64     
65     private Vector JavaDoc deploymentFactories = null;
66     
67     // Singleton instance
68
private static DeploymentFactoryManager JavaDoc deploymentFactoryManager = null;
69     
70     /** Creates new RIDeploymentFactoryManager */
71     private DeploymentFactoryManager() {
72         deploymentFactories = new Vector JavaDoc();
73     }
74     /**
75      * Retrieve the Singleton DeploymentFactoryManager
76      * @return DeploymentFactoryManager instance
77      *
78      */

79     public static DeploymentFactoryManager JavaDoc getInstance() {
80         if(deploymentFactoryManager == null){
81             deploymentFactoryManager = new DeploymentFactoryManager JavaDoc();
82         }
83         return deploymentFactoryManager;
84     }
85        
86     /**
87      * Retrieve the lists of currently registered DeploymentFactories.
88      *
89      * @return the list of DeploymentFactory objects or an empty array
90      * if there are none.
91      */

92     public DeploymentFactory JavaDoc[] getDeploymentFactories() {
93         Vector JavaDoc deploymentFactoriesSnapShot = null;
94         synchronized(this){
95             deploymentFactoriesSnapShot =
96                 (Vector JavaDoc)this.deploymentFactories.clone();
97         }
98         DeploymentFactory JavaDoc[] factoriesArray =
99             new DeploymentFactory JavaDoc[deploymentFactoriesSnapShot.size()];
100         deploymentFactoriesSnapShot.copyInto(factoriesArray);
101         return factoriesArray;
102     }
103     
104     /**
105      * Retrieves a DeploymentManager instance to use for deployment.
106      * The caller provides a URI and optional username and password,
107      * and all registered DeploymentFactories will be checked. The
108      * first one to understand the URI provided will attempt to
109      * initiate a server connection and return a ready DeploymentManager
110      * instance.
111      *
112      * @param uri The uri to check
113      * @param username An optional username (may be <tt>null</tt> if
114      * no authentication is required for this platform).
115      * @param password An optional password (may be <tt>null</yy> if
116      * no authentication is required for this platform).
117      * @return A ready DeploymentManager instance.
118      * @throws DeploymentManagerCreationException
119      * Occurs when the factory appropriate to the specified URI
120      * was unable to initialize a DeploymentManager instance
121      * (server down, unable to authenticate, etc.).
122      */

123     public DeploymentManager JavaDoc getDeploymentManager(String JavaDoc uri, String JavaDoc username,
124          String JavaDoc password) throws DeploymentManagerCreationException JavaDoc{
125         try{
126             DeploymentFactory JavaDoc[] factories = this.getDeploymentFactories();
127             for(int factoryIndex=0; factoryIndex < factories.length;
128                 factoryIndex++){
129                 if(factories[factoryIndex].handlesURI(uri)){
130                     return factories[factoryIndex].getDeploymentManager(uri,
131                             username,password);
132                 }
133             }
134             // No available factory supports the provided url.
135
throw new DeploymentManagerCreationException JavaDoc("URL ["+uri+
136                 "] not supported by any available factories");
137         }catch(Throwable JavaDoc t){
138             throw new DeploymentManagerCreationException JavaDoc(
139                 "Could not get DeploymentManager");
140         }
141     }
142     
143     /**
144      * Registers a DeploymentFactory so it will be able to handle
145      * requests.
146      */

147     public void registerDeploymentFactory(DeploymentFactory JavaDoc factory){
148         this.deploymentFactories.add(factory);
149     }
150     
151     /**
152      * Return a <tt>disconnected</tt> DeploymentManager instance.
153      *
154      * @param uri identifier of the disconnected DeploymentManager to
155      * return.
156      * @return A DeploymentManager instance.
157      * @throws DeploymentDriverException occurs if the DeploymentManager
158      * could not be created.
159      */

160     public DeploymentManager JavaDoc getDisconnectedDeploymentManager(String JavaDoc uri)
161                throws DeploymentManagerCreationException JavaDoc {
162         try{
163             DeploymentFactory JavaDoc[] factories = this.getDeploymentFactories();
164             for(int factoryIndex=0; factoryIndex < factories.length;
165                 factoryIndex++){
166                 if(factories[factoryIndex].handlesURI(uri)){
167                     return factories[factoryIndex].getDisconnectedDeploymentManager(uri);
168                 }
169             }
170             // No available factory supports the provided url.
171
throw new DeploymentManagerCreationException JavaDoc("URL ["+uri+
172                 "] not supported by any available factories");
173         }catch(Throwable JavaDoc t){
174             throw new DeploymentManagerCreationException JavaDoc(
175                 "Could not get DeploymentManager");
176         }
177     }
178 }
179
Popular Tags