001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    //
021    // This source code implements specifications defined by the Java
022    // Community Process. In order to remain compliant with the specification
023    // DO NOT add / change / or delete method signatures!
024    //
025    
026    package javax.enterprise.deploy.shared.factories;
027    
028    import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
029    import javax.enterprise.deploy.spi.DeploymentManager;
030    import javax.enterprise.deploy.spi.factories.DeploymentFactory;
031    import java.util.Iterator;
032    import java.util.ArrayList;
033    
034    /**
035     * The DeploymentFactoryManager class is a central registry for J2EE
036     * DeploymentFactory objects. The DeploymentFactoryManager retains references
037     * to DeploymentFactory objects loaded by a tool. A DeploymentFactory object
038     * provides a reference to a DeploymentManager. The DeploymentFactoryManager
039     * has been implemented as a singleton. A tool gets a reference to the
040     * DeploymentFactoryManager via the getInstance method. The
041     * DeploymentFactoryManager can return two types of DeploymentManagers, a
042     * connected DeploymentManager and a disconnected DeploymentManager. The
043     * connected DeploymentManager provides access to any product resources that
044     * may be required for configurations and deployment. The method to retrieve a
045     * connected DeploymentManager is getDeploymentManager. This method provides
046     * parameters for user name and password that the product may require for user
047     * authentication. A disconnected DeploymentManager does not provide access to
048     * a running J2EE product. The method to retrieve a disconnected
049     * DeploymentManager is getDisconnectedDeploymentManager. A disconnected
050     * DeploymentManager does not need user authentication information.
051     *
052     * @version $Rev: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $
053     */
054    public final class DeploymentFactoryManager {
055        private static DeploymentFactoryManager instance;
056    
057        private ArrayList deploymentFactories = new ArrayList();
058    
059        private DeploymentFactoryManager() {
060        }
061    
062        /**
063         * Retrieve the Singleton DeploymentFactoryManager
064         *
065         * @return DeploymentFactoryManager instance
066         */
067        public static DeploymentFactoryManager getInstance() {
068            if(instance == null) {
069                instance = new DeploymentFactoryManager();
070            }
071            return instance;
072        }
073    
074        /**
075         * Retrieve the lists of currently registered DeploymentFactories.
076         *
077         * @return the list of DeploymentFactory objects or an empty array if there are none.
078         */
079        public DeploymentFactory[] getDeploymentFactories() {
080            return (DeploymentFactory[])deploymentFactories.toArray(new DeploymentFactory[deploymentFactories.size()]);
081        }
082    
083        /**
084         * Retrieves a DeploymentManager instance to use for deployment. The caller
085         * provides a URI and optional username and password, and all registered
086         * DeploymentFactories will be checked. The first one to understand the URI
087         * provided will attempt to initiate a server connection and return a ready
088         * DeploymentManager instance.
089         *
090         * @param uri      The uri to check
091         * @param username An optional username (may be <tt>null</tt> if no
092         *                 authentication is required for this platform).
093         * @param password An optional password (may be <tt>null</tt> if no
094         *                 authentication is required for this platform).
095         *
096         * @return A ready DeploymentManager instance.
097         *
098         * @throws DeploymentManagerCreationException Occurs when the factory
099         *         appropriate to the specified URI was unable to initialize a
100         *         DeploymentManager instance (server down, unable to authenticate,
101         *         etc.).
102         */
103        public DeploymentManager getDeploymentManager(String uri, String username, String password) throws DeploymentManagerCreationException {
104            if(uri == null) {
105                throw new IllegalArgumentException("URI for DeploymentManager should not be null");
106            }
107            DeploymentManager manager = null;
108            for(Iterator i = deploymentFactories.iterator(); i.hasNext();) {
109                DeploymentFactory factory = (DeploymentFactory)i.next();
110                if(factory.handlesURI(uri)) {
111                    manager = factory.getDeploymentManager(uri, username, password);
112                    if(manager != null) {
113                        return manager;
114                    }
115                }
116            }
117            throw new DeploymentManagerCreationException("Could not get DeploymentManager; No registered DeploymentFactory handles this URI");
118        }
119    
120        /**
121         * Return a disconnected DeploymentManager instance.
122         *
123         * @param uri identifier of the disconnected DeploymentManager to return.
124         *
125         * @return A DeploymentManager instance.
126         *
127         * @throws DeploymentManagerCreationException occurs if the
128         *         DeploymentManager could not be created.
129         */
130        public DeploymentManager getDisconnectedDeploymentManager(String uri) throws DeploymentManagerCreationException {
131            if(uri == null) {
132                throw new IllegalArgumentException("URI for DeploymentManager should not be null");
133            }
134            DeploymentManager manager = null;
135            for(Iterator i = deploymentFactories.iterator(); i.hasNext();) {
136                DeploymentFactory factory = (DeploymentFactory)i.next();
137                if(factory.handlesURI(uri)) {
138                    manager = factory.getDisconnectedDeploymentManager(uri);
139                    if(manager != null) {
140                        return manager;
141                    }
142                }
143            }
144            throw new DeploymentManagerCreationException("Could not get DeploymentManager; No registered DeploymentFactory handles this URI");
145        }
146    
147        /**
148         * Registers a DeploymentFactory so it will be able to handle requests.
149         */ 
150        public void registerDeploymentFactory(DeploymentFactory factory) {
151            if(factory == null) {
152                throw new IllegalArgumentException("DeploymentFactory to register should not be null");
153            }
154            if(!deploymentFactories.contains(factory)) {
155                deploymentFactories.add(factory);
156            }
157        }
158    }