View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  //
19  // This source code implements specifications defined by the Java
20  // Community Process. In order to remain compliant with the specification
21  // DO NOT add / change / or delete method signatures!
22  //
23  
24  package javax.enterprise.deploy.shared.factories;
25  
26  import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
27  import javax.enterprise.deploy.spi.DeploymentManager;
28  import javax.enterprise.deploy.spi.factories.DeploymentFactory;
29  import java.util.Iterator;
30  import java.util.ArrayList;
31  
32  /**
33   * The DeploymentFactoryManager class is a central registry for J2EE
34   * DeploymentFactory objects. The DeploymentFactoryManager retains references
35   * to DeploymentFactory objects loaded by a tool. A DeploymentFactory object
36   * provides a reference to a DeploymentManager. The DeploymentFactoryManager
37   * has been implemented as a singleton. A tool gets a reference to the
38   * DeploymentFactoryManager via the getInstance method. The
39   * DeploymentFactoryManager can return two types of DeploymentManagers, a
40   * connected DeploymentManager and a disconnected DeploymentManager. The
41   * connected DeploymentManager provides access to any product resources that
42   * may be required for configurations and deployment. The method to retrieve a
43   * connected DeploymentManager is getDeploymentManager. This method provides
44   * parameters for user name and password that the product may require for user
45   * authentication. A disconnected DeploymentManager does not provide access to
46   * a running J2EE product. The method to retrieve a disconnected
47   * DeploymentManager is getDisconnectedDeploymentManager. A disconnected
48   * DeploymentManager does not need user authentication information.
49   *
50   * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
51   */
52  public final class DeploymentFactoryManager {
53      private static DeploymentFactoryManager instance;
54  
55      private ArrayList deploymentFactories = new ArrayList();
56  
57      private DeploymentFactoryManager() {
58      }
59  
60      /**
61       * Retrieve the Singleton DeploymentFactoryManager
62       *
63       * @return DeploymentFactoryManager instance
64       */
65      public static DeploymentFactoryManager getInstance() {
66          if(instance == null) {
67              instance = new DeploymentFactoryManager();
68          }
69          return instance;
70      }
71  
72      /**
73       * Retrieve the lists of currently registered DeploymentFactories.
74       *
75       * @return the list of DeploymentFactory objects or an empty array if there are none.
76       */
77      public DeploymentFactory[] getDeploymentFactories() {
78          return (DeploymentFactory[])deploymentFactories.toArray(new DeploymentFactory[deploymentFactories.size()]);
79      }
80  
81      /**
82       * Retrieves a DeploymentManager instance to use for deployment. The caller
83       * provides a URI and optional username and password, and all registered
84       * DeploymentFactories will be checked. The first one to understand the URI
85       * provided will attempt to initiate a server connection and return a ready
86       * DeploymentManager instance.
87       *
88       * @param uri      The uri to check
89       * @param username An optional username (may be <tt>null</tt> if no
90       *                 authentication is required for this platform).
91       * @param password An optional password (may be <tt>null</tt> if no
92       *                 authentication is required for this platform).
93       *
94       * @return A ready DeploymentManager instance.
95       *
96       * @throws DeploymentManagerCreationException Occurs when the factory
97       *         appropriate to the specified URI was unable to initialize a
98       *         DeploymentManager instance (server down, unable to authenticate,
99       *         etc.).
100      */
101     public DeploymentManager getDeploymentManager(String uri, String username, String password) throws DeploymentManagerCreationException {
102         if(uri == null) {
103             throw new IllegalArgumentException("URI for DeploymentManager should not be null");
104         }
105         DeploymentManager manager = null;
106         for(Iterator i = deploymentFactories.iterator(); i.hasNext();) {
107             DeploymentFactory factory = (DeploymentFactory)i.next();
108             if(factory.handlesURI(uri)) {
109                 manager = factory.getDeploymentManager(uri, username, password);
110                 if(manager != null) {
111                     return manager;
112                 }
113             }
114         }
115         throw new DeploymentManagerCreationException("Could not get DeploymentManager; No registered DeploymentFactory handles this URI");
116     }
117 
118     /**
119      * Return a disconnected DeploymentManager instance.
120      *
121      * @param uri identifier of the disconnected DeploymentManager to return.
122      *
123      * @return A DeploymentManager instance.
124      *
125      * @throws DeploymentManagerCreationException occurs if the
126      *         DeploymentManager could not be created.
127      */
128     public DeploymentManager getDisconnectedDeploymentManager(String uri) throws DeploymentManagerCreationException {
129         if(uri == null) {
130             throw new IllegalArgumentException("URI for DeploymentManager should not be null");
131         }
132         DeploymentManager manager = null;
133         for(Iterator i = deploymentFactories.iterator(); i.hasNext();) {
134             DeploymentFactory factory = (DeploymentFactory)i.next();
135             if(factory.handlesURI(uri)) {
136                 manager = factory.getDisconnectedDeploymentManager(uri);
137                 if(manager != null) {
138                     return manager;
139                 }
140             }
141         }
142         throw new DeploymentManagerCreationException("Could not get DeploymentManager; No registered DeploymentFactory handles this URI");
143     }
144 
145     /**
146      * Registers a DeploymentFactory so it will be able to handle requests.
147      */ 
148     public void registerDeploymentFactory(DeploymentFactory factory) {
149         if(factory == null) {
150             throw new IllegalArgumentException("DeploymentFactory to register should not be null");
151         }
152         if(!deploymentFactories.contains(factory)) {
153             deploymentFactories.add(factory);
154         }
155     }
156 }